rest controller practice
This commit is contained in:
@@ -6,10 +6,7 @@ import com.example.restcontroller.common.model.ResponseResult;
|
||||
import com.example.restcontroller.util.JWTUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@@ -30,4 +27,16 @@ public class ApiBoardScrapController {
|
||||
|
||||
return ResponseResult.result(boardService.scrapBoard(id, email));
|
||||
}
|
||||
|
||||
@DeleteMapping("/api/scrap/{id}")
|
||||
public ResponseEntity<?> chapter3_16(@PathVariable Long id,
|
||||
@RequestHeader("TOKEN") String token) {
|
||||
String email = "";
|
||||
try {
|
||||
email = JWTUtils.getIssuer(token);
|
||||
} catch (JWTVerificationException e) {
|
||||
return ResponseResult.fail("토큰 정보가 정확하지 않습니다.");
|
||||
}
|
||||
return ResponseResult.result(boardService.deleteScrapBoard(id, email));
|
||||
}
|
||||
}
|
||||
@@ -35,4 +35,6 @@ public interface BoardService {
|
||||
List<BoardReport> boardReportList();
|
||||
|
||||
ServiceResult scrapBoard(Long id, String email);
|
||||
|
||||
ServiceResult deleteScrapBoard(Long id, String email);
|
||||
}
|
||||
|
||||
@@ -275,4 +275,27 @@ public class BoardServiceImpl implements BoardService {
|
||||
|
||||
return ServiceResult.success();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public ServiceResult deleteScrapBoard(Long id, String email) {
|
||||
Optional<BoardScrap> boardScrap = boardScrapRepository.findById(id);
|
||||
if (!boardScrap.isPresent()) {
|
||||
return ServiceResult.fail("삭제할 스크랩이 없습니다");
|
||||
}
|
||||
BoardScrap boardScrapEntity = boardScrap.get();
|
||||
|
||||
Optional<User> optionalUser = userRepository.findByEmail(email);
|
||||
if (!optionalUser.isPresent()) {
|
||||
return ServiceResult.fail("회원 정보가 존재하지 않습니다");
|
||||
}
|
||||
User userEntity = optionalUser.get();
|
||||
|
||||
if (boardScrapEntity.getUser().getId() != userEntity.getId()) {
|
||||
return ServiceResult.fail("본인의 스크랩만 삭제할 수 있습니다");
|
||||
}
|
||||
|
||||
boardScrapRepository.delete(boardScrapEntity);
|
||||
return ServiceResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user