All files / src/band band.repository.ts

75% Statements 9/12
100% Branches 0/0
66.66% Functions 4/6
70% Lines 7/10

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 876x 6x           6x 6x                                                                       3x                     4x                                                 1x            
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CreateBandDto } from './dto/band.dto';
import { SearchBandDto } from './dto/search-band.dto';
import { Band, Prisma } from '@prisma/client';
 
@Injectable()
export class BandRepository {
  constructor(private readonly prisma: PrismaService) {}
 
  async createBand(data: CreateBandDto, founderId: any): Promise<any> {
    const { name, description, avatarId, members } = data;
 
    return this.prisma.band.create({
      data: {
        name,
        description,
        avatarId,
        founder: {
          connect: { id: founderId },
        },
        users: {
          create: members.map((member) => ({
            user: {
              connect: { id: member.id },
            },
            instrument: {
              connect: { id: member.instrumentId },
            },
          })),
        },
      },
      include: {
        users: {
          include: {
            user: true,
            instrument: true,
          },
        },
      },
    });
  }
 
  async searchBands(name: string): Promise<SearchBandDto[]> {
    return this.prisma.band.findMany({
      where: {
        name: {
          contains: name,
          mode: 'insensitive', // Case-insensitive search
        },
      },
    });
  }
 
  async findBandById(id: string): Promise<Band | null> {
    return this.prisma.band.findUnique({
      where: { id },
      include: {
        users: {
          select: {
            user: {
              select: {
                id: true,
                name: true,
              },
            },
            instrument: {
              select: {
                id: true,
                name: true,
              },
            },
          },
        },
        slots: true,
      },
    });
  }
 
  async updateBand(id: string, data: Prisma.BandUpdateInput): Promise<Band> {
    return this.prisma.band.update({
      where: { id },
      data,
    });
  }
}