2023-10-08 15:59:42 +07:00
|
|
|
package partner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"furtuna-be/internal/common/errors"
|
|
|
|
|
"furtuna-be/internal/entity"
|
|
|
|
|
"furtuna-be/internal/handlers/request"
|
|
|
|
|
"furtuna-be/internal/handlers/response"
|
|
|
|
|
"furtuna-be/internal/middlewares"
|
|
|
|
|
"furtuna-be/internal/services"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
|
service services.Partner
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
|
|
|
route := group.Group("/partner")
|
|
|
|
|
isSuperAdmin := middlewares.SuperAdminMiddleware()
|
|
|
|
|
|
|
|
|
|
route.POST("/", jwt, isSuperAdmin, h.Create)
|
|
|
|
|
route.GET("/list", jwt, isSuperAdmin, h.GetAll)
|
|
|
|
|
route.PUT("/:id", jwt, isSuperAdmin, h.Update)
|
|
|
|
|
route.GET("/:id", jwt, isSuperAdmin, h.GetByID)
|
|
|
|
|
route.DELETE("/:id", jwt, isSuperAdmin, h.Delete)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewHandler(service services.Partner) *Handler {
|
|
|
|
|
return &Handler{
|
|
|
|
|
service: service,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create handles the creation of a new Partner.
|
|
|
|
|
// @Summary Create a new Partner
|
|
|
|
|
// @Description Create a new Partner based on the provided data.
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param Authorization header string true "JWT token"
|
|
|
|
|
// @Param req body request.Partner true "New Partner details"
|
|
|
|
|
// @Success 200 {object} response.BaseResponse{data=response.Partner} "Partner created successfully"
|
|
|
|
|
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
|
|
|
|
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
|
|
|
|
// @Router /api/v1/Partner [post]
|
|
|
|
|
// @Tags Partner APIs
|
|
|
|
|
func (h *Handler) Create(c *gin.Context) {
|
|
|
|
|
ctx := request.GetMyContext(c)
|
|
|
|
|
|
2024-06-03 14:40:50 +07:00
|
|
|
var req request.CreatePartnerRequest
|
2023-10-08 15:59:42 +07:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate := validator.New()
|
|
|
|
|
if err := validate.Struct(req); err != nil {
|
|
|
|
|
response.ErrorWrapper(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := h.service.Create(ctx, req.ToEntity())
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ErrorWrapper(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Status: http.StatusOK,
|
|
|
|
|
Data: h.toPartnerResponse(res),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update handles the update of an existing Partner.
|
|
|
|
|
// @Summary Update an existing Partner
|
|
|
|
|
// @Description Update the details of an existing Partner based on the provided ID.
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param Authorization header string true "JWT token"
|
|
|
|
|
// @Param id path int64 true "Partner ID to update"
|
|
|
|
|
// @Param req body request.Partner true "Updated Partner details"
|
|
|
|
|
// @Success 200 {object} response.BaseResponse{data=response.Partner} "Partner updated successfully"
|
|
|
|
|
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
|
|
|
|
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
|
|
|
|
// @Router /api/v1/Partner/{id} [put]
|
|
|
|
|
// @Tags Partner APIs
|
|
|
|
|
func (h *Handler) Update(c *gin.Context) {
|
|
|
|
|
ctx := request.GetMyContext(c)
|
|
|
|
|
|
|
|
|
|
id := c.Param("id")
|
|
|
|
|
|
|
|
|
|
PartnerID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var req request.Partner
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 14:18:18 +07:00
|
|
|
updatedPartner, err := h.service.Update(ctx, req.ToEntityUpdate(PartnerID))
|
2023-10-08 15:59:42 +07:00
|
|
|
if err != nil {
|
|
|
|
|
response.ErrorWrapper(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Status: http.StatusOK,
|
|
|
|
|
Data: h.toPartnerResponse(updatedPartner),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAll retrieves a list of Partneres.
|
|
|
|
|
// @Summary Get a list of Partneres
|
|
|
|
|
// @Description Get a paginated list of Partneres based on query parameters.
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param Authorization header string true "JWT token"
|
|
|
|
|
// @Param Limit query int false "Number of items to retrieve (default 10)"
|
|
|
|
|
// @Param Offset query int false "Offset for pagination (default 0)"
|
|
|
|
|
// @Success 200 {object} response.BaseResponse{data=response.PartnerList} "List of Partneres"
|
|
|
|
|
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
|
|
|
|
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
|
|
|
|
// @Router /api/v1/Partner/list [get]
|
|
|
|
|
// @Tags Partner APIs
|
|
|
|
|
func (h *Handler) GetAll(c *gin.Context) {
|
|
|
|
|
var req request.PartnerParam
|
|
|
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Partners, total, err := h.service.GetAll(c.Request.Context(), req.ToEntity())
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ErrorWrapper(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Status: http.StatusOK,
|
|
|
|
|
Data: h.toPartnerResponseList(Partners, int64(total), req),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete handles the deletion of a Partner by ID.
|
|
|
|
|
// @Summary Delete a Partner by ID
|
|
|
|
|
// @Description Delete a Partner based on the provided ID.
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param Authorization header string true "JWT token"
|
|
|
|
|
// @Param id path int64 true "Partner ID to delete"
|
|
|
|
|
// @Success 200 {object} response.BaseResponse "Partner deleted successfully"
|
|
|
|
|
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
|
|
|
|
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
|
|
|
|
// @Router /api/v1/Partner/{id} [delete]
|
|
|
|
|
// @Tags Partner APIs
|
|
|
|
|
func (h *Handler) Delete(c *gin.Context) {
|
|
|
|
|
ctx := request.GetMyContext(c)
|
|
|
|
|
id := c.Param("id")
|
|
|
|
|
|
|
|
|
|
// Parse the ID into a uint
|
|
|
|
|
PartnerID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = h.service.Delete(ctx, PartnerID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, response.BaseResponse{
|
|
|
|
|
Success: false,
|
|
|
|
|
Status: http.StatusInternalServerError,
|
|
|
|
|
Message: err.Error(),
|
|
|
|
|
Data: nil,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Status: http.StatusOK,
|
|
|
|
|
Data: nil,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetByID retrieves details of a specific Partner by ID.
|
|
|
|
|
// @Summary Get details of a Partner by ID
|
|
|
|
|
// @Description Get details of a Partner based on the provided ID.
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param Authorization header string true "JWT token"
|
|
|
|
|
// @Param id path int64 true "Partner ID to retrieve"
|
|
|
|
|
// @Success 200 {object} response.BaseResponse{data=response.Partner} "Partner details"
|
|
|
|
|
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
|
|
|
|
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
|
|
|
|
// @Router /api/v1/Partner/{id} [get]
|
|
|
|
|
// @Tags Partner APIs
|
|
|
|
|
func (h *Handler) GetByID(c *gin.Context) {
|
|
|
|
|
id := c.Param("id")
|
|
|
|
|
|
|
|
|
|
// Parse the ID into a uint
|
|
|
|
|
PartnerID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := h.service.GetByID(c.Request.Context(), PartnerID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, response.BaseResponse{
|
|
|
|
|
Success: false,
|
|
|
|
|
Status: http.StatusInternalServerError,
|
|
|
|
|
Message: err.Error(),
|
|
|
|
|
Data: nil,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Status: http.StatusOK,
|
|
|
|
|
Data: h.toPartnerResponse(res),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) toPartnerResponse(resp *entity.Partner) response.Partner {
|
|
|
|
|
return response.Partner{
|
2024-07-30 14:18:18 +07:00
|
|
|
ID: &resp.ID,
|
|
|
|
|
Name: resp.Name,
|
|
|
|
|
Status: resp.Status,
|
|
|
|
|
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
|
|
|
|
|
UpdatedAt: resp.CreatedAt.Format(time.RFC3339),
|
|
|
|
|
Balance: resp.Balance,
|
|
|
|
|
AdminName: resp.AdminName,
|
|
|
|
|
AdminPhoneNumber: resp.AdminPhoneNumber,
|
|
|
|
|
AdminEmail: resp.AdminEmail,
|
|
|
|
|
BankAccountName: resp.BankName,
|
|
|
|
|
BankAccountHolderName: resp.BankAccountHolderName,
|
|
|
|
|
BankAccountHolderNumber: resp.BankAccountNumber,
|
2023-10-08 15:59:42 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) toPartnerResponseList(resp []*entity.Partner, total int64, req request.PartnerParam) response.PartnerList {
|
|
|
|
|
var Partneres []response.Partner
|
|
|
|
|
for _, b := range resp {
|
|
|
|
|
Partneres = append(Partneres, h.toPartnerResponse(b))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.PartnerList{
|
|
|
|
|
Partners: Partneres,
|
|
|
|
|
Total: total,
|
|
|
|
|
Limit: req.Limit,
|
|
|
|
|
Offset: req.Offset,
|
|
|
|
|
}
|
|
|
|
|
}
|