diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml new file mode 100644 index 0000000..516ead2 --- /dev/null +++ b/.github/workflows/blank.yml @@ -0,0 +1,76 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle + +name: Deployment + +on: + workflow_dispatch: + push: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + - name: Build with Gradle + uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 + with: + arguments: build + - uses: actions/upload-artifact@v3 + with: + name: jar + path: build/libs + + send-jar: + needs: build + runs-on: ubuntu-latest + steps: + - name: Download jar + uses: actions/download-artifact@v3 + with: + name: jar + - name: Send jar to remote server + uses: appleboy/scp-action@master + with: + host: 34.47.91.28 + username: isords20 + source: "devlog_springboot-0.0.1-SNAPSHOT.jar" + target: "/home/isords20" + key: ${{ secrets.PRIVATE_KEY }} + + run-app: + needs: send-jar + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Move deploy.sh + uses: appleboy/scp-action@master + with: + host: 34.47.91.28 + username: isords20 + source: "deploy.sh" + target: "/home/isords20" + key: ${{ secrets.PRIVATE_KEY }} + - name: Execute script + uses: appleboy/ssh-action@master + with: + username: isords20 + host: 34.47.91.28 + key: ${{ secrets.PRIVATE_KEY }} + script_stop: true + script: cd /home/isords20 && chmod +x deploy.sh && ./deploy.sh 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..8304d25 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/controller/ProjectController.java @@ -0,0 +1,47 @@ +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 lombok.AllArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import java.util.List; + +@RestController +@RequestMapping("/projects") +@AllArgsConstructor +public class ProjectController { + + private final ProjectService projectService; + + @GetMapping + public ResponseEntity> getProjects() { + return ResponseEntity.ok(projectService.getProjects()); + } + + @GetMapping("{projectId}") + public ResponseEntity getProject(@PathVariable Integer projectId) { + return ResponseEntity.ok(projectService.getProject(projectId)); + } + @PostMapping + public ResponseEntity createProject(@RequestBody ProjectRequest projectRequest) { + return ResponseEntity.ok(projectService.createProject(projectRequest)); + } + + @PutMapping("{projectId}") + public ResponseEntity updateProject( + @PathVariable Integer projectId, + @RequestBody ProjectRequest projectRequest + ) + { + return ResponseEntity.ok(projectService.updateProject(projectId, projectRequest)); + } + + @DeleteMapping("{projectId}") + public ResponseEntity deleteProject(@PathVariable Integer projectId) { + projectService.deleteProject(projectId); + return ResponseEntity.noContent().build(); + } + +} diff --git a/src/main/java/com/com/cnu/devlog_springboot/controller/exception/DevlogException.java b/src/main/java/com/com/cnu/devlog_springboot/controller/exception/DevlogException.java new file mode 100644 index 0000000..9da0b4e --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/controller/exception/DevlogException.java @@ -0,0 +1,11 @@ +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; +} diff --git a/src/main/java/com/com/cnu/devlog_springboot/controller/exception/GlobalExceptionHandler.java b/src/main/java/com/com/cnu/devlog_springboot/controller/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..e0a5359 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/controller/exception/GlobalExceptionHandler.java @@ -0,0 +1,27 @@ +package com.com.cnu.devlog_springboot.exception; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import com.com.cnu.devlog_springboot.model.response.ErrorResponse; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(DevlogException.class) + public ResponseEntity handlerDevlogException( + HttpServletRequest request, DevlogException e + ) + { + return ResponseEntity.status(e.getErrorCode().getHttpStatus()) + .body( + new ErrorResponse( + e.getErrorCode().getDescription(), + e.getErrorCode().getHttpStatus().value(), + e.getErrorCode().getErrorCode(), + request.getRequestURI() + ) + ); + } +} 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..c102932 --- /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 +) { +} 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..f9c509d --- /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 { +} 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..64e0e8c --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/service/ProjectService.java @@ -0,0 +1,56 @@ +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 lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@AllArgsConstructor +public class ProjectService { + private final ProjectRepository projectRepository; + + public List getProjects() { + return projectRepository.findAll(); + } + + public Project getProject(Integer projectId) { + return projectRepository.findById(projectId) + .orElseThrow(() -> new DevlogException(ErrorCode.PROJECT_NOT_FOUND)); + } + + public Project createProject(ProjectRequest projectRequest) { + return projectRepository.save( + new Project( + null, + projectRequest.title(), + projectRequest.summary(), + projectRequest.contents(), + projectRequest.startDate(), + projectRequest.endDate() + ) + ); + } + + public Project updateProject(Integer projectId, ProjectRequest projectRequest) { + return projectRepository.findById(projectId) + .map(project -> { + project.setTitle(projectRequest.title()); + project.setContents(projectRequest.contents()); + project.setSummary(projectRequest.summary()); + project.setStartDate(projectRequest.startDate()); + project.setEndDate((projectRequest.endDate())); + return projectRepository.save(project); + }).orElseThrow(() -> new DevlogException(ErrorCode.PROJECT_NOT_FOUND)); + } + + public void deleteProject(Integer projectId) { + projectRepository.findById(projectId) + .ifPresent(projectRepository::delete); + } +} 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..0604e13 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java @@ -0,0 +1,28 @@ +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, + 4001, + "해당 프로젝트를 찾을 수 없습니다.") + ; + + private final HttpStatus httpStatus; + private final Integer errorCode; + private final String description; + + ErrorCode(HttpStatus httpStatus, Integer errorCode, String description) { + this.httpStatus = httpStatus; + this.errorCode = errorCode; + this.description = description; + } +}