package productsmodule import ( productdto "api/application/products/dto" "api/libs/jwt" "api/libs/logger" "api/models/api" "github.com/gofiber/fiber/v2" ) func newRoutes(c Controller, app *fiber.App) { app.Post("/product", jwt.JwtProtected(), createProduct(c)) app.Put("/product", jwt.JwtProtected(), updateProduct(c)) app.Post("/product/validate", jwt.JwtProtected(), validateCode(c)) app.Get("/products", jwt.JwtProtected(), getProducts(c)) app.Delete("/product", jwt.JwtProtected(), deleteProduct(c)) } func getProducts(controller Controller) func(*fiber.Ctx) error { return func(c *fiber.Ctx) error { response, statusCode, err := controller.GetProducts() if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusInternalServerError) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } return c.Status(statusCode).JSON(response) } } func createProduct(controller Controller) func(*fiber.Ctx) error { return func(c *fiber.Ctx) error { userId := jwt.DecodeJwtSingleKey(c, "id").(string) product := &productdto.ProductDto{} err := c.BodyParser(product) if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusBadRequest) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } err = product.Validate() if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusBadRequest) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } response, statusCode, err := controller.CreateProduct(userId, product) if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusInternalServerError) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } return c.Status(statusCode).JSON(response) } } func updateProduct(controller Controller) func(*fiber.Ctx) error { return func(c *fiber.Ctx) error { userId := jwt.DecodeJwtSingleKey(c, "id").(string) product := &productdto.ProductDto{} err := c.BodyParser(product) if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusBadRequest) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } err = product.Validate() if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusBadRequest) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } response, statusCode, err := controller.UpdateProduct(userId, product) if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusInternalServerError) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } return c.Status(statusCode).JSON(response) } } func validateCode(controller Controller) func(*fiber.Ctx) error { return func(c *fiber.Ctx) error { codeValidate := &productdto.ValidateCodeDto{} err := c.BodyParser(codeValidate) if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusBadRequest) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } err = codeValidate.Validate() if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusBadRequest) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } response, statusCode, err := controller.VerifyCode(codeValidate) if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusInternalServerError) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } return c.Status(statusCode).JSON(response) } } func deleteProduct(controller Controller) func(*fiber.Ctx) error { return func(c *fiber.Ctx) error { delete := &productdto.DeleteProductDto{} err := c.BodyParser(delete) if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusBadRequest) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } err = delete.Validate() if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusBadRequest) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } response, statusCode, err := controller.DeleteProduct(delete) if err != nil { logger.Production.Info(err.Error()) c.Status(fiber.StatusInternalServerError) return c.JSON(api.Response{ Error: true, ErrorMessage: err.Error(), }) } return c.Status(statusCode).JSON(response) } }