This commit is contained in:
Untone 2024-03-02 14:06:20 +03:00
parent d9fd6c2b36
commit 7a1df30325

View File

@ -220,40 +220,40 @@ func (c *provider) GetBoolStoreEnvVariable(key string) (bool, error) {
return data == "1", nil return data == "1", nil
} }
type UserProfile struct { type AuthorProfile struct {
ID string `json:"id"` ID int `json:"id"`
// Add other fields as necessary // Add other fields as necessary
} }
// GetUserAppDataFromRedis retrieves user profile and follows from Redis, combines them into a JSON format, // GetUserAppDataFromRedis retrieves user profile and follows from Redis, combines them into a JSON format,
// and assigns the JSON string to the provided user's ID. // and assigns the JSON string to the provided user's ID.
func (c *provider) GetUserAppDataFromRedis(userId string) (string, error) { func (c *provider) GetUserAppDataFromRedis(userId string) (string, error) {
userProfileString, err := c.store.Get(c.ctx, fmt.Sprintf(`user:%s:author`, userId)).Result() authorProfileString, err := c.store.Get(c.ctx, fmt.Sprintf(`user:%s:author`, userId)).Result()
if err != nil { if err != nil {
return "", err return "", err
} }
// Parse userProfileString into a UserProfile struct // Parse userProfileString into a UserProfile struct
var userProfile UserProfile var authorProfile AuthorProfile
err = json.Unmarshal([]byte(userProfileString), &userProfile.ID) err = json.Unmarshal([]byte(authorProfileString), &authorProfile.ID)
if err != nil { if err != nil {
// If the ID is not a number, try unmarshalling it as a string instead // If the ID is not a number, try unmarshalling it as a string instead
err = json.Unmarshal([]byte(userProfileString), &userProfile.ID) err = json.Unmarshal([]byte(authorProfileString), &authorProfile.ID)
} }
if err != nil { if err != nil {
return "", err return "", err
} }
// Use userProfile.ID here if necessary // Use userProfile.ID here if necessary
authorId := userProfile.ID authorId := authorProfile.ID
userFollowsAuthors := c.store.Get(c.ctx, fmt.Sprintf(`author:%s:follows-authors`, authorId)) authorFollowsAuthorsString := c.store.Get(c.ctx, fmt.Sprintf(`author:%s:follows-authors`, authorId))
userFollowsTopics := c.store.Get(c.ctx, fmt.Sprintf(`author:%s:follows-topics`, authorId)) authorFollowsTopicsString := c.store.Get(c.ctx, fmt.Sprintf(`author:%s:follows-topics`, authorId))
userFollowers := c.store.Get(c.ctx, fmt.Sprintf(`author:%s:followers`, authorId)) authorFollowers := c.store.Get(c.ctx, fmt.Sprintf(`author:%s:followers`, authorId))
// Combine user data into a JSON string // Combine user data into a JSON string
combinedData := fmt.Sprintf( combinedData := fmt.Sprintf(
`{"profile": %s, "authors": %s, "topics": %s, "followers": %s}`, `{"profile": %s, "authors": %s, "topics": %s, "followers": %s}`,
userProfile, userFollowsAuthors, userFollowsTopics, userFollowers) authorProfileString, authorFollowsAuthorsString, authorFollowsTopicsString, authorFollowers)
return combinedData, nil return combinedData, nil
} }