-
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.
Browse files
Browse the repository at this point in the history
feat: 폴더에 스팟 저장/삭제/조회
- Loading branch information
Showing
8 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
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 |
---|---|---|
@@ -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 조회에 실패하였습니다."); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveReqeustDto.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,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(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveResponseDto.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,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<Spot> spots; | ||
|
||
public static SpotSaveResponseDto fromEntity(Long folderId, List<Spot> spots) { | ||
return SpotSaveResponseDto.builder() | ||
.folderId(folderId) | ||
.spots(spots) | ||
.build(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/umc/hackaton/snapspot/savespot/entity/SpotSave.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.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; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/umc/hackaton/snapspot/savespot/repository/SpotSaveRepository.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,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<SpotSave, Long> { | ||
List<SpotSave> findAllByUserFolder_Id(Long userFolderId); | ||
} |
2 changes: 2 additions & 0 deletions
2
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 |
---|---|---|
@@ -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<UserFolder, Long> { | ||
List<UserFolder> findAllByUserIdAndIsDeletedFalse(Long userId); | ||
UserFolder findByUser(User user); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/umc/hackaton/snapspot/savespot/service/SpotSaveService.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 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<SpotSave> spotSaves = spotSaveRepository.findAllByUserFolder_Id(folderId); | ||
List<Spot> spots = spotSaves.stream() | ||
.map(SpotSave::getSpot) | ||
.collect(Collectors.toList()); | ||
return SpotSaveResponseDto.fromEntity(folderId, spots); | ||
} | ||
} |
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