Skip to content

Commit

Permalink
create: bookmark module and funtionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
adiCacti committed May 16, 2022
1 parent 1a412ae commit 30f54dc
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/bookmark/bookmark.controller.ts
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);
}
}
7 changes: 6 additions & 1 deletion src/bookmark/bookmark.module.ts
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 {}
81 changes: 81 additions & 0 deletions src/bookmark/bookmark.service.ts
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,
},
});
}
}

0 comments on commit 30f54dc

Please sign in to comment.