Add Balance Api
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package balance
|
||||
|
||||
import (
|
||||
"furtuna-be/internal/common/errors"
|
||||
"furtuna-be/internal/entity"
|
||||
"furtuna-be/internal/handlers/request"
|
||||
"furtuna-be/internal/handlers/response"
|
||||
"furtuna-be/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service services.Balance
|
||||
}
|
||||
|
||||
func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
route := group.Group("/balance")
|
||||
|
||||
route.GET("/partner", jwt, h.GetPartnerBalance)
|
||||
}
|
||||
|
||||
func NewHandler(service services.Balance) *Handler {
|
||||
return &Handler{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) GetPartnerBalance(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
if !ctx.IsPartnerAdmin() {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
updatedBranch, err := h.service.GetByID(ctx, *ctx.GetPartnerID())
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: h.toBalanceResponse(updatedBranch),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) toBalanceResponse(resp *entity.Balance) response.Balance {
|
||||
return response.Balance{
|
||||
PartnerID: resp.PartnerID,
|
||||
Balance: resp.Balance,
|
||||
AuthBalance: resp.AuthBalance,
|
||||
}
|
||||
}
|
||||
@@ -1,264 +0,0 @@
|
||||
package branch
|
||||
|
||||
import (
|
||||
"furtuna-be/internal/common/errors"
|
||||
"furtuna-be/internal/entity"
|
||||
"furtuna-be/internal/handlers/request"
|
||||
"furtuna-be/internal/handlers/response"
|
||||
"furtuna-be/internal/services"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service services.Branch
|
||||
}
|
||||
|
||||
func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
route := group.Group("/branch")
|
||||
|
||||
route.POST("/", jwt, h.Create)
|
||||
route.GET("/list", jwt, h.GetAll)
|
||||
route.PUT("/:id", jwt, h.Update)
|
||||
route.GET("/:id", jwt, h.GetByID)
|
||||
route.DELETE("/:id", jwt, h.Delete)
|
||||
}
|
||||
|
||||
func NewHandler(service services.Branch) *Handler {
|
||||
return &Handler{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
// Create handles the creation of a new branch.
|
||||
// @Summary Create a new branch
|
||||
// @Description Create a new branch based on the provided data.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "JWT token"
|
||||
// @Param req body request.Branch true "New branch details"
|
||||
// @Success 200 {object} response.BaseResponse{data=response.Branch} "Branch created successfully"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/branch [post]
|
||||
// @Tags Branch APIs
|
||||
func (h *Handler) Create(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
var req request.Branch
|
||||
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.toBranchResponse(res),
|
||||
})
|
||||
}
|
||||
|
||||
// Update handles the update of an existing branch.
|
||||
// @Summary Update an existing branch
|
||||
// @Description Update the details of an existing branch based on the provided ID.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "JWT token"
|
||||
// @Param id path int64 true "Branch ID to update"
|
||||
// @Param req body request.Branch true "Updated branch details"
|
||||
// @Success 200 {object} response.BaseResponse{data=response.Branch} "Branch updated successfully"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/branch/{id} [put]
|
||||
// @Tags Branch APIs
|
||||
func (h *Handler) Update(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
branchID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var req request.Branch
|
||||
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
|
||||
}
|
||||
|
||||
updatedBranch, err := h.service.Update(ctx, branchID, req.ToEntity())
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: h.toBranchResponse(updatedBranch),
|
||||
})
|
||||
}
|
||||
|
||||
// GetAll retrieves a list of branches.
|
||||
// @Summary Get a list of branches
|
||||
// @Description Get a paginated list of branches 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.BranchList} "List of branches"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/branch/list [get]
|
||||
// @Tags Branch APIs
|
||||
func (h *Handler) GetAll(c *gin.Context) {
|
||||
var req request.BranchParam
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
branchs, 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.toBranchResponseList(branchs, int64(total), req),
|
||||
})
|
||||
}
|
||||
|
||||
// Delete handles the deletion of a branch by ID.
|
||||
// @Summary Delete a branch by ID
|
||||
// @Description Delete a branch based on the provided ID.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "JWT token"
|
||||
// @Param id path int64 true "Branch ID to delete"
|
||||
// @Success 200 {object} response.BaseResponse "Branch deleted successfully"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/branch/{id} [delete]
|
||||
// @Tags Branch APIs
|
||||
func (h *Handler) Delete(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
id := c.Param("id")
|
||||
|
||||
// Parse the ID into a uint
|
||||
branchID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.service.Delete(ctx, branchID)
|
||||
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 branch by ID.
|
||||
// @Summary Get details of a branch by ID
|
||||
// @Description Get details of a branch based on the provided ID.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "JWT token"
|
||||
// @Param id path int64 true "Branch ID to retrieve"
|
||||
// @Success 200 {object} response.BaseResponse{data=response.Branch} "Branch details"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/branch/{id} [get]
|
||||
// @Tags Branch APIs
|
||||
func (h *Handler) GetByID(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
// Parse the ID into a uint
|
||||
branchID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := h.service.GetByID(c.Request.Context(), branchID)
|
||||
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.toBranchResponse(res),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) toBranchResponse(resp *entity.Branch) response.Branch {
|
||||
return response.Branch{
|
||||
ID: &resp.ID,
|
||||
Name: resp.Name,
|
||||
Status: string(resp.Status),
|
||||
Location: resp.Location,
|
||||
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) toBranchResponseList(resp []*entity.Branch, total int64, req request.BranchParam) response.BranchList {
|
||||
var branches []response.Branch
|
||||
for _, b := range resp {
|
||||
branches = append(branches, h.toBranchResponse(b))
|
||||
}
|
||||
|
||||
return response.BranchList{
|
||||
Branches: branches,
|
||||
Total: total,
|
||||
Limit: req.Limit,
|
||||
Offset: req.Offset,
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,7 @@
|
||||
package response
|
||||
|
||||
type Branch struct {
|
||||
ID *int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Location string `json:"location"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type BranchList struct {
|
||||
Branches []Branch `json:"branches"`
|
||||
Total int64 `json:"total"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
type Balance struct {
|
||||
PartnerID int64 `json:"partner_id"`
|
||||
Balance float64 `json:"balance"`
|
||||
AuthBalance float64 `json:"auth_balance"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user