Skip to content

Commit

Permalink
[♻️refactor/#96]: 스크랩 색상 수정 api 리팩
Browse files Browse the repository at this point in the history
  • Loading branch information
JungYoonShin committed Sep 4, 2024
1 parent 449eff0 commit 0ae2c9f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ public ResponseEntity<SuccessResponse> createScrap(
return ResponseEntity.ok(SuccessResponse.of(SUCCESS_CREATE_SCRAP));
}

@DeleteMapping("/scraps/{scrapId}")
@DeleteMapping("/scraps/{internshipAnnouncementId}")
public ResponseEntity<SuccessResponse> deleteScrap(
@AuthenticationPrincipal Long userId,
@PathVariable Long scrapId) {
scrapService.deleteScrap(scrapId, userId);
@PathVariable Long internshipAnnouncementId) {
scrapService.deleteScrap(internshipAnnouncementId, userId);
return ResponseEntity.ok(SuccessResponse.of(SUCCESS_DELETE_SCRAP));
}

@PatchMapping("/scraps/{scrapId}")
@PatchMapping("/scraps/{internshipAnnouncementId}")
public ResponseEntity<SuccessResponse> updateScrapColor(
@AuthenticationPrincipal Long userId,
@PathVariable Long scrapId,
@PathVariable Long internshipAnnouncementId,
@RequestBody UpdateScrapRequestDto request) {
scrapService.updateScrapColor(scrapId, request, userId);
scrapService.updateScrapColor(internshipAnnouncementId, request, userId);
return ResponseEntity.ok(SuccessResponse.of(SUCCESS_UPDATE_SCRAP));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.terning.terningserver.dto.scrap.request;

public record CreateScrapRequestDto(
int color
String color
) {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.terning.terningserver.dto.scrap.request;

public record UpdateScrapRequestDto(
int color
String color
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public enum ErrorMessage {
NOT_FOUND_INTERN_CATEGORY(404, "해당 인턴 공고는 존재하지 않습니다"),
NOT_FOUND_INTERN_EXCEPTION(404, "해당 인턴 공고는 존재하지 않습니다"),
NOT_FOUND_USER_EXCEPTION(404, "해당 유저가 존재하지 않습니다"),
NOT_FOUND_SCRAP(404, "해당 스크랩 id에 대한 스크랩 정보가 존재하지 않습니다"),
NOT_FOUND_SCRAP(404, "스크랩 정보가 존재하지 않습니다"),
FORBIDDEN_DELETE_SCRAP(403, "해당 유저가 스크랩하지 않았으므로 스크랩 취소가 불가합니다");

private final int status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface ScrapRepository extends JpaRepository<Scrap, Long>, ScrapReposi

// List<Scrap> findByUserIdAndInternshipAnnouncement_Deadline(Long userId, LocalDate deadline);

void deleteByInternshipAnnouncementIdAndUserId(Long internshipId, Long userId);

List<Scrap> findByUserIdAndInternshipAnnouncement_DeadlineBetween(Long userId, LocalDate start, LocalDate end);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.terning.terningserver.dto.calendar.response.MonthlyListResponseDto;
import org.terning.terningserver.dto.user.response.UpcomingScrapResponseDto;
import org.terning.terningserver.exception.CustomException;
import org.terning.terningserver.exception.enums.ErrorMessage;
import org.terning.terningserver.repository.internship_announcement.InternshipRepository;
import org.terning.terningserver.repository.scrap.ScrapRepository;
import org.terning.terningserver.repository.user.UserRepository;
Expand Down Expand Up @@ -135,17 +136,17 @@ public void createScrap(Long internshipAnnouncementId, CreateScrapRequestDto req

@Override
@Transactional
public void deleteScrap(Long scrapId, Long userId) {
Scrap scrap = findScrap(scrapId);
public void deleteScrap(Long internshipAnnouncementId, Long userId) {
Scrap scrap = findScrap(internshipAnnouncementId, userId);
scrap.getInternshipAnnouncement().updateScrapCount(-1);
verifyScrapOwner(scrap, userId);
scrapRepository.deleteById(scrapId);
scrapRepository.deleteByInternshipAnnouncementIdAndUserId(internshipAnnouncementId, userId);
}

@Override
@Transactional
public void updateScrapColor(Long scrapId, UpdateScrapRequestDto request, Long userId) {
Scrap scrap = findScrap(scrapId);
public void updateScrapColor(Long internshipAnnouncementId, UpdateScrapRequestDto request, Long userId) {
Scrap scrap = findScrap(internshipAnnouncementId, userId);
verifyScrapOwner(scrap, userId);
scrap.updateColor(findColor(request.color()));
}
Expand All @@ -157,9 +158,9 @@ private void verifyScrapOwner(Scrap scrap, Long userId) {
}
}

private Color findColor(int color) {
private Color findColor(String color) {
return Arrays.stream(Color.values())
.filter(c-> c.getKey() == color)
.filter(c-> c.getName().equals(color))
.findAny().get();
}

Expand All @@ -173,8 +174,8 @@ private User findUser(Long userId) {
.orElseThrow(() -> new CustomException(NOT_FOUND_USER_EXCEPTION));
}

private Scrap findScrap(Long scrapId) {
return scrapRepository.findById(scrapId)
private Scrap findScrap(Long internshipAnnouncementId, Long userId) {
return scrapRepository.findByInternshipAnnouncementIdAndUserId(internshipAnnouncementId, userId)
.orElseThrow(() -> new CustomException(NOT_FOUND_SCRAP));
}
}
Expand Down

0 comments on commit 0ae2c9f

Please sign in to comment.