117 lines
4.4 KiB
Go
117 lines
4.4 KiB
Go
package controllers
|
|
|
|
import (
|
|
"GoMembership/internal/constants"
|
|
"GoMembership/internal/models"
|
|
"GoMembership/internal/services"
|
|
"GoMembership/internal/utils"
|
|
"GoMembership/pkg/errors"
|
|
"GoMembership/pkg/logger"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type CarController struct {
|
|
S services.CarServiceInterface
|
|
UserService services.UserServiceInterface
|
|
}
|
|
|
|
func (cr *CarController) Create(c *gin.Context) {
|
|
requestUser, err := cr.UserService.FromContext(c)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error extracting user from context in Create car handler", http.StatusBadRequest, errors.Responses.Fields.User, errors.Responses.Keys.NoAuthToken)
|
|
return
|
|
}
|
|
if !requestUser.HasPrivilege(constants.Priviliges.Create) {
|
|
utils.RespondWithError(c, errors.ErrNotAuthorized, fmt.Sprintf("Not allowed to create a car. RoleID(%v)<Privilige(%v)", requestUser.RoleID, constants.Priviliges.Create), http.StatusUnauthorized, errors.Responses.Fields.User, errors.Responses.Keys.Unauthorized)
|
|
return
|
|
}
|
|
var newCar models.Car
|
|
if err := c.ShouldBindJSON(&newCar); err != nil {
|
|
utils.HandleValidationError(c, err)
|
|
return
|
|
}
|
|
car, err := cr.S.Create(&newCar)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error creating car", http.StatusInternalServerError, errors.Responses.Fields.Car, errors.Responses.Keys.InternalServerError)
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, car)
|
|
}
|
|
|
|
func (cr *CarController) Update(c *gin.Context) {
|
|
requestUser, err := cr.UserService.FromContext(c)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error extracting user from context in Update car handler", http.StatusBadRequest, errors.Responses.Fields.User, errors.Responses.Keys.NoAuthToken)
|
|
return
|
|
}
|
|
if !requestUser.HasPrivilege(constants.Priviliges.Update) {
|
|
utils.RespondWithError(c, errors.ErrNotAuthorized, fmt.Sprintf("Not allowed to update a car. RoleID(%v)<Privilige(%v)", requestUser.RoleID, constants.Priviliges.Update), http.StatusUnauthorized, errors.Responses.Fields.User, errors.Responses.Keys.Unauthorized)
|
|
return
|
|
}
|
|
var car models.Car
|
|
if err := c.ShouldBindJSON(&car); err != nil {
|
|
utils.HandleValidationError(c, err)
|
|
return
|
|
}
|
|
logger.Error.Printf("updating car: %v", car)
|
|
updatedCar, err := cr.S.Update(&car)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error updating car", http.StatusInternalServerError, errors.Responses.Fields.Car, errors.Responses.Keys.InternalServerError)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, updatedCar)
|
|
}
|
|
|
|
func (cr *CarController) GetAll(c *gin.Context) {
|
|
requestUser, err := cr.UserService.FromContext(c)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error extracting user from context in GetAll car handler", http.StatusBadRequest, errors.Responses.Fields.User, errors.Responses.Keys.NoAuthToken)
|
|
return
|
|
}
|
|
|
|
if !requestUser.HasPrivilege(constants.Priviliges.View) {
|
|
utils.RespondWithError(c, errors.ErrNotAuthorized, fmt.Sprintf("Not allowed to access car data. RoleID(%v)<Privilige(%v)", requestUser.RoleID, constants.Priviliges.Delete), http.StatusUnauthorized, errors.Responses.Fields.User, errors.Responses.Keys.Unauthorized)
|
|
return
|
|
}
|
|
|
|
cars, err := cr.S.GetAll()
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error getting cars", http.StatusInternalServerError, errors.Responses.Fields.Car, errors.Responses.Keys.InternalServerError)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"cars": cars,
|
|
})
|
|
}
|
|
|
|
func (cr *CarController) Delete(c *gin.Context) {
|
|
type input struct {
|
|
ID uint `json:"id" binding:"required,numeric"`
|
|
}
|
|
var deleteData input
|
|
requestUser, err := cr.UserService.FromContext(c)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error extracting user from context in Delete car handler", http.StatusBadRequest, errors.Responses.Fields.User, errors.Responses.Keys.NoAuthToken)
|
|
return
|
|
}
|
|
|
|
if !requestUser.HasPrivilege(constants.Priviliges.Delete) {
|
|
utils.RespondWithError(c, errors.ErrNotAuthorized, fmt.Sprintf("Not allowed to delete a car. RoleID(%v)<Privilige(%v)", requestUser.RoleID, constants.Priviliges.Delete), http.StatusUnauthorized, errors.Responses.Fields.User, errors.Responses.Keys.Unauthorized)
|
|
return
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&deleteData); err != nil {
|
|
utils.HandleValidationError(c, err)
|
|
return
|
|
}
|
|
err = cr.S.Delete(&deleteData.ID)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error deleting car", http.StatusInternalServerError, errors.Responses.Fields.Car, errors.Responses.Keys.InternalServerError)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, "Car deleted")
|
|
}
|