Skip to content

Commit

Permalink
Merge pull request #27 from UMC-HACKATHON-SnapSpot/feat/#21
Browse files Browse the repository at this point in the history
feat: Visit 저장/조회 추가
  • Loading branch information
wonjun0120 authored Jul 4, 2024
2 parents e4ea623 + fb19baf commit 2183c12
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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("스팟 방문 저장에 실패하였습니다.");
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitDto.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/umc/hackaton/snapspot/visit/entity/Visit.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Visit, Long> {

Optional<Visit> findBySpotId(Long spotId);
}
Original file line number Diff line number Diff line change
@@ -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> visit = visitRepository.findBySpotId(spotId);

if (visit.isEmpty()) {
throw new IllegalArgumentException("해당 스팟에 대한 방문 정보가 없습니다.");
}

return VisitResponseDto.fromEntity(visit.get());
}
}

0 comments on commit 2183c12

Please sign in to comment.