diff --git a/src/main/java/com/umc/hackaton/snapspot/config/entity/BaseEntity.java b/src/main/java/com/umc/hackaton/snapspot/config/entity/BaseEntity.java index 242d783..011dd9c 100644 --- a/src/main/java/com/umc/hackaton/snapspot/config/entity/BaseEntity.java +++ b/src/main/java/com/umc/hackaton/snapspot/config/entity/BaseEntity.java @@ -5,6 +5,7 @@ import jakarta.persistence.MappedSuperclass; import java.time.LocalDateTime; import lombok.Getter; +import lombok.Setter; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -22,6 +23,7 @@ public abstract class BaseEntity { @Column(name = "updated_at") private LocalDateTime updatedAt; + @Setter @Column(name = "deleted", nullable = false) private Boolean isDeleted = false; } diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.java b/src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.java new file mode 100644 index 0000000..f9c02ce --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.java @@ -0,0 +1,4 @@ +package com.umc.hackaton.snapspot.savespot.controller; + +public class SaveSpotController { +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/controller/UserFolderController.java b/src/main/java/com/umc/hackaton/snapspot/savespot/controller/UserFolderController.java new file mode 100644 index 0000000..ea2292e --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/controller/UserFolderController.java @@ -0,0 +1,72 @@ +package com.umc.hackaton.snapspot.savespot.controller; + +import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto; +import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.savespot.service.UserFolderService; +import com.umc.hackaton.snapspot.user.service.UserService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RequiredArgsConstructor +@RestController +@Slf4j +@RequestMapping("/api/v1/folders") +public class UserFolderController { + private final UserFolderService userFolderService; + private final UserService userService; + + @PostMapping("/") + public ResponseEntity createFolder(@RequestBody UserFolderRequestDto folder) { + try { + userFolderService.save(folder); + return ResponseEntity.ok().body(folder); + } catch (Exception e){ + log.info("유저 폴더 생성에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("폴더 생성에 실패하였습니다."); + } + } + + @DeleteMapping("/{folderId}") + public ResponseEntity deleteFolder( + @PathVariable("folderId") Long folderId + ) { + try { + userFolderService.delete(folderId); + return ResponseEntity.ok().body("Success"); + } catch (Exception e){ + log.info("유저 폴더 삭제에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("폴더 삭제에 실패하였습니다."); + } + } + + @PatchMapping("/{folderId}/") + public ResponseEntity updateFolder( + @PathVariable("folderId") Long folderId, + @RequestBody UserFolderRequestDto folder + ) { + try { + UserFolder userFolder = userFolderService.update(folderId, folder); + return ResponseEntity.ok().body(userFolder); + } catch (Exception e){ + log.info("유저 폴더 업데이트에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("폴더 삭제에 실패하였습니다."); + } + } + + @GetMapping("/{userId}") + public ResponseEntity getFolders(@PathVariable("userId") Long userId) { + try { + List userFolders = userFolderService.getAllFoldersByUserId(userId); + return ResponseEntity.ok().body(userFolders); + } catch (Exception e) { + log.info("유저 폴더 조회에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("폴더 조회에 실패하였습니다."); + } + } + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/dto/UserFolderRequestDto.java b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/UserFolderRequestDto.java new file mode 100644 index 0000000..ce0ec57 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/UserFolderRequestDto.java @@ -0,0 +1,21 @@ +package com.umc.hackaton.snapspot.savespot.dto; + +import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.user.entity.User; +import jakarta.validation.constraints.NotNull; +import lombok.Builder; +import lombok.Getter; + +@Getter +public class UserFolderRequestDto { + @NotNull private Long userId; + @NotNull private String folderName; + + @Builder + public UserFolder toEntity(User user){ + return UserFolder.builder() + .user(user) + .folderName(folderName) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/dto/UserFolderResponseDto.java b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/UserFolderResponseDto.java new file mode 100644 index 0000000..39c65cc --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/UserFolderResponseDto.java @@ -0,0 +1,4 @@ +package com.umc.hackaton.snapspot.savespot.dto; + +public class UserFolderResponseDto { +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/entity/UserFolder.java b/src/main/java/com/umc/hackaton/snapspot/savespot/entity/UserFolder.java new file mode 100644 index 0000000..420743e --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/entity/UserFolder.java @@ -0,0 +1,30 @@ +package com.umc.hackaton.snapspot.savespot.entity; + +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.user.entity.User; +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class UserFolder extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + @JsonBackReference + private User user; + + @Setter + @Column(name = "folder_name", nullable = false) + private String folderName; +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.java b/src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.java new file mode 100644 index 0000000..f7dc6ab --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.java @@ -0,0 +1,10 @@ +package com.umc.hackaton.snapspot.savespot.repository; + +import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface UserFolderRepository extends JpaRepository { + List findAllByUserIdAndIsDeletedFalse(Long userId); +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.java b/src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.java new file mode 100644 index 0000000..e7dc67e --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.java @@ -0,0 +1,42 @@ +package com.umc.hackaton.snapspot.savespot.service; + +import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto; +import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.savespot.repository.UserFolderRepository; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class UserFolderService { + private final UserFolderRepository userFolderRepository; + private final UserRepository userRepository; + + @Transactional + public void save(UserFolderRequestDto userFolder) { + User user = userRepository.findById(userFolder.getUserId()).orElse(null); + userFolderRepository.save(userFolder.toEntity(user)); + } + + public void delete(Long folderId) { + UserFolder userFolder = userFolderRepository.findById(folderId).orElse(null); + userFolder.setIsDeleted(true); + userFolderRepository.save(userFolder); + } + + public UserFolder update(Long folderId, UserFolderRequestDto userFolderRequestDto) { + UserFolder userFolder = userFolderRepository.findById(folderId).orElse(null); + userFolder.setFolderName(userFolderRequestDto.getFolderName()); + userFolderRepository.save(userFolder); + return userFolderRepository.findById(folderId).get(); + } + + public List getAllFoldersByUserId(Long userId) { + return userFolderRepository.findAllByUserIdAndIsDeletedFalse(userId); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/user/entity/User.java b/src/main/java/com/umc/hackaton/snapspot/user/entity/User.java index a37e667..e66495d 100644 --- a/src/main/java/com/umc/hackaton/snapspot/user/entity/User.java +++ b/src/main/java/com/umc/hackaton/snapspot/user/entity/User.java @@ -1,11 +1,13 @@ package com.umc.hackaton.snapspot.user.entity; +import com.fasterxml.jackson.annotation.JsonManagedReference; import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.savespot.entity.UserFolder; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; + +import java.util.LinkedHashSet; +import java.util.Set; @Entity @Getter @@ -42,4 +44,10 @@ public void prePersist() { public void updateProfileImg(String profileImg) { this.profileImg = profileImg; } + + @ToString.Exclude + @JsonManagedReference + @OrderBy("id") + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) + private final Set userFolders = new LinkedHashSet<>(); }