-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ Feat: Scrap 엔티티 생성 & Member 테이블 Auditing Scrap 테이블 * ✨ Feat: Scrap 코드 베이스 작성 컨트롤러, 서비스, 리포지토리, 컨버터, DTO * ✨ Feat: 비즈니스 로직 작성 Service create 메소드 * ✨ Feat: 스크랩 취소, 내 스크랩 조회 API 기능 완료
- Loading branch information
Showing
15 changed files
with
549 additions
and
2,323 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/briefing/member/exception/MemberException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package briefing.member.exception; | ||
|
||
import briefing.exception.ErrorCode; | ||
import briefing.exception.GeneralException; | ||
|
||
public class MemberException extends GeneralException { | ||
public MemberException(ErrorCode errorCode){ | ||
super(errorCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package briefing.scrap.api; | ||
|
||
import briefing.common.response.CommonResponse; | ||
import briefing.scrap.application.ScrapCommandService; | ||
import briefing.scrap.application.ScrapQueryService; | ||
import briefing.scrap.application.dto.ScrapRequest; | ||
import briefing.scrap.application.dto.ScrapResponse; | ||
import briefing.scrap.domain.Scrap; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@Tag(name = "05-Scrap 📁", description = "스크랩 관련 API") | ||
@RestController | ||
@RequestMapping("/scraps") | ||
@RequiredArgsConstructor | ||
public class ScrapApi { | ||
private final ScrapQueryService scrapQueryService; | ||
private final ScrapCommandService scrapCommandService; | ||
|
||
@Operation(summary = "05-01 Scrap📁 스크랩하기 #FRAME", description = "브리핑을 스크랩하는 API입니다.") | ||
@PostMapping("/briefings") | ||
public CommonResponse<ScrapResponse.CreateDTO> create(@RequestBody ScrapRequest.CreateDTO request) { | ||
Scrap createdScrap = scrapCommandService.create(request); | ||
return CommonResponse.onSuccess(ScrapConverter.toCreateDTO(createdScrap)); | ||
} | ||
|
||
@Operation(summary = "05-02 Scrap📁 스크랩 취소 #FRAME", description = "스크랩을 취소하는 API입니다.") | ||
@DeleteMapping("/{scrapId}") | ||
public CommonResponse<ScrapResponse.DeleteDTO> delete(@PathVariable Long scrapId) { | ||
Scrap deletedScrap = scrapCommandService.delete(scrapId); | ||
return CommonResponse.onSuccess(ScrapConverter.toDeleteDTO(deletedScrap)); | ||
} | ||
|
||
@Operation(summary = "05-03 Scrap📁 내 스크랩 조회 #FRAME", description = "내 스크랩을 조회하는 API입니다.") | ||
@GetMapping("/briefings/members/{memberId}") | ||
public CommonResponse<List<ScrapResponse.ReadDTO>> getScrapsByMember(@PathVariable Long memberId) { | ||
List<Scrap> scraps = scrapQueryService.getScrapsByMemberId(memberId); | ||
return CommonResponse.onSuccess(scraps.stream().map(ScrapConverter::toReadDTO).toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package briefing.scrap.api; | ||
|
||
import briefing.briefing.domain.Briefing; | ||
import briefing.member.domain.Member; | ||
import briefing.scrap.application.dto.ScrapResponse; | ||
import briefing.scrap.domain.Scrap; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class ScrapConverter { | ||
public static ScrapResponse.CreateDTO toCreateDTO(Scrap createdScrap) { | ||
return ScrapResponse.CreateDTO.builder() | ||
.scrapId(createdScrap.getId()) | ||
.memberId(createdScrap.getMember().getId()) | ||
.briefingId(createdScrap.getBriefing().getId()) | ||
.createdAt(createdScrap.getCreatedAt()) | ||
.build(); | ||
} | ||
|
||
public static Scrap toScrap(Member member, Briefing briefing) { | ||
return Scrap.builder() | ||
.member(member) | ||
.briefing(briefing) | ||
.build(); | ||
} | ||
|
||
public static ScrapResponse.DeleteDTO toDeleteDTO(Scrap deletedScrap) { | ||
return ScrapResponse.DeleteDTO.builder() | ||
.scrapId(deletedScrap.getId()) | ||
.deletedAt(LocalDateTime.now()) | ||
.build(); | ||
} | ||
|
||
public static ScrapResponse.ReadDTO toReadDTO(Scrap scrap) { | ||
return ScrapResponse.ReadDTO.builder() | ||
.briefingId(scrap.getBriefing().getId()) | ||
.ranks(scrap.getBriefing().getRanks()) | ||
.title(scrap.getBriefing().getTitle()) | ||
.subtitle(scrap.getBriefing().getSubtitle()) | ||
.date(scrap.getBriefing().getCreatedAt().toLocalDate()) | ||
.build(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/briefing/scrap/application/ScrapCommandService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package briefing.scrap.application; | ||
|
||
import briefing.briefing.domain.Briefing; | ||
import briefing.briefing.domain.repository.BriefingRepository; | ||
import briefing.briefing.exception.BriefingException; | ||
import briefing.briefing.exception.BriefingExceptionType; | ||
import briefing.exception.ErrorCode; | ||
import briefing.member.domain.Member; | ||
import briefing.member.domain.repository.MemberRepository; | ||
import briefing.member.exception.MemberException; | ||
import briefing.scrap.api.ScrapConverter; | ||
import briefing.scrap.application.dto.ScrapRequest; | ||
import briefing.scrap.domain.Scrap; | ||
import briefing.scrap.domain.repository.ScrapRepository; | ||
import briefing.scrap.exception.ScrapException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class ScrapCommandService { | ||
|
||
private final ScrapRepository scrapRepository; | ||
private final MemberRepository memberRepository; | ||
private final BriefingRepository briefingRepository; | ||
|
||
|
||
public Scrap create(ScrapRequest.CreateDTO request) { | ||
// 이미 스크랩한경우 | ||
if(scrapRepository.existsByMember_IdAndBriefing_Id(request.getMemberId(), request.getBriefingId())) | ||
throw new ScrapException(ErrorCode.SCRAP_ALREADY_EXISTS); | ||
|
||
Member member = memberRepository.findById(request.getMemberId()) | ||
.orElseThrow(() -> new MemberException(ErrorCode.MEMBER_NOT_FOUND)); | ||
|
||
Briefing briefing = briefingRepository.findById(request.getBriefingId()) | ||
.orElseThrow(() -> new BriefingException(BriefingExceptionType.NOT_FOUND_BRIEFING)); | ||
|
||
Scrap scrap = ScrapConverter.toScrap(member, briefing); | ||
|
||
// Scrap 엔티티 저장 및 반환 | ||
return scrapRepository.save(scrap); | ||
} | ||
|
||
public Scrap delete(Long scrapId) { | ||
Scrap scrap = scrapRepository.findById(scrapId) | ||
.orElseThrow(() -> new ScrapException(ErrorCode.SCRAP_NOT_FOUND)); | ||
scrapRepository.delete(scrap); | ||
return scrap; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/briefing/scrap/application/ScrapQueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package briefing.scrap.application; | ||
|
||
import briefing.scrap.domain.Scrap; | ||
import briefing.scrap.domain.repository.ScrapRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class ScrapQueryService { | ||
|
||
private final ScrapRepository scrapRepository; | ||
|
||
public List<Scrap> getScrapsByMemberId(Long memberId) { | ||
return scrapRepository.findByMember_Id(memberId); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/briefing/scrap/application/dto/ScrapRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package briefing.scrap.application.dto; | ||
|
||
import lombok.Getter; | ||
|
||
public class ScrapRequest { | ||
|
||
@Getter | ||
public static class CreateDTO { | ||
private Long memberId; | ||
private Long briefingId; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/briefing/scrap/application/dto/ScrapResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package briefing.scrap.application.dto; | ||
|
||
import jakarta.persistence.Column; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
|
||
public class ScrapResponse { | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class CreateDTO { | ||
private Long scrapId; | ||
private Long memberId; | ||
private Long briefingId; | ||
private LocalDateTime createdAt; | ||
} | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class DeleteDTO { | ||
private Long scrapId; | ||
private LocalDateTime deletedAt; | ||
} | ||
|
||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class ReadDTO { | ||
private Long briefingId; | ||
private Integer ranks; | ||
private String title; | ||
private String subtitle; | ||
private LocalDate date; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package briefing.scrap.domain; | ||
|
||
import briefing.base.BaseDateTimeEntity; | ||
import briefing.briefing.domain.Briefing; | ||
import briefing.member.domain.Member; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Getter @Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class Scrap extends BaseDateTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(nullable = false) | ||
private Member member; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(nullable = false) | ||
private Briefing briefing; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/briefing/scrap/domain/repository/ScrapRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package briefing.scrap.domain.repository; | ||
|
||
import briefing.scrap.domain.Scrap; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface ScrapRepository extends JpaRepository<Scrap, Long> { | ||
|
||
boolean existsByMember_IdAndBriefing_Id(Long memberId, Long briefingId); | ||
|
||
List<Scrap> findByMember_Id(Long memberId); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/briefing/scrap/exception/ScrapException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package briefing.scrap.exception; | ||
|
||
import briefing.exception.ErrorCode; | ||
import briefing.exception.GeneralException; | ||
|
||
public class ScrapException extends GeneralException { | ||
public ScrapException(ErrorCode errorCode){ | ||
super(errorCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters