From 19e2de7d9fd2b7db15fd713d1353e15577f91c6b Mon Sep 17 00:00:00 2001 From: KimYeonWoo Date: Mon, 6 May 2024 23:48:33 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B3=BC=EC=A0=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProjectController.java | 65 +++++++++++++++++++ .../exception/DevlogException.java | 12 ++++ .../exception/GlobalExceptionHandler.java | 29 +++++++++ .../model/response/ErrorResponse.java | 9 +++ .../repository/ProjectRepository.java | 7 ++ .../service/ProjectService.java | 63 ++++++++++++++++++ .../cnu/devlog_springboot/type/ErrorCode.java | 20 ++++++ 7 files changed, 205 insertions(+) create mode 100644 src/main/java/com/com/cnu/devlog_springboot/controller/ProjectController.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/exception/DevlogException.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/model/response/ErrorResponse.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/repository/ProjectRepository.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/service/ProjectService.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java diff --git a/src/main/java/com/com/cnu/devlog_springboot/controller/ProjectController.java b/src/main/java/com/com/cnu/devlog_springboot/controller/ProjectController.java new file mode 100644 index 0000000..da43149 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/controller/ProjectController.java @@ -0,0 +1,65 @@ +package com.com.cnu.devlog_springboot.controller; + +import com.com.cnu.devlog_springboot.model.Project; +import com.com.cnu.devlog_springboot.model.request.ProjectRequest; +import com.com.cnu.devlog_springboot.service.ProjectService; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 요구사항 + * projectrequest 로 요청을 받아 글 저장 + * 저장한 모든 프로젝트글을 한 번에 조회 (param 없이) + * 프로젝트 단건 조회 id로 else -> 404 + * 수정 else -> 404 + * 삭제 -> 404 + */ + +@RestController +@RequestMapping("/projects") +@RequiredArgsConstructor +public class ProjectController { + private final ProjectService projectService; + + @GetMapping + public ResponseEntity> getProjects() { + final List projects = projectService.getProjects(); + return ResponseEntity.ok(projects); + } + + @GetMapping("/{projectId}") + public ResponseEntity getProject(@PathVariable("projectId") final Integer projectId) { + final Project project = projectService.getProject(projectId); + return ResponseEntity.ok(project); + } + + @PostMapping + public ResponseEntity createProject(@RequestBody final ProjectRequest request) { + final Project project = projectService.createProject(request); + return ResponseEntity.ok(project); + } + + @PutMapping("/{projectId}") + public ResponseEntity updateProject( + @PathVariable("projectId") final Integer projectId, + @RequestBody final ProjectRequest request + ) { + final Project project = projectService.updateProject(projectId, request); + return ResponseEntity.ok(project); + } + + @DeleteMapping("/{projectId}") + public ResponseEntity deleteProject(@PathVariable("projectId") final Integer projectId) { + projectService.deleteProject(projectId); + return ResponseEntity.noContent().build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/exception/DevlogException.java b/src/main/java/com/com/cnu/devlog_springboot/exception/DevlogException.java new file mode 100644 index 0000000..fe351f9 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/exception/DevlogException.java @@ -0,0 +1,12 @@ +package com.com.cnu.devlog_springboot.exception; + +import com.com.cnu.devlog_springboot.type.ErrorCode; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class DevlogException extends RuntimeException { + + private final ErrorCode errorCode; +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/exception/GlobalExceptionHandler.java b/src/main/java/com/com/cnu/devlog_springboot/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..3dac019 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/exception/GlobalExceptionHandler.java @@ -0,0 +1,29 @@ +package com.com.cnu.devlog_springboot.exception; + +import com.com.cnu.devlog_springboot.model.response.ErrorResponse; +import com.com.cnu.devlog_springboot.type.ErrorCode; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(DevlogException.class) + public ResponseEntity handleDevlogException( + final HttpServletRequest request, + final DevlogException e + ) { + final ErrorCode errorCode = e.getErrorCode(); + return ResponseEntity.status(e.getErrorCode().getHttpStatus()) + .body( + new ErrorResponse( + errorCode.getDescription(), + errorCode.getHttpStatus().value(), + errorCode.getErrorCode(), + request.getRequestURI() + ) + ); + } +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/model/response/ErrorResponse.java b/src/main/java/com/com/cnu/devlog_springboot/model/response/ErrorResponse.java new file mode 100644 index 0000000..81e82db --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/model/response/ErrorResponse.java @@ -0,0 +1,9 @@ +package com.com.cnu.devlog_springboot.model.response; + +public record ErrorResponse( + String title, + Integer status, + Integer code, + String instance +) { +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/repository/ProjectRepository.java b/src/main/java/com/com/cnu/devlog_springboot/repository/ProjectRepository.java new file mode 100644 index 0000000..af5c534 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/repository/ProjectRepository.java @@ -0,0 +1,7 @@ +package com.com.cnu.devlog_springboot.repository; + +import com.com.cnu.devlog_springboot.model.Project; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProjectRepository extends JpaRepository { +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/service/ProjectService.java b/src/main/java/com/com/cnu/devlog_springboot/service/ProjectService.java new file mode 100644 index 0000000..c25a649 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/service/ProjectService.java @@ -0,0 +1,63 @@ +package com.com.cnu.devlog_springboot.service; + +import com.com.cnu.devlog_springboot.exception.DevlogException; +import com.com.cnu.devlog_springboot.model.Project; +import com.com.cnu.devlog_springboot.model.request.ProjectRequest; +import com.com.cnu.devlog_springboot.repository.ProjectRepository; +import com.com.cnu.devlog_springboot.type.ErrorCode; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class ProjectService { + + private final ProjectRepository projectRepository; + + public List getProjects() { + return projectRepository.findAll(); + } + + public Project getProject(final Integer projectId) { + return projectRepository.findById(projectId) + .orElseThrow(() -> new DevlogException(ErrorCode.PROJECT_NOT_FOUND)); + } + + @Transactional + public Project createProject(final ProjectRequest request) { + final Project project = new Project( + null, + request.title(), + request.summary(), + request.contents(), + request.startDate(), + request.endDate() + ); + final Project savedProject = projectRepository.save(project); + return savedProject; + } + + @Transactional + public Project updateProject(final Integer projectId, final ProjectRequest request) { + final Project updatedProject = projectRepository.findById(projectId) + .map(project -> { + project.setTitle(request.title()); + project.setSummary(request.summary()); + project.setContents(request.contents()); + project.setStartDate(request.startDate()); + project.setEndDate(request.endDate()); + return project; + }) + .orElseThrow(() -> new DevlogException(ErrorCode.PROJECT_NOT_FOUND)); + return updatedProject; + } + + @Transactional + public void deleteProject(final Integer projectId) { + projectRepository.findById(projectId) + .ifPresent(projectRepository::delete); + } +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java b/src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java new file mode 100644 index 0000000..04babb0 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java @@ -0,0 +1,20 @@ +package com.com.cnu.devlog_springboot.type; + +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public enum ErrorCode { + POST_NOT_FOUND(HttpStatus.NOT_FOUND, 4000, "해당 게시글을 찾을 수 없습니다."), + PROJECT_NOT_FOUND(HttpStatus.NOT_FOUND, 5000, "해당 프로젝트 글을 찾을 수 없습니다."); + + private final HttpStatus httpStatus; + private final Integer errorCode; + private final String description; + + ErrorCode(final HttpStatus httpStatus, final Integer errorCode, final String description) { + this.httpStatus = httpStatus; + this.errorCode = errorCode; + this.description = description; + } +} \ No newline at end of file