Skip to content

Commit

Permalink
Merge pull request #34 from UMC-HACKATHON-SnapSpot/feat/#33
Browse files Browse the repository at this point in the history
feat: 기능 추가 완료
m3k0813 authored Jul 4, 2024
2 parents 35b3dbf + e94fa99 commit 7e4c766
Showing 3 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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("스팟 수정에 실패하였습니다.");
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 7e4c766

Please sign in to comment.