Skip to content
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
18 changes: 9 additions & 9 deletions .github/workflows/deployment_scp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ jobs:
- name: Send jar to remote server
uses: appleboy/scp-action@master
with:
host: {나의 public ip}
username: {나의 userName}
host: {172.27.31.116}
username: {dohyun123}
source: "devlog_springboot-0.0.1-SNAPSHOT.jar"
target: "/home/{나의 userName}"
target: "/home/{dohyun123}"
key: ${{ secrets.PRIVATE_KEY }}

run-app:
Expand All @@ -61,16 +61,16 @@ jobs:
- name: Move deploy.sh
uses: appleboy/scp-action@master
with:
host: {나의 public ip}
username: {나의 userName}
host: {172.27.31.116}
username: {dohyun123}
source: "deploy.sh"
target: "/home/{나의 userName}"
target: "/home/{dohyun123}"
key: ${{ secrets.PRIVATE_KEY }}
- name: Execute script
uses: appleboy/ssh-action@master
with:
username: {나의 userName}
host: {나의 public ip}
username: {dohyun123}
host: {172.27.31.116}
key: ${{ secrets.PRIVATE_KEY }}
script_stop: true
script: cd /home/{나의 userName} && chmod +x deploy.sh && ./deploy.sh
script: cd /home/{dohyun123} && chmod +x deploy.sh && ./deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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;

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

private final ProjectService projectService;

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

@PutMapping("{id}")
public ResponseEntity<Project> updateProject(
@PathVariable("id") Long id,
@RequestBody ProjectRequest request
) {
Project project = projectService.update(id, request);
return ResponseEntity.ok(project);
}

@DeleteMapping("{id}")
public ResponseEntity<Void> deleteProject(
@PathVariable("id") Long id
) {
projectService.delete(id);
return ResponseEntity.noContent().build();
}

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

@GetMapping("/{id}")
public ResponseEntity<Project> getById(
@PathVariable("id") Long id
) {
Project project = projectService.getById(id);
return ResponseEntity.ok(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.com.cnu.devlog_springboot.exception;

import lombok.Getter;

@Getter
public abstract class DevlogException extends RuntimeException {

private final int status;

protected DevlogException(String message, int status) {
super(message);
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.com.cnu.devlog_springboot.exception;

public record ErrorResponse(
String message
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.com.cnu.devlog_springboot.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionControllerAdvice {

@ExceptionHandler({DevlogException.class})
public ResponseEntity<ErrorResponse> handleException(DevlogException e) {
return ResponseEntity.status(e.getStatus())
.body(new ErrorResponse(e.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.com.cnu.devlog_springboot.exception;

public class NotFoundException extends DevlogException {

public NotFoundException(String message) {
super(message, 404);
}
}
30 changes: 26 additions & 4 deletions src/main/java/com/com/cnu/devlog_springboot/model/Project.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.com.cnu.devlog_springboot.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.*;

import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Setter
@Entity(name = "projects")
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED, force = true)
public class Project {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;
Expand All @@ -22,4 +24,24 @@ public class Project {
String contents;
LocalDate startDate;
LocalDate endDate;
}
private Integer id;
private String title;
private String summary;
private String contents;
private LocalDate startDate;
private LocalDate endDate;

public void update(
String title,
String summary,
String contents,
LocalDate startDate,
LocalDate endDate
) {
this.title = title;
this.summary = summary;
this.contents = contents;
this.startDate = startDate;
this.endDate = endDate;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.com.cnu.devlog_springboot.model.request;

import com.com.cnu.devlog_springboot.model.Project;
import java.time.LocalDate;

public record ProjectRequest(
String title,
String summary,
String contents,
@@ -9,4 +10,14 @@ public record ProjectRequest(
LocalDate startDate,
LocalDate endDate
) {
}
public Project toProject() {
return new Project(
null,
title,
summary,
contents,
startDate,
endDate
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.com.cnu.devlog_springboot.repository;

import com.com.cnu.devlog_springboot.exception.NotFoundException;
import com.com.cnu.devlog_springboot.model.Project;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProjectRepository extends JpaRepository<Project, Long> {

default Project getById(Long id) {
return findById(id).orElseThrow(() -> new NotFoundException("project가 존재하지 않음"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.com.cnu.devlog_springboot.service;

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 java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Transactional
@Service
public class ProjectService {

private final ProjectRepository projectRepository;

public Project create(ProjectRequest request) {
Project project = request.toProject();
return projectRepository.save(project);
}

public Project update(Long id, ProjectRequest request) {
Project project = projectRepository.getById(id);
project.update(
request.title(),
request.summary(),
request.contents(),
request.startDate(),
request.endDate()
);
return project;
}

public void delete(Long id) {
Project project = projectRepository.getById(id);
projectRepository.delete(project);
}

@Transactional(readOnly = true)
public List<Project> findAll() {
return projectRepository.findAll();
}

@Transactional(readOnly = true)
public Project getById(Long id) {
return projectRepository.getById(id);
}
}