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

Sireal 과제 #24

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
# UMC Spring A팀 레포지토리입니다.

1. erd

![My ERD](https://github.com/UMC-CAU-6th/Spring-A/blob/sireal/%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202024-06-25%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%204.07.37.png)


Table Member {
id INTEGER [pk]
email VARCHAR
password VARCHAR
nickname VARCHAR
}

Table Board {
id INTEGER [pk]
name VARCHAR
description VARCHAR
}

Table Post {
id INTEGER [pk]
title VARCHAR
content TEXT
created_at DATETIME
updated_at DATETIME
member_id INTEGER
board_id INTEGER
}

Table Comment {
id INTEGER [pk]
content TEXT
created_at DATETIME
updated_at DATETIME
member_id INTEGER
post_id INTEGER
}

Ref: Member.id > Post.member_id
Ref: Member.id > Comment.member_id
Ref: Post.id > Comment.post_id
Ref: Board.id > Post.board_id
6 changes: 4 additions & 2 deletions umc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ dependencies {
testImplementation 'org.springframework.security:spring-security-test'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtimeOnly 'com.mysql:mysql-connector-j'
implementation group: 'org.hibernate', name: 'hibernate-spatial', version: '5.6.15.Final'

// 이미지업로드
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

// jwt
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.15'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'

}

tasks.named('bootBuildImage') {
Expand Down
2 changes: 1 addition & 1 deletion umc/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion umc/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
20 changes: 10 additions & 10 deletions umc/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
5 changes: 3 additions & 2 deletions umc/src/main/java/com/umc/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;


@SpringBootApplication
@EnableJpaAuditing
public class DemoApplication {
public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);
}
}

34 changes: 34 additions & 0 deletions umc/src/main/java/com/umc/common/aws/s3/AmazonS3Manager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.umc.common.aws.s3;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.umc.common.config.AmazonConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Component
@RequiredArgsConstructor
public class AmazonS3Manager{

private final AmazonS3 amazonS3;

private final AmazonConfig amazonConfig;

public String uploadFile(String keyName, MultipartFile file) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
try {
amazonS3.putObject(new PutObjectRequest(amazonConfig.getBucket(), keyName, file.getInputStream(), metadata));
} catch (IOException e) {
log.error("error at AmazonS3Manager uploadFile : {}", (Object) e.getStackTrace());
}
return amazonS3.getUrl(amazonConfig.getBucket(), keyName).toString();
}
}

51 changes: 51 additions & 0 deletions umc/src/main/java/com/umc/common/config/AmazonConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.umc.common.config;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.auth.AWSCredentialsProvider;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Getter
@Setter
public class AmazonConfig {

private AWSCredentials awsCredentials;

@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;

@Value("${cloud.aws.credentials.secretKey}")
private String secretKey;

@Value("${cloud.aws.region.static}")
private String region;

@Value("${cloud.aws.s3.bucket")
private String bucket;

@Bean
public AmazonS3 amazonS3() {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
return AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}

@Bean
public AWSCredentialsProvider awsCredentialsProvider() {
return new AWSStaticCredentialsProvider(awsCredentials);
}



}
9 changes: 4 additions & 5 deletions umc/src/main/java/com/umc/common/entity/BaseTimeEntity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.umc.common.entity;


import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
Expand All @@ -22,4 +21,4 @@ public abstract class BaseTimeEntity {
@LastModifiedDate
@Column(nullable = false)
private LocalDateTime modifiedAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.umc.domain.board.controller;

import com.umc.common.aws.s3.AmazonS3Manager;
import com.umc.common.response.ApiResponse;
import com.umc.domain.board.dto.BoardRequestDto;
import com.umc.domain.board.dto.BoardResponseDto;
import com.umc.domain.board.service.BoardService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/boards")
public class BoardController {

private final BoardService boardService;
private final AmazonS3Manager amazonS3Manager;


public BoardController(BoardService boardService, AmazonS3Manager amazonS3Manager) {
this.boardService = boardService;
this.amazonS3Manager = amazonS3Manager;
}

@PostMapping
public ApiResponse<Void> createBoard(
@RequestParam("file") MultipartFile file) {

String imageUrl = amazonS3Manager.uploadFile(UUID.randomUUID().toString(), file);
boardService.createBoard(imageUrl);
return ApiResponse.onSuccess(null);
}

@GetMapping
public ResponseEntity<ApiResponse<List<BoardResponseDto>>> getAllBoards(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
ApiResponse<List<BoardResponseDto>> response = boardService.getAllBoards(page, size);
return ResponseEntity.ok(response);
}

@GetMapping("/{id}")
public ResponseEntity<ApiResponse<BoardResponseDto>> getBoardById(@PathVariable Long id) {
ApiResponse<BoardResponseDto> response = boardService.getBoardById(id);
return ResponseEntity.ok(response);
}

@PutMapping("/{id}")
public ResponseEntity<ApiResponse<Void>> updateBoard(@PathVariable Long id, @RequestBody BoardRequestDto request) {
ApiResponse<Void> response = boardService.updateBoard(id, request);
return ResponseEntity.ok(response);
}

@DeleteMapping("/{id}")
public ResponseEntity<ApiResponse<Void>> deleteBoard(@PathVariable Long id) {
ApiResponse<Void> response = boardService.deleteBoard(id);
return ResponseEntity.ok(response);
}
}
11 changes: 11 additions & 0 deletions umc/src/main/java/com/umc/domain/board/dto/BoardRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.umc.domain.board.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BoardRequestDto {
private String name;
private String description;
}
14 changes: 14 additions & 0 deletions umc/src/main/java/com/umc/domain/board/dto/BoardResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.umc.domain.board.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class BoardResponseDto {
private Long id;
private String name;
private String description;
}
20 changes: 20 additions & 0 deletions umc/src/main/java/com/umc/domain/board/entity/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.umc.domain.board.entity;

import jakarta.persistence.*;
import lombok.*;

@Getter
@Builder
@Setter
@NoArgsConstructor
@Entity
@AllArgsConstructor
public class Board {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;

private String imageUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.umc.domain.board.repository;
import com.umc.domain.board.entity.Board;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.awt.print.Pageable;

@Repository
public interface BoardRepository extends JpaRepository<Board, Long> {
Page<Board> findAll(Pageable pageable);
}

Loading