Skip to content

Commit

Permalink
Added default value to the bandmembership's status attribute (Pending).
Browse files Browse the repository at this point in the history
  • Loading branch information
Danka Marcell committed Nov 27, 2024
1 parent 8ea31da commit 4acb56e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "BandMembership" ALTER COLUMN "status" SET DEFAULT 'PENDING';
2 changes: 1 addition & 1 deletion apps/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ model BandMembership {
userId Int?
band Band? @relation(fields: [bandId], references: [id])
bandId Int?
status BandMembershipStatus
status BandMembershipStatus @default(PENDING)
}

model Comment {
Expand Down
36 changes: 36 additions & 0 deletions apps/backend/src/band/band.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from 'nestjs-prisma';
import { User } from 'src/users/entities/user.entity';

import { CreateBandDto } from './dto/create-band.dto';
import { UpdateBandDto } from './dto/update-band.dto';
Expand Down Expand Up @@ -52,4 +53,39 @@ export class BandService {
throw new NotFoundException('No bands found');
}
}

async findMembers(id: number): Promise<User[]> {
try {
const bandmemberships = await this.prisma.bandMembership.findMany({ where: { bandId: id } });
if (!bandmemberships) throw new Error();
const members = await this.prisma.user.findMany({
where: { id: { in: bandmemberships.map((membership) => membership.userId) } },
});
return members;
} catch (error) {
throw new NotFoundException('No members found');
}
}

async addMember(bandId: number, userId: number): Promise<User> {
try {
const res = await this.prisma.bandMembership.create({
data: { band: { connect: { id: bandId } }, user: { connect: { id: userId } } },
});
if (!res) throw new Error();
return await this.prisma.user.findUnique({ where: { id: userId } });
} catch (error) {
throw new NotFoundException('No member found');
}
}

async removeMember(bandId: number, userId: number): Promise<User> {
try {
const res = await this.prisma.bandMembership.deleteMany({ where: { bandId, userId } });
if (!res) throw new Error();
return await this.prisma.user.findUnique({ where: { id: userId } });
} catch (error) {
throw new NotFoundException('No member found');
}
}
}

0 comments on commit 4acb56e

Please sign in to comment.