136 lines
3.3 KiB
Go
136 lines
3.3 KiB
Go
package endpoints
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"regexp"
|
|
"relynolli-server/models"
|
|
"relynolli-server/services"
|
|
"relynolli-server/status"
|
|
"relynolli-server/storage"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
type handlers struct{}
|
|
|
|
type getTotalRequest struct {
|
|
FuserId int `json:"fuserId"`
|
|
Coupon *string `json:"coupon,omitempty"`
|
|
}
|
|
|
|
type getTotalResponse struct {
|
|
TotalProductPrice float64 `json:"total_product_price"`
|
|
}
|
|
|
|
type makeOrderRequest struct {
|
|
FuserId int `json:"fuserId" validate:"required"`
|
|
PhoneNumber string `json:"phoneNumber" validate:"required"`
|
|
FullName string `json:"fullName" validate:"required"`
|
|
Email string `json:"email" validate:"required,email"`
|
|
DeliveryType string `json:"deliveryType,omitempty"`
|
|
Address string `json:"address,omitempty"`
|
|
}
|
|
|
|
func (h handlers) GetTotal(c *gin.Context) {
|
|
req := getTotalRequest{}
|
|
err := c.ShouldBindJSON(&req)
|
|
meta := models.Meta{
|
|
RequestStarted: time.Now().Unix(),
|
|
}
|
|
ctx := context.Background()
|
|
s := storage.NewStorageOrder()
|
|
resp := models.Response{
|
|
Status: status.STATUS_OK,
|
|
Meta: &meta,
|
|
}
|
|
|
|
if err != nil {
|
|
meta.RequestFinished = time.Now().Unix()
|
|
resp.Status = status.STATUS_BAD_REQUEST
|
|
resp.Info = fmt.Sprintf("Error: %s", err.Error())
|
|
c.JSON(400, resp)
|
|
return
|
|
}
|
|
|
|
data, err := s.GetTotal(ctx, int64(req.FuserId), req.Coupon)
|
|
|
|
if err != nil {
|
|
resp.Status = status.STATUS_SERVER_ERROR
|
|
resp.Info = err.Error()
|
|
meta.RequestFinished = time.Now().Unix()
|
|
c.JSON(500, resp)
|
|
}
|
|
resp.Data = data
|
|
meta.RequestFinished = time.Now().Unix()
|
|
c.JSON(200, resp)
|
|
}
|
|
|
|
func (h handlers) MakeOrder(c *gin.Context) {
|
|
ctx := context.Background()
|
|
// VALIDATION
|
|
validate := validator.New(validator.WithRequiredStructEnabled())
|
|
req := makeOrderRequest{}
|
|
err := c.ShouldBindJSON(&req)
|
|
meta := models.Meta{
|
|
RequestStarted: time.Now().Unix(),
|
|
}
|
|
resp := models.Response{
|
|
Status: status.STATUS_OK,
|
|
Meta: &meta,
|
|
}
|
|
if err != nil {
|
|
meta.RequestFinished = time.Now().Unix()
|
|
resp.Info = fmt.Sprintf("ERROR: %s", err.Error())
|
|
resp.Status = status.STATUS_BAD_REQUEST
|
|
c.JSON(400, resp)
|
|
return
|
|
}
|
|
|
|
validationErr := validate.Struct(req)
|
|
|
|
if validationErr != nil {
|
|
responseErr := validationErr.(validator.ValidationErrors)[0]
|
|
meta.RequestFinished = time.Now().Unix()
|
|
resp.Info = responseErr.Error()
|
|
resp.Status = status.STATUS_BAD_REQUEST
|
|
c.JSON(http.StatusBadRequest, resp)
|
|
return
|
|
}
|
|
phoneMatched, err := regexp.Match("^\\d{11}$", []byte(req.PhoneNumber))
|
|
if phoneMatched == false {
|
|
meta.RequestFinished = time.Now().Unix()
|
|
resp.Info = "Phone number is not valid"
|
|
resp.Status = status.STATUS_BAD_REQUEST
|
|
c.JSON(http.StatusBadRequest, resp)
|
|
return
|
|
}
|
|
|
|
kassaResult, serviceErr := services.MakeOrder(ctx, req.FuserId, req.Email, req.FullName, req.PhoneNumber)
|
|
|
|
if serviceErr != nil {
|
|
meta.RequestFinished = time.Now().Unix()
|
|
resp.Info = fmt.Sprintf("Error: %s", serviceErr.Error())
|
|
resp.Status = status.STATUS_SERVER_ERROR
|
|
c.JSON(http.StatusInternalServerError, resp)
|
|
return
|
|
}
|
|
|
|
resp.Data = kassaResult
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
}
|
|
|
|
type Handlers interface {
|
|
GetTotal(c *gin.Context)
|
|
MakeOrder(c *gin.Context)
|
|
}
|
|
|
|
func GetHandlers() Handlers {
|
|
return &handlers{}
|
|
}
|