diff --git a/src/main/java/shop/hooking/hooking/controller/CopyController.java b/src/main/java/shop/hooking/hooking/controller/CopyController.java index 88dfda5..631f8cf 100644 --- a/src/main/java/shop/hooking/hooking/controller/CopyController.java +++ b/src/main/java/shop/hooking/hooking/controller/CopyController.java @@ -9,10 +9,16 @@ import shop.hooking.hooking.dto.CardSearchCondition; import shop.hooking.hooking.dto.request.CopyReqDto; import shop.hooking.hooking.dto.request.CrawlingReqDto; +import shop.hooking.hooking.dto.request.FolderReqDto; +import shop.hooking.hooking.dto.request.ScrapReqDto; import shop.hooking.hooking.dto.request.RandomSeedDto; import shop.hooking.hooking.dto.response.CopyResDto; import shop.hooking.hooking.dto.response.CopySearchResDto; +import shop.hooking.hooking.entity.Card; +import shop.hooking.hooking.entity.Contain; +import shop.hooking.hooking.entity.Scrap; import shop.hooking.hooking.exception.OutOfIndexException; +import shop.hooking.hooking.repository.ContainRepository; import shop.hooking.hooking.service.CopyService; import javax.servlet.http.HttpServletRequest; import java.util.*; @@ -24,6 +30,7 @@ public class CopyController { private final CopyService copyService; + private final ContainRepository containRepository; @Operation(summary = "전체 카피라이팅 조회하기") @@ -38,6 +45,7 @@ public ResponseEntity> getCopyList(HttpServletRequest httpReque } + @Operation(summary = "브랜드 카피라이팅 검색하기") @PostMapping("/search/brand/{index}") public ResponseEntity searchBrandList(HttpServletRequest httpRequest, @@ -81,11 +89,51 @@ public ResponseEntity> getCopyFilter(HttpServletRequest httpReq } + @Operation(summary= "사용자의 폴더 리스트 보여주기") + @GetMapping("/folder") + public ResponseEntity> getFolderList(HttpServletRequest httpRequest){ + return ResponseEntity.ok(copyService.getFolderList(httpRequest)); + + } + + @Operation(summary = "폴더 스크랩 세부 조회하기") + @GetMapping("/folder/{folderId}") + public ResponseEntity> getFolderScrap(HttpServletRequest httpRequest, @PathVariable Long folderId) { + // Contain 엔티티에서 폴더에 속한 스크랩 조회 + List contains = containRepository.findByFolderId(folderId); + List copyResDtos = new ArrayList<>(); + + // 조회된 스크랩이 없는 경우 + if (contains.isEmpty()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ArrayList<>()); // 빈 목록 반환 + } + + for (Contain contain : contains) { + Scrap scrap = contain.getScrap(); + Card card = scrap.getCard(); // 스크랩에서 카드 정보 가져오기 + + // CopyResDto 객체 생성 + CopyResDto copyResDto = CopyResDto.builder() + .id(card.getId()) + .brand(scrap.getCard().getBrand()) + .text(card.getText()) + .createdAt(scrap.getCreatedAt()) + .cardLink(scrap.getCard().getUrl()) + .build(); + + copyResDtos.add(copyResDto); + } + + // 스크랩된 CopyResDto 목록을 반환 + return ResponseEntity.ok(copyResDtos); + } + @Operation(summary = "스크랩 하기") @PostMapping("/scrap") - public ResponseEntity createScrap(HttpServletRequest httpRequest, @RequestBody CopyReqDto copyReqDto) { - return ResponseEntity.ok(copyService.createScrap(httpRequest,copyReqDto)); + public ResponseEntity createScrap(HttpServletRequest httpRequest, @RequestBody ScrapReqDto scrapReqDto) { + copyService.createScrap(httpRequest, scrapReqDto); + return ResponseEntity.ok().build(); } diff --git a/src/main/java/shop/hooking/hooking/dto/request/FolderReqDto.java b/src/main/java/shop/hooking/hooking/dto/request/FolderReqDto.java new file mode 100644 index 0000000..1303eaf --- /dev/null +++ b/src/main/java/shop/hooking/hooking/dto/request/FolderReqDto.java @@ -0,0 +1,19 @@ +package shop.hooking.hooking.dto.request; + +import lombok.Builder; +import lombok.Data; +import shop.hooking.hooking.entity.Folder; + +import java.util.List; + +@Data +@Builder +public class FolderReqDto { + private Long folderId; + + @Builder + public FolderReqDto(Long folderId){ + this.folderId=folderId; + + } +} diff --git a/src/main/java/shop/hooking/hooking/dto/request/ScrapReqDto.java b/src/main/java/shop/hooking/hooking/dto/request/ScrapReqDto.java new file mode 100644 index 0000000..ab863df --- /dev/null +++ b/src/main/java/shop/hooking/hooking/dto/request/ScrapReqDto.java @@ -0,0 +1,23 @@ +package shop.hooking.hooking.dto.request; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import shop.hooking.hooking.entity.Card; +import shop.hooking.hooking.entity.Folder; + + +@Getter +@NoArgsConstructor +public class ScrapReqDto { + private Long cardId; + private Long folderId; + private String folderName; + + @Builder + public ScrapReqDto(Card card, Folder folder){ + this.cardId=card.getId(); + this.folderId = folder.getId(); + this.folderName = folder.getName(); + } +} diff --git a/src/main/java/shop/hooking/hooking/dto/response/FolderResDto.java b/src/main/java/shop/hooking/hooking/dto/response/FolderResDto.java new file mode 100644 index 0000000..e427e71 --- /dev/null +++ b/src/main/java/shop/hooking/hooking/dto/response/FolderResDto.java @@ -0,0 +1,23 @@ +package shop.hooking.hooking.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import shop.hooking.hooking.entity.Card; +import shop.hooking.hooking.entity.Folder; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; +import java.util.List; + +@Data +@Builder +public class FolderResDto { + private List folders; + + @Builder + public FolderResDto(List folders){ + this.folders=folders; + + } +} diff --git a/src/main/java/shop/hooking/hooking/dto/response/ScrapResDto.java b/src/main/java/shop/hooking/hooking/dto/response/ScrapResDto.java new file mode 100644 index 0000000..9bc5279 --- /dev/null +++ b/src/main/java/shop/hooking/hooking/dto/response/ScrapResDto.java @@ -0,0 +1,21 @@ +package shop.hooking.hooking.dto.response; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import shop.hooking.hooking.entity.Card; +import shop.hooking.hooking.entity.Folder; + + +@Getter +@NoArgsConstructor +public class ScrapResDto { + private Long cardId; + private Long folderId; + + @Builder + public ScrapResDto(Card card, Folder folder){ + this.cardId=card.getId(); + this.folderId = folder.getId(); + } +} \ No newline at end of file diff --git a/src/main/java/shop/hooking/hooking/entity/Contain.java b/src/main/java/shop/hooking/hooking/entity/Contain.java new file mode 100644 index 0000000..5e4803b --- /dev/null +++ b/src/main/java/shop/hooking/hooking/entity/Contain.java @@ -0,0 +1,32 @@ +package shop.hooking.hooking.entity; + +import lombok.*; +import javax.persistence.*; + +@Getter +@Setter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +@Table(name = "contain") +public class Contain { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "contain_id") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="foler_id") + private Folder folder; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="scrap_id") + private Scrap scrap; + + + + @Builder + public Contain(Folder folder, Scrap scrap) { + this.folder=folder; + this.scrap=scrap; + } +} diff --git a/src/main/java/shop/hooking/hooking/entity/Folder.java b/src/main/java/shop/hooking/hooking/entity/Folder.java new file mode 100644 index 0000000..d853b57 --- /dev/null +++ b/src/main/java/shop/hooking/hooking/entity/Folder.java @@ -0,0 +1,32 @@ +package shop.hooking.hooking.entity; + +import lombok.*; +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +@Table(name = "folder") +public class Folder { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "folder_id") + private Long id; + + @Column(name = "name") + private String name; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User user; + + @Builder + public Folder(User user, String name) { + this.user = user; + this.name = name; + } +} diff --git a/src/main/java/shop/hooking/hooking/repository/ContainRepository.java b/src/main/java/shop/hooking/hooking/repository/ContainRepository.java new file mode 100644 index 0000000..506391c --- /dev/null +++ b/src/main/java/shop/hooking/hooking/repository/ContainRepository.java @@ -0,0 +1,16 @@ +package shop.hooking.hooking.repository; + + +import org.springframework.data.jpa.repository.JpaRepository; +import shop.hooking.hooking.entity.Contain; +import shop.hooking.hooking.entity.Scrap; +import shop.hooking.hooking.entity.User; + +import java.util.List; +import java.util.Optional; + +public interface ContainRepository extends JpaRepository { + + + List findByFolderId(Long folderId); +} diff --git a/src/main/java/shop/hooking/hooking/repository/FolderRepository.java b/src/main/java/shop/hooking/hooking/repository/FolderRepository.java new file mode 100644 index 0000000..46ba3b4 --- /dev/null +++ b/src/main/java/shop/hooking/hooking/repository/FolderRepository.java @@ -0,0 +1,14 @@ +package shop.hooking.hooking.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import shop.hooking.hooking.entity.Folder; +import shop.hooking.hooking.entity.User; + +import java.util.*; + +public interface FolderRepository extends JpaRepository{ + + Optional findFolderById(Long aLong); + + List findByUser(User user); +} diff --git a/src/main/java/shop/hooking/hooking/service/CopyService.java b/src/main/java/shop/hooking/hooking/service/CopyService.java index bbc07ab..c69430b 100644 --- a/src/main/java/shop/hooking/hooking/service/CopyService.java +++ b/src/main/java/shop/hooking/hooking/service/CopyService.java @@ -5,7 +5,9 @@ import org.springframework.stereotype.Service; import shop.hooking.hooking.config.enumtype.BrandType; import shop.hooking.hooking.config.enumtype.MoodType; +import shop.hooking.hooking.dto.request.ScrapReqDto; import shop.hooking.hooking.dto.request.RandomSeedDto; + import shop.hooking.hooking.dto.response.CopyResDto; import shop.hooking.hooking.global.jwt.JwtTokenProvider; import shop.hooking.hooking.dto.CardSearchCondition; @@ -31,6 +33,8 @@ public class CopyService { private final ScrapRepository scrapRepository; private final CardJpaRepository cardJpaRepository; private final JwtTokenProvider jwtTokenProvider; + private final FolderRepository folderRepository; + private final ContainRepository containRepository; //상속 -> 일반 부모 , 카카오 자식 public List getCopyList(HttpServletRequest httpRequest, int index, Long randomSeedDto) { @@ -51,8 +55,6 @@ public List getCopyList(HttpServletRequest httpRequest, int index, L } - - public List getCopyByIndex(List copyResList, int index) { int startIndex = index * 30; int endIndex = Math.min(startIndex + 30, copyResList.size()); @@ -245,29 +247,54 @@ public List getScrapList(HttpServletRequest httpRequest, int index) } // - @Transactional - public Long createScrap(HttpServletRequest httpRequest, CopyReqDto copyReq) { + public void createScrap(HttpServletRequest httpRequest, ScrapReqDto scrapReqDto) { User user = jwtTokenProvider.getUserInfoByToken(httpRequest); - System.out.println(user.getEmail()+" createScrap"); - Card card = cardRepository.findCardById(copyReq.getCardId()); + System.out.println(user.getEmail() + " createScrap"); + Card card = cardRepository.findCardById(scrapReqDto.getCardId()); + Optional optionalFolder = folderRepository.findById(scrapReqDto.getFolderId()); - if (hasScrapped(user, card)) { - throw new DuplicateScrapException(); - } + if (optionalFolder.isPresent()) { + Folder folder = optionalFolder.get(); + + if (hasScrapped(user, card)) { + throw new DuplicateScrapException(); + } + + Scrap scrap = saveCopy(user, card); // 스크랩 저장 + + // 폴더에 스크랩 추가 + Contain newContain = Contain.builder() + .folder(folder) + .scrap(scrap) + .build(); + containRepository.save(newContain); + } else { + // 폴더가 없는 경우 새로운 폴더 생성 + Folder newFolder = Folder.builder() + .name(scrapReqDto.getFolderName()) + .user(user) + .build(); + folderRepository.save(newFolder); - Long cardId = card.getId(); - saveCopy(user, card); + Scrap scrap = saveCopy(user, card); // 스크랩 저장 - return cardId; + // 새로 생성된 폴더에 스크랩 추가 + Contain newContain = Contain.builder() + .folder(newFolder) + .scrap(scrap) + .build(); + containRepository.save(newContain); + } } + private boolean hasScrapped(User user, Card card) { Scrap existingScrap = scrapRepository.findByUserAndCard(user, card); return existingScrap != null; } @Transactional - public Long saveCopy(User user, Card card) { + public Scrap saveCopy(User user, Card card) { Scrap scrap = Scrap.builder() .user(user) @@ -276,7 +303,7 @@ public Long saveCopy(User user, Card card) { Scrap savedScrap = scrapRepository.save(scrap); - return savedScrap.getId(); + return savedScrap; } @@ -370,6 +397,22 @@ public void saveCrawlingData(CrawlingReqDto crawlingReq) { cardRepository.save(card); } } + @Transactional + public List getFolderList(HttpServletRequest httpRequest) { + User user = jwtTokenProvider.getUserInfoByToken(httpRequest); + List folders = folderRepository.findByUser(user); + + List folderNames = new ArrayList<>(); + + for (Folder folder : folders) { + String folderName = folder.getName(); + folderNames.add(folderName); + } + + return folderNames; + } + + } \ No newline at end of file