import axios from 'axios' import {CreateItemQuery} from '../queries/CreateItemQuery' import {ItemPriceResponse} from '../response/ItemPriceResponse' import {OrderItemPriceResponse} from '../response/OrderItemPriceResponse' import {SetItemPriceQuery} from '../queries/SetItemPriceQuery' import {GetCurrentPriceQuery} from '../queries/GetCurrentPriceQuery' import {AddItemToOrderQuery} from '../queries/AddItemToOrderQuery' import {GetOrderItemsQuery} from '../queries/GetOrderItemsQuery' import {SetItemMadeRequest} from '../request/SetItemMadeRequest' import {OrderFilledResponse} from '../response/OrderFilledResponse' import {SetItemQuantityRequest} from '../request/SetItemQuantityRequest' import {DeleteOrderItemRequest} from '../request/DeleteOrderItemRequest' import { ItemHistoryResponse } from '../response/ItemHistoryResponse' export const CreateItem = async (query: CreateItemQuery): Promise => { const queryParams = new URLSearchParams({ item_name: query.item_name, in_season: query.in_season, item_price: query.item_price }) const res = await axios.post(process.env.NEXT_PUBLIC_API_URL + `/item/create?${queryParams.toString()}`, {}, {withCredentials: true}) return res.data } export const SetItemPrice = async (query: SetItemPriceQuery) => { const queryParams = new URLSearchParams({ item_id: query.item_id, item_price: query.item_price }) const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + `/item/price?${queryParams.toString()}`, {}, {withCredentials: true}) if (res.data.Location) { window.location.href = res.data.Location } } export const GetCurrentPrice = async (query: GetCurrentPriceQuery): Promise => { const queryParams = new URLSearchParams({ item_id: query.item_id }) const res = await axios.get(process.env.API_URL + `/item/price?${queryParams.toString()}`) if (res.data.Location) { window.location.href = res.data.Location } return res.data } export const AddItemToOrder = async (query: AddItemToOrderQuery): Promise => { const queryParams = new URLSearchParams({ item_id: query.item_id, order_id: query.order_id, quantity: query.quantity }) console.log(process.env.NEXT_PUBLIC_API_URL + `/order/item?${queryParams.toString()}`) const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + `/order/item?${queryParams.toString()}`, {}, {withCredentials: true}) if (res.data?.Location) { window.location.href = res.data.Location } return res.data } export const GetOrderItems = async (query: GetOrderItemsQuery): Promise => { const queryParams = new URLSearchParams({ order_id: query.order_id }) const res = await axios.get(process.env.NEXT_PUBLIC_API_URL + `/order/items?${queryParams.toString()}`, {withCredentials: true}) if (res.data?.Location) { window.location.href = res.data.Location } return res.data || [] } export const SetItemMade = async (request: SetItemMadeRequest): Promise => { const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + '/item/made', request, {withCredentials: true}) if (res.data?.Location) { window.location.href = res.data.Location } return res.data } export const SetItemQuantity = async (request: SetItemQuantityRequest): Promise => { const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + `/item/quantity`, request, {withCredentials: true}) if (res.data.Location) { window.location.href = res.data.Location } return res.data } export const DeleteOrderItem = async (request: DeleteOrderItemRequest) => { const res = await axios.delete(process.env.API_URL + `/order/item?order_id=${request.order_id}&item_id=${request.item_id}`) if (res.data.Location) { window.location.href = res.data.Location } } export const DeleteItem = async (itemId: number) => { const queryParams = new URLSearchParams({ item_id: itemId.toString() }) const res = await axios.delete(process.env.API_URL + `/item?${queryParams.toString()}`) if (res.data.Location) { window.location.href = res.data.Location } } export const GetItems = async (): Promise => { const res = await axios.get(process.env.NEXT_PUBLIC_API_URL + `/items`, {withCredentials: true}) if (res.data.Location) { window.location.href = res.data.Location } return res.data } export const GetItemHistory = async (itemId: number): Promise => { const res = await axios.get(process.env.NEXT_PUBLIC_API_URL + `/item/history?item_id=${itemId.toString()}`, {withCredentials: true}) return res.data }