Skip to content

Commit

Permalink
feat: 글 생성, 수정, 삭제 + 토큰 받기(#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinheekim committed Aug 1, 2024
1 parent 75d5ff9 commit da5809d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public PostController(PostService postService) {

// 글 저장
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> postSave(@RequestPart("post") PostSaveReqDto postSaveReqDto, @RequestPart("imgUrl") MultipartFile imgUrl, Principal principal) throws IOException {
public ResponseEntity<String> postSave(
@RequestPart("post") PostSaveReqDto postSaveReqDto,
@RequestPart("imgUrl") MultipartFile imgUrl,
Principal principal) throws IOException {
postService.postSave(postSaveReqDto, imgUrl, principal);
return new ResponseEntity<>("Successful Post Save", HttpStatus.CREATED);
}
Expand Down Expand Up @@ -77,15 +80,19 @@ public ResponseEntity<PostListResDto> postFindByUserId(@PathVariable("userId") L

// 글 수정
@PatchMapping("/{postId}")
public ResponseEntity<String> postUpdate(@PathVariable("postId") Long postId, @RequestPart("post") PostUpdateReqDto postUpdateReqDto, @RequestPart("imgUrl") MultipartFile imgUrl) throws IOException {
postService.postUpdate(postId, postUpdateReqDto, imgUrl);
public ResponseEntity<String> postUpdate(
@PathVariable("postId") Long postId,
@RequestPart("post") PostUpdateReqDto postUpdateReqDto,
@RequestPart("imgUrl") MultipartFile imgUrl,
Principal principal) throws IOException {
postService.postUpdate(postId, postUpdateReqDto, imgUrl, principal);
return new ResponseEntity<>("Successful Post Update", HttpStatus.OK);
}

// 글 삭제
@DeleteMapping("/{postId}")
public ResponseEntity<String> postDelete(@PathVariable("postId") Long postId) throws IOException {
postService.postDelete(postId);
public ResponseEntity<String> postDelete(@PathVariable("postId") Long postId, Principal principal) throws IOException {
postService.postDelete(postId, principal);
return new ResponseEntity<>("Successful Post Delete", HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class PostService {
public void postSave(PostSaveReqDto postSaveReqDto, MultipartFile multipartFile, Principal principal) throws IOException {
String imgUrl = s3Service.upload(multipartFile, "post");
String loginId = principal.getName();
System.out.println(loginId);

User user = userRepository.findByLoginId(loginId)
.orElseThrow(() -> new IllegalArgumentException("User not found with id = " + loginId));
Expand Down Expand Up @@ -145,11 +144,18 @@ public PostListResDto postFindByUserId(Long userId) {

// 글 수정
@Transactional
public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto, MultipartFile multipartFile) throws IOException {
public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto, MultipartFile multipartFile, Principal principal) throws IOException {
Post post = postRepository.findById(postId).orElseThrow(
() -> new IllegalArgumentException("해당 글을 수정할 수 없습니다. postId = " + postId)
);

String loginId = principal.getName();
User currentUser = userRepository.findByLoginId(loginId)
.orElseThrow(() -> new IllegalArgumentException("현재 사용자 정보를 찾을 수 없습니다. username = " + loginId));
if (!post.getUser().getLoginId().equals(currentUser.getLoginId())) {
throw new SecurityException("이 글을 수정할 권한이 없습니다.");
}

Location location = locationRepository.findById(postUpdateReqDto.locationId())
.orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postUpdateReqDto.locationId()));

Expand All @@ -162,16 +168,21 @@ public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto, Multipart
String imgUrl = s3Service.upload(multipartFile, "post");

post.update(location, category, postUpdateReqDto, mood, imgUrl);
PostInfoResDto.from(post);
postRepository.save(post);
}

// 글 삭제
@Transactional
public void postDelete(Long postId) throws IOException {
public void postDelete(Long postId, Principal principal) throws IOException {
Post post = postRepository.findById(postId).orElseThrow(
() -> new IllegalArgumentException("해당 글을 삭제할 수 없습니다. postId = " + postId)
);

String loginId = principal.getName();
User currentUser = userRepository.findByLoginId(loginId)
.orElseThrow(()-> new IllegalArgumentException("현재 사용자 정보를 찾을 수 없습니다. username = " + loginId));
if (!post.getUser().getLoginId().equals(currentUser.getLoginId())) {
throw new SecurityException("이 글을 삭제할 권한이 없습니다.");
}
Optional<String> imgUrl = Optional.ofNullable(post.getImgUrl());

imgUrl.ifPresentOrElse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public Post(String title, String content, Location location, Integer time, Integ
this.category = category;
this.mood = mood;
this.imgUrl = imgUrl;
this.user = user;
}

public void update(Location location, Category category, PostUpdateReqDto postUpdateReqDto, Mood mood, String imgUrl) {
Expand Down

0 comments on commit da5809d

Please sign in to comment.