Skip to content

Commit

Permalink
Add friends rating component (#214)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Weinberger <[email protected]>
  • Loading branch information
ddusichka and Alexcchip authored Dec 4, 2024
1 parent 3b8402a commit 86e720e
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 182 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package reviews

import (
"github.com/gofiber/fiber/v2"
)

func (h *Handler) GetUserFollowingReviewsOfMedia(c *fiber.Ctx) error {

userId := c.Params("userId")
mediaId := c.Params("mediaId")
typeString := c.Query("media_type")

review, _ := h.reviewRepository.GetUserFollowingReviewsOfMedia(c.Context(), typeString, mediaId, userId)

return c.Status(fiber.StatusOK).JSON(review)
}
2 changes: 2 additions & 0 deletions backend/internal/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func setupRoutes(app *fiber.App, repo *storage.Repository, config config.Config)
// Get Reviews by ID which can be used to populate a preview
r.Get("/:id", reviewHandler.GetReviewByID)
r.Get("/media/:mediaId/:userID", reviewHandler.GetUserReviewsOfMedia)
r.Get("/media/:mediaId/:userID/following", reviewHandler.GetUserFollowingReviewsOfMedia)

r.Get("/user/:id", reviewHandler.GetReviewsByUserID)
r.Post("/vote", func(c *fiber.Ctx) error {
return reviewHandler.UserVote(c, "review")
Expand Down
129 changes: 129 additions & 0 deletions backend/internal/storage/postgres/schema/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,135 @@ func (r *ReviewRepository) GetUserReviewsOfMedia(ctx context.Context, media_type

}

func (r *ReviewRepository) GetUserFollowingReviewsOfMedia(ctx context.Context, media_type string, mediaID string, userID string) ([]*models.Preview, error) {
query := `
SELECT
r.id,
r.user_id,
u.username,
u.display_name,
u.profile_picture,
r.media_type,
r.media_id,
r.rating,
r.title,
r.comment,
r.created_at,
r.updated_at,
COALESCE(a.cover, t.cover) AS media_cover,
COALESCE(a.title, t.title) AS media_title,
COALESCE(a.artists, t.artists) AS media_artist,
ARRAY_AGG(tag.name) FILTER (WHERE tag.name IS NOT NULL) AS tags
FROM review r
INNER JOIN "user" u ON u.id = r.user_id
LEFT JOIN (
SELECT t.title, t.id, STRING_AGG(ar.name, ', ') AS artists, cover
FROM track t
JOIN track_artist ta on t.id = ta.track_id
JOIN artist ar ON ta.artist_id = ar.id
JOIN album a on t.album_id = a.id
GROUP BY t.id, cover, t.title
) t ON r.media_type = 'track' AND r.media_id = t.id
LEFT JOIN (
SELECT a.id, a.title, STRING_AGG(ar.name, ', ') AS artists, cover
FROM album a
JOIN album_artist aa on a.id = aa.album_id
JOIN artist ar ON aa.artist_id = ar.id
GROUP BY a.id, cover, a.title
) a ON r.media_type = 'album' AND r.media_id = a.id
LEFT JOIN review_tag rt ON r.id = rt.review_id
LEFT JOIN tag tag ON rt.tag_id = tag.id
LEFT JOIN (
SELECT post_id as review_id, COUNT(*) AS vote_count
FROM user_vote
WHERE post_type = 'review'
GROUP BY post_id
) v ON r.id = v.review_id
WHERE r.media_id = $2 AND r.media_type = $3 AND r.user_id IN (
SELECT followee_id FROM follower WHERE follower_id = $1
)
GROUP BY r.id, r.user_id, u.username, u.display_name, u.profile_picture, r.media_type, r.media_id, r.rating, r.comment, r.created_at, r.updated_at, media_cover, media_title, media_artist, v.vote_count
`

rows, err := r.Query(ctx, query, userID, mediaID, media_type)

if err != nil {
fmt.Println(err)
return nil, err
}
defer rows.Close()

var previews []*models.Preview

// Scan results into the feedPosts slice
for rows.Next() {
var preview models.Preview
var title, comment sql.NullString // Use sql.NullString for nullable strings
err := rows.Scan(
&preview.ReviewID,
&preview.UserID,
&preview.Username,
&preview.DisplayName,
&preview.ProfilePicture,
&preview.MediaType,
&preview.MediaID,
&preview.Rating,
&title,
&comment,
&preview.CreatedAt,
&preview.UpdatedAt,
&preview.MediaCover,
&preview.MediaTitle,
&preview.MediaArtist,
&preview.Tags,
)
if err != nil {
fmt.Println(err)
return nil, err
}

// Assign comment to feedPost.Comment, handling null case
if comment.Valid {
preview.Comment = &comment.String // Point to the string if valid
} else {
preview.Comment = nil // Set to nil if null
}

if title.Valid {
preview.Title = &title.String // Point to the string if valid
} else {
preview.Title = nil // Set to nil if null
}

// Ensure tags is an empty array if null
if preview.Tags == nil {
preview.Tags = []string{}
}

// Fetch review statistics for the current review
reviewStat, err := r.GetReviewStats(ctx, strconv.Itoa(preview.ReviewID))
if err != nil {
return nil, err
}

// If reviewStat is not nil, populate the corresponding fields in FeedPost
if reviewStat != nil {
preview.ReviewStat = *reviewStat
}

// Append the populated FeedPost to the feedPosts slice
previews = append(previews, &preview)
}

// Check for errors after looping through rows
if err := rows.Err(); err != nil {
return nil, err
}

return previews, nil

}

func (r *ReviewRepository) GetReviewsByUserID(ctx context.Context, userId string) ([]*models.Preview, error) {

query := `
Expand Down
1 change: 1 addition & 0 deletions backend/internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type UserRepository interface {

type ReviewRepository interface {
GetUserReviewsOfMedia(ctx context.Context, media_type string, mediaID string, userID string) ([]*models.Preview, error)
GetUserFollowingReviewsOfMedia(ctx context.Context, media_type string, mediaID string, userID string) ([]*models.Preview, error)
GetReviewsByUserID(ctx context.Context, id string) ([]*models.Preview, error)
CreateReview(ctx context.Context, review *models.Review) (*models.Review, error)
ReviewExists(ctx context.Context, id string) (bool, error)
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/MediaPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export default function MediaPage() {
)}
<View style={styles.socialContainer}>
<YourRatings media_id={mediaId} media_type={mediaType} />
<FriendRatings count={5} />
<FriendRatings media_id={mediaId} media_type={mediaType} />
</View>
<View>
{reviews?.slice(0, 5).map((review) => (
Expand Down
Loading

0 comments on commit 86e720e

Please sign in to comment.