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

짧은 리뷰 삭제 및 입덕포인트 반환 uri 변경 #143 #156

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 19 additions & 6 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ include::{snippets}/bookmark/checkBookmarked/notExist/http-response.adoc[]

== shortReview

=== GET api/v1/short-reviews/animeId/1
=== GET api/v1/short-reviews/animeId/:animeId
==== 성공시, cursor가 없을 경우
.curl-request
include::{snippets}/getShortReviews/success/curl-request.adoc[]
Expand Down Expand Up @@ -719,7 +719,7 @@ include::{snippets}/getShortReviews/withCursor/success/response-body.adoc[]
include::{snippets}/getShortReviews/withCursor/success/response-fields.adoc[]
==== 실패시

=== POST api/v1/short-reviews/1
=== POST api/v1/short-reviews/:id

.curl-request
include::{snippets}/postShortReview/success/curl-request.adoc[]
Expand All @@ -739,7 +739,7 @@ include::{snippets}/postShortReview/success/http-response.adoc[]

==== 실패시

=== shortReview/getAttractionPoint api/v1/short-reviews//attraction-points
=== GET api/v1/short-reviews/attraction-points

.curl-request
include::{snippets}/shortReview/getAttractionPoints/success/curl-request.adoc[]
Expand All @@ -766,7 +766,7 @@ include::{snippets}/shortReview/getAttractionPoints/success/http-response.adoc[]
.response-fields
include::{snippets}/shortReview/getAttractionPoints/success/response-fields.adoc[]

=== PATCH api/v1/short-reviews/1
=== PATCH api/v1/short-reviews/:id

.curl-request
include::{snippets}/patchShortReview/success/curl-request.adoc[]
Expand All @@ -787,9 +787,22 @@ include::{snippets}/patchShortReview/success/request-fields.adoc[]
.http-response
include::{snippets}/patchShortReview/success/http-response.adoc[]

.response-fields
include::{snippets}/patchShortReview/success/response-fields.adoc[]
==== 실패시

=== DELETE api/v1/short-reivews/animes/:id
.curl-request
include::{snippets}/deleteShortReview/success/curl-request.adoc[]

.http-request
include::{snippets}/deleteShortReview/success/http-request.adoc[]

.request-param
include::{snippets}/deleteShortReview/success/path-parameters.adoc[]

==== 성공시

.http-response
include::{snippets}/deleteShortReview/success/http-response.adoc[]

==== 실패시

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@Validated
@RequestMapping("/short-reviews")
Expand Down Expand Up @@ -58,20 +51,28 @@ public ResponseEntity<?> getShortReviews(
@GetMapping("/attraction-points")
public ResponseEntity<?> patchShortReview(
@LoginUser AuthUser user,
@RequestParam(required = false) Long animeId,
@RequestParam(required = false) String name) {
@RequestBody @Valid ShortReviewReqDto.PatchShortReview req) {
//TODO : 짧은 리뷰 수정, 입덕포인트 반환
IsAttractionPoint attractionPointRes = attractionPointService.isAttractionPoint(user.getId(), animeId);
IsAttractionPoint attractionPointRes = attractionPointService.isAttractionPoint(user.getId(), req.getAnimeId());
return ResponseEntity.ok(attractionPointRes);
}

@PatchMapping("/{reviewId}")
@PatchMapping("/{shortReviewId}")
public ResponseEntity<?> patchShortReview(
@LoginUser AuthUser user,
@PathVariable Long reviewId,
@PathVariable Long shortReviewId,
@RequestBody @Valid ShortReviewReqDto.ShortReviewReq req) {
//TODO : 짧은 리뷰 수정
shortReviewService.update(user.getId(), reviewId, req);
shortReviewService.update(user.getId(), shortReviewId, req);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/{shortReviewId}")
public ResponseEntity<?> deleteShortReview(
@LoginUser AuthUser user,
@PathVariable Long shortReviewId) {
//TODO : 짧은 리뷰 삭제
shortReviewService.delete(user.getId(), shortReviewId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ public class ShortReviewReqDto {
@Builder
public static class ShortReviewReq{
private Long animeId;
// private String name;
private boolean hasSpoiler;
@NotBlank
@Length(min = 10, max = 100,
message = "최소 10에서 100자 까지 입력 가능합니다.")
private String content;
}

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class PatchShortReview{
private Long animeId;
}

@Getter
@AllArgsConstructor
public enum Sort{
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/io/oduck/api/domain/review/entity/ShortReview.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -46,8 +47,9 @@ public class ShortReview extends BaseEntity {
@Column(nullable = false)
private boolean hasSpoiler;

@OneToMany(mappedBy = "shortReview", cascade = CascadeType.PERSIST)
private List<ShortReviewLike> shortReviewLikes;
@OneToMany(mappedBy = "shortReview", cascade = CascadeType.PERSIST, orphanRemoval = true)
@Builder.Default
private List<ShortReviewLike> shortReviewLikes = new ArrayList<>();


@Builder
Expand Down Expand Up @@ -75,4 +77,9 @@ public void updateSpoiler(boolean hasSpoiler){
this.hasSpoiler = hasSpoiler;
}
}

public void delete(){
this.shortReviewLikes.clear();
this.deletedAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import io.oduck.api.domain.review.entity.ShortReview;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface ShortReviewRepository extends JpaRepository<ShortReview,Long>, ShortReviewRepositoryCustom {
Long countByMemberId(Long memberId);
Optional<ShortReview> findByIdAndDeletedAtIsNull(Long memberId);

Long countByMemberIdAndDeletedAtIsNull(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Slice<ShortReviewDsl> selectShortReviews(Long animeId, String cursor,
.leftJoin(shortReviewLike).on(shortReview.id.eq(shortReviewLike.shortReview.id))
.join(starRating).on(starRating.anime.id.eq(shortReview.anime.id)
.and(starRating.member.id.eq(shortReview.member.id)))
.where(anime.id.eq(animeId))
.where(anime.id.eq(animeId).and(shortReview.deletedAt.isNull()))
.groupBy(shortReview.id, member.id)
.having(cursorCondition(cursor, pageable))
.limit(pageable.getPageSize());
Expand Down Expand Up @@ -91,7 +91,7 @@ public Slice<ShortReviewDslWithTitle> selectShortReviewsByMemberId(Long memberId
.leftJoin(starRating).on(starRating.anime.id.eq(shortReview.anime.id)
.and(starRating.member.id.eq(shortReview.member.id)))
.leftJoin(shortReviewLike).on(shortReview.id.eq(shortReviewLike.shortReview.id))
.where(shortReview.member.id.eq(memberId))
.where(shortReview.member.id.eq(memberId).and(shortReview.deletedAt.isNull()))
.groupBy(shortReview.id)
.having(cursorCondition(cursor, pageable))
.limit(pageable.getPageSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public interface ShortReviewService {
void update(Long memberId, Long reviewId, ShortReviewReq req);

//애니 리뷰 삭제

void delete(Long memberId, Long reviewId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import io.oduck.api.global.common.SliceResponse;
import io.oduck.api.global.exception.BadRequestException;
import io.oduck.api.global.exception.NotFoundException;

import java.util.List;
import java.util.Optional;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Slice;
Expand All @@ -34,7 +36,7 @@
@Slf4j
@Service
@RequiredArgsConstructor
public class ShortReviewServiceImpl implements ShortReviewService{
public class ShortReviewServiceImpl implements ShortReviewService {

private final ShortReviewRepository shortReviewRepository;
private final MemberRepository memberRepository;
Expand All @@ -47,15 +49,15 @@ public void save(Long memberId, ShortReviewReq shortReviewReq) {

//애니 입력
Anime anime = animeRepository.findByIdForUpdate(shortReviewReq.getAnimeId())
.orElseThrow(
() -> new NotFoundException("Anime")
);
.orElseThrow(
() -> new NotFoundException("Anime")
);

//회원 입력
Member member = memberRepository.findById(memberId)
.orElseThrow(
() -> new NotFoundException("Member")
);
Member member = memberRepository.findByIdAndDeletedAtIsNull(memberId)
.orElseThrow(
() -> new NotFoundException("Member")
);

ShortReview shortReview = ShortReview
.builder()
Expand All @@ -73,68 +75,68 @@ public void save(Long memberId, ShortReviewReq shortReviewReq) {

@Override
@Transactional
public SliceResponse<ShortReviewRes> getShortReviews(Long animeId, String cursor,ShortReviewReqDto.Sort sort, OrderDirection order, int size) {
public SliceResponse<ShortReviewRes> getShortReviews(Long animeId, String cursor, ShortReviewReqDto.Sort sort, OrderDirection order, int size) {
Sort sortList = Sort.by(
Direction.fromString(order.getOrder()),
sort.getSort()
Direction.fromString(order.getOrder()),
sort.getSort()
);

if(sort == ShortReviewReqDto.Sort.LIKE_COUNT){
if (sort == ShortReviewReqDto.Sort.LIKE_COUNT) {
sortList = sortList.and(Sort.by(Direction.DESC, "createdAt"));
}else if(sort == ShortReviewReqDto.Sort.SCORE){
} else if (sort == ShortReviewReqDto.Sort.SCORE) {
sortList = sortList.and(Sort.by(Direction.DESC, "createdAt"));
}

Slice<ShortReviewDsl> shortReviews = shortReviewRepository.selectShortReviews(
animeId,
cursor,
applyPageableForNonOffset(
size,
sortList
)
animeId,
cursor,
applyPageableForNonOffset(
size,
sortList
)
);

List<ShortReviewRes> res = shortReviews.getContent()
.stream()
.map(ShortReviewRes::of)
.toList();
.stream()
.map(ShortReviewRes::of)
.toList();

return SliceResponse.of(shortReviews, res, sort.getSort());
}

@Override
public ShortReviewCountRes getShortReviewCountByMemberId(Long memberId) {
Long count = shortReviewRepository.countByMemberId(memberId);
Long count = shortReviewRepository.countByMemberIdAndDeletedAtIsNull(memberId);
return ShortReviewCountRes.builder()
.count(count)
.build();
.count(count)
.build();
}

@Override
public SliceResponse<ShortReviewResWithTitle> getShortReviewsByMemberId(Long memberId, String cursor,
ShortReviewReqDto.SortForProfile sort, OrderDirection order, int size) {
ShortReviewReqDto.SortForProfile sort, OrderDirection order, int size) {
Sort sortList = Sort.by(
Direction.fromString(order.getOrder()),
sort.getSort()
Direction.fromString(order.getOrder()),
sort.getSort()
);

if(sort.equals(SortForProfile.SCORE)){
if (sort.equals(SortForProfile.TITLE) || sort.equals(SortForProfile.SCORE)) {
sortList = sortList.and(Sort.by(Direction.DESC, "createdAt"));
}

Slice<ShortReviewDslWithTitle> shortReviews = shortReviewRepository.selectShortReviewsByMemberId(
memberId,
cursor,
applyPageableForNonOffset(
size,
sortList
)
memberId,
cursor,
applyPageableForNonOffset(
size,
sortList
)
);

List<ShortReviewResWithTitle> res = shortReviews.getContent()
.stream()
.map(ShortReviewResWithTitle::of)
.toList();
.stream()
.map(ShortReviewResWithTitle::of)
.toList();

return SliceResponse.of(shortReviews, res, sort.getSort());
}
Expand All @@ -153,25 +155,32 @@ public void update(Long memberId, Long reviewId, ShortReviewReq req) {

//리뷰 작성자 인지 확인
Optional
.ofNullable(findMemberId)
.ifPresent(
id -> {
if(!findMemberId.equals(memberId)) {
throw new BadRequestException("Not the author of the review.");
}
findShortReview.updateContent(req.getContent());
findShortReview.updateSpoiler(req.isHasSpoiler());
}
);
.ofNullable(findMemberId)
.ifPresent(
id -> {
if (!findMemberId.equals(memberId)) {
throw new BadRequestException("Not the author of the review.");
}
findShortReview.updateContent(req.getContent());
findShortReview.updateSpoiler(req.isHasSpoiler());
}
);
findAnime.increaseReviewCount();
shortReviewRepository.save(findShortReview);
}

@Override
public void delete(Long memberId, Long reviewId) {
ShortReview find = getShortReview(reviewId);
find.delete();
shortReviewRepository.save(find);
}

private ShortReview getShortReview(Long reviewId){
return shortReviewRepository.findById(reviewId)
.orElseThrow(
() -> new NotFoundException("shortReview")
);

private ShortReview getShortReview(Long reviewId) {
return shortReviewRepository.findByIdAndDeletedAtIsNull(reviewId)
.orElseThrow(
() -> new NotFoundException("shortReview")
);
}
}
Loading