-
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.
create: bookmark module and funtionality added
- Loading branch information
Showing
3 changed files
with
148 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpCode, | ||
HttpStatus, | ||
Param, | ||
ParseIntPipe, | ||
Patch, | ||
Post, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { GetUser } from '../auth/decorator'; | ||
import { JwtGuard } from '../auth/guard'; | ||
import { BookmarkService } from './bookmark.service'; | ||
import { CreateBookmarkDto, EditBookmarkDto } from './dto'; | ||
|
||
@UseGuards(JwtGuard) | ||
@Controller('bookmarks') | ||
export class BookmarkController { | ||
constructor(private bookmarkService: BookmarkService) {} | ||
@Get() | ||
getBookmarks(@GetUser('id') userId: number) { | ||
return this.bookmarkService.getBookmarks(userId); | ||
} | ||
|
||
@Get(':id') | ||
getBookmarkById( | ||
@GetUser('id') userId: number, | ||
@Param('id', ParseIntPipe) bookmarkId: number, | ||
) { | ||
return this.bookmarkService.getBookmarkById(userId, bookmarkId); | ||
} | ||
|
||
@Post() | ||
createBookmark( | ||
@GetUser('id') userId: number, | ||
@Body() dto: CreateBookmarkDto, | ||
) { | ||
return this.bookmarkService.createBookmark(userId, dto); | ||
} | ||
|
||
@Patch(':id') | ||
editBookmarkById( | ||
@GetUser('id') userId: number, | ||
@Param('id', ParseIntPipe) bookmarkId: number, | ||
@Body() dto: EditBookmarkDto, | ||
) { | ||
return this.bookmarkService.editBookmarkById(userId, bookmarkId, dto); | ||
} | ||
|
||
@HttpCode(HttpStatus.NO_CONTENT) | ||
@Delete(':id') | ||
deleteBookmarkById( | ||
@GetUser('id') userId: number, | ||
@Param('id', ParseIntPipe) bookmarkId: number, | ||
) { | ||
return this.bookmarkService.deleteBookmarkById(userId, bookmarkId); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { BookmarkController } from './bookmark.controller'; | ||
import { BookmarkService } from './bookmark.service'; | ||
|
||
@Module({}) | ||
@Module({ | ||
controllers: [BookmarkController], | ||
providers: [BookmarkService], | ||
}) | ||
export class BookmarkModule {} |
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,81 @@ | ||
import { ForbiddenException, Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { CreateBookmarkDto, EditBookmarkDto } from './dto'; | ||
|
||
@Injectable() | ||
export class BookmarkService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
getBookmarks(userId: number) { | ||
return this.prisma.bookmark.findMany({ | ||
where: { | ||
userId, | ||
}, | ||
}); | ||
} | ||
|
||
getBookmarkById(userId: number, bookmarkId: number) { | ||
return this.prisma.bookmark.findFirst({ | ||
where: { | ||
id: bookmarkId, | ||
userId, | ||
}, | ||
}); | ||
} | ||
|
||
async createBookmark(userId: number, dto: CreateBookmarkDto) { | ||
const bookmark = await this.prisma.bookmark.create({ | ||
data: { | ||
userId, | ||
...dto, | ||
}, | ||
}); | ||
|
||
return bookmark; | ||
} | ||
|
||
async editBookmarkById( | ||
userId: number, | ||
bookmarkId: number, | ||
dto: EditBookmarkDto, | ||
) { | ||
// get the bookmark by id | ||
const bookmark = await this.prisma.bookmark.findUnique({ | ||
where: { | ||
id: bookmarkId, | ||
}, | ||
}); | ||
|
||
// check if the user owns the bookmark | ||
if (!bookmark || bookmark.userId !== userId) { | ||
throw new ForbiddenException('Access to resources denied'); | ||
} | ||
|
||
return this.prisma.bookmark.update({ | ||
where: { | ||
id: bookmarkId, | ||
}, | ||
data: { | ||
...dto, | ||
}, | ||
}); | ||
} | ||
|
||
async deleteBookmarkById(userId: number, bookmarkId: number) { | ||
const bookmark = await this.prisma.bookmark.findUnique({ | ||
where: { | ||
id: bookmarkId, | ||
}, | ||
}); | ||
|
||
// check if user owns the bookmark | ||
if (!bookmark || bookmark.userId !== userId) | ||
throw new ForbiddenException('Access to resources denied'); | ||
|
||
await this.prisma.bookmark.delete({ | ||
where: { | ||
id: bookmarkId, | ||
}, | ||
}); | ||
} | ||
} |