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

[#134] feat: 실패기록 삭제와 수정 #140

Merged
merged 2 commits into from
Nov 4, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public Failure queryFailure(Long failureId) {
.findById(failureId)
.orElseThrow(() -> FailureNotFoundException.EXCEPTION);
}

@Override
public void delete(Failure failure) {
failureRepository.delete(failure);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.todaysfail.common.BaseTimeEntity;
import com.todaysfail.config.converter.LongArrayConverter;
import com.todaysfail.domains.failure.exception.FailureOwnedByUserException;
import com.todaysfail.domains.failure.exception.FailureNotOwnedByUserException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -57,9 +57,17 @@ public void like() {
}

public void validateOwnership(Long userId) {
// 본인이 생성 한 실패 기록일 경우
if (this.userId.equals(userId)) {
throw FailureOwnedByUserException.EXCEPTION;
if (!this.userId.equals(userId)) {
throw FailureNotOwnedByUserException.EXCEPTION;
}
}

public void modify(Failure failure) {
this.categoryId = failure.getCategoryId();
this.failureDate = failure.getFailureDate();
this.title = failure.getTitle();
this.content = failure.getContent();
this.tags = failure.getTags();
this.secret = failure.isSecret();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum FailureErrorCode implements BaseErrorCode {
FAILURE_DATE_IS_FUTURE(NOT_FOUND, "FAILURE_400_2", "입력 한 날짜가 미래일 수 없습니다."),

@ExplainError("본인이 생성한 실패 기록 입니다.")
FAILURE_OWNED_BY_USER(BAD_REQUEST, "FAILURE_400_3", "본인이 생성한 실패 기록 입니다."),
FAILURE_NOT_OWNED_BY_USER(BAD_REQUEST, "FAILURE_400_3", "본인이 생성한 실패 기록이 아닙니다."),
;

private Integer status;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.todaysfail.domains.failure.exception;

import com.todaysfail.common.exception.TodaysFailCodeException;

public class FailureNotOwnedByUserException extends TodaysFailCodeException {
public static final TodaysFailCodeException EXCEPTION = new FailureNotOwnedByUserException();

public FailureNotOwnedByUserException() {
super(FailureErrorCode.FAILURE_NOT_OWNED_BY_USER);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface FailureCommandPort {
Failure save(Failure failure);

Failure queryFailure(Long failureId);

void delete(Failure failure);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ public Failure register(final Failure failure, Category category, List<Tag> tags
return failureCommandPort.save(failure);
}

@Transactional
@RedissonLock(lockName = "실패좋아요", identifier = "failureId")
public void likeFailure(Long userId, Long failureId) {
Failure failure = failureCommandPort.queryFailure(failureId);
failure.like();
failureLikeQueryPort.checkAlreadyLiked(userId, failureId);
failureLikeCommandPort.save(FailureLike.of(userId, failureId));
}

@Transactional
public Failure modifyFailure(Long failureId, Failure failure, Category category) {
validateFailureTagSize(failure.getTags());
validateFailureDate(failure.getFailureDate());
category.validateOwnership(failure.getUserId());
Failure originFailure = failureCommandPort.queryFailure(failureId);
originFailure.modify(failure);
return originFailure;
}

private void validateFailureTagSize(List<Long> tags) {
/** 태그는 최대 3개까지만 등록 가능하다. */
if (tags.size() > 3) {
Expand All @@ -52,13 +71,9 @@ private void validateFailureDate(LocalDate date) {
}
}

@Transactional
@RedissonLock(lockName = "실패좋아요", identifier = "failureId")
public void likeFailure(Long userId, Long failureId) {
public void deleteFailure(final Long failureId, final Long userId) {
Failure failure = failureCommandPort.queryFailure(failureId);
failure.validateOwnership(userId);
failure.like();
failureLikeQueryPort.checkAlreadyLiked(userId, failureId);
failureLikeCommandPort.save(FailureLike.of(userId, failureId));
failureCommandPort.delete(failure);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.todaysfail.api.web.failure;

import com.todaysfail.api.web.common.SliceResponse;
import com.todaysfail.api.web.failure.dto.request.FailureModifyRequest;
import com.todaysfail.api.web.failure.dto.request.FailureRegisterRequest;
import com.todaysfail.api.web.failure.dto.response.FailureByCategoryResponse;
import com.todaysfail.api.web.failure.dto.response.FailureMonthlyDailyStatusResponse;
import com.todaysfail.api.web.failure.dto.response.FailureResponse;
import com.todaysfail.api.web.failure.usecase.FailureByCategoryQueryUseCase;
import com.todaysfail.api.web.failure.usecase.FailureDeleteUseCase;
import com.todaysfail.api.web.failure.usecase.FailureFeedQueryUseCase;
import com.todaysfail.api.web.failure.usecase.FailureLikeUseCase;
import com.todaysfail.api.web.failure.usecase.FailureModifyUseCase;
import com.todaysfail.api.web.failure.usecase.FailureMonthlyDailyStatusUseCase;
import com.todaysfail.api.web.failure.usecase.FailureRegisterUseCase;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -21,9 +24,11 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
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;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -36,17 +41,32 @@
@RequiredArgsConstructor
public class FailureController {
private final FailureRegisterUseCase failureRegisterUseCase;
private final FailureModifyUseCase failureModifyUseCase;
private final FailureFeedQueryUseCase failureFeedQueryUseCase;
private final FailureLikeUseCase failureLikeUseCase;
private final FailureMonthlyDailyStatusUseCase failureMonthlyDailyStatusUseCase;
private final FailureByCategoryQueryUseCase failureByCategoryQueryUseCase;
private final FailureDeleteUseCase failureDeleteUseCase;

@Operation(summary = "실패 등록")
@PostMapping
public FailureResponse registerFailure(@RequestBody @Valid FailureRegisterRequest request) {
return failureRegisterUseCase.execute(request);
}

@Operation(summary = "실패 수정")
@PutMapping("/{failureId}")
public FailureResponse modifyFailure(
@PathVariable Long failureId, @RequestBody FailureModifyRequest request) {
return failureModifyUseCase.execute(failureId, request);
}

@Operation(summary = "실패 삭제")
@DeleteMapping("/{failureId}")
public void deleteFailure(@PathVariable Long failureId) {
failureDeleteUseCase.execute(failureId);
}

@Operation(summary = "피드")
@GetMapping("/feed")
public SliceResponse<FailureResponse> queryFeed(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.todaysfail.api.web.failure.dto.request;

import java.time.LocalDate;
import java.util.Set;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.format.annotation.DateTimeFormat;

public record FailureModifyRequest(
@NotNull Long categoryId,
@NotNull @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
@NotNull @Size(min = 1, max = 50) String title,
@NotNull @Size(min = 1, max = 500) String content,
@NotNull @Size(min = 1, max = 500) String impression,
@NotNull @Size(min = 1, max = 3) Set<String> tagNameSet,
@NotNull boolean secret) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.todaysfail.api.web.failure.usecase;

import com.todaysfail.common.annotation.UseCase;
import com.todaysfail.config.security.SecurityUtils;
import com.todaysfail.domains.failure.service.FailureDomainService;
import lombok.RequiredArgsConstructor;

@UseCase
@RequiredArgsConstructor
public class FailureDeleteUseCase {
private final FailureDomainService failureDomainService;

public void execute(final Long failureId) {
final Long userId = SecurityUtils.getCurrentUserId();
failureDomainService.deleteFailure(failureId, userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.todaysfail.api.web.failure.usecase;

import com.todaysfail.api.web.failure.dto.request.FailureModifyRequest;
import com.todaysfail.api.web.failure.dto.response.FailureResponse;
import com.todaysfail.api.web.failure.mapper.FailureMapper;
import com.todaysfail.common.annotation.UseCase;
import com.todaysfail.config.security.SecurityUtils;
import com.todaysfail.domains.category.domain.Category;
import com.todaysfail.domains.category.port.CategoryQueryPort;
import com.todaysfail.domains.failure.domain.Failure;
import com.todaysfail.domains.failure.service.FailureDomainService;
import com.todaysfail.domains.tag.domain.Tag;
import com.todaysfail.domains.tag.service.TagDomainService;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;

@UseCase
@RequiredArgsConstructor
public class FailureModifyUseCase {
private final FailureMapper failureMapper;
private final FailureDomainService failureDomainService;
private final TagDomainService tagDomainService;
private final CategoryQueryPort categoryQueryPort;

public FailureResponse execute(final Long failureId, final FailureModifyRequest request) {
final Long userId = SecurityUtils.getCurrentUserId();
List<Tag> tags = tagDomainService.saveAndRetrieveAllTags(request.tagNameSet());
Category category = categoryQueryPort.queryCategory(request.categoryId());
Failure failure =
Failure.builder()
.userId(userId)
.categoryId(request.categoryId())
.failureDate(request.date())
.title(request.title())
.content(request.content())
.impression(request.impression())
.tags(tags.stream().map(Tag::getId).collect(Collectors.toList()))
.secret(request.secret())
.build();
Failure modifyFailure = failureDomainService.modifyFailure(failureId, failure, category);
return failureMapper.toFailureResponse(modifyFailure);
}
}
Loading