Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
122 changes: 122 additions & 0 deletions adapters/http/rating.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package http

import (
"net/http"
"strings"

"github.com/labstack/echo"
"github.com/ucladevx/BPool/interfaces"
"github.com/ucladevx/BPool/models"
"github.com/ucladevx/BPool/services"
"github.com/ucladevx/BPool/utils/auth"
)

type (
// RatingService is used to provide and check ratings
RatingService interface {
Create(models.Rating, *auth.UserClaims) (*models.Rating, error)
GetByID(string, *auth.UserClaims) (*models.Rating, error)
GetRatingByUserID(string) (float32, error)
Delete(string, *auth.UserClaims) error
}

// RatingController http adapter
RatingController struct {
service RatingService
passengerService PassengerService
logger interfaces.Logger
}
)

// NewRatingController creates a new rating controller
func NewRatingController(ratingService RatingService, p PassengerService, l interfaces.Logger) *RatingController {
return &RatingController{
service: ratingService,
passengerService: p,
logger: l,
}
}

// MountRoutes mounts the rating routes
func (ratingController *RatingController) MountRoutes(c *echo.Group) {
c.DELETE(
"/ratings/:id",
ratingController.delete,
auth.NewAuthMiddleware(services.AdminLevel, ratingController.logger),
)

c.Use(auth.NewAuthMiddleware(services.UserLevel, ratingController.logger))

c.GET("/ratings/:id", ratingController.show)
c.GET("/ratings/user/:id", ratingController.getUserRating)
c.POST("/ratings", ratingController.create)
}

func (ratingController *RatingController) show(c echo.Context) error {
id := c.Param("id")
user := userClaimsFromContext(c)

rating, err := ratingController.service.GetByID(id, user)

if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

return c.JSON(http.StatusOK, echo.Map{
"data": rating,
})
}

func (ratingController *RatingController) getUserRating(c echo.Context) error {
userID := c.Param("id")

rating, err := ratingController.service.GetRatingByUserID(userID)

if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

return c.JSON(http.StatusOK, echo.Map{
"data": rating,
})
}

func (ratingController *RatingController) create(c echo.Context) error {
data := models.Rating{}

if err := c.Bind(&data); err != nil {
message := err.Error()
if strings.HasPrefix(message, "code=400, message=Syntax error") {
message = "Invalid JSON"
}

return echo.NewHTTPError(http.StatusBadRequest, message)
}

userClaims := userClaimsFromContext(c)
rating, err := ratingController.service.Create(data, userClaims)

if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

return c.JSON(http.StatusOK, echo.Map{
"data": rating,
})
}

func (ratingController *RatingController) delete(c echo.Context) error {
id := c.Param("id")
userClaims := userClaimsFromContext(c)

err := ratingController.service.Delete(id, userClaims)

if err != nil {
status := 400
// fill in other errors

return echo.NewHTTPError(status, err.Error())
}

return c.NoContent(http.StatusNoContent)
}
5 changes: 5 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,22 @@ func Start() {
carStore := postgres.NewCarStore(db)
rideStore := postgres.NewRideStore(db)
passengerStore := postgres.NewPassengerStore(db)
ratingStore := postgres.NewRatingStore(db)

postgres.CreateTables(
userStore,
carStore,
rideStore,
passengerStore,
ratingStore,
)

userService := services.NewUserService(userStore, tokenizer, logger)
carService := services.NewCarService(carStore, logger)
rideService := services.NewRideService(rideStore, carService, logger)
passengerService := services.NewPassengerService(passengerStore, rideService, logger)
feedService := services.NewFeedService(rideStore, logger)
ratingService := services.NewRatingService(ratingStore, passengerService, logger)

userController := http.NewUserController(
userService,
Expand All @@ -95,6 +98,7 @@ func Start() {
carController := http.NewCarController(carService, logger)
passengersController := http.NewPassengerController(passengerService, logger)
feedController := http.NewFeedController(feedService, logger)
ratingController := http.NewRatingController(ratingService, passengerService, logger)

app := echo.New()
app.HTTPErrorHandler = handleError(logger)
Expand Down Expand Up @@ -126,6 +130,7 @@ func Start() {
carController.MountRoutes(app.Group("/api/v1"))
passengersController.MountRoutes(app.Group("/api/v1"))
feedController.MountRoutes(app.Group("/api/v1"))
ratingController.MountRoutes(app.Group("/api/v1"))

logger.Info("CONFIG", "env", env)
port := ":" + conf.Get("port")
Expand Down
107 changes: 107 additions & 0 deletions mocks/RatingStore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions models/rating.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package models

import (
"fmt"
"time"

validation "github.com/go-ozzo/ozzo-validation"
)

// Rating model instance
type Rating struct {
ID string `json:"id"`
Rating int `json:"rating"`
RideID string `json:"ride_id" db:"ride_id"`
RaterID string `json:"rater_id"`
RateeID string `json:"ratee_id"`
Comment string `json:"comment"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

// Validate validates rating before insertion
func (r *Rating) Validate() error {
return validation.ValidateStruct(r,
validation.Field(&r.Rating, validation.Required, validation.Min(1)),
validation.Field(&r.RideID, validation.Required),
validation.Field(&r.RaterID, validation.Required),
validation.Field(&r.RateeID, validation.Required),
)
}

func (r *Rating) String() string {
return fmt.Sprintf("<Rating id: %s, rating: %d, rideID: %s, rater: %s, ratee: %s",
r.ID, r.Rating, r.RideID, r.RaterID, r.RateeID,
)
}
Loading