Skip to content

Commit

Permalink
πŸš€ μ‚°μ±…λ‘œ 포슀트 μ‚­μ œ API μΆ”κ°€
Browse files Browse the repository at this point in the history
πŸš€ μ‚°μ±…λ‘œ 포슀트 μ‚­μ œ API μΆ”κ°€
  • Loading branch information
ASak1104 authored Aug 5, 2024
2 parents 28da476 + 537b967 commit 9bc56c5
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -82,6 +83,21 @@ public ResponseEntity<PostDetailResponse> getById(
return ResponseEntity.ok(response);
}

@DeleteMapping("/api/v1/posts/{id}")
@Operation(summary = "μ‚°μ±…λ‘œ 포슀트 μ‚­μ œ")
@ApiResponse(responseCode = "204")
public ResponseEntity<Void> deleteById(
@AuthenticationPrincipal
Long userId,
@PathVariable
Long id
) {
postService.deleteById(userId, id);

return ResponseEntity.noContent()
.build();
}

@GetMapping(value = "/api/v1/posts", produces = "application/json")
@Operation(summary = "μ‚°μ±…λ‘œ λͺ©λ‘ 쑰회")
@ApiResponse(responseCode = "200")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum PostErrorCode implements ErrorCode {
NULL_USER(202_90000, "μ‚°μ±…λ‘œ 포슀트 μ‚¬μš©μžλ₯Ό 확인할 수 μ—†μŠ΅λ‹ˆλ‹€"),
LOGIN_USER_NOT_FOUND(202_90400, "λ‘œκ·ΈμΈν•œ μ‚¬μš©μžκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€"),
AUTHOR_NOT_FOUND(202_90401, "μ‚°μ±…λ‘œ 포슀트의 μž‘μ„±μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€"),
AUTHOR_NOT_MATCHING(202_90402, "μ‚¬μš©μžμ™€ μ‚°μ±…λ‘œ 포슀트의 μž‘μ„±μžκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€"),

INVALID_IP_ADDRESS(202_16001, "IP μ£Όμ†Œκ°€ μ˜¬λ°”λ₯΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package team.silvertown.masil.post.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import team.silvertown.masil.post.domain.Post;
import team.silvertown.masil.post.domain.PostPin;

public interface PostPinRepository extends JpaRepository<PostPin, Long> {

@Modifying
@Query("DELETE FROM PostPin pp WHERE pp.post = :post")
void deleteAllByPost(Post post);

}
18 changes: 18 additions & 0 deletions src/main/java/team/silvertown/masil/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import org.springframework.web.context.request.ServletRequestAttributes;
import team.silvertown.masil.common.exception.DataNotFoundException;
import team.silvertown.masil.common.exception.ErrorCode;
import team.silvertown.masil.common.exception.ForbiddenException;
import team.silvertown.masil.common.map.KakaoPointMapper;
import team.silvertown.masil.common.scroll.dto.NormalListRequest;
import team.silvertown.masil.common.scroll.dto.ScrollRequest;
import team.silvertown.masil.common.scroll.dto.ScrollResponse;
import team.silvertown.masil.common.validator.Validator;
import team.silvertown.masil.post.domain.Post;
import team.silvertown.masil.post.domain.PostLike;
import team.silvertown.masil.post.domain.PostLikeId;
Expand Down Expand Up @@ -70,6 +72,18 @@ public PostDetailResponse getById(Long userId, Long id) {
return PostDetailResponse.from(post, pins, isLiked);
}

@Transactional
public void deleteById(Long userId, Long id) {
Post post = postRepository.findById(id)
.orElseThrow(getNotFoundException(PostErrorCode.POST_NOT_FOUND));
User author = post.getUser();

Validator.throwIf(!author.isSameId(userId), getForbiddenException(PostErrorCode.AUTHOR_NOT_MATCHING));

postPinRepository.deleteAllByPost(post);
postRepository.deleteById(id);
}

@Transactional(readOnly = true)
public ScrollResponse<SimplePostResponse> getScrollByAddress(
Long loginId,
Expand Down Expand Up @@ -100,6 +114,10 @@ private Supplier<DataNotFoundException> getNotFoundException(ErrorCode errorCode
return () -> new DataNotFoundException(errorCode);
}

private Supplier<RuntimeException> getForbiddenException(ErrorCode errorCode) {
return () -> new ForbiddenException(errorCode);
}

private Post createPost(CreatePostRequest request, User user) {
Post post = Post.builder()
.user(user)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/team/silvertown/masil/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public class User extends BaseEntity {
@Column(name = "social_id", length = 50)
private String socialId;

public boolean isSameId(Long userId) {
return id.equals(userId);
}

public void updateNickname(String nickname) {
UserValidator.validateNickname(nickname, UserErrorCode.INVALID_NICKNAME);
this.nickname = nickname;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum UserErrorCode implements ErrorCode {
INVALID_SEX(10011001, "μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ ν˜•μ‹μ˜ 성별 μ •λ³΄μž…λ‹ˆλ‹€."),
INVALID_EXERCISE_INTENSITY(10011002, "μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ ν˜•μ‹μ˜ μš΄λ™κ°•λ„ μ •λ³΄μž…λ‹ˆλ‹€."),
INVALID_HEIGHT(10012000, "μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ ν˜•μ‹μ˜ ν‚€ μ •λ³΄μž…λ‹ˆλ‹€."),
INVALID_WEIGHT(10012001, "μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ ν˜•μ‹μ˜ λͺΈλ¬΄ μ •λ³΄μž…λ‹ˆλ‹€."),
INVALID_WEIGHT(10012001, "μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ ν˜•μ‹μ˜ λͺΈλ¬΄κ²Œ μ •λ³΄μž…λ‹ˆλ‹€."),
INVALID_BIRTH_DATE(10014000, "μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ ν˜•μ‹μ˜ 생일 μ •λ³΄μž…λ‹ˆλ‹€"),
USER_NOT_FOUND(10020401, "ν•΄λ‹Ή μœ μ €λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
DUPLICATED_NICKNAME(10020900, "이미 μ‘΄μž¬ν•˜λŠ” λ‹‰λ„€μž„μž…λ‹ˆλ‹€."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -21,6 +22,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import team.silvertown.masil.common.exception.DataNotFoundException;
import team.silvertown.masil.common.exception.ForbiddenException;
import team.silvertown.masil.common.map.KakaoPoint;
import team.silvertown.masil.common.scroll.OrderType;
import team.silvertown.masil.common.scroll.dto.NormalListRequest;
Expand Down Expand Up @@ -172,6 +174,54 @@ void cleanUp() {
assertThat(actual.pins()).hasSize(pinSize);
}

@Test
void μ‚°μ±…λ‘œ_포슀트λ₯Ό_μ‚­μ œν•œλ‹€() {
// given
Post post = postRepository.save(PostTexture.createDependentPost(user, 3));

entityManager.clear();

// when
postService.deleteById(user.getId(), post.getId());

// then
Optional<Post> actual = postRepository.findById(post.getId());

assertThat(actual).isEmpty();
}

@Test
void μ‘΄μž¬ν•˜μ§€_μ•ŠλŠ”_μ‚°μ±…λ‘œ_ν¬μŠ€νŠΈλŠ”_μ‚­μ œμ—_μ‹€νŒ¨ν•œλ‹€() {
// given
Post post = postRepository.save(PostTexture.createDependentPost(user, 3));

entityManager.clear();

// when
ThrowingCallable deleteNotExistPost = () -> postService.deleteById(user.getId(), post.getId() + 1);

// then
assertThatExceptionOfType(DataNotFoundException.class)
.isThrownBy(deleteNotExistPost)
.withMessage(PostErrorCode.POST_NOT_FOUND.getMessage());
}

@Test
void λ‘œκ·ΈμΈν•œ_μ‚¬μš©μžμ™€_μ‚°μ±…λ‘œ_포슀트_μž‘μ„±μžκ°€_λ‹€λ₯Ό_경우_μ‚­μ œμ—_μ‹€νŒ¨ν•œλ‹€() {
// given
Post post = postRepository.save(PostTexture.createDependentPost(user, 3));

entityManager.clear();

// when
ThrowingCallable deleteByNotMatchingUser = () -> postService.deleteById(user.getId() + 1, post.getId());

// then
assertThatExceptionOfType(ForbiddenException.class)
.isThrownBy(deleteByNotMatchingUser)
.withMessage(PostErrorCode.AUTHOR_NOT_MATCHING.getMessage());
}

@Test
void 같은_IP둜_μ‚°μ±…λ‘œ_포슀트_단일_쑰회_μ‹œ_μ‘°νšŒμˆ˜κ°€_ν•œ_번만_μ¦κ°€ν•œλ‹€() {
// given
Expand Down

0 comments on commit 9bc56c5

Please sign in to comment.