feat: order get requests
This commit is contained in:
@@ -231,3 +231,53 @@ func AddItemToOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
ctx.JSON(http.StatusOK, order_item_price)
|
||||
}
|
||||
}
|
||||
|
||||
func GetOrderItems(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
log.Printf("GetOrderItems(): ERROR: Failed to connect... %s", conn_err.Error())
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
order_id := ctx.Query("order_id")
|
||||
|
||||
if order_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderItems(): ERROR: order id not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
_, parse_err := strconv.ParseInt(order_id, 10, 64)
|
||||
|
||||
if parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderItem(): ERROR... order id not an integer")
|
||||
return
|
||||
}
|
||||
|
||||
rows, query_err := conn.Query(context.Background(), queries.GET_ORDER_ITEMS, order_id)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetOrderItems(): ERROR... failed to query database.")
|
||||
log.Printf("GetOrderItems(): ERROR... Failed to query database... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []dto.OrderItemPriceResponse
|
||||
|
||||
for rows.Next() {
|
||||
var item dto.OrderItemPriceResponse
|
||||
scan_err := rows.Scan(&item.ItemId, &item.OrderId, &item.ItemName, &item.Quantity, &item.Made, &item.CreatedAt, &item.TotalPrice, &item.UnitPrice)
|
||||
if scan_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetOrderItems(): ERROR... Failed to scan data...")
|
||||
log.Printf("GetOrderItems(): ERROR... failed to scan data... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, items)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@ import (
|
||||
"net/http"
|
||||
"ordr-api/dto"
|
||||
"ordr-api/queries"
|
||||
"ordr-api/utils"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
@@ -71,7 +74,7 @@ func CreateOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
}
|
||||
|
||||
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)
|
||||
order_query_err := conn.QueryRow(context.Background(), queries.GET_TOTAL_ORDER_FROM_ORDER_INFORMATION, current_user.Id, orderer, date_placed).Scan(&order.Id, &order.UserId, &order.Orderer, &order.DateDue, &order.DatePlaced, &order.AmountPaid, &order.OrderTotal, &order.AmountDue, &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())
|
||||
@@ -82,6 +85,93 @@ func CreateOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func GetOrderByOrderId(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
log.Printf("GetOrderByOrderId(): ERROR: Failed to connect... %s", conn_err.Error())
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
order_id := ctx.Query("order_id")
|
||||
_, order_id_parse_err := strconv.ParseInt(order_id, 10, 64)
|
||||
if order_id == "" || order_id_parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderByOrderId(): ERROR... order_id not valid")
|
||||
return
|
||||
}
|
||||
|
||||
row := conn.QueryRow(context.Background(), queries.GET_TOTAL_ORDER_FROM_ORDER_ID, order_id)
|
||||
|
||||
var order_response dto.OrderResponse
|
||||
|
||||
scan_err := row.Scan(&order_response.Id, &order_response.UserId, &order_response.Orderer, &order_response.DateDue, &order_response.DatePlaced, &order_response.AmountPaid, &order_response.OrderTotal, &order_response.AmountDue, &order_response.Filled, &order_response.Delivered)
|
||||
|
||||
if scan_err != nil {
|
||||
if scan_err == pgx.ErrNoRows {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderByOrderId(): ERROR... no order matches the query")
|
||||
return
|
||||
}
|
||||
ctx.String(http.StatusInternalServerError, "GetOrderByOrderId(): ERROR... internal server error")
|
||||
log.Printf("GetOrderByOrderId(): ERROR... Failed to scan row... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, order_response)
|
||||
}
|
||||
}
|
||||
|
||||
func GetOrderTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
log.Printf("GetOrderTable(): ERROR: Failed to connect... %s", conn_err.Error())
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
page := ctx.Query("page")
|
||||
page_int, page_parse_err := strconv.ParseInt(page, 10, 64)
|
||||
if page == "" || page_parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderTable(): Invalid page number")
|
||||
return
|
||||
}
|
||||
|
||||
filter := ctx.Query("filter")
|
||||
filter_int, filter_parse_err := strconv.ParseInt(filter, 10, 64)
|
||||
if filter == "" || filter_parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderTable(): Invalid Filter Options")
|
||||
return
|
||||
}
|
||||
|
||||
query_string := utils.GetOrderTableQueryString(filter_int)
|
||||
rows, query_err := conn.Query(context.Background(), query_string, utils.PAGE_SIZE*page_int, utils.PAGE_SIZE)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetOrdertable(): ERROR... internal server error")
|
||||
log.Printf("GetOrderTable(): ERROR... Failed to query table...\nQUERY: %s\n %s", query_string, query_err.Error())
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var orders []dto.OrderResponse
|
||||
for rows.Next() {
|
||||
var order_response dto.OrderResponse
|
||||
scan_err := rows.Scan(&order_response.Id, &order_response.UserId, &order_response.Orderer, &order_response.DateDue, &order_response.DatePlaced, &order_response.AmountPaid, &order_response.OrderTotal, &order_response.AmountDue, &order_response.Filled, &order_response.Delivered)
|
||||
if scan_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetOrdertable(): ERROR... internal server error")
|
||||
log.Printf("GetOrderTable(): ERROR... Failed to scan query... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
orders = append(orders, order_response)
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, orders)
|
||||
}
|
||||
}
|
||||
|
||||
func SetOrderFilled(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"ordr-api/dto"
|
||||
"ordr-api/queries"
|
||||
"ordr-api/utils"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -118,7 +119,7 @@ func GetUserTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
ctx.String(http.StatusBadRequest, "GetUserTable(): Not an integer")
|
||||
return
|
||||
}
|
||||
rows, query_err := conn.Query(context.Background(), queries.USER_GET_TABLE_DATA, page_int*10, 10)
|
||||
rows, query_err := conn.Query(context.Background(), queries.USER_GET_TABLE_DATA, page_int*utils.PAGE_SIZE, utils.PAGE_SIZE)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetUserTable(): Failed to query database...")
|
||||
log.Printf("GetUserTable(): ERROR... %s", query_err.Error())
|
||||
|
||||
Reference in New Issue
Block a user