Skip to content

Commit

Permalink
✨ feat(api): add api for update, delete PP (#30)
Browse files Browse the repository at this point in the history
* ✨ feat(api): add api for update, delete PP

* ♻️ refactor: apply requested reviews

* ♻️ refactor: apply requested reviews
  • Loading branch information
j2woo authored Jan 15, 2023
1 parent b6dd2cc commit dc42a6e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 15 deletions.
46 changes: 38 additions & 8 deletions src/api/routes/v1/individual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default (app: Router) => {
req.body as PersonalRollingPaperDTO,
);

return res.status(201).json({ result: personalRollingPaper });
return res.status(201).json({ result: personalRollingPaper });
}),
);

Expand All @@ -57,12 +57,12 @@ export default (app: Router) => {

// 페이퍼 정보 조회
const { personalRollingPaper } =
await personalServiceInstance.viewPersonalRollingPaper(
await personalServiceInstance.getPersonalRollingPaper(
req.params as PersonalRollingPaperInputDTO,
);

// 포스트 전부 조회
const { personalPosts } = await personalServiceInstance.viewAllPosts(
const { personalPosts } = await personalServiceInstance.getAllPersonalPosts(
req.params as PersonalRollingPaperInputDTO,
);
return res
Expand Down Expand Up @@ -105,23 +105,22 @@ export default (app: Router) => {
// 비즈니스 로직을 처리할 service객체 받아오기
const personalServiceInstance = Container.get(PersonalService);

const { personalRollingPapers } = await personalServiceInstance.viewListPersonalRollingPaper(
const { personalRollingPapers } = await personalServiceInstance.getListPersonalRollingPaper(
req.params as UserInputDTO,
);

return res.status(200).json({ result: personalRollingPapers });
}),
);

// !GET, req 참고하세요!
// 개인 롤링페이퍼 포스트 디테일 조회
route.get(
"/posts/:postid",
"/posts/:personalPostId",

asyncHandler(async (req: Request<PersonalPostInputDTO>, res: Response) => {
logger.debug(req.params);
const personalServiceInstance = Container.get(PersonalService);
const { personalPost } = await personalServiceInstance.viewDetailPost(
const { personalPost } = await personalServiceInstance.getDetailPost(
req.params as PersonalPostInputDTO,
);
return res.status(200).json({ result: personalPost });
Expand All @@ -134,11 +133,42 @@ export default (app: Router) => {
asyncHandler(async (req: Request, res: Response) => {
logger.debug(req.body);
const personalServiceInstance = Container.get(PersonalService);
const { personalPost } = await personalServiceInstance.createPost(
const { personalPost } = await personalServiceInstance.createPersonalPost(
req.body as PersonalPostDTO,
);

return res.status(201).json({ result: personalPost });
}),
);

// ? 수정 화면에서는 전에 포스트 정보 불러오기인데 그건 url 뭐일까여?
// 개인 포스트 수정
route.put(
"/posts",
asyncHandler(async (req: Request, res: Response) => {
logger.debug(req.body);
const personalServiceInstance = Container.get(PersonalService);
const { personalPost } = await personalServiceInstance.updatePersonalPost(
req.body as PersonalPostInputDTO,
req.body as PersonalPostDTO,
);
// ! 수정한 후 디테일 뷰로 이동!
// 수정한 포스트 결과
return res.status(201).json({ result: personalPost });
}),
);

// 개인 포스트 삭제
route.delete(
"/posts",
asyncHandler(async (req: Request, res: Response) => {
logger.debug(req.body);
const personalServiceInstance = Container.get(PersonalService);
const { personalPost } = await personalServiceInstance.deletePersonalPost(
req.body as PersonalPostInputDTO,
);
// 삭제한 포스트 결과
return res.status(200).json({ result: personalPost });
}),
);
};
69 changes: 62 additions & 7 deletions src/services/individual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class PersonalService {

// 개인 롤링페이퍼 조회해주는 함수
// 롤링페이퍼 id로 찾는다.
public async viewPersonalRollingPaper(
public async getPersonalRollingPaper(
personalRollingPaperInputDTO: PersonalRollingPaperInputDTO,
): Promise<{ personalRollingPaper: PersonalRollingPaper }> {
try {
Expand All @@ -73,15 +73,15 @@ export default class PersonalService {
}

// 포스트 전부 조회해주는 함수
public async viewAllPosts(
public async getAllPersonalPosts(
personalRollingPaperInputDTO: PersonalRollingPaperInputDTO,
): Promise<{ personalPosts: PersonalPost[] }> {
try {
const personalRollingPaperId =
personalRollingPaperInputDTO.paperId;

const personalPosts = await this.personalPostModel.findAll({
where: { personal_rolling_paper_id: personalRollingPaperId },
where: { personal_rolling_paper_id: personalRollingPaperId , deleted_at:null},
});

if (!personalPosts) {
Expand Down Expand Up @@ -116,7 +116,7 @@ export default class PersonalService {
}

// 개인 롤링페이퍼 목록 조회해주는 함수
public async viewListPersonalRollingPaper(
public async getListPersonalRollingPaper(
userInputDTO: UserInputDTO,
): Promise<{ personalRollingPapers: PersonalRollingPaper[] }> {
try {
Expand All @@ -138,12 +138,12 @@ export default class PersonalService {
}

// 포스트 아이디(UUID)를 DB에서 찾은 후 JSON으로 전달해주는 함수
public async viewDetailPost(
public async getDetailPost(
personalPostInputDTO: PersonalPostInputDTO,
): Promise<{ personalPost: PersonalPost }> {
try {
const personalPostId = personalPostInputDTO.personalPostId;

const personalPost = await this.personalPostModel.findOne({
where: { personal_post_id: personalPostId },
});
Expand All @@ -159,8 +159,31 @@ export default class PersonalService {
}
}

// TODO: 삭제, 유저, 작성자 확인, 비밀번호
public async deletePersonalPost(
personalPostInputDTO: PersonalPostInputDTO,
): Promise<{ personalPost: PersonalPost }> {
try {
const personalPostId = personalPostInputDTO.personalPostId;

const personalPost = await this.personalPostModel.destroy({
where: { personal_post_id: personalPostId },
});

if (!personalPost) {
throw new Error("Post does not exist");
}

return { personalPost };
} catch (error) {
this.logger.error(error);
throw error;
}
}

// TODO: 페이퍼 주인은 안돼, 로그인 or 비로그인 확인
// 포스트 생성해주는 함수
public async createPost(
public async createPersonalPost(
personalPostDTO: PersonalPostDTO,
): Promise<{ personalPost: PersonalPost }> {
try {
Expand All @@ -176,4 +199,36 @@ export default class PersonalService {
throw error;
}
}

// TODO: 롤링페이퍼 주인인지 작성자인지 확인 + 작성자라면 로그인인지 비로그인인지 확인 > 이건 server에서, 로그인 로직 완료 후 수정하기
// 포스트 수정 함수
public async updatePersonalPost(
personalPostInputDTO: PersonalPostInputDTO,
personalPostDTO: PersonalPostDTO,
): Promise<{ personalPost: PersonalPost }> {
try {
const personalPostId = personalPostInputDTO.personalPostId;

// ! 수정한 사람 누구인지 확인 로직 필요, 비로그인 유저면 비밀번호 확인해줘야해!
// 이건 로그인 회원
const personalPost = await this.personalPostModel.update(
{
content:personalPostDTO.content,
post_color:personalPostDTO.post_color,
anonymous_type: personalPostDTO.anonymous_type,
},
{ where: { personal_post_id: personalPostId } },
);
// 비로그인 회원은 필요한 거- 비밀번호 확인

if (!personalPost) {
throw new Error("Unable to update post");
}

return { personalPost };
} catch (error) {
this.logger.error(error);
throw error;
}
}
}

0 comments on commit dc42a6e

Please sign in to comment.