67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import { Controller, Get, Post, Put, Body, Param, Headers, HttpException, HttpStatus } from '@nestjs/common';
|
|
import { MenuService } from './menu.service';
|
|
import { MenuItem } from './menu.interface';
|
|
|
|
@Controller('menu')
|
|
export class MenuController {
|
|
constructor(private readonly menuService: MenuService) { }
|
|
|
|
@Get('full')
|
|
async getFullMenu(@Headers('if-modified-since') ifModifiedSince?: string) {
|
|
console.log('GET /menu/full requested');
|
|
try {
|
|
const result = await this.menuService.getFullMenuWithCache(ifModifiedSince);
|
|
|
|
if (!result.fresh && ifModifiedSince) {
|
|
throw new HttpException('Not Modified', HttpStatus.NOT_MODIFIED);
|
|
}
|
|
|
|
return result.menu;
|
|
} catch (error) {
|
|
if (error.status === HttpStatus.NOT_MODIFIED) {
|
|
throw error;
|
|
}
|
|
throw new HttpException(
|
|
error.message || 'Failed to load menu',
|
|
HttpStatus.INTERNAL_SERVER_ERROR
|
|
);
|
|
}
|
|
}
|
|
/*
|
|
@Get('full')
|
|
async getFullMenu() {
|
|
console.log('Simplified endpoint called');
|
|
return { test: 'OK' }; // Простейший ответ
|
|
} */
|
|
|
|
@Get('check-updates')
|
|
async checkUpdates(@Headers('if-modified-since') ifModifiedSince: string) {
|
|
if (!ifModifiedSince) {
|
|
throw new HttpException('If-Modified-Since header is required', HttpStatus.BAD_REQUEST);
|
|
}
|
|
|
|
const hasUpdates = await this.menuService.checkForUpdates(ifModifiedSince);
|
|
return { hasUpdates };
|
|
}
|
|
|
|
@Post('save')
|
|
async saveMenu() {
|
|
await this.menuService.saveMenuToFile();
|
|
return { status: 'saved' };
|
|
}
|
|
|
|
@Post('overrides')
|
|
async saveOverrides(@Body() data: { overrides: Partial<MenuItem>[] }) {
|
|
await this.menuService.saveOverrides(data.overrides);
|
|
return { status: 'success' };
|
|
}
|
|
|
|
@Put(':id')
|
|
async updateMenuItem(
|
|
@Param('id') id: string,
|
|
@Body() update: Partial<MenuItem>
|
|
) {
|
|
const updatedItem = await this.menuService.updateMenuItem(id, update);
|
|
return updatedItem;
|
|
}
|
|
} |