From 0aeb1bbb15fdbefc08acc6d06096b69bcbe1d788 Mon Sep 17 00:00:00 2001 From: Luisa <103768169+luisa-li@users.noreply.github.com> Date: Thu, 19 Sep 2024 14:27:53 -0400 Subject: [PATCH] I FIXED IT --- backend/internal/models/review.go | 16 ++++++++-------- backend/internal/service/handler/review.go | 9 ++------- .../internal/storage/postgres/schema/review.go | 12 +++++++----- backend/internal/storage/postgres/storage.go | 3 ++- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/backend/internal/models/review.go b/backend/internal/models/review.go index 418aba13..8ad3889a 100644 --- a/backend/internal/models/review.go +++ b/backend/internal/models/review.go @@ -3,12 +3,12 @@ package models import "time" type Review struct { - ID string `json:"id"` - UserID int `json:"user_id"` - MediaID int `json:"media_id"` - MediaType string `json:"media_type"` - Rating int `json:"rating"` - Comment string `json:"comment"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` + ID string `json:"id"` + UserID string `json:"user_id"` + MediaID string `json:"media_id"` + MediaType string `json:"media_type"` + Rating string `json:"rating"` + Comment string `json:"comment"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } diff --git a/backend/internal/service/handler/review.go b/backend/internal/service/handler/review.go index 45565a44..a05bac8a 100644 --- a/backend/internal/service/handler/review.go +++ b/backend/internal/service/handler/review.go @@ -1,8 +1,6 @@ package handler import ( - "fmt" - "platnm/internal/errs" "platnm/internal/storage" "github.com/gofiber/fiber/v2" @@ -20,15 +18,12 @@ func NewReviewHandler(reviewRepository storage.ReviewRepository) *ReviewHandler func (h *ReviewHandler) GetReviewsByUserID(c *fiber.Ctx) error { id := c.Params("id") - if id == "" { - return errs.BadRequest(fmt.Errorf("received invalid ID. got %s", id)) - } - user, err := h.reviewRepository.GetReviewsByUserID(c.Context(), id) + review, err := h.reviewRepository.GetReviewsByUserID(c.Context(), id) if err != nil { print(err.Error(), "from transactions err ") return err } - return c.Status(fiber.StatusOK).JSON(user) + return c.Status(fiber.StatusOK).JSON(review) } diff --git a/backend/internal/storage/postgres/schema/review.go b/backend/internal/storage/postgres/schema/review.go index 7b27bf72..93a54f19 100644 --- a/backend/internal/storage/postgres/schema/review.go +++ b/backend/internal/storage/postgres/schema/review.go @@ -13,9 +13,9 @@ type ReviewRepository struct { db *pgxpool.Pool } -func (r *ReviewRepository) GetReviewsByUserID(id string, ctx context.Context) ([]*models.Review, error) { +func (r *ReviewRepository) GetReviewsByUserID(ctx context.Context, id string) ([]*models.Review, error) { - rows, err := r.db.Query(context.Background(), "SELECT * FROM review WHERE user_id = $1", id) + rows, err := r.db.Query(ctx, "SELECT * FROM review WHERE user_id = $1", id) if err != nil { print(err.Error(), "from transactions err ") return []*models.Review{}, err @@ -27,8 +27,7 @@ func (r *ReviewRepository) GetReviewsByUserID(id string, ctx context.Context) ([ var review models.Review - var ID, mediaType, comment *string - var userID, mediaID, rating *int + var mediaType, comment, userID, mediaID, rating *string var createdAt, updatedAt *time.Time if err := rows.Scan(&review.ID, &userID, &mediaID, &mediaType, &rating, &comment, &createdAt, &updatedAt); err != nil { @@ -36,7 +35,8 @@ func (r *ReviewRepository) GetReviewsByUserID(id string, ctx context.Context) ([ return reviews, err } - review.ID = *ID + print("are we here plsssss") + review.UserID = *userID review.MediaID = *mediaID review.MediaType = *mediaType @@ -45,6 +45,8 @@ func (r *ReviewRepository) GetReviewsByUserID(id string, ctx context.Context) ([ review.CreatedAt = *createdAt review.UpdatedAt = *updatedAt + print("chekc2 check2") + reviews = append(reviews, &review) } diff --git a/backend/internal/storage/postgres/storage.go b/backend/internal/storage/postgres/storage.go index 532def31..3b43e90a 100644 --- a/backend/internal/storage/postgres/storage.go +++ b/backend/internal/storage/postgres/storage.go @@ -40,6 +40,7 @@ func ConnectDatabase(host, user, password, dbname, port string) *pgxpool.Pool { func NewRepository(db *pgxpool.Pool) *storage.Repository { return &storage.Repository{ - User: user.NewUserRepository(db), + User: user.NewUserRepository(db), + Review: user.NewReviewRepository(db), } }