Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

getProject Api 구현 #1

Merged
merged 2 commits into from
Sep 7, 2024
Merged
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
50 changes: 50 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# github repository Actions 페이지에 나타낼 이름
name: devocean A CI with Gradle

# event trigger
# main, develop 브랜치에 pr 생성시 실행되는 트리거
on:
# pr 생성시 CI까지만 진행
pull_request:
branches: [ "main", "develop" ]


# permissions: write-all


jobs:
build:
runs-on: ubuntu-20.04
steps:
## jdk setting
- uses: actions/checkout@v3
- name: 🐧Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

## gradle caching
- name: 🐧Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: 🐧application.yml 파일을 생성 합니다.
run: |
cd ./src/main/resources
touch ./application.yml
echo "${{ secrets.PROPERTIES }}" > ./application.yml
shell: bash

- name: 🐧gradle build를 위한 권한을 부여합니다.
run: chmod +x gradlew

- name: 🐧gradle build 중입니다.
run: ./gradlew build
shell: bash # ci는 여기까지
68 changes: 68 additions & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# github repository Actions 페이지에 나타낼 이름..
name: devocean A CI/CD with Gradle

# event trigger
# main, develop 브랜치에 push 시실행되는 트리거
on:
# 푸시했을 때 ci/cd
push:
branches: [ "main", "develop" ]

# permissions: write-all


jobs:
build:
runs-on: ubuntu-20.04
steps:
## jdk setting
- uses: actions/checkout@v3
- name: 🐧Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

## gradle caching
- name: 🐧Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: 🐧gradle build를 위한 권한을 부여합니다.
run: chmod +x gradlew

- name: 🐧gradle build 중입니다.
run: ./gradlew build
shell: bash # ci는 여기까지

- name: 🐧docker image build 후 docker hub에 push합니다.
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} .
docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }}

## deploy to production
- name: docker hub에서 pull 후 deploy합니다.
uses: appleboy/ssh-action@master
with:
username: ${{ secrets.EC2_USERNAME }}
host: ${{ secrets.EC2_HOST }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }}:latest
sudo docker stop $(sudo docker ps -q) 2>/dev/null || true
sudo docker run --rm -d --network='host' -p 80:8080 --name ${{ secrets.DOCKER_IMAGE }} \
-e RDB_HOST=${{ secrets.RDB_HOST }} \
-e RDB_PORT=${{ secrets.RDB_PORT }} \
-e RDB_SCHEMA=${{ secrets.RDB_SCHEMA }} \
-e RDB_USER=${{ secrets.RDB_USER }} \
-e RDB_PASSWORD=${{ secrets.RDB_PASSWORD }} \
${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }}:latest
sudo docker system prune -f
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/paws/devocean/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.paws.devocean.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*") // 허용할 출처 : 특정 도메인만 받을 수 있음
.allowedMethods("GET", "POST") // 허용할 HTTP method
.allowCredentials(true); // 쿠키 인증 요청 허용
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/paws/devocean/controller/ProjectController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.paws.devocean.controller;

import com.paws.devocean.dto.ProjectDetailResponse;
import com.paws.devocean.dto.ProjectListResponse;
import com.paws.devocean.service.ProjectService;
import com.paws.devocean.vo.Type;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
@RequestMapping("/projects")
public class ProjectController {
private final ProjectService projectService;

@GetMapping
public ProjectListResponse getProjects(@RequestParam Type type) {
return projectService.getProject(type);
}

@GetMapping("/{id}")
public ProjectDetailResponse getProject(@PathVariable Long id) {
return projectService.getProjectById(id);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/paws/devocean/dto/ProjectDetailResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.paws.devocean.dto;

import lombok.Builder;

@Builder
public record ProjectDetailResponse(
String title,
String content
) {
}
11 changes: 11 additions & 0 deletions src/main/java/com/paws/devocean/dto/ProjectListResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.paws.devocean.dto;

import lombok.Builder;

import java.util.List;

@Builder
public record ProjectListResponse (
List<ProjectResponse> projects
){}

10 changes: 10 additions & 0 deletions src/main/java/com/paws/devocean/dto/ProjectResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.paws.devocean.dto;

import lombok.Builder;

@Builder
public record ProjectResponse(
String title,
String thumbnail,
Long project_id
){}
30 changes: 30 additions & 0 deletions src/main/java/com/paws/devocean/entity/Project.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.paws.devocean.entity;

import com.paws.devocean.vo.Type;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor
public class Project {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String thumbnail;

@Column(nullable = false)
private String title;

@Column(columnDefinition = "TEXT", nullable = false)
private String content;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private Type type;
}
11 changes: 11 additions & 0 deletions src/main/java/com/paws/devocean/repository/ProjectRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.paws.devocean.repository;

import com.paws.devocean.entity.Project;
import com.paws.devocean.vo.Type;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ProjectRepository extends JpaRepository<Project, Long> {
List<Project> findAllByType(Type type);
}
38 changes: 38 additions & 0 deletions src/main/java/com/paws/devocean/service/ProjectService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.paws.devocean.service;

import com.paws.devocean.dto.ProjectDetailResponse;
import com.paws.devocean.dto.ProjectListResponse;
import com.paws.devocean.dto.ProjectResponse;
import com.paws.devocean.entity.Project;
import com.paws.devocean.repository.ProjectRepository;
import com.paws.devocean.vo.Type;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@Transactional
@RequiredArgsConstructor
public class ProjectService {
private final ProjectRepository projectRepository;

public ProjectListResponse getProject(Type type) {
return new ProjectListResponse(
projectRepository.findAllByType(type).stream()
.map(project -> ProjectResponse.builder()
.thumbnail(project.getThumbnail())
.title(project.getTitle())
.project_id(project.getId())
.build())
.toList()
);
}

public ProjectDetailResponse getProjectById(Long projectId) {
Project project = projectRepository.findById(projectId).orElseThrow();
return ProjectDetailResponse.builder()
.title(project.getTitle())
.content(project.getContent())
.build();
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/paws/devocean/vo/Type.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.paws.devocean.vo;

public enum Type {
DEVOCEAN_YOUNG,
OPEN_LAB
}
Empty file.
Empty file.
12 changes: 9 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
spring:
profiles:
active:
- prod
datasource:
url: jdbc:mysql://${RDB_HOST}:${RDB_PORT}/${RDB_SCHEMA}?serverTimezone=Asia/Seoul&characterEncoding=utf8
username: ${RDB_USER}
password: ${RDB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver

jpa:
hibernate:
ddl-auto: update
Loading