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,
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,31 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
"furtuna-be/internal/constants/transaction"
|
||||
"furtuna-be/internal/entity"
|
||||
)
|
||||
|
||||
type Transaction struct {
|
||||
PaymentMethod transaction.PaymentMethod
|
||||
}
|
||||
|
||||
type TransactionSearch struct {
|
||||
Id string `form:"id" json:"id" example:"1"`
|
||||
Date string `form:"date" json:"date" example:"1"`
|
||||
Limit int `form:"limit,default=10"`
|
||||
Offset int `form:"offset,default=0"`
|
||||
Status string `form:"status,default="`
|
||||
Type string `form:"type,default="`
|
||||
}
|
||||
|
||||
func (t *TransactionSearch) ToEntity(ctx mycontext.Context) entity.TransactionSearch {
|
||||
return entity.TransactionSearch{
|
||||
PartnerID: ctx.GetPartnerID(),
|
||||
Type: t.Type,
|
||||
Status: t.Status,
|
||||
Limit: t.Limit,
|
||||
Offset: t.Offset,
|
||||
Date: t.Date,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package response
|
||||
|
||||
import "time"
|
||||
|
||||
type TransactionListItem struct {
|
||||
ID string `json:"id"`
|
||||
TransactionType string `json:"transaction_type"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
SiteName string `json:"site_name"`
|
||||
Amount int64 `json:"amount"`
|
||||
PartnerName string `json:"partner_name"`
|
||||
}
|
||||
|
||||
type TransactionListResponse struct {
|
||||
Transactions []TransactionListItem `json:"transactions"`
|
||||
TotalCount int `json:"total_count"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
Reference in New Issue
Block a user