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/202000827 #88

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
@@ -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<List<Project>> getProjects() {
final List<Project> projects = projectService.getProjects();
return ResponseEntity.ok(projects);
}

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

@PostMapping
public ResponseEntity<Project> createProject(@RequestBody final ProjectRequest request) {
final Project project = projectService.createProject(request);
return ResponseEntity.ok(project);
}

@PutMapping("/{projectId}")
public ResponseEntity<Project> 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<Void> deleteProject(@PathVariable("projectId") final Integer projectId) {
projectService.deleteProject(projectId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<ErrorResponse> 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()
)
);
}
}
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
@@ -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
@@ -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<Project> 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);
}
}
20 changes: 20 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,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;
}
}