upodate system
This commit is contained in:
@@ -3,6 +3,7 @@ package request
|
||||
import "enaklo-pos-be/internal/entity"
|
||||
|
||||
type OpenCashierSessionRequest struct {
|
||||
PartnerID int64 `json:"partner_id" validate:"required"`
|
||||
OpeningAmount float64 `json:"opening_amount" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
@@ -12,6 +13,7 @@ type CloseCashierSessionRequest struct {
|
||||
|
||||
func (o *OpenCashierSessionRequest) ToEntity(cashierID int64) *entity.CashierSession {
|
||||
return &entity.CashierSession{
|
||||
PartnerID: o.PartnerID,
|
||||
CashierID: cashierID,
|
||||
OpeningAmount: o.OpeningAmount,
|
||||
}
|
||||
|
||||
@@ -30,19 +30,17 @@ func (p *ProductParam) ToEntity(partnerID int64) entity.ProductSearch {
|
||||
}
|
||||
|
||||
type Product struct {
|
||||
ID int64 `json:"id,omitempty"`
|
||||
PartnerID int64 `json:"partner_id"`
|
||||
SiteID int64 `json:"site_id"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Type string `json:"type"`
|
||||
Price float64 `json:"price" validate:"required"`
|
||||
IsWeekendTicket bool `json:"is_weekend_ticket"`
|
||||
IsSeasonTicket bool `json:"is_season_ticket"`
|
||||
Status string `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Stock int64 `json:"stock"`
|
||||
Image string `json:"image"`
|
||||
CategoryID int64 `json:"category_id"`
|
||||
ID int64 `json:"id,omitempty"`
|
||||
PartnerID int64 `json:"partner_id"`
|
||||
SiteID int64 `json:"site_id"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Type string `json:"type"`
|
||||
Price float64 `json:"price" validate:"required"`
|
||||
Status string `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Stock int64 `json:"stock"`
|
||||
Image string `json:"image"`
|
||||
CategoryID int64 `json:"category_id"`
|
||||
}
|
||||
|
||||
func (e *Product) ToEntity() *entity.Product {
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"enaklo-pos-be/internal/entity"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type QueryParser struct {
|
||||
defaultLimit int
|
||||
maxLimit int
|
||||
}
|
||||
|
||||
func NewQueryParser() *QueryParser {
|
||||
return &QueryParser{
|
||||
defaultLimit: 20,
|
||||
maxLimit: 100,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *QueryParser) ParseSearchRequest(c *gin.Context) (*entity.SearchRequest, error) {
|
||||
req := &entity.SearchRequest{}
|
||||
|
||||
if status := c.Query("status"); status != "" {
|
||||
req.Status = status
|
||||
}
|
||||
|
||||
limit, err := p.parseLimit(c.Query("limit"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid limit parameter")
|
||||
}
|
||||
req.Limit = limit
|
||||
|
||||
offset, err := p.parseOffset(c.Query("offset"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid offset parameter")
|
||||
}
|
||||
req.Offset = offset
|
||||
|
||||
if err := p.parseDateRange(c, req); err != nil {
|
||||
return nil, errors.Wrap(err, "invalid date parameters")
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (p *QueryParser) parseLimit(limitStr string) (int, error) {
|
||||
if limitStr == "" {
|
||||
return p.defaultLimit, nil
|
||||
}
|
||||
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
return 0, errors.New("limit must be a valid integer")
|
||||
}
|
||||
|
||||
if limit <= 0 {
|
||||
return p.defaultLimit, nil
|
||||
}
|
||||
|
||||
if limit > p.maxLimit {
|
||||
limit = p.maxLimit
|
||||
}
|
||||
|
||||
return limit, nil
|
||||
}
|
||||
|
||||
func (p *QueryParser) parseOffset(offsetStr string) (int, error) {
|
||||
if offsetStr == "" {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
offset, err := strconv.Atoi(offsetStr)
|
||||
if err != nil {
|
||||
return 0, errors.New("offset must be a valid integer")
|
||||
}
|
||||
|
||||
if offset < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
return offset, nil
|
||||
}
|
||||
|
||||
func (p *QueryParser) parseDateRange(c *gin.Context, req *entity.SearchRequest) error {
|
||||
if startDateStr := c.Query("start_date"); startDateStr != "" {
|
||||
startDate, err := p.parseDate(startDateStr)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "invalid start_date format")
|
||||
}
|
||||
req.Start = startDate
|
||||
}
|
||||
|
||||
if endDateStr := c.Query("end_date"); endDateStr != "" {
|
||||
endDate, err := p.parseDate(endDateStr)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "invalid end_date format")
|
||||
}
|
||||
req.End = endDate
|
||||
}
|
||||
|
||||
if !req.Start.IsZero() && !req.End.IsZero() && req.Start.After(req.End) {
|
||||
return errors.New("start_date cannot be after end_date")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *QueryParser) parseDate(dateStr string) (time.Time, error) {
|
||||
formats := []string{
|
||||
time.RFC3339,
|
||||
"2006-01-02T15:04:05",
|
||||
"2006-01-02",
|
||||
"2006-01-02 15:04:05",
|
||||
}
|
||||
|
||||
for _, format := range formats {
|
||||
if date, err := time.Parse(format, dateStr); err == nil {
|
||||
return date, nil
|
||||
}
|
||||
}
|
||||
|
||||
return time.Time{}, errors.New("unsupported date format")
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"enaklo-pos-be/internal/entity"
|
||||
"github.com/pkg/errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RequestValidator struct{}
|
||||
|
||||
func NewRequestValidator() *RequestValidator {
|
||||
return &RequestValidator{}
|
||||
}
|
||||
|
||||
func (v *RequestValidator) ValidateSearchRequest(req *entity.SearchRequest) error {
|
||||
if req.Status != "" {
|
||||
validStatuses := []string{"pending", "confirmed", "processing", "completed", "cancelled"}
|
||||
if !v.isValidStatus(req.Status, validStatuses) {
|
||||
return errors.New("invalid status value")
|
||||
}
|
||||
}
|
||||
|
||||
if !req.Start.IsZero() && !req.End.IsZero() {
|
||||
if req.Start.After(req.End) {
|
||||
return errors.New("start date cannot be after end date")
|
||||
}
|
||||
|
||||
if req.End.Sub(req.Start) > 365*24*time.Hour {
|
||||
return errors.New("date range cannot exceed 1 year")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *RequestValidator) isValidStatus(status string, validStatuses []string) bool {
|
||||
for _, validStatus := range validStatuses {
|
||||
if status == validStatus {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user