diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/controller/VisitController.java b/src/main/java/com/umc/hackaton/snapspot/visit/controller/VisitController.java new file mode 100644 index 0000000..5aab69f --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/controller/VisitController.java @@ -0,0 +1,26 @@ +package com.umc.hackaton.snapspot.visit.controller; + +import com.umc.hackaton.snapspot.visit.dto.VisitRequestDto; +import com.umc.hackaton.snapspot.visit.service.VisitService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/v1/visit") +@RequiredArgsConstructor +public class VisitController { + + private final VisitService visitService; + + @PostMapping + public ResponseEntity saveVisit(@RequestBody VisitRequestDto dto) { + try { + visitService.saveVisit(dto); + return ResponseEntity.ok().body("스팟 방문 저장 성공."); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 방문 저장에 실패하였습니다."); + } + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitDto.java b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitDto.java new file mode 100644 index 0000000..bff014a --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitDto.java @@ -0,0 +1,21 @@ +package com.umc.hackaton.snapspot.visit.dto; + +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.visit.entity.Visit; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class VisitDto { + private Spot spot; + private User user; + + public Visit toEntity() { + return Visit.builder() + .spot(spot) + .user(user) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitRequestDto.java b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitRequestDto.java new file mode 100644 index 0000000..2ca697d --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitRequestDto.java @@ -0,0 +1,12 @@ +package com.umc.hackaton.snapspot.visit.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class VisitRequestDto { + private Long spotId; + private Long userId; + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitResponseDto.java b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitResponseDto.java new file mode 100644 index 0000000..860f71b --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitResponseDto.java @@ -0,0 +1,21 @@ +package com.umc.hackaton.snapspot.visit.dto; + +import com.umc.hackaton.snapspot.visit.entity.Visit; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class VisitResponseDto { + private Long id; + private Long spotId; + private Long userId; + + public static VisitResponseDto fromEntity(Visit visit) { + return VisitResponseDto.builder() + .id(visit.getId()) + .spotId(visit.getSpot().getId()) + .userId(visit.getUser().getId()) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/entity/Visit.java b/src/main/java/com/umc/hackaton/snapspot/visit/entity/Visit.java new file mode 100644 index 0000000..6c9beea --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/entity/Visit.java @@ -0,0 +1,30 @@ +package com.umc.hackaton.snapspot.visit.entity; + +import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class Visit extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(name = "spot_id") + private Spot spot; + + @ManyToOne + @JoinColumn(name = "user_id") + private User user; +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/repository/VisitRepository.java b/src/main/java/com/umc/hackaton/snapspot/visit/repository/VisitRepository.java new file mode 100644 index 0000000..9f72371 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/repository/VisitRepository.java @@ -0,0 +1,11 @@ +package com.umc.hackaton.snapspot.visit.repository; + +import com.umc.hackaton.snapspot.visit.entity.Visit; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface VisitRepository extends JpaRepository { + + Optional findBySpotId(Long spotId); +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/service/VisitService.java b/src/main/java/com/umc/hackaton/snapspot/visit/service/VisitService.java new file mode 100644 index 0000000..e0790a6 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/service/VisitService.java @@ -0,0 +1,54 @@ +package com.umc.hackaton.snapspot.visit.service; + +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 com.umc.hackaton.snapspot.visit.dto.VisitDto; +import com.umc.hackaton.snapspot.visit.dto.VisitRequestDto; +import com.umc.hackaton.snapspot.visit.dto.VisitResponseDto; +import com.umc.hackaton.snapspot.visit.entity.Visit; +import com.umc.hackaton.snapspot.visit.repository.VisitRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class VisitService { + + private final VisitRepository visitRepository; + private final UserRepository userRepository; + private final SpotRepository spotRepository; + + @Transactional + public void saveVisit(VisitRequestDto dto) { + + User user = userRepository.findById(dto.getUserId()) + .orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); + + Spot spot = spotRepository.findById(dto.getSpotId()) + .orElseThrow(() -> new IllegalArgumentException("스팟을 찾을 수 없습니다.")); + + VisitDto visitDto = new VisitDto(); + visitDto.setUser(user); + visitDto.setSpot(spot); + + visitRepository.save(visitDto.toEntity()); + + } + + @Transactional + public VisitResponseDto getVisit(Long spotId) { + Optional visit = visitRepository.findBySpotId(spotId); + + if (visit.isEmpty()) { + throw new IllegalArgumentException("해당 스팟에 대한 방문 정보가 없습니다."); + } + + return VisitResponseDto.fromEntity(visit.get()); + } +}