package restricted import ( "strconv" "git.ma-al.com/goc_daniel/b2b/app/service/addressesService" "git.ma-al.com/goc_daniel/b2b/app/utils/i18n" "git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor" "git.ma-al.com/goc_daniel/b2b/app/utils/logger" "git.ma-al.com/goc_daniel/b2b/app/utils/nullable" "git.ma-al.com/goc_daniel/b2b/app/utils/response" "git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors" "github.com/gofiber/fiber/v3" ) type AddressesHandler struct { addressesService *addressesService.AddressesService } func NewAddressesHandler() *AddressesHandler { addressesService := addressesService.New() return &AddressesHandler{ addressesService: addressesService, } } func AddressesHandlerRoutes(r fiber.Router) fiber.Router { handler := NewAddressesHandler() r.Get("/get-template", handler.GetTemplate) r.Post("/add-new-address", handler.AddNewAddress) r.Post("/modify-address", handler.ModifyAddress) r.Get("/retrieve-addresses", handler.RetrieveAddressesInfo) r.Delete("/delete-address", handler.DeleteAddress) return r } func (h *AddressesHandler) GetTemplate(c fiber.Ctx) error { country_id_attribute := c.Query("country_id") country_id, err := strconv.Atoi(country_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } template, err := h.addressesService.GetTemplate(uint(country_id)) if err != nil { logger.Error("failed to get address template", "handler", "AddressesHandler.GetTemplate", "country_id", country_id, "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(&template, 0, i18n.T_(c, response.Message_OK))) } func (h *AddressesHandler) AddNewAddress(c fiber.Ctx) error { userID, ok := localeExtractor.GetUserID(c) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } address_info := string(c.Body()) if address_info == "" { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } country_id_attribute := c.Query("country_id") country_id, err := strconv.Atoi(country_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } err = h.addressesService.AddNewAddress(userID, address_info, uint(country_id)) if err != nil { logger.Error("failed to add new address", "handler", "AddressesHandler.AddNewAddress", "user_id", userID, "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) } func (h *AddressesHandler) ModifyAddress(c fiber.Ctx) error { userID, ok := localeExtractor.GetUserID(c) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } address_id_attribute := c.Query("address_id") address_id, err := strconv.Atoi(address_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } address_info := string(c.Body()) if address_info == "" { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } country_id_attribute := c.Query("country_id") country_id, err := strconv.Atoi(country_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } err = h.addressesService.ModifyAddress(userID, uint(address_id), address_info, uint(country_id)) if err != nil { logger.Error("failed to modify address", "handler", "AddressesHandler.ModifyAddress", "user_id", userID, "address_id", address_id, "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) } func (h *AddressesHandler) RetrieveAddressesInfo(c fiber.Ctx) error { userID, ok := localeExtractor.GetUserID(c) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } addresses, err := h.addressesService.RetrieveAddresses(userID) if err != nil { logger.Error("failed to retrieve addresses", "handler", "AddressesHandler.RetrieveAddressesInfo", "user_id", userID, "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(addresses, 0, i18n.T_(c, response.Message_OK))) } func (h *AddressesHandler) DeleteAddress(c fiber.Ctx) error { userID, ok := localeExtractor.GetUserID(c) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } address_id_attribute := c.Query("address_id") address_id, err := strconv.Atoi(address_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } err = h.addressesService.DeleteAddress(userID, uint(address_id)) if err != nil { logger.Error("failed to delete address", "handler", "AddressesHandler.DeleteAddress", "user_id", userID, "address_id", address_id, "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) }