diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java b/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java index b0f394f..249985c 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java @@ -1,18 +1,20 @@ package com.umc.hackaton.snapspot.spot.controller; import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto; +import com.umc.hackaton.snapspot.spot.entity.Spot; 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.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.List; @RequiredArgsConstructor @RestController +@Slf4j @RequestMapping("/api/v1/spots") public class SpotController { @@ -28,4 +30,45 @@ public ResponseEntity upload(@RequestBody SpotRequestDto dto){ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 업로드에 실패하였습니다."); } } + + @GetMapping("/{spotId}") + public ResponseEntity showSpot( + @PathVariable("spotId") Long spotId + ) { + try { + Spot spot = spotService.getSpot(spotId); + return ResponseEntity.ok().body(spot); + } catch (Exception e){ + log.info("스팟 조회에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 조회에 실패하였습니다."); + } + } + + @DeleteMapping("/{spotId}") + public ResponseEntity deleteSpot( + @PathVariable("spotId") Long spotId + ) { + try { + spotService.deleteSpot(spotId); + return ResponseEntity.ok().body("success"); + } catch (Exception e){ + log.info("스팟 삭제에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 삭제에 실패하였습니다."); + } + } + + + @PatchMapping("/{spotId}") + public ResponseEntity patchSpot( + @PathVariable("spotId") Long spotId, + @RequestBody SpotRequestDto dto + ) { + try { + Spot spot = spotService.updateSpot(spotId, dto); + return ResponseEntity.ok().body(spot); + } catch (Exception e){ + log.info("스팟 수정에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 수정에 실패하였습니다."); + } + } } diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java index 252c1e0..d07a61b 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java @@ -1,6 +1,7 @@ package com.umc.hackaton.snapspot.spot.entity; import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto; import com.umc.hackaton.snapspot.user.entity.User; import jakarta.persistence.*; import lombok.AllArgsConstructor; @@ -56,6 +57,13 @@ public void decreaseLike() { this.likeNum += 1; } + public Spot update(SpotRequestDto dto){ + this.description = dto.getDescription(); + this.imgUrl = dto.getImgUrl();; + this.title = dto.getTitle(); + return this; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java b/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java index ce13d75..dcd5376 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java @@ -13,6 +13,7 @@ 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; @@ -58,4 +59,17 @@ public void upload(SpotRequestDto dto) { categorySpotRepository.saveAll(categorySpots); } + + public Spot getSpot(Long spotId) { + return spotRepository.findById(spotId).orElse(null); + } + + public void deleteSpot(Long spotId) { + spotRepository.deleteById(spotId); + } + + public Spot updateSpot(Long spotId, SpotRequestDto dto) { + Spot spot = spotRepository.findById(spotId).orElse(null); + return spot.update(dto); + } }