Skip to content

Commit

Permalink
Merge pull request #36 from UMC-HACKATHON-SnapSpot/feat/#28
Browse files Browse the repository at this point in the history
feat: 폴더에 스팟 저장/삭제/조회
  • Loading branch information
wonjun0120 authored Jul 4, 2024
2 parents a2402ae + 983267b commit 00cddd8
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 0 deletions.
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 조회에 실패하였습니다.");
}
}
}
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();
}
}
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();
}
}
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;

}
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);
}
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);
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
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;
import org.springframework.stereotype.Service;
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) {
Expand All @@ -39,4 +45,11 @@ public UserFolder update(Long folderId, UserFolderRequestDto userFolderRequestDt
public List<UserFolder> getAllFoldersByUserId(Long userId) {
return userFolderRepository.findAllByUserIdAndIsDeletedFalse(userId);
}

public List<Spot> getSpotsByFolderId(Long folderId) {
List<SpotSave> spotSaves = spotSaveRepository.findAllByUserFolder_Id(folderId);
return spotSaves.stream()
.map(SpotSave::getSpot)
.collect(Collectors.toList());
}
}

0 comments on commit 00cddd8

Please sign in to comment.