All files / src/band band.service.ts

88.46% Statements 23/26
66.66% Branches 2/3
85.71% Functions 6/7
87.5% Lines 21/24

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 766x         6x       6x     6x   6x 6x                       3x             4x   2x 2x       2x 1x     1x 1x       2x   2x 2x 1x     1x       1x         2x                  
import {
  ConflictException,
  Injectable,
  NotFoundException,
} from '@nestjs/common';
import { BandRepository } from './band.repository';
import { BandDto, CreateBandDto } from './dto/band.dto';
import { SearchBandDto } from './dto/search-band.dto';
import { UpdateBandDto } from './dto/update-band.dto';
import { UserService } from '../user/user.service';
 
@Injectable()
export class BandService {
  constructor(
    private readonly bandRepository: BandRepository,
    private readonly userService: UserService,
  ) {}
 
  async create(
    bandDTO: CreateBandDto,
    founderId: string,
  ): Promise<{ message: string }> {
    await this.bandRepository.createBand(bandDTO, founderId);
    return { message: 'Band has been created' };
  }
 
  async searchBands(name: string): Promise<SearchBandDto[]> {
    return this.bandRepository.searchBands(name);
  }
 
  async updateBand(
    userId: string,
    updateBandDto: UpdateBandDto,
  ): Promise<{ message: string }> {
    await this.userService.validateUser(userId);
 
    const band = await this.bandRepository.findBandById(updateBandDto.id);
    Iif (!band) {
      throw new NotFoundException({ message: 'Band not found' });
    }
 
    if (userId !== band.founderId) {
      throw new ConflictException({ message: 'User must be the band founder' });
    }
 
    await this.bandRepository.updateBand(updateBandDto.id, updateBandDto);
    return { message: 'User updated successfully' };
  }
 
  async getBandById(userId: string, bandId: string): Promise<BandDto> {
    await this.userService.validateUser(userId);
 
    const band = await this.bandRepository.findBandById(bandId);
    if (!band) {
      throw new NotFoundException({ message: 'Band not found' });
    }
 
    return this.toBandDto(band);
  }
 
  private toBandDto(band: any): BandDto {
    return {
      bandId: band.id,
      name: band.name,
      description: band.description,
      icon: band.avatarId,
      members: band.users.map((user: { user: any; instrument: any }) => ({
        id: user.user.id,
        userName: user.user.name,
        instrumentId: user.instrument.id,
        instrumentName: user.instrument.name,
      })),
    };
  }
}