Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[server] 게시글 북마크 on 기능 구현 #348

Merged
merged 18 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/server/src/bookmark/bookmark.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Controller, Param, Post, Req, UseGuards } from "@nestjs/common";
import { ApiHeader, ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
import { Public } from "src/commons/decorators/public.decorator";
import { UserAuthGuard } from "src/commons/guards/user-auth.guard";

import { BookmarkService } from "./bookmark.service";

@Public()
@Controller({
path: "bookmark",
})
@ApiTags("[subscribe] 게시글 북마크 ON API")
export class BookmarkControlelr {
constructor(private readonly bookmarkService: BookmarkService) {}

@Post("articles/:articleId")
@ApiOperation({
summary: "게시글 북마트 ON API",
description: "특정 유저가 북마크한 게시글을 저장합니다.",
})
@ApiResponse({
status: 200,
description: "성공 여부",
})
@ApiHeader({
name: "uuid",
description: "user uuid",
})
@UseGuards(UserAuthGuard)
async create(@Req() req, @Param("articleId") articleId: number) {
const { user } = req;
return this.bookmarkService.create(user, articleId);
}
}
49 changes: 44 additions & 5 deletions packages/server/src/bookmark/bookmark.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { AdminService } from "src/admin/admin.service";
import { AdminRepository } from "src/admin/repository/admin.repository";
import { ArticleRepository } from "src/article/article.repository";
import { ArticleService } from "src/article/article.service";
import { ArticleImageRepository } from "src/articleImage/articleImage.repository";
import { ArticleImageService } from "src/articleImage/articleImage.service";
import { BoardRepository } from "src/board/board.repository";
import { BoardService } from "src/board/board.service";
import { BoardTreeRepository } from "src/boardTree/boardTree.repository";
import { BoardTreeService } from "src/boardTree/boardTree.service";
import { HitRepository } from "src/hit/hit.repository";
import { AwsService } from "src/image/aws.service";
import { ImageRepository } from "src/image/image.repository";
import { ImageService } from "src/image/image.service";
import { SubscribeRepository } from "src/subscribe/subscribe.repository";
import { SubscribeService } from "src/subscribe/subscribe.service";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

북마크 모듈에서 필요한것만 import하는게 좋을것 같아요

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4c4c5a5
수정했음다~~


import { BookmarkControlelr } from "./bookmark.controller";
import { BookmarkRepository } from "./bookmark.repository";
import { BookmarkService } from "./bookmark.service";

@Module({
imports: [ TypeOrmModule.forFeature([ BookmarkRepository ]) ],
providers: [ BookmarkService ],
exports: [ BookmarkService ],
imports: [
TypeOrmModule.forFeature([
ArticleRepository,
BookmarkRepository,
HitRepository,
BoardRepository,
AdminRepository,
BoardTreeRepository,
ArticleImageRepository,
ImageRepository,
SubscribeRepository,
]),
],
controllers: [ BookmarkControlelr ],
providers: [
BookmarkService,
ArticleService,
BoardService,
AdminService,
BoardTreeService,
ArticleImageService,
ImageService,
SubscribeService,
AwsService,
ImageService,
],
})

export class BookmarkModule {}
export class BookmarkModule {}
10 changes: 10 additions & 0 deletions packages/server/src/bookmark/bookmark.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ export class BookmarkRepository extends Repository<Bookmark> {
.where("bookmark.article_id = :articleId", { articleId })
.getCount();
}

async existsByUserAndArticle(
userId: number,
articleId: number,
): Promise<number> {
return this.createQueryBuilder("article")
.where("article.user_id = :userId", { userId })
.andWhere("article.article_id = :articleId", { articleId })
.getCount();
}
}
23 changes: 22 additions & 1 deletion packages/server/src/bookmark/bookmark.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import { Injectable } from "@nestjs/common";
import { ArticleService } from "src/article/article.service";
import { User } from "src/commons/entities/user.entity";
import { Errors } from "src/commons/exception/exception.global";

import { BookmarkRepository } from "./bookmark.repository";

const { ALREADY_SUBSCRIBE_BOOKMARK } = Errors;

@Injectable()
export class BookmarkService {
constructor(private readonly bookmarkRepository: BookmarkRepository) {}
constructor(
private readonly bookmarkRepository: BookmarkRepository,
private readonly articleService: ArticleService,
) {}

async create(user: User, articleId: number) {
const article = await this.articleService.findById(articleId);

// DESCRIBE: 요청한 유저가 article을 이미 구독 중인지 확인
if (
await this.bookmarkRepository.existsByUserAndArticle(user.id, articleId)
)
throw ALREADY_SUBSCRIBE_BOOKMARK;

await this.bookmarkRepository.save(article);
return "success";
}
}
6 changes: 5 additions & 1 deletion packages/server/src/commons/exception/exception.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export const Errors = {
ALREADY_SUBSCRIBE_BOARD: new ConflictException(
"이미 구독 중인 board 입니다.",
),
NOT_SUBSCRIBED_BOARD: new NotFoundException("구독 중인 게시판이 아닙니다."),
// bookmark 도메인에 대한 예외 메세지
ALREADY_SUBSCRIBE_BOOKMARK: new ConflictException(
"이미 구독 중인 bookmark 입니다.",
),
NOT_SUBSCRIBED_BOARD: new NotFoundException("구독 중인 board가 아닙니다."),

// image 도메인에 대한 예외 메세지
IMAGE_ID_NOT_FOUND: new NotFoundException(
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/subscribe/subscribe.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class SubscribeControlelr {
})
@UseGuards(UserAuthGuard)
async create(@Req() req, @Param("boardId") boardId: number) {
console.log("req 객체", req);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.log는 지우고 올리는게 좋을 것 같습니다!

console.log("============================");
const { user } = req;
return this.subscribeService.create(user, boardId);
}
Expand Down