feat: frontend
This commit is contained in:
@@ -27,12 +27,12 @@ func CreateItem(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
item_name := ctx.Query("item_name")
|
||||
in_season := ctx.Query("in_season") == "1"
|
||||
if item_name == "" {
|
||||
ctx.String(http.StatusBadRequest, "CreateItem(): ERROR... Item Name not supplied")
|
||||
ctx.JSON(http.StatusBadRequest, "CreateItem(): ERROR... Item Name not supplied")
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), queries.CREATE_ITEM, item_name, in_season)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "CreateItem(): ERROR... Failed to create item")
|
||||
ctx.JSON(http.StatusInternalServerError, "CreateItem(): ERROR... Failed to create item")
|
||||
log.Printf("CreateItem(): ERROR... Failed to create item... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func CreateItem(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
var item_id int
|
||||
query_err := conn.QueryRow(context.Background(), "SELECT id FROM item WHERE item_name = $1", item_name).Scan(&item_id)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "CreateItem(): ERROR... Failed to get nwely created item id")
|
||||
ctx.JSON(http.StatusInternalServerError, "CreateItem(): ERROR... Failed to get nwely created item id")
|
||||
log.Printf("CreateItem(): ERROR... Failed to get newly created item... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -48,20 +48,20 @@ func CreateItem(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
item_price := ctx.Query("item_price")
|
||||
|
||||
if item_price == "" {
|
||||
ctx.String(http.StatusBadRequest, "CreateItem(): ERROR... item_price not supplied")
|
||||
ctx.JSON(http.StatusBadRequest, "CreateItem(): ERROR... item_price not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
item_price_float, convert_err := strconv.ParseFloat(item_price, 64)
|
||||
if convert_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "CreateUser(): ERROR... Failed to create item price: item price invalid format")
|
||||
ctx.JSON(http.StatusBadRequest, "CreateUser(): ERROR... Failed to create item price: item price invalid format")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_price_err := conn.Exec(context.Background(), queries.CREATE_ITEM_PRICE, item_id, item_price)
|
||||
|
||||
if exec_price_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "CreateItem(): ERROR... Failed to insert price...")
|
||||
ctx.JSON(http.StatusInternalServerError, "CreateItem(): ERROR... Failed to insert price...")
|
||||
log.Printf("CreateItem(): ERROR... Failed to insert price... %s", exec_price_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -87,42 +87,42 @@ func SetItemPrice(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
item_id_string := ctx.Query("item_id")
|
||||
if item_id_string == "" {
|
||||
ctx.String(http.StatusBadRequest, "SetItemPrice(): ERROR... item id not provided")
|
||||
ctx.JSON(http.StatusBadRequest, "SetItemPrice(): ERROR... item id not provided")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_date_err := conn.Exec(context.Background(), queries.SET_ITEM_PRICE_VALID_TO_DATE, item_id_string)
|
||||
if exec_date_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "SetItemPrice(): ERROR... Failed to set valid_to date")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetItemPrice(): ERROR... Failed to set valid_to date")
|
||||
log.Printf("SetItemPrice(): ERROR... Failed to set valid_to date... %s", exec_date_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
item_price_string := ctx.Query("item_price")
|
||||
if item_price_string == "" {
|
||||
ctx.String(http.StatusBadRequest, "SetItemPrice(): ERROR... Item Price Not Provided")
|
||||
ctx.JSON(http.StatusBadRequest, "SetItemPrice(): ERROR... Item Price Not Provided")
|
||||
return
|
||||
}
|
||||
|
||||
item_price_float, item_price_conv_err := strconv.ParseFloat(item_price_string, 64)
|
||||
if item_price_conv_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "SetItemPrice(): ERROR... item price not valid")
|
||||
ctx.JSON(http.StatusBadRequest, "SetItemPrice(): ERROR... item price not valid")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_new_price_err := conn.Exec(context.Background(), queries.CREATE_ITEM_PRICE, item_id_string, item_price_float)
|
||||
|
||||
if exec_new_price_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "SetItemPrice(): ERROR... failed to create item price")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetItemPrice(): ERROR... failed to create item price")
|
||||
log.Printf("SetItemPrice(): ERROR... failed to create item price... %s", exec_new_price_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var item_price_object dto.ItemPriceResponse
|
||||
query_item_price_err := conn.QueryRow(context.Background(), queries.GET_CURRENT_ITEM_PRICE, item_id_string).Scan(&item_price_object.ItemId, &item_price_object.ItemName, &item_price_object.ItemPrice)
|
||||
query_item_price_err := conn.QueryRow(context.Background(), queries.GET_CURRENT_ITEM_PRICE, item_id_string).Scan(&item_price_object.ItemId, &item_price_object.ItemName, &item_price_object.ItemPrice, &item_price_object.InSeason)
|
||||
if query_item_price_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "SetItemPrice(): ERROR... Failed to query item price")
|
||||
log.Println("SetItemPrice(): ERROR... Failed to query item price... %s", query_item_price_err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, "SetItemPrice(): ERROR... Failed to query item price")
|
||||
log.Printf("SetItemPrice(): ERROR... Failed to query item price... %s", query_item_price_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -142,21 +142,22 @@ func GetCurrentItemPrice(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
item_id := ctx.Query("item_id")
|
||||
if item_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "GetCurrentItemPrice(): ERROR... item id not provided")
|
||||
ctx.JSON(http.StatusBadRequest, "GetCurrentItemPrice(): ERROR... item id not provided")
|
||||
return
|
||||
}
|
||||
|
||||
var item_price dto.ItemPriceResponse
|
||||
|
||||
query_err := conn.QueryRow(context.Background(), queries.GET_CURRENT_ITEM_PRICE, item_id).Scan(&item_price.ItemId, &item_price.ItemName, &item_price.ItemPrice)
|
||||
query_err := conn.QueryRow(context.Background(), queries.GET_CURRENT_ITEM_PRICE, item_id).Scan(&item_price.ItemId, &item_price.ItemName, &item_price.ItemPrice, &item_price.InSeason)
|
||||
|
||||
if query_err != nil {
|
||||
if query_err == pgx.ErrNoRows {
|
||||
ctx.String(http.StatusBadRequest, "GetCurrentItemPrice(): ERROR... no rows match id")
|
||||
ctx.JSON(http.StatusBadRequest, "GetCurrentItemPrice(): ERROR... no rows match id")
|
||||
return
|
||||
}
|
||||
ctx.String(http.StatusInternalServerError, "GetCurrentItemPrice(): ERROR... failed to query db")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetCurrentItemPrice(): ERROR... failed to query db")
|
||||
log.Printf("GetCurrentItemPrice(): ERROR... failed to query db... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, item_price)
|
||||
@@ -179,7 +180,7 @@ func AddItemToOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
created_at := ctx.Query("created_at")
|
||||
|
||||
if item_id == "" || order_id == "" || quantity == "" {
|
||||
ctx.String(http.StatusBadRequest, "AddItemToOrder(): ERROR... item_id, order_id, or quantity missing")
|
||||
ctx.JSON(http.StatusBadRequest, "AddItemToOrder(): ERROR... item_id, order_id, or quantity missing")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -190,29 +191,29 @@ func AddItemToOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
int_item_id, item_id_parse_err := strconv.ParseInt(item_id, 10, 64)
|
||||
if item_id_parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "AddItemToOrder: ERROR... item id is not an int")
|
||||
ctx.JSON(http.StatusBadRequest, "AddItemToOrder: ERROR... item id is not an int")
|
||||
return
|
||||
}
|
||||
int_order_id, order_id_parse_err := strconv.ParseInt(order_id, 10, 64)
|
||||
if order_id_parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "AddItemToOrder: ERROR... order id is not an int")
|
||||
ctx.JSON(http.StatusBadRequest, "AddItemToOrder: ERROR... order id is not an int")
|
||||
return
|
||||
}
|
||||
int_quantity, quantity_parse_err := strconv.ParseInt(quantity, 10, 64)
|
||||
if quantity_parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "AddItemToOrder: ERROR... quantity is not an int")
|
||||
ctx.JSON(http.StatusBadRequest, "AddItemToOrder: ERROR... quantity is not an int")
|
||||
return
|
||||
}
|
||||
|
||||
_, time_parse_err := time.Parse(time.RFC3339, created_at)
|
||||
if time_parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "AddItemToOrder(): ERROR... time is bad")
|
||||
ctx.JSON(http.StatusBadRequest, "AddItemToOrder(): ERROR... time is bad")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), queries.ADD_ITEM_TO_ORDER, int_item_id, int_order_id, int_quantity, created_at)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "AddItemToOrder(): ERROR... failed to insert rows")
|
||||
ctx.JSON(http.StatusInternalServerError, "AddItemToOrder(): ERROR... failed to insert rows")
|
||||
log.Printf("AddItemToOrder(): ERROR... Failed to insert rows... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -220,7 +221,7 @@ func AddItemToOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
_, update_order_filled_exec_err := conn.Exec(context.Background(), queries.SET_ORDER_FILLED, false, order_id)
|
||||
|
||||
if update_order_filled_exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "AddItemToOrder(): ERROR... failed to set order state")
|
||||
ctx.JSON(http.StatusInternalServerError, "AddItemToOrder(): ERROR... failed to set order state")
|
||||
log.Printf("AddItemToOrder(): ERROR... failed to set order state... %s", update_order_filled_exec_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -231,7 +232,7 @@ func AddItemToOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
price_scan_err := price_data_row.Scan(&order_item_price.ItemId, &order_item_price.OrderId, &order_item_price.ItemName, &order_item_price.Quantity, &order_item_price.Made, &order_item_price.CreatedAt, &order_item_price.TotalPrice, &order_item_price.UnitPrice)
|
||||
if price_scan_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "AddItemToOrder(): ERROR... Failed to scan price data")
|
||||
ctx.JSON(http.StatusInternalServerError, "AddItemToOrder(): ERROR... Failed to scan price data")
|
||||
log.Printf("AddItemToOrder(): ERROR... Failed to scan price data... %s", price_scan_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -253,20 +254,20 @@ func GetOrderItems(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
order_id := ctx.Query("order_id")
|
||||
|
||||
if order_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderItems(): ERROR: order id not supplied")
|
||||
ctx.JSON(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")
|
||||
ctx.JSON(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.")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetOrderItems(): ERROR... failed to query database.")
|
||||
log.Printf("GetOrderItems(): ERROR... Failed to query database... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -278,7 +279,7 @@ func GetOrderItems(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
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...")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetOrderItems(): ERROR... Failed to scan data...")
|
||||
log.Printf("GetOrderItems(): ERROR... failed to scan data... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -303,21 +304,21 @@ func SetItemMade(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
var request dto.ItemOrderSetMadeRequest
|
||||
request_read_err := ctx.ShouldBindJSON(&request)
|
||||
if request_read_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "SetItemMade(): ERROR... Error Request in bad format")
|
||||
ctx.JSON(http.StatusBadRequest, "SetItemMade(): ERROR... Error Request in bad format")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_set_made_err := conn.Exec(context.Background(), queries.SET_MADE, request.Made, request.ItemId, request.OrderId)
|
||||
|
||||
if exec_set_made_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "SetItemMade(): ERROR... Internal server error")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetItemMade(): ERROR... Internal server error")
|
||||
log.Printf("SetItemMade(): ERROR... Failed to set item made... %s", exec_set_made_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_update_order_err := conn.Exec(context.Background(), queries.UPDATE_ORDER_FILLED, request.OrderId)
|
||||
if exec_update_order_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "SetItemMade(): ERROR... Failed to update order")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetItemMade(): ERROR... Failed to update order")
|
||||
log.Printf("SetItemMade(): ERROR... Failed to update order... %s", exec_update_order_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -326,10 +327,10 @@ func SetItemMade(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
scan_err := conn.QueryRow(context.Background(), queries.GET_ORDER_MADE_DETAILS, request.OrderId).Scan(&order_filled.OrderId, &order_filled.Filled)
|
||||
if scan_err != nil {
|
||||
if scan_err == pgx.ErrNoRows {
|
||||
ctx.String(http.StatusBadRequest, "SetItemMade(): Invalid data")
|
||||
ctx.JSON(http.StatusBadRequest, "SetItemMade(): Invalid data")
|
||||
return
|
||||
}
|
||||
ctx.String(http.StatusInternalServerError, "SetItemMade(): ERROR... Error querying result")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetItemMade(): ERROR... Error querying result")
|
||||
log.Printf("SetItemMade(): ERROR... Error querying result... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -350,21 +351,21 @@ func SetItemQuantity(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
var request dto.ItemOrderSetQuantityRequest
|
||||
request_read_err := ctx.ShouldBindJSON(&request)
|
||||
if request_read_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "SetItemQuantity(): ERROR... Error Request in bad format")
|
||||
ctx.JSON(http.StatusBadRequest, "SetItemQuantity(): ERROR... Error Request in bad format")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_set_quantity_err := conn.Exec(context.Background(), queries.SET_QUANTITY, request.Quantity, request.ItemId, request.OrderId)
|
||||
|
||||
if exec_set_quantity_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "SetItemMade(): ERROR... Internal server error")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetItemMade(): ERROR... Internal server error")
|
||||
log.Printf("SetItemQuantity(): ERROR... Failed to set item quantity... %s", exec_set_quantity_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_update_order_err := conn.Exec(context.Background(), queries.UPDATE_ORDER_FILLED, request.OrderId)
|
||||
if exec_update_order_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "SetItemMade(): ERROR... Failed to update order")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetItemMade(): ERROR... Failed to update order")
|
||||
log.Printf("SetItemQuantity(): ERROR... Failed to update order... %s", exec_update_order_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -373,10 +374,10 @@ func SetItemQuantity(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
scan_err := conn.QueryRow(context.Background(), queries.GET_ORDER_MADE_DETAILS, request.OrderId).Scan(&order_filled.OrderId, &order_filled.Filled)
|
||||
if scan_err != nil {
|
||||
if scan_err == pgx.ErrNoRows {
|
||||
ctx.String(http.StatusBadRequest, "SetItemQuantity(): Invalid data")
|
||||
ctx.JSON(http.StatusBadRequest, "SetItemQuantity(): Invalid data")
|
||||
return
|
||||
}
|
||||
ctx.String(http.StatusInternalServerError, "SetItemQuantity(): ERROR... Error querying result")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetItemQuantity(): ERROR... Error querying result")
|
||||
log.Printf("SetItemQuantity(): ERROR... Error querying result... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -395,16 +396,25 @@ func DeleteOrderItem(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
var request dto.DeleteOrderItemRequest
|
||||
request_read_err := ctx.ShouldBindJSON(&request)
|
||||
if request_read_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "DeleteOrderItem(): ERROR... invalid request")
|
||||
item_id := ctx.Query("item_id")
|
||||
order_id := ctx.Query("order_id")
|
||||
|
||||
if item_id == "" || order_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "DeleteOrderItem(): Bad Request")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), queries.REMOVE_ITEM_FROM_ORDER, request.ItemId, request.OrderId)
|
||||
item_id_int, item_parse_err := strconv.ParseInt(item_id, 10, 64)
|
||||
order_id_int, order_parse_err := strconv.ParseInt(order_id, 10, 64)
|
||||
|
||||
if item_parse_err != nil || order_parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "DeleteOrderItem(): Bad Request")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), queries.REMOVE_ITEM_FROM_ORDER, item_id_int, order_id_int)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "DeleteOrderItem(): ERROR.. Failed to delete item")
|
||||
ctx.JSON(http.StatusInternalServerError, "DeleteOrderItem(): ERROR.. Failed to delete item")
|
||||
log.Printf("DeleteOrderItem(): ERROR... Failed to delte item... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -424,15 +434,100 @@ func DeleteItem(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
item_id := ctx.Query("item_id")
|
||||
item_id_int, parse_err := strconv.ParseInt(item_id, 10, 64)
|
||||
if parse_err != nil || item_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "DeleteItem(): Invalid data entry")
|
||||
ctx.JSON(http.StatusBadRequest, "DeleteItem(): Invalid data entry")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), "DELETE FROM item WHERE id = $1", item_id_int)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "DeleteItem(): Failed to delete item")
|
||||
ctx.JSON(http.StatusInternalServerError, "DeleteItem(): Failed to delete item")
|
||||
log.Printf("DeleteItem(): ERROR... Failed to delete item... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetItems(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
log.Printf("DeleteItem(): ERROR: Failed to connect... %s", conn_err.Error())
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
rows, query_err := conn.Query(context.Background(), queries.GET_CURRENT_ITEMS_PRICE)
|
||||
|
||||
if query_err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, "INTERNAL SERVER ERROR")
|
||||
log.Printf("GetItems(): ERROR... Failed to query table... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var items []dto.ItemPriceResponse
|
||||
|
||||
for rows.Next() {
|
||||
var item dto.ItemPriceResponse
|
||||
scan_err := rows.Scan(&item.ItemId, &item.ItemName, &item.ItemPrice, &item.InSeason)
|
||||
|
||||
if scan_err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, "INTERNAL SERVER ERROR")
|
||||
log.Printf("GetItems(): ERROR... failed to scan info...%s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
ctx.JSON(http.StatusOK, items)
|
||||
}
|
||||
}
|
||||
|
||||
func GetItemHistory(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
log.Printf("DeleteItem(): ERROR: Failed to connect... %s", conn_err.Error())
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
item_id := ctx.Query("item_id")
|
||||
|
||||
if item_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "Bad Request")
|
||||
return
|
||||
}
|
||||
|
||||
item_id_int, parse_err := strconv.ParseInt(item_id, 10, 64)
|
||||
|
||||
if parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "Bad Request")
|
||||
return
|
||||
}
|
||||
|
||||
rows, query_err := conn.Query(ctx, queries.GET_ITEM_PRICE_HISTORY, item_id_int)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "Internal Server Error")
|
||||
|
||||
log.Printf("GetItemHistory(): ERROR... Failed to query table... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var item_history_records []dto.ItemHistory
|
||||
for rows.Next() {
|
||||
var item_history_record dto.ItemHistory
|
||||
scan_err := rows.Scan(&item_history_record.ItemId, &item_history_record.ItemName, &item_history_record.ItemPrice, &item_history_record.ValidFrom, &item_history_record.ValidTo)
|
||||
if scan_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "Internal Server Error")
|
||||
|
||||
log.Printf("GetItemHistory(): ERROR... Failed to query table... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
item_history_records = append(item_history_records, item_history_record)
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, item_history_records)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ func CreateOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
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")
|
||||
ctx.JSON(http.StatusInternalServerError, "CreateOrder(): ERROR Failed to query user")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func CreateOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
date_placed := ctx.Query("date_placed")
|
||||
|
||||
if orderer == "" || date_due == "" {
|
||||
ctx.String(http.StatusBadRequest, "CreateOrder(): ERROR orderer or date_due not supplied")
|
||||
ctx.JSON(http.StatusBadRequest, "CreateOrder(): ERROR orderer or date_due not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -55,20 +55,20 @@ func CreateOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
_, placed_date_err := time.Parse(time.RFC3339, date_placed)
|
||||
if placed_date_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "CreateOrder(): Bad time format")
|
||||
ctx.JSON(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")
|
||||
ctx.JSON(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")
|
||||
ctx.JSON(http.StatusInternalServerError, "CreateOrder(): Failed to create order")
|
||||
log.Printf("CreateOrder(): ERROR... Failed to create order... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -76,7 +76,7 @@ func CreateOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
var order dto.OrderResponse
|
||||
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")
|
||||
ctx.JSON(http.StatusInternalServerError, "CreateOrder(): Failed to create order")
|
||||
log.Printf("CreateOrder(): ERROR... failed to query order after creation ... %s", order_query_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func GetOrderByOrderId(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
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")
|
||||
ctx.JSON(http.StatusBadRequest, "GetOrderByOrderId(): ERROR... order_id not valid")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -110,10 +110,10 @@ func GetOrderByOrderId(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
if scan_err != nil {
|
||||
if scan_err == pgx.ErrNoRows {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderByOrderId(): ERROR... no order matches the query")
|
||||
ctx.JSON(http.StatusBadRequest, "GetOrderByOrderId(): ERROR... no order matches the query")
|
||||
return
|
||||
}
|
||||
ctx.String(http.StatusInternalServerError, "GetOrderByOrderId(): ERROR... internal server error")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetOrderByOrderId(): ERROR... internal server error")
|
||||
log.Printf("GetOrderByOrderId(): ERROR... Failed to scan row... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -135,7 +135,7 @@ func GetOrderTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
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")
|
||||
ctx.JSON(http.StatusBadRequest, "GetOrderTable(): Invalid page number")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -143,21 +143,21 @@ func GetOrderTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
bind_err := ctx.ShouldBindJSON(&table_query)
|
||||
if bind_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderTable(): ERROR... invalid query")
|
||||
ctx.JSON(http.StatusBadRequest, "GetOrderTable(): ERROR... invalid query")
|
||||
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")
|
||||
ctx.JSON(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, table_query.Orderer, table_query.DateDue, table_query.DatePlaced)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetOrdertable(): ERROR... internal server error")
|
||||
ctx.JSON(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
|
||||
}
|
||||
@@ -168,7 +168,7 @@ func GetOrderTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
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")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetOrdertable(): ERROR... internal server error")
|
||||
log.Printf("GetOrderTable(): ERROR... Failed to scan query... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -193,13 +193,13 @@ func DeleteOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
order_id := ctx.Query("order_id")
|
||||
order_id_int, parse_err := strconv.ParseInt(order_id, 10, 64)
|
||||
if parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "DeleteOrder(): Invalid input")
|
||||
ctx.JSON(http.StatusBadRequest, "DeleteOrder(): Invalid input")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), "DELETE FROM order_record WHERE id = $1", order_id_int)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "DeleteOrder(): ERROR... failed to delete order")
|
||||
ctx.JSON(http.StatusInternalServerError, "DeleteOrder(): ERROR... failed to delete order")
|
||||
log.Printf("DeleteOrder(): ERROR... Failed to delete order %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -12,5 +12,5 @@ func BaseFunction(context *gin.Context) {
|
||||
}
|
||||
|
||||
func PublicEndpoint(context *gin.Context) {
|
||||
context.String(http.StatusOK, "Public endpoint for you to land at")
|
||||
context.JSON(http.StatusOK, "Public endpoint for you to land at")
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func SetUserName(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
defer conn.Release()
|
||||
if err != nil {
|
||||
// TODO: Log this error
|
||||
ctx.String(http.StatusInternalServerError, err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||
log.Printf("SetUserName(): ERROR... Failed to acquire connection... %s", err.Error())
|
||||
return
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func SetUserName(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
user_name := ctx.Query("user_name")
|
||||
|
||||
if user_name == "" {
|
||||
ctx.String(http.StatusBadRequest, "CreateUser(): ERROR: user name not supplied")
|
||||
ctx.JSON(http.StatusBadRequest, "CreateUser(): ERROR: user name not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -46,18 +46,18 @@ func PromoteUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
defer conn.Release()
|
||||
if conn_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, conn_err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, conn_err.Error())
|
||||
}
|
||||
|
||||
user_id := ctx.Query("user_id")
|
||||
if user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "PromoteUser(): ERROR Missing user id")
|
||||
ctx.JSON(http.StatusBadRequest, "PromoteUser(): ERROR Missing user id")
|
||||
return
|
||||
}
|
||||
|
||||
_, update_err := conn.Exec(context.Background(), queries.USER_SET_IS_ADMIN_QUERY, user_id)
|
||||
if update_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, update_err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, update_err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,18 +66,18 @@ func DemoteUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, conn_err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, conn_err.Error())
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
user_id := ctx.Query("user_id")
|
||||
if user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "ERROR: User Id Not Supplied")
|
||||
ctx.JSON(http.StatusBadRequest, "ERROR: User Id Not Supplied")
|
||||
return
|
||||
}
|
||||
_, exec_err := conn.Exec(context.Background(), queries.USER_REVOKE_ADMIN_QUERY, user_id)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, exec_err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, exec_err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func DeactivateUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
user_id := ctx.Query("user_id")
|
||||
if user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "DeactivateUser(): User id not supplied")
|
||||
ctx.JSON(http.StatusBadRequest, "DeactivateUser(): User id not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -101,6 +101,25 @@ func DeactivateUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func ActivateUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
log.Printf("DeactivateUser(): ERROR: Failed to connect... %s", conn_err.Error())
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
user_id := ctx.Query("user_id")
|
||||
if user_id == "" {
|
||||
ctx.JSON(http.StatusBadRequest, "DeactivateUser(): User id not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
conn.Exec(context.Background(), queries.USER_SET_ACTIVE_QUERY, user_id)
|
||||
}
|
||||
}
|
||||
|
||||
func GetUserTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
@@ -113,23 +132,23 @@ func GetUserTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
var request dto.UserRequest
|
||||
body_read_err := ctx.ShouldBindJSON(&request)
|
||||
if body_read_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "GetUserTable(): ERROR... Invalid user query object")
|
||||
ctx.JSON(http.StatusBadRequest, "GetUserTable(): ERROR... Invalid user query object")
|
||||
return
|
||||
}
|
||||
|
||||
page := ctx.Query("page")
|
||||
if page == "" {
|
||||
ctx.String(http.StatusBadRequest, "GetUserTable(): Missing page")
|
||||
ctx.JSON(http.StatusBadRequest, "GetUserTable(): Missing page")
|
||||
return
|
||||
}
|
||||
page_int, conv_err := strconv.Atoi(page)
|
||||
if conv_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "GetUserTable(): Not an integer")
|
||||
ctx.JSON(http.StatusBadRequest, "GetUserTable(): Not an integer")
|
||||
return
|
||||
}
|
||||
rows, query_err := conn.Query(context.Background(), queries.USER_GET_TABLE_DATA, page_int*utils.PAGE_SIZE, utils.PAGE_SIZE, request.Name, request.JobPosition)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetUserTable(): Failed to query database...")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetUserTable(): Failed to query database...")
|
||||
log.Printf("GetUserTable(): ERROR... %s", query_err.Error())
|
||||
}
|
||||
defer rows.Close()
|
||||
@@ -139,7 +158,7 @@ func GetUserTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
var user dto.UserResponse
|
||||
scan_err := rows.Scan(&user.Id, &user.Name, &user.JobPosition, &user.Active, &user.Admin)
|
||||
if scan_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetUserTable(): ERROR: Failed to scan..")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetUserTable(): ERROR: Failed to scan..")
|
||||
log.Printf("GetUserTable(): ERROR... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -168,7 +187,7 @@ func GetCurrentAuthenticatedUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
query_err := conn.QueryRow(context.Background(), queries.GET_CURRENT_USER_OBJECT, sub_id).Scan(&user.Id, &user.Name, &user.JobPosition, &user.Active, &user.Admin)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetCurrentAuthenticatedUser(): ERROR.... Failed to query")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetCurrentAuthenticatedUser(): ERROR.... Failed to query")
|
||||
log.Printf("GetCurrentAuthenticatedUser(): ERROR in querying user table... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -189,13 +208,13 @@ func CreatePosition(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
position_name := ctx.Query("position_name")
|
||||
if position_name == "" {
|
||||
ctx.String(http.StatusBadRequest, "CreatePosition(): ERROR... Position name not supplied!")
|
||||
ctx.JSON(http.StatusBadRequest, "CreatePosition(): ERROR... Position name not supplied!")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), queries.CREATE_POSITION, position_name)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "CreatePosition(): ERROR... exec failed")
|
||||
ctx.JSON(http.StatusInternalServerError, "CreatePosition(): ERROR... exec failed")
|
||||
log.Println("CreatePosition(): ERROR... exec failed... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -215,7 +234,7 @@ func SetUserPosition(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
user_id := ctx.Query("user_id")
|
||||
|
||||
if position_name == "" || user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "SetUserPosition(): ERROR... Missing required parameter")
|
||||
ctx.JSON(http.StatusBadRequest, "SetUserPosition(): ERROR... Missing required parameter")
|
||||
}
|
||||
|
||||
var position_id string
|
||||
@@ -223,17 +242,17 @@ func SetUserPosition(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
query_err := conn.QueryRow(context.Background(), queries.POSITION_GET_POSITION, position_name).Scan(&position_id, &position_name_query)
|
||||
if query_err != nil {
|
||||
if query_err == pgx.ErrNoRows {
|
||||
ctx.String(http.StatusBadRequest, "SetUserPosition(): ERROR... No such position exists.")
|
||||
ctx.JSON(http.StatusBadRequest, "SetUserPosition(): ERROR... No such position exists.")
|
||||
return
|
||||
}
|
||||
ctx.String(http.StatusInternalServerError, "SetUserPosition(): ERROR... Failed to query")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetUserPosition(): ERROR... Failed to query")
|
||||
log.Println("SetUserPosition(): ERROR... Failed to query position table... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), queries.USER_SET_POSITION, position_id, user_id)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "SetUserPosition(): ERROR... failed to update user object")
|
||||
ctx.JSON(http.StatusInternalServerError, "SetUserPosition(): ERROR... failed to update user object")
|
||||
log.Println("SetUserPosition(): ERROR... Failed to update user object... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user