154 lines
5.2 KiB
TypeScript
154 lines
5.2 KiB
TypeScript
import {
|
|
Body,
|
|
ClassSerializerInterceptor,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Patch,
|
|
Post,
|
|
UseGuards,
|
|
UseInterceptors,
|
|
} from '@nestjs/common';
|
|
import {
|
|
ApiBody,
|
|
ApiOkResponse,
|
|
ApiOperation,
|
|
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 {
|
|
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) {}
|
|
|
|
@Get(':storageId')
|
|
@ApiOperation({ summary: 'Get storage by ID' })
|
|
@ApiOkResponse({ type: StorageResponseDto })
|
|
async getStorage(
|
|
@CurrentStorage() storage: Storage,
|
|
): Promise<StorageResponseDto> {
|
|
return this.service.formatStorageNoItems(storage);
|
|
}
|
|
|
|
@Patch(':storageId')
|
|
@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);
|
|
}
|
|
|
|
@UseGuards(StorageSetGuard)
|
|
@Get('set/:storageSetId')
|
|
@ApiOperation({ summary: 'Get storage set' })
|
|
@ApiOkResponse({ type: StorageSetResponseDto })
|
|
async getStorageSet(@CurrentStorageSet() set: StorageSet) {
|
|
return this.service.formatStorageSetNoItems(set);
|
|
}
|
|
|
|
@UseGuards(StorageSetGuard)
|
|
@Patch('set/:storageSetId')
|
|
@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')
|
|
@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')
|
|
@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')
|
|
@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')
|
|
@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')
|
|
@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')
|
|
@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);
|
|
}
|
|
}
|