Add Transaction
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
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)
|
||||
}
|
||||
|
||||
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) 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,
|
||||
SiteName: transaction.SiteName,
|
||||
Amount: transaction.Amount,
|
||||
PartnerName: transaction.PartnerName,
|
||||
}
|
||||
}
|
||||
|
||||
return response.TransactionListResponse{
|
||||
Transactions: responseItems,
|
||||
TotalCount: totalCount,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user