Skip to content

Commit

Permalink
Get reviews for user (#20)
Browse files Browse the repository at this point in the history
Me when reviews are retrieved by user ids
  • Loading branch information
luisa-li authored Sep 23, 2024
1 parent ec0ab7a commit 403be01
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 7 deletions.
30 changes: 30 additions & 0 deletions backend/internal/service/handler/reviews/get_user_reviews.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package reviews

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

"platnm/internal/errs"
)

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

id := c.Params("id")

exists, err := h.userRepository.UserExists(c.Context(), id)

if err != nil {
return err
}

if !exists {
return errs.NotFound("User", "id", id)
}

review, err := h.reviewRepository.GetReviewsByUserID(c.Context(), id)

if err != nil {
return err
}

return c.Status(fiber.StatusOK).JSON(review)
}
5 changes: 4 additions & 1 deletion backend/internal/service/handler/reviews/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package reviews

import "platnm/internal/storage"


type Handler struct {
reviewRepository storage.ReviewRepository
userRepository storage.UserRepository
}

func NewHandler(reviewRepository storage.ReviewRepository) *Handler {
func NewHandler(reviewRepository storage.ReviewRepository, userRepository storage.UserRepository) *Handler {
return &Handler{
reviewRepository,
userRepository,
}
}
3 changes: 2 additions & 1 deletion backend/internal/service/handler/users/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (h *Handler) GetUsers(c *fiber.Ctx) error {

func (h *Handler) GetUserById(c *fiber.Ctx) error {
id := c.Params("id")
user, err := h.userRepository.GetUserByID(id, c.Context())
user, err := h.userRepository.GetUserByID(c.Context(), id)

if err != nil {
print(err.Error(), "from transactions err ")
Expand All @@ -36,3 +36,4 @@ func (h *Handler) GetUserById(c *fiber.Ctx) error {

return c.Status(fiber.StatusOK).JSON(user)
}

3 changes: 2 additions & 1 deletion backend/internal/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ func setupRoutes(app *fiber.App, config config.Config) {
})

app.Route("/reviews", func(r fiber.Router) {
reviewHandler := reviews.NewHandler(repository.Review)
reviewHandler := reviews.NewHandler(repository.Review, repository.User)
r.Post("/", reviewHandler.CreateReview)
r.Get("/:id", reviewHandler.GetReviewsByUserID)
})

// this store can be passed to other oauth handlers that need to manage state/verifier values
Expand Down
39 changes: 39 additions & 0 deletions backend/internal/storage/postgres/schema/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,45 @@ func (r *ReviewRepository) CreateReview(ctx context.Context, review *models.Revi
return review, nil
}

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

rows, err := r.Query(ctx, "SELECT * FROM review WHERE user_id = $1", id)

if !rows.Next() {
return []*models.Review{}, nil
}

if err != nil {
return []*models.Review{}, err
}

defer rows.Close()

var reviews []*models.Review
for rows.Next() {
var review models.Review
if err := rows.Scan(
&review.ID,
&review.UserID,
&review.MediaID,
&review.MediaType,
&review.Rating,
&review.Comment,
&review.CreatedAt,
&review.UpdatedAt,
); err != nil {
return nil, err
}
reviews = append(reviews, &review)
}

if err := rows.Err(); err != nil {
return []*models.Review{}, err
}

return reviews, nil
}

func NewReviewRepository(db *pgxpool.Pool) *ReviewRepository {
return &ReviewRepository{
db,
Expand Down
21 changes: 18 additions & 3 deletions backend/internal/storage/postgres/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type UserRepository struct {
}

func (r *UserRepository) GetUsers(ctx context.Context) ([]*models.User, error) {
rows, err := r.db.Query(context.Background(), "SELECT user_id, first_name, last_name, phone, email, address, profile_picture FROM users")
rows, err := r.db.Query(ctx, "SELECT user_id, first_name, last_name, phone, email, address, profile_picture FROM users")
if err != nil {
print(err.Error(), "from transactions err ")
return []*models.User{}, err
Expand Down Expand Up @@ -46,9 +46,9 @@ func (r *UserRepository) GetUsers(ctx context.Context) ([]*models.User, error) {
return users, nil
}

func (r *UserRepository) GetUserByID(id string, ctx context.Context) (*models.User, error) {
func (r *UserRepository) GetUserByID(ctx context.Context, id string) (*models.User, error) {
var user models.User
err := r.db.QueryRow(context.Background(), "SELECT user_id, first_name, last_name, phone, email, profile_picture FROM users WHERE user_id = $1", id).Scan(&user.UserID, &user.FirstName, &user.LastName, &user.Phone, &user.Email, &user.ProfilePicture)
err := r.db.QueryRow(ctx, "SELECT user_id, first_name, last_name, phone, email, profile_picture FROM users WHERE user_id = $1", id).Scan(&user.UserID, &user.FirstName, &user.LastName, &user.Phone, &user.Email, &user.ProfilePicture)

if err != nil {
print(err.Error(), "from transactions err ")
Expand All @@ -58,6 +58,21 @@ func (r *UserRepository) GetUserByID(id string, ctx context.Context) (*models.Us
return &user, nil
}

func (r *UserRepository) UserExists(ctx context.Context, id string) (bool, error) {

rows, err := r.db.Query(ctx, `SELECT * FROM "user" WHERE id = $1`, id)
if err != nil {
return false, err
}
defer rows.Close()

if rows.Next() {
return true, nil
}

return false, nil
}

func NewUserRepository(db *pgxpool.Pool) *UserRepository {
return &UserRepository{
db: db,
Expand Down
6 changes: 5 additions & 1 deletion backend/internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (

type UserRepository interface {
GetUsers(ctx context.Context) ([]*models.User, error)
GetUserByID(id string, ctx context.Context) (*models.User, error)
GetUserByID(ctx context.Context, id string) (*models.User, error)
UserExists(ctx context.Context, id string) (bool, error)
}

type ReviewRepository interface {
GetReviewsByUserID(ctx context.Context, id string) ([]*models.Review, error)
CreateReview(ctx context.Context, review *models.Review) (*models.Review, error)
}

Expand All @@ -19,3 +21,5 @@ type Repository struct {
User UserRepository
Review ReviewRepository
}


Empty file modified frontend/assets/fonts/SpaceMono-Regular.ttf
100755 → 100644
Empty file.
Empty file modified frontend/scripts/reset-project.js
100755 → 100644
Empty file.

0 comments on commit 403be01

Please sign in to comment.