120 lines
3.5 KiB
Go
120 lines
3.5 KiB
Go
package transaction
|
|
|
|
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 TransactionHandler struct {
|
|
service services.Transaction
|
|
}
|
|
|
|
func (h *TransactionHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
route := group.Group("/transaction")
|
|
|
|
route.GET("/search", jwt, h.Search)
|
|
route.POST("/approval", jwt, h.Approval)
|
|
}
|
|
|
|
func New(service services.Transaction) *TransactionHandler {
|
|
return &TransactionHandler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
// Search retrieves a list of studios based on search criteria.
|
|
// @Summary Search for studios
|
|
// @Description Search for studios based on query parameters.
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "JWT token"
|
|
// @Param Name query string false "Studio name for search"
|
|
// @Param Status query string false "Studio status for search"
|
|
// @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.StudioList} "List of studios"
|
|
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
|
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
|
// @Router /api/v1/studio/search [get]
|
|
// @Tags Studio APIs
|
|
func (h *TransactionHandler) Search(c *gin.Context) {
|
|
var req request.TransactionSearch
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
ctx := request.GetMyContext(c)
|
|
|
|
transactions, total, err := h.service.GetTransactionList(ctx, req.ToEntity(ctx))
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: h.ToTransactionListResponse(transactions, total, req.Limit, req.Offset),
|
|
})
|
|
}
|
|
|
|
func (h *TransactionHandler) Approval(c *gin.Context) {
|
|
var req request.ApprovalRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
ctx := request.GetMyContext(c)
|
|
|
|
if !ctx.IsAdmin() {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := req.Validate(); err != nil {
|
|
response.ErrorWrapper(c, errors.NewError(errors.ErrorBadRequest.ErrorType(), err.Error()))
|
|
return
|
|
}
|
|
|
|
err := h.service.Approval(ctx, req.ToEntity())
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
})
|
|
}
|
|
|
|
func (h *TransactionHandler) ToTransactionListResponse(transactions []*entity.TransactionList, totalCount, limit, offset int) response.TransactionListResponse {
|
|
responseItems := make([]response.TransactionListItem, len(transactions))
|
|
for i, transaction := range transactions {
|
|
responseItems[i] = response.TransactionListItem{
|
|
ID: transaction.ID,
|
|
TransactionType: transaction.TransactionType,
|
|
Status: transaction.Status,
|
|
CreatedAt: transaction.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
SiteName: transaction.SiteName,
|
|
Amount: transaction.Amount,
|
|
PartnerName: transaction.PartnerName,
|
|
Total: transaction.Total,
|
|
Fee: transaction.Fee,
|
|
}
|
|
}
|
|
|
|
return response.TransactionListResponse{
|
|
Transactions: responseItems,
|
|
TotalCount: totalCount,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
}
|
|
}
|