Skip to content

Commit

Permalink
feat: Add Argument Resolver(#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
eastmeet committed Jan 19, 2023
1 parent b827494 commit ea9c580
Show file tree
Hide file tree
Showing 32 changed files with 360 additions and 405 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class AttractionController {

// 1. 명소 등록 핸들러
@PostMapping("/upload")
public ResponseEntity postAttraction(AttractionPostDto attractionPostDto) throws IOException {
public ResponseEntity<DataResponseDto<?>> postAttraction(AttractionPostDto attractionPostDto) throws IOException {

Attraction attraction = mapper.attractionPostDtoToAttraction(attractionPostDto);

Expand All @@ -64,7 +64,7 @@ public ResponseEntity postAttraction(AttractionPostDto attractionPostDto) throws

// 2. 명소 수정 핸들러
@PatchMapping("/{attraction-id}")
public ResponseEntity patchAttraction(@PathVariable("attraction-id") @Positive long attractionId,
public ResponseEntity<DataResponseDto<?>> patchAttraction(@PathVariable("attraction-id") @Positive long attractionId,
AttractionPatchDto attractionPatchDto) throws IOException {

attractionPatchDto.setAttractionId(attractionId);
Expand All @@ -91,9 +91,9 @@ public ResponseEntity patchAttraction(@PathVariable("attraction-id") @Positive l
// 3. 명소 1개 정보 요청을 처리하는 핸들러
// 반환하는 정보 : 명소 정보(Id,이름, 설명, 주소, 이미지 주소), 좋아요 수, 좋아요 눌렀는지, 즐겨찾기 수, 즐겨찾기 눌렀는지
@GetMapping("/{attraction-id}")
public ResponseEntity getAttraction(HttpServletRequest request,
public ResponseEntity<DataResponseDto<?>> getAttraction(HttpServletRequest request,
@PathVariable("attraction-id") @Positive long attractionId){
Member member = memberService.findMember(extractedUsername(request));
Member member = memberService.findMemberByMemberEmail(extractedUsername(request));
Attraction attraction = attractionService.findAttraction(attractionId);
AttractionDetailResponseDto response =
mapper.attractionToAttractionDetailResponseDto(attraction);
Expand All @@ -105,7 +105,7 @@ public ResponseEntity getAttraction(HttpServletRequest request,
// 4. 찾는 '구' 리스트를 받아 명소 Id 기준으로 명소 여러개의 정보 요청을 처리하는 핸들러
// 반환하는 정보 : 명소 정보(id, 이름, 이미지 주소), 좋아요 수, 즐겨찾기 수(아직 구현안됨)
@GetMapping("/filter")
public ResponseEntity getFilteredAttractions(@Positive @RequestParam(required = false, defaultValue = "1") int page,
public ResponseEntity<MultiResponseDto<?>> getFilteredAttractions(@Positive @RequestParam(required = false, defaultValue = "1") int page,
@Positive @RequestParam(required = false, defaultValue = "9") int size,
@RequestParam(required = false, defaultValue = "latest") String sort,
@RequestBody AttractionFilterDto filterDto){
Expand All @@ -126,30 +126,30 @@ public ResponseEntity getFilteredAttractions(@Positive @RequestParam(required =
// 5. 명소 Id를 기준으로 명소 여러개의 정보 요청을 처리하는 핸들러
// 반환하는 정보 : 명소 정보(id, 이름, 이미지 주소), 좋아요 수, 즐겨찾기 수
@GetMapping
public ResponseEntity getAttractions(@Positive @RequestParam(required = false, defaultValue = "1") int page,
public ResponseEntity<MultiResponseDto<?>> getAttractions(@Positive @RequestParam(required = false, defaultValue = "1") int page,
@Positive @RequestParam(required = false, defaultValue = "9") int size){
Page<Attraction> attractionPage = attractionService.findAttractions(page-1, size);
List<Attraction> attractions = attractionPage.getContent();
return new ResponseEntity(new MultiResponseDto<>(
return new ResponseEntity<>(new MultiResponseDto<>(
mapper.attractionsToAttractionResponseDtos(attractions),attractionPage), HttpStatus.OK);
}


// 6. 명소를 아예 삭제하는 요청을 처리하는 핸들러
@DeleteMapping("/{attraction-id}")
public ResponseEntity deleteAttraction(@PathVariable("attraction-id") @Positive long attractionId){
public ResponseEntity<HttpStatus> deleteAttraction(@PathVariable("attraction-id") @Positive long attractionId){
attractionService.deleteAttraction(attractionId);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

// 7. 명소 좋아요!
@PostMapping("/likes/{attraction-id}")
public ResponseEntity voteAttraction(HttpServletRequest request,
public ResponseEntity<DataResponseDto<?>> voteAttraction(HttpServletRequest request,
@PathVariable("attraction-id") @Positive long attractionId){
// 회원 정보를 받아온다
String userEmail = extractedUsername(request);
Member member = memberService.findMember(userEmail);
Member member = memberService.findMemberByMemberEmail(userEmail);

// 명소 정보를 찾는다
Attraction attraction = attractionService.findAttraction(attractionId);
Expand All @@ -160,15 +160,15 @@ public ResponseEntity voteAttraction(HttpServletRequest request,
// responseDto 생성
AttractionLikesResponseDto response = new AttractionLikesResponseDto();
response.setIsVoted(status);
return new ResponseEntity(new DataResponseDto<>(response), HttpStatus.OK);
return new ResponseEntity<>(new DataResponseDto<>(response), HttpStatus.OK);
}

// 8. 명소 즐겨찾기
@PostMapping("/saves/{attraction-id}")
public ResponseEntity saveAttraction(HttpServletRequest request,
public ResponseEntity<DataResponseDto<?>> saveAttraction(HttpServletRequest request,
@PathVariable("attraction-id") @Positive long attractionId){
String userEmail = extractedUsername(request);
Member member = memberService.findMember(userEmail);
Member member = memberService.findMemberByMemberEmail(userEmail);

// 명소 정보를 찾는다
Attraction attraction = attractionService.findAttraction(attractionId);
Expand All @@ -177,7 +177,7 @@ public ResponseEntity saveAttraction(HttpServletRequest request,

AttractionSaveResponseDto response = new AttractionSaveResponseDto();
response.setIsSaved(status);
return new ResponseEntity(new DataResponseDto<>(response), HttpStatus.OK);
return new ResponseEntity<>(new DataResponseDto<>(response), HttpStatus.OK);
}

private String extractedUsername(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.main36.picha.domain.comment.controller;

import com.main36.picha.domain.comment.dto.CommentPostDto;
import com.main36.picha.domain.comment.dto.CommentDto;
import com.main36.picha.domain.comment.dto.CommentResponseDto;
import com.main36.picha.domain.comment.entity.Comment;
import com.main36.picha.domain.comment.mapper.CommentMapper;
import com.main36.picha.domain.comment.service.CommentService;
import com.main36.picha.domain.member.entity.Member;
import com.main36.picha.domain.member.service.MemberService;
import com.main36.picha.domain.post.service.PostService;
import com.main36.picha.global.authorization.jwt.JwtTokenizer;
import com.main36.picha.global.exception.BusinessLogicException;
import com.main36.picha.global.exception.ExceptionCode;
import com.main36.picha.global.authorization.resolver.ClientId;
import com.main36.picha.global.response.DataResponseDto;
import com.main36.picha.global.response.MultiResponseDto;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -41,18 +39,17 @@ public class CommentController {

private final CommentMapper mapper;

@PostMapping("/upload/{member-id}/{post-id}")
public ResponseEntity<DataResponseDto<?>> postComment(@PathVariable("member-id") @Positive long memberId,
@PostMapping("/upload/{post-id}")
public ResponseEntity<DataResponseDto<?>> postComment(@ClientId Long clientId,
@PathVariable("post-id") @Positive long postId,
@RequestBody @Valid CommentPostDto commentPostDto) {

@RequestBody @Valid CommentDto.Post commentPostDto) {
Comment.CommentBuilder commentBuilder = Comment.builder();

Comment comment =
commentService.createComment(
commentBuilder
.commentContent(commentPostDto.getCommentContent())
.member(memberService.findVerifiedMemberById(memberId))
.member(memberService.findMemberByMemberId(clientId))
.post(postService.findPost(postId))
.build()
);
Expand All @@ -62,12 +59,12 @@ public ResponseEntity<DataResponseDto<?>> postComment(@PathVariable("member-id")
return new ResponseEntity<>(new DataResponseDto<>(commentResponseDto), HttpStatus.CREATED);
}

@PatchMapping("/edit/{member-id}/{comment-id}")
public ResponseEntity<DataResponseDto<?>> patchComment(@PathVariable("member-id") @Positive long memberId,
@PatchMapping("/edit/{comment-id}")
public ResponseEntity<DataResponseDto<?>> patchComment(@ClientId Long clientId,
@PathVariable("comment-id") @Positive long commentId,
@RequestBody @Valid CommentPostDto commentPostDto) {
Comment comment = verifiedById(memberId, commentId);
comment.setCommentContent(commentPostDto.getCommentContent());
@RequestBody @Valid CommentDto.Patch commentPatchDto) {
Comment comment = commentService.verifyClientId(clientId, commentId);
comment.setCommentContent(commentPatchDto.getCommentContent());

CommentResponseDto commentResponseDto = mapper.commentToCommentResponseDto(comment);

Expand All @@ -92,23 +89,13 @@ public ResponseEntity<MultiResponseDto<?>> getComment(@Positive @RequestParam(re
return ResponseEntity.ok(new MultiResponseDto<>(commentResponseDtos, commentPage));
}

@DeleteMapping("/delete/{member-id}/{comment-id}")
public ResponseEntity<HttpStatus> deleteComment(@PathVariable("member-id") @Positive long memberId,
@DeleteMapping("/delete/{comment-id}")
public ResponseEntity<HttpStatus> deleteComment(@ClientId Long clientId,
@PathVariable("comment-id") @Positive long commentId) {
verifiedById(memberId, commentId);
commentService.deleteComment(commentId);
Comment comment = commentService.verifyClientId(clientId, commentId);
commentService.deleteComment(comment);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

private Comment verifiedById(long memberId, long commentId) {
Member member = memberService.findVerifiedMemberById(memberId);
Comment comment = commentService.findComment(commentId);

if (!comment.getMember().getMemberId().equals(memberId)) {
throw new BusinessLogicException(ExceptionCode.NOT_AUTHOR);
}

return comment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.main36.picha.domain.comment.dto;

import lombok.Data;
import lombok.Getter;

import javax.validation.constraints.NotBlank;


@Data
public class CommentDto {

@Getter
public static class Post {
@NotBlank(message = "댓글 내용을 입력해주세요.")
private String commentContent;
}

@Getter
public static class Patch {
@NotBlank(message = "댓글 내용을 입력해주세요.")
private String commentContent;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.main36.picha.domain.comment.mapper;

import com.main36.picha.domain.comment.dto.CommentPostDto;
import com.main36.picha.domain.comment.dto.CommentDto;
import com.main36.picha.domain.comment.dto.CommentResponseDto;
import com.main36.picha.domain.comment.entity.Comment;
import org.mapstruct.Mapper;
Expand All @@ -11,7 +11,7 @@
@Mapper(componentModel = "spring")
public interface CommentMapper {

Comment CommentDtoToComment(CommentPostDto commentPostDto);
Comment CommentDtoToComment(CommentDto commentDto);

@Mapping(target = "username", source = "member.username")
@Mapping(target = "memberId", source = "member.memberId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;

import java.util.Optional;

@Service
@Transactional
@RequiredArgsConstructor
Expand All @@ -34,15 +32,23 @@ public Page<Comment> findComments(int page, int size) {
));
}

public void deleteComment(long commentId){
Comment findComment = findVerifiedComment(commentId);
commentRepository.delete(findComment);
public void deleteComment(Comment comment){
commentRepository.delete(comment);
}

private Comment findVerifiedComment(long commentId){
public Comment findVerifiedComment(long commentId){
return commentRepository.findById(commentId)
.orElseThrow(()-> new BusinessLogicException(ExceptionCode.COMMENT_NOT_FOUND));
}

public Comment verifyClientId(long clientId, long commentId) {
Comment comment = findComment(commentId);
if (!comment.getMember().getMemberId().equals(clientId)) {
throw new BusinessLogicException(ExceptionCode.NOT_AUTHOR);
}

return comment;
}


}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.main36.picha.domain.member.controller;


import com.main36.picha.domain.member.dto.MemberPatchDto;
import com.main36.picha.domain.member.dto.MemberPostDto;
import com.main36.picha.domain.member.dto.MemberDto;
import com.main36.picha.domain.member.entity.Member;
import com.main36.picha.domain.member.mapper.MemberMapper;
import com.main36.picha.domain.member.service.MemberService;
import com.main36.picha.global.authorization.resolver.ClientId;
import com.main36.picha.global.response.DataResponseDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -24,7 +24,6 @@
@RestController
@Validated
@RequiredArgsConstructor
@RequestMapping("/users")
public class MemberController {

private final MemberService memberService;
Expand All @@ -33,7 +32,10 @@ public class MemberController {

//멤버 회원가입
@PostMapping("/signup")
public ResponseEntity<DataResponseDto<?>> postMember(@Valid @RequestBody MemberPostDto memberPostDto) {
public ResponseEntity<DataResponseDto<?>> postMember(@Valid @RequestBody MemberDto.Post memberPostDto) {
log.info("email={}", memberPostDto.getEmail());
log.info("username={}", memberPostDto.getUsername());

Member member = mapper.memberPostDtoToMember(memberPostDto);
member.setPicture("https://drive.google.com/file/d/1OmsgU1GLU9iUBYe9ruw_Uy1AcrN57n4g/view?usp=sharing");
Member createMember = memberService.createMember(member);
Expand All @@ -48,6 +50,7 @@ public ResponseEntity<?> getToken(@RequestParam String access_token,
@RequestParam String refresh_token) {
log.info("at={}", access_token);
log.info("rt={}", refresh_token);

String at = "Bearer " + access_token;

// 1. 헤더에 담아서 보내기
Expand All @@ -65,9 +68,12 @@ public ResponseEntity<?> getToken(@RequestParam String access_token,
// return ResponseEntity.ok(tokenBuilder);
}

@PatchMapping("/edit/{member-id}")
public ResponseEntity<DataResponseDto<?>> patchMember(@PathVariable("member-id") @Positive long memberId,
@Valid @RequestBody MemberPatchDto memberPatchDto) {
@PatchMapping("/users/edit/{member-id}")
public ResponseEntity<DataResponseDto<?>> patchMember(@ClientId Long clientId,
@PathVariable("member-id") @Positive Long memberId,
@Valid @RequestBody MemberDto.Patch memberPatchDto) {
memberService.isEqualToClientIdAndMemberId(clientId, memberId);

memberPatchDto.setMemberId(memberId);
Member member = memberService.updateMember(mapper.memberPatchDtoToMember(memberPatchDto));

Expand All @@ -77,27 +83,22 @@ public ResponseEntity<DataResponseDto<?>> patchMember(@PathVariable("member-id")
);
}

@GetMapping("/{member-id}/{email}")
public ResponseEntity<DataResponseDto<?>> getMemberProfile(@Positive @PathVariable("member-id") long memberId,
@PathVariable("email") String email) {
//TODO: verifyLoginMember
Member member = memberService.findMember(memberId, email);
@GetMapping("/users/profile/{member-id}")
public ResponseEntity<DataResponseDto<?>> getMemberProfile(@ClientId Long clientId,
@PathVariable("member-id") @Positive Long memberId) {
Member member = memberService.isEqualToClientIdAndMemberId(clientId, memberId);

return new ResponseEntity<>(new DataResponseDto<>(mapper.memberToProfileHomeDto(member)),
HttpStatus.OK);
}

@GetMapping()
public ResponseEntity<HttpStatus> getMembers(@RequestParam(defaultValue = "1", required = false) int page,
@RequestParam(defaultValue = "9", required = false) int size) {
// Page<Member> pageMember =
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/users/delete/{member-id}")
public ResponseEntity<HttpStatus> deleteMember(@ClientId Long clientId,
@PathVariable("member-id") @Positive Long memberId) {
Member member = memberService.isEqualToClientIdAndMemberId(clientId, memberId);
memberService.deleteMember(member);

@DeleteMapping("/delete/{member-id}/confirm")
public ResponseEntity<HttpStatus> deleteMember(@PathVariable("member-id") @Positive long memberId) {
//TODO: verifyLoginMember
memberService.deleteMember(memberId);
return new ResponseEntity(HttpStatus.NO_CONTENT);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

}
Loading

0 comments on commit ea9c580

Please sign in to comment.