homemanager-be/src/app-storage/app-storage.controller.ts

217 lines
7.5 KiB
TypeScript

import {
Body,
ClassSerializerInterceptor,
Controller,
Delete,
Get,
Param,
ParseIntPipe,
Patch,
Post,
Query,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import {
ApiBody,
ApiOkResponse,
ApiOperation,
ApiParam,
ApiSecurity,
ApiTags,
} from '@nestjs/swagger';
import { Room } from 'src/objects/building/entities/room.entity';
import { StorageSet } from 'src/objects/storage/entities/storage-set.entity';
import { Storage } from 'src/objects/storage/entities/storage.entity';
import { User } from 'src/objects/user/user.entity';
import { CurrentRoom } from 'src/shared/decorators/current-room.decorator';
import { CurrentStorageSet } from 'src/shared/decorators/current-storage-set.decorator';
import { CurrentStorage } from 'src/shared/decorators/current-storage.decorator';
import { LoggedInUser } from 'src/shared/decorators/user.decorator';
import { AuthGuard } from 'src/shared/guards/auth.guard';
import { BuildingGuard } from 'src/shared/guards/building.guard';
import { RoomGuard } from 'src/shared/guards/room.guard';
import { StorageSetGuard } from 'src/shared/guards/storage-set.guard';
import { StorageGuard } from 'src/shared/guards/storage.guard';
import { AppStorageService } from './app-storage.service';
import {
StorageAddExistingItemRequestDto,
StorageAddItemRequestDto,
} from './dto/storage-add-item-request.dto';
import { StorageItemRequestQueryDto } from './dto/storage-item-request.dto';
import {
StorageItemSearchResponseDto,
StorageStoredItemResponseDto,
} from './dto/storage-item-response.dto';
import {
StorageCreateRequestDto,
StorageUpdateRequestDto,
} from './dto/storage-request.dto';
import { StorageResponseDto } from './dto/storage-response.dto';
import {
StorageSetCreateRequestDto,
StorageSetUpdateRequestDto,
} from './dto/storage-set-request.dto';
import { StorageSetResponseDto } from './dto/storage-set-response.dto';
@Controller({
path: 'storage',
})
@ApiTags('storage')
@ApiSecurity('Bearer token')
@UseInterceptors(ClassSerializerInterceptor)
@UseGuards(AuthGuard, BuildingGuard, RoomGuard, StorageGuard)
export class AppStorageController {
constructor(private readonly service: AppStorageService) {}
@UseGuards(StorageSetGuard)
@Get('set/:storageSetId')
@ApiParam({ name: 'storageSetId', description: 'Storage set ID' })
@ApiOperation({ summary: 'Get storage set' })
@ApiOkResponse({ type: StorageSetResponseDto })
async getStorageSet(@CurrentStorageSet() set: StorageSet) {
return this.service.formatStorageSetNoItems(set);
}
@UseGuards(StorageSetGuard)
@Patch('set/:storageSetId')
@ApiParam({ name: 'storageSetId', description: 'Storage set ID' })
@ApiBody({ type: StorageSetUpdateRequestDto })
@ApiOperation({ summary: 'Update storage set by ID' })
@ApiOkResponse({ type: StorageSetResponseDto })
async updateStorageSet(
@CurrentStorageSet() set: StorageSet,
@Body() body: StorageSetUpdateRequestDto,
): Promise<StorageSetResponseDto> {
return this.service.updateStorageSet(set, body);
}
@UseGuards(StorageSetGuard)
@Post('set/:storageSetId/:storageId')
@ApiParam({ name: 'storageSetId', description: 'Storage set ID' })
@ApiParam({ name: 'storageId', description: 'Storage ID' })
@ApiOperation({ summary: 'Move storage to storage set' })
@ApiOkResponse({ type: StorageSetResponseDto })
async moveStorage(
@CurrentStorageSet() set: StorageSet,
@CurrentStorage() storage: Storage,
): Promise<StorageSetResponseDto> {
return this.service.moveStorage(set, storage);
}
@UseGuards(StorageSetGuard)
@Delete('set/:storageSetId/:storageId')
@ApiParam({ name: 'storageSetId', description: 'Storage set ID' })
@ApiParam({ name: 'storageId', description: 'Storage ID' })
@ApiOperation({ summary: 'Remove storage from storage set' })
@ApiOkResponse({ type: StorageSetResponseDto })
async removeStorageFromSet(
@CurrentStorageSet() set: StorageSet,
@CurrentStorage() storage: Storage,
): Promise<StorageSetResponseDto> {
return this.service.removeFromSet(set, storage);
}
@Get('room/:roomId')
@ApiParam({ name: 'roomId', description: 'Room ID' })
@ApiOperation({ summary: 'Get storages in room' })
@ApiOkResponse({ type: StorageResponseDto, isArray: true })
async getStorages(@CurrentRoom() room: Room) {
return this.service.getStoragesInRoom(room.id);
}
@Get('set/room/:roomId')
@ApiParam({ name: 'roomId', description: 'Room ID' })
@ApiOperation({ summary: 'Get storage sets in room' })
@ApiOkResponse({ type: StorageSetResponseDto, isArray: true })
async getStorageSets(@CurrentRoom() room: Room) {
return this.service.getStorageSetsInRoom(room.id);
}
@Post('set/room/:roomId')
@ApiParam({ name: 'roomId', description: 'Room ID' })
@ApiBody({ type: StorageCreateRequestDto })
@ApiOperation({ summary: 'Create storage sets in room' })
@ApiOkResponse({ type: StorageSetResponseDto, isArray: true })
async createStorageSet(
@LoggedInUser() user: User,
@Body() body: StorageSetCreateRequestDto,
@CurrentRoom() room: Room,
) {
return this.service.createStorageSet(user, room, body);
}
@Post('room/:roomId')
@ApiParam({ name: 'roomId', description: 'Room ID' })
@ApiBody({ type: StorageCreateRequestDto })
@ApiOperation({ summary: 'Add a new storage to room' })
@ApiOkResponse({ type: StorageResponseDto })
async addStorage(
@LoggedInUser() user: User,
@Body() body: StorageCreateRequestDto,
@CurrentRoom() room: Room,
): Promise<StorageResponseDto> {
return this.service.createStorage(user, room, body);
}
@Get('item')
@ApiOperation({ summary: 'Search for an item' })
@ApiOkResponse({ type: StorageItemSearchResponseDto, isArray: true })
async searchForItem(
@LoggedInUser() user: User,
@Query() search: StorageItemRequestQueryDto,
) {
return this.service.searchForItems(user, search);
}
@Post('item/:storageId')
@ApiParam({ name: 'storageId', description: 'Storage ID' })
@ApiBody({ type: StorageAddItemRequestDto })
@ApiOperation({ summary: 'Add a new item to storage' })
@ApiOkResponse({ type: StorageStoredItemResponseDto })
async addItemToStorage(
@LoggedInUser() user: User,
@Body() body: StorageAddItemRequestDto,
@CurrentStorage() storage: Storage,
) {
return;
}
@Post('item/:storageId/:itemId')
@ApiParam({ name: 'storageId', description: 'Storage ID' })
@ApiParam({ name: 'itemId', description: 'Existing item ID' })
@ApiBody({ type: StorageAddExistingItemRequestDto })
@ApiOperation({ summary: 'Add an instance of an existing item to storage' })
@ApiOkResponse({ type: StorageStoredItemResponseDto })
async addExistingItemToStorage(
@Param('itemId', ParseIntPipe) itemId: number,
@LoggedInUser() user: User,
@Body() body: StorageAddExistingItemRequestDto,
@CurrentStorage() storage: Storage,
) {
return;
}
@Get(':storageId')
@ApiParam({ name: 'storageId', description: 'Storage ID' })
@ApiOperation({ summary: 'Get storage by ID' })
@ApiOkResponse({ type: StorageResponseDto })
async getStorage(
@CurrentStorage() storage: Storage,
): Promise<StorageResponseDto> {
return this.service.formatStorageNoItems(storage);
}
@Patch(':storageId')
@ApiParam({ name: 'storageId', description: 'Storage ID' })
@ApiBody({ type: StorageUpdateRequestDto })
@ApiOperation({ summary: 'Update storage by ID' })
@ApiOkResponse({ type: StorageResponseDto })
async updateStorage(
@CurrentStorage() storage: Storage,
@Body() body: StorageUpdateRequestDto,
): Promise<StorageResponseDto> {
return this.service.updateStorage(storage, body);
}
}