Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get reviews for user #20

Merged
merged 21 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ require (
require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/goccy/go-json v0.10.3
github.com/gofiber/storage v1.3.3
github.com/google/uuid v1.5.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
2 changes: 0 additions & 2 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/gofiber/storage v1.3.3 h1:XvcaYqEVcoXllhnkvH8sTIbuB1IbidIe3UlxzMvzBrE=
github.com/gofiber/storage v1.3.3/go.mod h1:ZGviBmsCFkBSQInsD4C5e390gXwnH/MKcpKk//Fwv6o=
github.com/gofiber/storage/memory v1.3.4 h1:VxTq8Vrdvk73VsfvtTgc3LjXClbBrHXanzEo+w0cpgo=
github.com/gofiber/storage/memory v1.3.4/go.mod h1:pYsCUle/+4exGfsG7IlpmFYBVmNntP8OIDBvmABU8PE=
github.com/gofiber/utils v1.0.1 h1:knct4cXwBipWQqFrOy1Pv6UcgPM+EXo9jDgc66V1Qio=
Expand Down
14 changes: 14 additions & 0 deletions backend/internal/models/review.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package models

import "time"

type Review struct {
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"`
}
28 changes: 28 additions & 0 deletions backend/internal/service/handler/users/review.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package users
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure this lives in package reviews and a corresponding reviews folder!

also, following a more modular handler structure where each handler method gets its own file is a good way for us to avoid having massive files - see this PR's create_review file as an example

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!


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) GetReviewsByUserID(c *fiber.Ctx) error {
id := c.Params("id")
review,err := h.reviewRepository.GetReviewsByUserID(c.Context(), id)

if err != nil {
return err
}

return c.Status(fiber.StatusOK).JSON(review)
}
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 *UserHandler) GetUsers(c *fiber.Ctx) error {

func (h *UserHandler) 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 *UserHandler) GetUserById(c *fiber.Ctx) error {

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

5 changes: 5 additions & 0 deletions backend/internal/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func setupRoutes(app *fiber.App, config config.Config) {
r.Get("/:id", userHandler.GetUserById)
})

reviewHandler := users.NewReviewHandler(repository.Review)
app.Route("/reviews", func(r fiber.Router) {
r.Get("/:id", reviewHandler.GetReviewsByUserID)
})

// this store can be passed to other oauth handlers that need to manage state/verifier values
store := oauth.NewSessionValueStore(session.Config{
Storage: memory.New(),
Expand Down
71 changes: 71 additions & 0 deletions backend/internal/storage/postgres/schema/review.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package user

import (
"context"
"platnm/internal/models"

"time"

"github.com/jackc/pgx/v5/pgxpool"

"fmt"
)

type ReviewRepository struct {
db *pgxpool.Pool
}

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

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

if !rows.Next() {
fmt.Println("No rows found, likely an invalid user_id.")
}

if err != nil {
print(err.Error(), "from transactions err ")
ddusichka marked this conversation as resolved.
Show resolved Hide resolved
print("id %s is not a valid id ", id)
return []*models.Review{}, err
}

defer rows.Close()

var reviews []*models.Review
for rows.Next() {

var review models.Review

var mediaType, comment, userID, mediaID, rating *string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think u need these temp vars. u can bind directly to fields of review struct that u define on line 28

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see this from the docs

var createdAt, updatedAt *time.Time

if err := rows.Scan(&review.ID, &userID, &mediaID, &mediaType, &rating, &comment, &createdAt, &updatedAt); err != nil {
print(err.Error(), "from transactions err ")
ddusichka marked this conversation as resolved.
Show resolved Hide resolved
return reviews, err
}

review.UserID = *userID
review.MediaID = *mediaID
review.MediaType = *mediaType
review.Comment = *comment
review.Rating = *rating
review.CreatedAt = *createdAt
review.UpdatedAt = *updatedAt

reviews = append(reviews, &review)
}

if err := rows.Err(); err != nil {
print(err.Error(), "from transactions err ")
ddusichka marked this conversation as resolved.
Show resolved Hide resolved
return []*models.Review{}, err
}

return reviews, nil

}

func NewReviewRepository(db *pgxpool.Pool) *ReviewRepository {
return &ReviewRepository{
db: db,
}
}
6 changes: 3 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 Down
3 changes: 2 additions & 1 deletion backend/internal/storage/postgres/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewRepository(config config.DB) *storage.Repository {
db := ConnectDatabase(config)

return &storage.Repository{
User: user.NewUserRepository(db),
User: user.NewUserRepository(db),
Review: user.NewReviewRepository(db),
}
}
9 changes: 8 additions & 1 deletion backend/internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ 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)
}

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

// Repository storage of all repositories.
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.
Loading