generated from kir-dev/next-nest-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/5-event-implement
- Loading branch information
Showing
4 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
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,54 @@ | ||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common'; | ||
import { Prisma, Role } from '@prisma/client'; | ||
|
||
import { GroupsService } from './groups.service'; | ||
|
||
@Controller('groups') | ||
export class GroupsController { | ||
constructor(private readonly groupsService: GroupsService) {} | ||
|
||
@Post() | ||
create(@Body() data: Prisma.GroupCreateInput) { | ||
return this.groupsService.create(data); | ||
} | ||
|
||
@Get() | ||
findAll() { | ||
return this.groupsService.findAll(); | ||
} | ||
|
||
@Get(':id') | ||
findOne(@Param('id') id: string) { | ||
return this.groupsService.findOne(Number(id)); | ||
} | ||
|
||
@Get(':id/members') | ||
findMembers(@Param('id') id: string) { | ||
return this.groupsService.findMembers(Number(id)); | ||
} | ||
|
||
@Patch(':id') | ||
update(@Param('id') id: string, @Body() data: Prisma.GroupUpdateInput) { | ||
return this.groupsService.update(Number(id), data); | ||
} | ||
|
||
@Delete(':id') | ||
remove(@Param('id') id: string) { | ||
return this.groupsService.remove(Number(id)); | ||
} | ||
|
||
@Post(':id/members/:userId') | ||
addMember(@Param('id') id: string, @Param('userId') userId: string) { | ||
return this.groupsService.addMember(Number(id), Number(userId)); | ||
} | ||
|
||
@Patch(':id/members/:userId') | ||
updateRole(@Param('id') id: string, @Param('userId') userId: string, @Body() newRole: { role: Role }) { | ||
return this.groupsService.updateMemberRole(Number(id), Number(userId), newRole); | ||
} | ||
|
||
@Delete(':id/members/:userId') | ||
removeMember(@Param('id') id: string, @Param('userId') userId: string) { | ||
return this.groupsService.removeMember(Number(id), Number(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 { GroupsController } from './groups.controller'; | ||
import { GroupsService } from './groups.service'; | ||
|
||
@Module({ | ||
controllers: [GroupsController], | ||
providers: [GroupsService], | ||
}) | ||
export class GroupsModule {} |
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,120 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { Group, GroupMembers, Prisma, Role, User } from '@prisma/client'; | ||
import { PrismaService } from 'nestjs-prisma'; | ||
|
||
@Injectable() | ||
export class GroupsService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async create(data: Prisma.GroupCreateInput): Promise<Group> { | ||
try { | ||
return await this.prisma.group.create({ data }); | ||
} catch { | ||
throw new NotFoundException('Group could not be created'); | ||
} | ||
} | ||
|
||
async findAll(): Promise<Group[]> { | ||
try { | ||
return await this.prisma.group.findMany(); | ||
} catch { | ||
throw new NotFoundException('Groups could not be found'); | ||
} | ||
} | ||
|
||
async findOne(id: number): Promise<Group> { | ||
try { | ||
return await this.prisma.group.findUnique({ where: { id } }); | ||
} catch { | ||
throw new NotFoundException(`Group with ID ${id} not found`); | ||
} | ||
} | ||
|
||
async findMembers(id: number): Promise<{ user: User; role: Role }[]> { | ||
const groupMembers = await this.prisma.groupMembers.findMany({ | ||
where: { groupId: id }, | ||
include: { User: true }, | ||
}); | ||
if (!groupMembers) { | ||
throw new NotFoundException(`Group with ID ${id} not found`); | ||
} | ||
return groupMembers.map((groupMember) => ({ | ||
user: groupMember.User, | ||
role: groupMember.role, | ||
})); | ||
} | ||
|
||
async update(id: number, data: Prisma.GroupUpdateInput): Promise<Group> { | ||
try { | ||
return await this.prisma.group.update({ where: { id }, data }); | ||
} catch { | ||
throw new NotFoundException(`Group with ID ${id} could not be updated`); | ||
} | ||
} | ||
|
||
async remove(id: number): Promise<Group> { | ||
try { | ||
return await this.prisma.group.delete({ where: { id } }); | ||
} catch { | ||
throw new NotFoundException(`Group with ID ${id} could not be deleted`); | ||
} | ||
} | ||
|
||
async addMember(groupId: number, userId: number): Promise<GroupMembers> { | ||
if (await this.prisma.groupMembers.findUnique({ where: { groupId_userId: { groupId, userId } } })) { | ||
throw new NotFoundException('User is already a member of the group'); | ||
} | ||
if (!(await this.prisma.user.findUnique({ where: { id: userId } }))) { | ||
throw new NotFoundException('User does not exist'); | ||
} | ||
try { | ||
return await this.prisma.groupMembers.create({ | ||
data: { | ||
groupId, | ||
userId, | ||
}, | ||
}); | ||
} catch { | ||
throw new NotFoundException('User could not be added to the group'); | ||
} | ||
} | ||
|
||
async updateMemberRole(groupId: number, userId: number, newRole: { role: Role }): Promise<GroupMembers> { | ||
if (!(await this.prisma.groupMembers.findUnique({ where: { groupId_userId: { groupId, userId } } }))) { | ||
throw new NotFoundException(`User with id: ${userId} is not a member of the group.`); | ||
} | ||
try { | ||
return await this.prisma.groupMembers.update({ | ||
where: { | ||
groupId_userId: { | ||
groupId, | ||
userId, | ||
}, | ||
}, | ||
data: { | ||
role: newRole.role, | ||
}, | ||
}); | ||
} catch { | ||
throw new NotFoundException('User role could not be updated.'); | ||
} | ||
} | ||
|
||
async removeMember(groupId: number, userId: number): Promise<GroupMembers> { | ||
if (!(await this.prisma.groupMembers.findUnique({ where: { groupId_userId: { groupId, userId } } }))) { | ||
throw new NotFoundException('User is not a member of the group'); | ||
} | ||
try { | ||
return await this.prisma.groupMembers.delete({ | ||
where: { | ||
groupId_userId: { | ||
groupId, | ||
userId, | ||
}, | ||
}, | ||
}); | ||
} catch { | ||
throw new NotFoundException('User could not be removed from the group'); | ||
} | ||
} | ||
} |