-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
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
4 changes: 4 additions & 0 deletions
4
src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.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,4 @@ | ||
package com.umc.hackaton.snapspot.savespot.controller; | ||
|
||
public class SaveSpotController { | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/umc/hackaton/snapspot/savespot/controller/UserFolderController.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,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<UserFolder> userFolders = userFolderService.getAllFoldersByUserId(userId); | ||
return ResponseEntity.ok().body(userFolders); | ||
} catch (Exception e) { | ||
log.info("유저 폴더 조회에 실패하였습니다.", e); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("폴더 조회에 실패하였습니다."); | ||
} | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/umc/hackaton/snapspot/savespot/dto/UserFolderRequestDto.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 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(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/umc/hackaton/snapspot/savespot/dto/UserFolderResponseDto.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,4 @@ | ||
package com.umc.hackaton.snapspot.savespot.dto; | ||
|
||
public class UserFolderResponseDto { | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/umc/hackaton/snapspot/savespot/entity/UserFolder.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,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; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.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 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<UserFolder, Long> { | ||
List<UserFolder> findAllByUserIdAndIsDeletedFalse(Long userId); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.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,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<UserFolder> getAllFoldersByUserId(Long userId) { | ||
return userFolderRepository.findAllByUserIdAndIsDeletedFalse(userId); | ||
} | ||
} |
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