-
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: Visit 저장/조회 추가
- Loading branch information
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/com/umc/hackaton/snapspot/visit/controller/VisitController.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,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
21
src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitDto.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,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(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitRequestDto.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,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; | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitResponseDto.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,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
30
src/main/java/com/umc/hackaton/snapspot/visit/entity/Visit.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.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; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/umc/hackaton/snapspot/visit/repository/VisitRepository.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.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); | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/umc/hackaton/snapspot/visit/service/VisitService.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,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()); | ||
} | ||
} |