Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feature/202002550 #63

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 0 additions & 76 deletions .github/workflows/deployment_scp.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.com.cnu.devlog_springboot.model.Post;
import com.com.cnu.devlog_springboot.model.request.PostRequest;
import com.com.cnu.devlog_springboot.service.PostService;
import com.com.cnu.devlog_springboot.type.Tag;
import io.micrometer.common.lang.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -17,8 +19,8 @@ public class PostController {

// GET /posts
@GetMapping
public ResponseEntity<List<Post>> getPosts() {
return ResponseEntity.ok(postService.getPosts());
public ResponseEntity<List<Post>> getPosts(@RequestParam @Nullable Tag tag) {
return ResponseEntity.ok(postService.getPosts(tag));
}

// GET /posts/{postId}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.AllArgsConstructor;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/projects")
@AllArgsConstructor
public class ProjectController {

private final ProjectService projectService;

@GetMapping
public ResponseEntity<List<Project>> getProjects() {
return ResponseEntity.ok(projectService.getProjects());
}

@GetMapping("{projectId}")
public ResponseEntity<Project> getProject(@PathVariable Integer projectId) {
return ResponseEntity.ok(projectService.getProject(projectId));
}

@PostMapping
public ResponseEntity<Project> createProject(@RequestBody ProjectRequest projectRequest) {
return ResponseEntity.ok(projectService.createProject(projectRequest));
}

@PostMapping("{projectId}")
public ResponseEntity<Project> updateProject(
@PathVariable Integer projectId,
@RequestBody ProjectRequest projectRequest
) {
return ResponseEntity.ok(projectService.updateProject(projectId, projectRequest));
}

@DeleteMapping("{projectId}")
public ResponseEntity<Void> deleteProject(@PathVariable Integer projectId) {
projectService.deleteProject(projectId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.com.cnu.devlog_springboot.exception;

import com.com.cnu.devlog_springboot.model.response.ErrorResponse;
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<ErrorResponse> 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()
)
);

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.com.cnu.devlog_springboot.model;

import com.com.cnu.devlog_springboot.model.request.ProjectRequest;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.com.cnu.devlog_springboot.model.response;

public record ErrorResponse(
String title,
Integer status,
Integer code,
String instance
) {
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.com.cnu.devlog_springboot.repository;

import com.com.cnu.devlog_springboot.model.Post;
import com.com.cnu.devlog_springboot.type.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface PostRepository extends JpaRepository<Post, Integer> {
// Post save(Post post);
// List<Post> findAll();
// Optional<Post> findById(Integer postId);
// void delete(Post post);
List<Post> findAllByTag(Tag tag);
}
Original file line number Diff line number Diff line change
@@ -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<Project, Integer> {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.com.cnu.devlog_springboot.service;

import com.com.cnu.devlog_springboot.exception.DevlogException;
import com.com.cnu.devlog_springboot.model.Post;
import com.com.cnu.devlog_springboot.model.request.PostRequest;
import com.com.cnu.devlog_springboot.repository.PostRepository;
import com.com.cnu.devlog_springboot.type.ErrorCode;
import com.com.cnu.devlog_springboot.type.Tag;
import io.micrometer.common.lang.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -13,7 +17,10 @@
public class PostService {
private final PostRepository postRepository;

public List<Post> getPosts() {
public List<Post> getPosts(@Nullable Tag tag) {
if(tag != null) {
return postRepository.findAllByTag(tag);
}
return postRepository.findAll();
}

Expand All @@ -33,12 +40,12 @@ public Post updatePost(Integer postId, PostRequest postRequest) {
post.setContents(postRequest.contents());
return postRepository.save(post);
})
.orElse(null);
.orElseThrow(() -> new DevlogException(ErrorCode.POST_NOT_FOUND));
}

public Post getPost(Integer postId) {
return postRepository.findById(postId)
.orElse(null);
.orElseThrow(() -> new DevlogException(ErrorCode.POST_NOT_FOUND));
}

public void deletePost(Integer postId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class ProjectService {
private final ProjectRepository projectRepository;

public List<Project> getProjects() {
return projectRepository.findAll();
}

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 Project getProject(Integer projectId) {
return projectRepository.findById(projectId)
.orElseThrow(() -> new DevlogException(ErrorCode.PROJECT_NOT_FOUND));
}

public void deleteProject(Integer projectId) {
projectRepository.findById(projectId)
.ifPresent(projectRepository::delete);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -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;
}
}