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 index f9c02ce..0262fef 100644 --- a/src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.java +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.java @@ -1,4 +1,54 @@ package com.umc.hackaton.snapspot.savespot.controller; +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveReqeustDto; +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveResponseDto; +import com.umc.hackaton.snapspot.savespot.service.SpotSaveService; +import com.umc.hackaton.snapspot.savespot.service.UserFolderService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/savespot") +@Slf4j public class SaveSpotController { + + private final SpotSaveService spotSaveService; + private final UserFolderService userFolderService; + + @PostMapping + public ResponseEntity saveSpotSave(@RequestBody SpotSaveReqeustDto spotSaveRequestDto) { + try { + spotSaveService.save(spotSaveRequestDto); + return ResponseEntity.ok().body("Userfolder에 Spot 저장 성공"); + } catch (Exception e) { + log.error("스팟 저장에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Userfolder에 Spot 저장에 실패하였습니다."); + } + } + + @DeleteMapping + public ResponseEntity deleteSpotSave(@RequestParam Long spotSaveId) { + try { + spotSaveService.delete(spotSaveId); + return ResponseEntity.ok().body("Userfolder에 Spot 삭제 성공"); + } catch (Exception e) { + log.info("스팟 삭제에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Userfolder에 Spot 삭제에 실패하였습니다."); + } + } + + @GetMapping("/{id}") + public ResponseEntity getSpotSave(@PathVariable("id") Long id) { + try { + SpotSaveResponseDto spotSaveList = spotSaveService.getSpotsByFolderId(id); + return ResponseEntity.ok().body(spotSaveList); + } catch (Exception e) { + log.info("스팟 조회에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Userfolder에 Spot 조회에 실패하였습니다."); + } + } } diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveReqeustDto.java b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveReqeustDto.java new file mode 100644 index 0000000..55b353a --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveReqeustDto.java @@ -0,0 +1,22 @@ +package com.umc.hackaton.snapspot.savespot.dto; + +import com.umc.hackaton.snapspot.savespot.entity.SpotSave; +import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import jakarta.validation.constraints.NotNull; +import lombok.Builder; +import lombok.Getter; + +@Getter +public class SpotSaveReqeustDto { + @NotNull private Long folderId; + @NotNull private Long spotId; + + @Builder + public SpotSave toEntity(UserFolder userFolder, Spot spot){ + return SpotSave.builder() + .userFolder(userFolder) + .spot(spot) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveResponseDto.java b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveResponseDto.java new file mode 100644 index 0000000..7c0fcf9 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveResponseDto.java @@ -0,0 +1,23 @@ +package com.umc.hackaton.snapspot.savespot.dto; + +import com.umc.hackaton.snapspot.spot.entity.Spot; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Builder +@Getter +@Setter +public class SpotSaveResponseDto { + private Long folderId; + private List spots; + + public static SpotSaveResponseDto fromEntity(Long folderId, List spots) { + return SpotSaveResponseDto.builder() + .folderId(folderId) + .spots(spots) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/entity/SpotSave.java b/src/main/java/com/umc/hackaton/snapspot/savespot/entity/SpotSave.java new file mode 100644 index 0000000..acda4f1 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/entity/SpotSave.java @@ -0,0 +1,30 @@ +package com.umc.hackaton.snapspot.savespot.entity; + +import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class SpotSave extends BaseEntity { + + @Id + @GeneratedValue(strategy = jakarta.persistence.GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "userfolder_id") + private UserFolder userFolder; + + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "spot_id") + private Spot spot; + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/repository/SpotSaveRepository.java b/src/main/java/com/umc/hackaton/snapspot/savespot/repository/SpotSaveRepository.java new file mode 100644 index 0000000..437e1e7 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/repository/SpotSaveRepository.java @@ -0,0 +1,11 @@ +package com.umc.hackaton.snapspot.savespot.repository; + +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveResponseDto; +import com.umc.hackaton.snapspot.savespot.entity.SpotSave; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface SpotSaveRepository extends JpaRepository { + List findAllByUserFolder_Id(Long userFolderId); +} 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 index f7dc6ab..30114d7 100644 --- a/src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.java +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.java @@ -1,10 +1,12 @@ package com.umc.hackaton.snapspot.savespot.repository; import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface UserFolderRepository extends JpaRepository { List findAllByUserIdAndIsDeletedFalse(Long userId); + UserFolder findByUser(User user); } diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/service/SpotSaveService.java b/src/main/java/com/umc/hackaton/snapspot/savespot/service/SpotSaveService.java new file mode 100644 index 0000000..3f4b5b8 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/service/SpotSaveService.java @@ -0,0 +1,53 @@ +package com.umc.hackaton.snapspot.savespot.service; + +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveReqeustDto; +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveResponseDto; +import com.umc.hackaton.snapspot.savespot.entity.SpotSave; +import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.savespot.repository.SpotSaveRepository; +import com.umc.hackaton.snapspot.savespot.repository.UserFolderRepository; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.spot.repository.SpotRepository; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.user.repository.UserRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@RequiredArgsConstructor +@Service +public class SpotSaveService { + + private final SpotSaveRepository spotSaveRepository; + private final UserFolderRepository userFolderRepository; + private final SpotRepository spotRepository; + private final UserRepository userRepository; + + @Transactional + public SpotSaveResponseDto save(SpotSaveReqeustDto dto) { + UserFolder userFolder = userFolderRepository.findById(dto.getFolderId()) + .orElseThrow(() -> new IllegalArgumentException("Invalid folder ID")); + Spot spot = spotRepository.findById(dto.getSpotId()) + .orElseThrow(() -> new IllegalArgumentException("Invalid spot ID")); + SpotSave spotSave = dto.toEntity(userFolder, spot); + SpotSave savedSpotSave = spotSaveRepository.save(spotSave); + return SpotSaveResponseDto.fromEntity(userFolder.getId(), List.of(spot)); + } + + @Transactional + public void delete(Long spotSaveId) { + spotSaveRepository.deleteById(spotSaveId); + } + + @Transactional + public SpotSaveResponseDto getSpotsByFolderId(Long folderId) { + List spotSaves = spotSaveRepository.findAllByUserFolder_Id(folderId); + List spots = spotSaves.stream() + .map(SpotSave::getSpot) + .collect(Collectors.toList()); + return SpotSaveResponseDto.fromEntity(folderId, spots); + } +} 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 index e7dc67e..1ea3f2b 100644 --- a/src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.java +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.java @@ -1,8 +1,12 @@ package com.umc.hackaton.snapspot.savespot.service; +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveResponseDto; import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto; +import com.umc.hackaton.snapspot.savespot.entity.SpotSave; import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.savespot.repository.SpotSaveRepository; import com.umc.hackaton.snapspot.savespot.repository.UserFolderRepository; +import com.umc.hackaton.snapspot.spot.entity.Spot; import com.umc.hackaton.snapspot.user.entity.User; import com.umc.hackaton.snapspot.user.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -10,12 +14,14 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class UserFolderService { private final UserFolderRepository userFolderRepository; private final UserRepository userRepository; + private final SpotSaveRepository spotSaveRepository; @Transactional public void save(UserFolderRequestDto userFolder) { @@ -39,4 +45,11 @@ public UserFolder update(Long folderId, UserFolderRequestDto userFolderRequestDt public List getAllFoldersByUserId(Long userId) { return userFolderRepository.findAllByUserIdAndIsDeletedFalse(userId); } + + public List getSpotsByFolderId(Long folderId) { + List spotSaves = spotSaveRepository.findAllByUserFolder_Id(folderId); + return spotSaves.stream() + .map(SpotSave::getSpot) + .collect(Collectors.toList()); + } }