feat: history-order-list

This commit is contained in:
ferdiansyah783
2024-07-26 11:37:22 +07:00
parent f4d782653d
commit 195371f5c6
9 changed files with 218 additions and 6 deletions
+52 -1
View File
@@ -6,9 +6,11 @@ import (
"furtuna-be/internal/handlers/request"
"furtuna-be/internal/handlers/response"
"furtuna-be/internal/services"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"net/http"
)
type Handler struct {
@@ -20,6 +22,7 @@ func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
route.POST("/inquiry", jwt, h.Inquiry)
route.POST("/execute", jwt, h.Execute)
route.GET("/history", jwt, h.GetAllHistoryOrders)
}
func NewHandler(service services.Order) *Handler {
@@ -151,3 +154,51 @@ func MapOrderToExecuteOrderResponse(orderResponse *entity.ExecuteOrderResponse)
RedirectURL: orderResponse.RedirectURL,
}
}
func (h *Handler) toHistoryOrderResponse(resp *entity.HistoryOrder) response.HistoryOrder {
return response.HistoryOrder{
ID: resp.ID,
Employee: resp.Employee,
Site: resp.Site,
Timestamp: resp.Timestamp.Format(time.RFC3339),
BookingTime: resp.BookingTime.Format(time.RFC3339),
Tickets: resp.Tickets,
PaymentType: resp.PaymentType,
Status: resp.Status,
Amount: resp.Amount,
}
}
func (h *Handler) GetAllHistoryOrders(c *gin.Context) {
var req request.HistoryOrderParam
if err := c.ShouldBindQuery(&req); err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
orders, total, err := h.service.GetAllHistoryOrders(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.toHistoryOrderList(orders, int64(total), req),
})
}
func (h *Handler) toHistoryOrderList(resp []*entity.HistoryOrder, total int64, req request.HistoryOrderParam) response.HistoryOrderList {
var orders []response.HistoryOrder
for _, b := range resp {
orders = append(orders, h.toHistoryOrderResponse(b))
}
return response.HistoryOrderList{
Orders: orders,
Total: total,
Limit: req.Limit,
Offset: req.Offset,
}
}
+12
View File
@@ -11,6 +11,18 @@ type Order struct {
OrderItems []OrderItem `json:"order_items" validate:"required"`
}
type HistoryOrderParam struct {
Limit int `form:"limit" json:"limit" example:"10"`
Offset int `form:"offset" json:"offset" example:"0"`
}
func (o *HistoryOrderParam) ToEntity() entity.HistoryOrderSearch {
return entity.HistoryOrderSearch{
Limit: o.Limit,
Offset: o.Offset,
}
}
type OrderItem struct {
ProductID int64 `json:"product_id" validate:"required"`
Quantity int64 `json:"quantity" validate:"required"`
+19
View File
@@ -21,6 +21,18 @@ type Order struct {
UpdatedAt string `json:"updated_at"`
}
type HistoryOrder struct {
ID int64 `json:"id"`
Employee string `json:"employee"`
Site string `json:"site"`
Timestamp string `json:"timestamp"`
BookingTime string `json:"booking_time"`
Tickets []string `json:"tickets"`
PaymentType string `json:"payment_type"`
Status string `json:"status"`
Amount float64 `json:"amount"`
}
type OrderItem struct {
OrderItemID int64 `json:"order_item_id" `
ItemID int64 `json:"item_id" `
@@ -39,6 +51,13 @@ type OrderList struct {
Offset int `json:"offset"`
}
type HistoryOrderList struct {
Orders []HistoryOrder `json:"history_orders"`
Total int64 `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
type OrderMonthlyRevenue struct {
TotalRevenue float64 `json:"total_revenue"`
TotalTransaction int64 `json:"total_transaction"`