-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrating.go
122 lines (95 loc) · 2.99 KB
/
rating.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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)
}