All files / src/band band.controller.ts

100% Statements 31/31
100% Branches 0/0
100% Functions 8/8
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 1449x                           9x 9x 9x   9x 9x 9x   9x                           9x 9x                                   9x       2x 2x                     9x 3x               9x       4x 4x                     9x       5x 5x           9x       2x 2x                     9x 2x                     9x       4x 4x      
import {
  Controller,
  Post,
  Body,
  HttpCode,
  HttpStatus,
  Get,
  Put,
  Query,
  Req,
  UseGuards,
  Param,
  Delete,
} from '@nestjs/common';
import { BandService } from './band.service';
import { BandDto, CreateBandDto } from './dto/band.dto';
import { AuthGuard } from '../auth/auth.guard';
import { SearchBandDto } from './dto/search-band.dto';
import { UpdateBandDto } from './dto/update-band.dto';
import { ApiRequest } from '../auth/types/api-request';
import { CreateSlotDto } from './dto/create-slot.dto';
import { BandDto as UserBandDto } from '../user/dto/user.dto';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
 
@ApiTags('Bands')
@ApiBearerAuth()
@Controller('bands')
@UseGuards(AuthGuard)
export class BandController {
  constructor(private readonly bandService: BandService) {}
 
  @Post('/')
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Create a new band' })
  @ApiResponse({ status: 201, description: 'Band created successfully' })
  @ApiResponse({
    status: 404,
    description: 'Users with IDs {userIds} not found',
  })
  @ApiResponse({
    status: 404,
    description: 'Founder with ID {founderId} not found',
  })
  @ApiBody({
    type: CreateBandDto,
    description: 'Details of the band to be created',
  })
  async createBand(
    @Req() req: ApiRequest,
    @Body() bandDTO: CreateBandDto,
  ): Promise<UserBandDto | undefined> {
    const founderId = req.user.userId;
    return await this.bandService.create(bandDTO, founderId);
  }
 
  @Get('search')
  @ApiOperation({ summary: 'Search bands by name' })
  @ApiResponse({ status: 200, description: '{searched band data}' })
  @ApiQuery({
    name: 'name',
    type: String,
    description: 'Name of the band to search for',
  })
  async searchBands(@Query('name') name: string): Promise<SearchBandDto[]> {
    return this.bandService.searchBands(name);
  }
 
  @Put()
  @ApiOperation({ summary: 'Update band details' })
  @ApiBody({ type: UpdateBandDto, description: 'Updated details of the band' })
  @ApiResponse({ status: 201, description: 'Band updated successfully' })
  @ApiResponse({ status: 409, description: 'User must be the band founder' })
  async updateBand(
    @Req() req: ApiRequest,
    @Body() updateBandDto: UpdateBandDto,
  ): Promise<{ message: string }> {
    const userId = req.user.userId;
    return await this.bandService.updateBand(userId, updateBandDto);
  }
 
  @Post('slots')
  @ApiOperation({ summary: 'Create a new slot for a band' })
  @ApiResponse({ status: 201, description: 'Slot created successfully' })
  @ApiResponse({ status: 404, description: 'Band with ID {bandId} not found' })
  @ApiBody({
    type: CreateSlotDto,
    description: 'Details of the slot to be created',
  })
  async createSlot(
    @Req() req: ApiRequest,
    @Body() slotDto: CreateSlotDto,
  ): Promise<{ message: string }> {
    const userId = req.user.userId;
    return await this.bandService.createSlot(userId, slotDto);
  }
 
  @Get(':id')
  @ApiOperation({ summary: 'Get band by ID' })
  @ApiParam({ name: 'id', type: String, description: 'ID of the band' })
  async getBandById(
    @Req() req: ApiRequest,
    @Param('id') bandId: string,
  ): Promise<BandDto> {
    const userId = req.user.userId;
    return await this.bandService.getBandById(userId, bandId);
  }
 
  // delete band by id
  @Delete(':id')
  @ApiOperation({ summary: 'Delete band by ID' })
  @ApiParam({
    name: 'id',
    type: String,
    description: 'ID of the band to be deleted',
  })
  async deleteBand(@Param('id') id: string): Promise<{ message: string }> {
    return this.bandService.deleteBand(id);
  }
 
  @Delete('slots/:slotId')
  @ApiOperation({ summary: 'Delete slot by ID' })
  @ApiParam({
    name: 'slotId',
    type: String,
    description: 'ID of the slot to be deleted',
  })
  @HttpCode(HttpStatus.OK)
  async deleteSlot(
    @Req() req: ApiRequest,
    @Param('slotId') slotId: string,
  ): Promise<{ message: string }> {
    const userId = req.user.userId;
    return this.bandService.deleteSlot(slotId, userId);
  }
}