Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 스팟 상세 조회 #38

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import com.umc.hackaton.snapspot.spot.service.SpotService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
Expand All @@ -22,7 +20,7 @@ public class SpotController {
private final SpotService spotService;

@PostMapping
public ResponseEntity<?> upload(@RequestBody SpotRequestDto dto){
public ResponseEntity<?> upload(@RequestBody SpotRequestDto dto) {
try {
spotService.upload(dto);
return ResponseEntity.ok().body("스팟 업로드 성공.");
Expand All @@ -31,14 +29,20 @@ public ResponseEntity<?> upload(@RequestBody SpotRequestDto dto){
}
}

@GetMapping("/list/{spotId}")
public Spot getSpotById(@PathVariable Long spotId) {
return spotService.getSpotById(spotId);

}

@GetMapping("/{spotId}")
public ResponseEntity<?> showSpot(
@PathVariable("spotId") Long spotId
) {
try {
Spot spot = spotService.getSpot(spotId);
return ResponseEntity.ok().body(spot);
} catch (Exception e){
} catch (Exception e) {
log.info("스팟 조회에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 조회에 실패하였습니다.");
}
Expand All @@ -51,13 +55,12 @@ public ResponseEntity<?> deleteSpot(
try {
spotService.deleteSpot(spotId);
return ResponseEntity.ok().body("success");
} catch (Exception e){
} catch (Exception e) {
log.info("스팟 삭제에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 삭제에 실패하였습니다.");
}
}


@PatchMapping("/{spotId}")
public ResponseEntity<?> patchSpot(
@PathVariable("spotId") Long spotId,
Expand All @@ -66,9 +69,10 @@ public ResponseEntity<?> patchSpot(
try {
Spot spot = spotService.updateSpot(spotId, dto);
return ResponseEntity.ok().body(spot);
} catch (Exception e){
} catch (Exception e) {
log.info("스팟 수정에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 수정에 실패하였습니다.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface SpotRepository extends JpaRepository<Spot, Long> {
List<Spot> findAllByUser(User user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
import com.umc.hackaton.snapspot.spot.dto.SpotDto;
import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto;
import com.umc.hackaton.snapspot.spot.entity.Spot;
import com.umc.hackaton.snapspot.user.entity.User;
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.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand Down Expand Up @@ -60,6 +58,13 @@ public void upload(SpotRequestDto dto) {

}

@Transactional
public Spot getSpotById(Long spotId) {
// spotId를 사용하여 Repository를 통해 Spot을 가져옴
return spotRepository.findById(spotId)
.orElseThrow(() -> new RuntimeException("Spot not found with id: " + spotId));
}

public Spot getSpot(Long spotId) {
return spotRepository.findById(spotId).orElse(null);
}
Expand Down