94 lines
2.2 KiB
TypeScript
94 lines
2.2 KiB
TypeScript
'use server'
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import { sites } from '../_config/sites';
|
|
import { Stock, StockCreate, StockUpdate } from '../types/inventory/stockContent.types';
|
|
|
|
export async function stockCreate(data: StockCreate) {
|
|
|
|
const result = await fetch('http://192.168.50.205/api/stock', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
})
|
|
|
|
const resultData = await result.json();
|
|
|
|
if (!result.ok) {
|
|
return {
|
|
success: false,
|
|
errors: resultData.message, // kommt aus NestJS
|
|
};
|
|
}
|
|
|
|
|
|
revalidatePath(sites.stock.new.url)
|
|
}
|
|
|
|
export async function stockUpdate(id: string, data: StockUpdate): Promise<ActionResult> {
|
|
|
|
const result = await fetch(`http://192.168.50.205/api/stocks/${id}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
})
|
|
|
|
const resultData = await result.json();
|
|
|
|
if (!result.ok) {
|
|
return {
|
|
success: false,
|
|
errors: resultData.message, // kommt aus NestJS
|
|
};
|
|
}
|
|
|
|
revalidatePath(sites.stock.update.url + '/' + id);
|
|
return { success: true };
|
|
|
|
}
|
|
|
|
export async function stockFindAll(): Promise<Stock[]> {
|
|
|
|
const result = await fetch(`http://192.168.50.205/api/stocks`, {
|
|
method: 'GET',
|
|
headers: { 'Content-Type': 'application/json' }
|
|
})
|
|
const data = await result.json() as Stock[];
|
|
|
|
return data;
|
|
}
|
|
|
|
export async function stockFindById(id: string): Promise<any> {
|
|
|
|
const result = await fetch(`http://192.168.50.205/api/stocks/${id}`, {
|
|
method: 'GET',
|
|
headers: { 'Content-Type': 'application/json' }
|
|
})
|
|
const data = await result.json() as Stock
|
|
console.log('getArticle', data)
|
|
return data;
|
|
}
|
|
|
|
|
|
export async function stockDelete(id: string,revalidateUrl?:string): Promise<ActionResult> {
|
|
|
|
|
|
const result = await fetch(`http://192.168.50.205/api/stocks/${id}`, {
|
|
method: 'DELETE',
|
|
headers: { 'Content-Type': 'application/json' }
|
|
})
|
|
|
|
const resultData = await result.json();
|
|
|
|
if (!result.ok) {
|
|
return {
|
|
success: false,
|
|
errors: resultData.message, // kommt aus NestJS
|
|
};
|
|
}
|
|
|
|
if(revalidateUrl) revalidatePath(revalidateUrl);
|
|
|
|
return { success: true };
|
|
|
|
} |