-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial code creation. working on creating review schema and model se…
…tup and handler functions
- Loading branch information
adescoteaux1
committed
Sep 19, 2024
1 parent
37c4fad
commit 749843e
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package models | ||
|
||
type Review struct { | ||
UserID string `json:"user_id"` | ||
MediaType string `json:"media_type"` | ||
MediaID string `json:"media_id"` | ||
Rating string `json:"rating"` | ||
Desc *string `json:"desc,omitempty"` | ||
DateTime string `json:"date_time"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package handler | ||
|
||
import ( | ||
"platnm/internal/storage" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
type ReviewHandler struct { | ||
reviewRepository storage.ReviewRepository | ||
} | ||
|
||
func NewReviewHandler(reviewRepository storage.ReviewRepository) *ReviewHandler { | ||
return &ReviewHandler{ | ||
reviewRepository, | ||
} | ||
} | ||
|
||
func (h *ReviewHandler) GetReviews(c *fiber.Ctx) error { | ||
review, err := h.reviewRepository.GetReviews(c.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON(users) | ||
} | ||
|
||
func (h *ReviewHandler) GetReviewById(c *fiber.Ctx, mediaType MediaType) error { | ||
id := c.Params("id") | ||
offset := c.Query("offset", "0") | ||
limit := c.Query("limit", "10") | ||
review, err := h.reviewRepository.GetReviewByID(id, mediaType, c.Context()) | ||
|
||
// Parse offset and limit as integers | ||
offset, err := strconv.Atoi(offsetStr) | ||
if err != nil || offset < 0 { | ||
offset = 0 // Ensure offset is non-negative | ||
} | ||
|
||
limit, err := strconv.Atoi(limitStr) | ||
if err != nil || limit <= 0 { | ||
limit = 10 // Ensure limit is positive | ||
} | ||
|
||
// Fetch the review based on ID and media type | ||
review, err := h.reviewRepository.GetReviewByID(id, mediaType, c.Context(), offset, limit) | ||
if err != nil { | ||
// If error, log it and return 500 | ||
fmt.Println(err.Error(), "from transactions err ") | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ | ||
"error": "Unable to retrieve reviews", | ||
}) | ||
} | ||
|
||
// If no review is found, return a 404 status | ||
if review == nil || len(review) == 0 { | ||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ | ||
"message": "No reviews found", | ||
}) | ||
} | ||
|
||
// Calculate average rating | ||
var avgRating float64 | ||
for _, r := range review { | ||
avgRating += r.Rating | ||
} | ||
if len(review) > 0 { | ||
avgRating /= float64(len(review)) | ||
} | ||
|
||
// Return the reviews and average rating | ||
response := fiber.Map{ | ||
"avgRating": avgRating, | ||
"reviews": review, | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON(response) | ||
|
||
if err != nil { | ||
print(err.Error(), "from transactions err ") | ||
return err | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON(review) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
"platnm/internal/models" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
) | ||
|
||
type ReviewRepository struct { | ||
db *pgxpool.Pool | ||
} | ||
|
||
func (r *ReviewRepository) GetReviews(ctx context.Context) ([]*models.Review, error) { | ||
rows, err := r.db.Query(context.Background(), "SELECT user_id, media_id, media_type, desc, rating, date_time FROM review") | ||
if err != nil { | ||
print(err.Error(), "from transactions err ") | ||
return []*models.Review{}, err | ||
} | ||
defer rows.Close() | ||
|
||
var reviews []*models.Review | ||
for rows.Next() { | ||
var review models.Review | ||
var user_id, media_id, media_type, desc, rating, date_time *string | ||
|
||
if err := rows.Scan(&review.UserID, &mediaID, &mediaType, &desc, &rating, &dateTime); err != nil { | ||
print(err.Error(), "from transactions err ") | ||
return review, err | ||
} | ||
|
||
review.UserID = *userID | ||
review.MediaID = *mediaID | ||
review.MediaType = *mediaType | ||
review.Desc = *desc | ||
review.Rating = *rating | ||
review.DateTime = *dateTime | ||
|
||
reviews = append(reviews, &review) | ||
} | ||
|
||
if err := rows.Err(); err != nil { | ||
print(err.Error(), "from transactions err ") | ||
return []*models.Review{}, err | ||
} | ||
|
||
return reviews, nil | ||
} | ||
|
||
func (r *ReviewRepository) GetReviewByID(id string, media_type string, ctx context.Context) (*models.Review, error) { | ||
var review models.Review | ||
if media_type = "album" { | ||
err := r.db.QueryRow(context.Background(), "SELECT user_id, media_id, media_type, desc, rating, date_time FROM review WHERE media_id = $1 and media_type = 'album'", id).Scan(&review.UserID, &mediaID, &mediaType, &desc, &rating, &dateTime) | ||
} else if media_type = "track" { | ||
err := r.db.QueryRow(context.Background(), "SELECT user_id, media_id, media_type, desc, rating, date_time FROM review WHERE media_id = $1 and media_type = 'track'", id).Scan(&review.UserID, &mediaID, &mediaType, &desc, &rating, &dateTime) | ||
} | ||
|
||
if err != nil { | ||
print(err.Error(), "from transactions err ") | ||
return nil, err | ||
} | ||
|
||
return &review, nil | ||
} | ||
|
||
func NewReviewRepository(db *pgxpool.Pool) *ReviewRepository { | ||
return &ReviewRepository{ | ||
db: db, | ||
} | ||
} |