generated from kir-dev/next-nest-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from kir-dev/band-crud
Band crud
- Loading branch information
Showing
13 changed files
with
243 additions
and
40 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
apps/backend/prisma/migrations/20241127200839_default_status/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "BandMembership" ALTER COLUMN "status" SET DEFAULT 'PENDING'; |
9 changes: 9 additions & 0 deletions
9
apps/backend/prisma/migrations/20241212122822_multiple_genres/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
Warnings: | ||
- The `genres` column on the `Band` table would be dropped and recreated. This will lead to data loss if there is data in the column. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "Band" DROP COLUMN "genres", | ||
ADD COLUMN "genres" TEXT[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post } from '@nestjs/common'; | ||
|
||
import { BandService } from './band.service'; | ||
import { CreateBandDto } from './dto/create-band.dto'; | ||
import { UpdateBandDto } from './dto/update-band.dto'; | ||
|
||
@Controller('band') | ||
export class BandController { | ||
constructor(private readonly bandService: BandService) {} | ||
|
||
@Post() | ||
create(@Body() createBandDto: CreateBandDto) { | ||
return this.bandService.create(createBandDto); | ||
} | ||
|
||
@Get() | ||
findAll() { | ||
return this.bandService.findAll(); | ||
} | ||
|
||
@Get(':id') | ||
findOne(@Param('id', ParseIntPipe) id: number) { | ||
return this.bandService.findOne(id); | ||
} | ||
|
||
@Patch(':id') | ||
update(@Param('id', ParseIntPipe) id: number, @Body() updateBandDto: UpdateBandDto) { | ||
return this.bandService.update(id, updateBandDto); | ||
} | ||
|
||
@Delete(':id') | ||
remove(@Param('id', ParseIntPipe) id: number) { | ||
return this.bandService.remove(id); | ||
} | ||
|
||
@Get(':id/members') | ||
findMembers(@Param('id', ParseIntPipe) id: number) { | ||
return this.bandService.findMembers(id); | ||
} | ||
|
||
@Post(':id/members/:userId') | ||
addMember(@Param('id', ParseIntPipe) bandId: number, @Param('userId', ParseIntPipe) userId: number) { | ||
return this.bandService.addMember(bandId, userId); | ||
} | ||
|
||
@Delete(':id/members/:userId') | ||
removeMember(@Param('id', ParseIntPipe) bandId: number, @Param('userId', ParseIntPipe) userId: number) { | ||
return this.bandService.removeMember(bandId, userId); | ||
} | ||
|
||
@Patch(':id/members/:userId') | ||
approveMember(@Param('id', ParseIntPipe) bandId: number, @Param('userId', ParseIntPipe) userId: number) { | ||
return this.bandService.approveMember(bandId, userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { BandController } from './band.controller'; | ||
import { BandService } from './band.service'; | ||
|
||
@Module({ | ||
controllers: [BandController], | ||
providers: [BandService], | ||
}) | ||
export class BandModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { BandMembership, BandMembershipStatus } from '@prisma/client'; | ||
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'; | ||
import { Band } from './entities/band.entity'; | ||
|
||
@Injectable() | ||
export class BandService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async create(createBandDto: CreateBandDto): Promise<Band> { | ||
return await this.prisma.band.create({ data: createBandDto }); | ||
} | ||
|
||
async findAll(): Promise<Band[]> { | ||
const res = await this.prisma.band.findMany({ | ||
include: { members: { include: { user: { select: { name: true } } } } }, | ||
}); | ||
return res; | ||
} | ||
|
||
async findOne(id: number): Promise<Band> { | ||
try { | ||
const res = await this.prisma.band.findUniqueOrThrow({ where: { id } }); | ||
return res; | ||
} catch (error) { | ||
throw new NotFoundException('No band found'); | ||
} | ||
} | ||
|
||
async update(id: number, updateBandDto: UpdateBandDto): Promise<Band> { | ||
return await this.prisma.band.update({ where: { id }, data: updateBandDto }); | ||
} | ||
|
||
async remove(id: number): Promise<Band> { | ||
try { | ||
const res = await this.prisma.band.delete({ where: { id } }); | ||
return res; | ||
} catch (error) { | ||
throw new NotFoundException('No bands found'); | ||
} | ||
} | ||
|
||
async findMembers(id: number): Promise<User[]> { | ||
try { | ||
const bandmemberships = await this.prisma.bandMembership.findMany({ | ||
where: { bandId: id }, | ||
include: { user: true }, | ||
}); | ||
return bandmemberships.map((membership) => membership.user); | ||
} catch (error) { | ||
throw new NotFoundException('No members found'); | ||
} | ||
} | ||
|
||
async addMember(bandId: number, userId: number): Promise<BandMembership> { | ||
try { | ||
const res = await this.prisma.bandMembership.create({ | ||
data: { band: { connect: { id: bandId } }, user: { connect: { id: userId } } }, | ||
}); | ||
return res; | ||
} catch (error) { | ||
throw new NotFoundException('Member could not be added'); | ||
} | ||
} | ||
|
||
async removeMember(bandId: number, userId: number) { | ||
try { | ||
const res = await this.prisma.bandMembership.deleteMany({ where: { bandId, userId } }); | ||
const members = await this.findMembers(bandId); | ||
if (members.length === 0) { | ||
await this.remove(bandId); | ||
} | ||
return res; | ||
} catch (error) { | ||
throw new NotFoundException('No member found'); | ||
} | ||
} | ||
|
||
async approveMember(bandId: number, userId: number) { | ||
try { | ||
return await this.prisma.bandMembership.updateMany({ | ||
where: { bandId, userId }, | ||
data: { status: BandMembershipStatus.ACCEPTED }, | ||
}); | ||
} catch (error) { | ||
throw new NotFoundException('No member found'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { OmitType } from '@nestjs/swagger'; | ||
|
||
import { Band } from '../entities/band.entity'; | ||
|
||
export class CreateBandDto extends OmitType(Band, ['id']) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
|
||
import { CreateBandDto } from './create-band.dto'; | ||
|
||
export class UpdateBandDto extends PartialType(CreateBandDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { IsNotEmpty, IsNumber, IsOptional, IsPositive, IsString } from 'class-validator'; | ||
|
||
export class Band { | ||
@IsNotEmpty() | ||
@IsNumber() | ||
@IsPositive() | ||
id: number; | ||
|
||
@IsNotEmpty() | ||
name: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
email: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
webPage: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
description: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
genres: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters