90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"ordr-api/dto"
|
|
"ordr-api/queries"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func CreateOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
|
return func(ctx *gin.Context) {
|
|
conn, conn_err := pool.Acquire(ctx)
|
|
if conn_err != nil {
|
|
log.Printf("CreateItem(): ERROR: Failed to connect... %s", conn_err.Error())
|
|
ctx.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer conn.Release()
|
|
|
|
user_profile, _ := ctx.Get("user_profile")
|
|
|
|
sub_id := user_profile.(dto.UserProfileResponse).Sub
|
|
|
|
var current_user dto.UserResponse
|
|
|
|
user_query_err := conn.QueryRow(context.Background(), queries.GET_CURRENT_USER_OBJECT, sub_id).Scan(¤t_user.Id, ¤t_user.Name, ¤t_user.Job_Position, ¤t_user.Active, ¤t_user.Admin)
|
|
|
|
if user_query_err != nil {
|
|
log.Printf("CreateOrder(): ERROR Failed to query user... %s", user_query_err.Error())
|
|
ctx.String(http.StatusInternalServerError, "CreateOrder(): ERROR Failed to query user")
|
|
return
|
|
}
|
|
|
|
orderer := ctx.Query("orderer")
|
|
date_due := ctx.Query("date_due")
|
|
date_placed := ctx.Query("date_placed")
|
|
|
|
if orderer == "" || date_due == "" {
|
|
ctx.String(http.StatusBadRequest, "CreateOrder(): ERROR orderer or date_due not supplied")
|
|
return
|
|
}
|
|
|
|
if date_placed == "" {
|
|
currentTime := time.Now()
|
|
date_placed = currentTime.Format(time.RFC3339)
|
|
}
|
|
|
|
_, placed_date_err := time.Parse(time.RFC3339, date_placed)
|
|
if placed_date_err != nil {
|
|
ctx.String(http.StatusBadRequest, "CreateOrder(): Bad time format")
|
|
return
|
|
}
|
|
|
|
_, due_date_err := time.Parse(time.RFC3339, date_due)
|
|
if due_date_err != nil {
|
|
ctx.String(http.StatusBadRequest, "CreateOrder(): Bad Time format")
|
|
return
|
|
}
|
|
|
|
_, exec_err := conn.Exec(context.Background(), queries.CREATE_ORDER, current_user.Id, orderer, date_due, date_placed)
|
|
|
|
if exec_err != nil {
|
|
ctx.String(http.StatusInternalServerError, "CreateOrder(): Failed to create order")
|
|
log.Printf("CreateOrder(): ERROR... Failed to create order... %s", exec_err.Error())
|
|
return
|
|
}
|
|
|
|
var order dto.OrderResponse
|
|
order_query_err := conn.QueryRow(context.Background(), queries.GET_ORDER_BY_ORDER_INFORMATION, current_user.Id, orderer, date_placed).Scan(&order.Id, &order.UserId, &order.Orderer, &order.DateDue, &order.DatePlaced, &order.AmountPaid, &order.Filled, &order.Delivered)
|
|
if order_query_err != nil {
|
|
ctx.String(http.StatusInternalServerError, "CreateOrder(): Failed to create order")
|
|
log.Printf("CreateOrder(): ERROR... failed to query order after creation ... %s", order_query_err.Error())
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, order)
|
|
}
|
|
}
|
|
|
|
func SetOrderFilled(pool *pgxpool.Pool) gin.HandlerFunc {
|
|
return func(ctx *gin.Context) {
|
|
|
|
}
|
|
}
|