From e21e20f07951ddf816ab94e8ab71c4f0aee79f2b Mon Sep 17 00:00:00 2001 From: nanna29 Date: Thu, 18 Jul 2024 23:21:30 +0900 Subject: [PATCH 01/13] feat: initial commit (#2) --- .../server/introduce/common/BaseResponse.java | 30 +++++++++++++++++++ .../controller/IntroduceController.java | 4 +++ .../server/introduce/domain/Introduce.java | 4 +++ .../introduce/domain/IntroduceRepository.java | 4 +++ .../introduce/error/BaseErrorResponse.java | 19 ++++++++++++ .../server/introduce/error/BaseException.java | 16 ++++++++++ .../error/GlobalExceptionHandler.java | 21 +++++++++++++ .../introduce/service/IntroduceService.java | 4 +++ src/main/resources/application.properties | 3 +- 9 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/main/java/umc/kkijuk/server/introduce/common/BaseResponse.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/controller/IntroduceController.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/domain/Introduce.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/domain/IntroduceRepository.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/error/BaseException.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/service/IntroduceService.java diff --git a/src/main/java/umc/kkijuk/server/introduce/common/BaseResponse.java b/src/main/java/umc/kkijuk/server/introduce/common/BaseResponse.java new file mode 100644 index 00000000..155a4765 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/common/BaseResponse.java @@ -0,0 +1,30 @@ +package umc.kkijuk.server.introduce.common; + +import lombok.Getter; + +@Getter +public class BaseResponse { + + private final int status; + private String message; + private T data; + + // Response constructor that includes status, message, and data + public BaseResponse(int status, String message, T data) { + this.status = status; + this.message = message; + this.data = data; + } + + // Response constructor that includes status and message only + public BaseResponse(int status, String message) { + this.status = status; + this.message = message; + } + + // Response constructor that includes status and data only + public BaseResponse(int status, T data) { + this.status = status; + this.data = data; + } +} diff --git a/src/main/java/umc/kkijuk/server/introduce/controller/IntroduceController.java b/src/main/java/umc/kkijuk/server/introduce/controller/IntroduceController.java new file mode 100644 index 00000000..0fe7a02c --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/controller/IntroduceController.java @@ -0,0 +1,4 @@ +package umc.kkijuk.server.introduce.controller; + +public class IntroduceController { +} diff --git a/src/main/java/umc/kkijuk/server/introduce/domain/Introduce.java b/src/main/java/umc/kkijuk/server/introduce/domain/Introduce.java new file mode 100644 index 00000000..1686b474 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/domain/Introduce.java @@ -0,0 +1,4 @@ +package umc.kkijuk.server.introduce.domain; + +public class Introduce { +} diff --git a/src/main/java/umc/kkijuk/server/introduce/domain/IntroduceRepository.java b/src/main/java/umc/kkijuk/server/introduce/domain/IntroduceRepository.java new file mode 100644 index 00000000..1811c2b1 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/domain/IntroduceRepository.java @@ -0,0 +1,4 @@ +package umc.kkijuk.server.introduce.domain; + +public class IntroduceRepository { +} diff --git a/src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java b/src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java new file mode 100644 index 00000000..d9d32a33 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java @@ -0,0 +1,19 @@ +package umc.kkijuk.server.introduce.error; +import umc.kkijuk.server.introduce.error.BaseException; +import lombok.Getter; + +@Getter +public class BaseErrorResponse { + private final int status; + private final String message; + + public BaseErrorResponse(int status, String message) { + this.status = status; + this.message = message; + } + + public BaseErrorResponse(BaseException baseException) { + this.status = baseException.getCode(); + this.message = baseException.getMessage(); + } +} diff --git a/src/main/java/umc/kkijuk/server/introduce/error/BaseException.java b/src/main/java/umc/kkijuk/server/introduce/error/BaseException.java new file mode 100644 index 00000000..a39c5c37 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/error/BaseException.java @@ -0,0 +1,16 @@ +package umc.kkijuk.server.introduce.error; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class BaseException extends RuntimeException{ + private final int code; + private final String message; + + public BaseException(int code, String message) { + this.code = code; + this.message = message; + } +} diff --git a/src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java b/src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java new file mode 100644 index 00000000..275b0914 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java @@ -0,0 +1,21 @@ +package umc.kkijuk.server.introduce.error; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; // 여기에 추가 + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(Exception.class) + public ResponseEntity handleException(Exception e) { + BaseException exception = new BaseException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()); + return ResponseEntity.status(exception.getCode()).body(new BaseErrorResponse(exception)); + } + + @ExceptionHandler(BaseException.class) + public ResponseEntity handleBaseException(BaseException e) { + return ResponseEntity.status(e.getCode()).body(new BaseErrorResponse(e)); + } +} diff --git a/src/main/java/umc/kkijuk/server/introduce/service/IntroduceService.java b/src/main/java/umc/kkijuk/server/introduce/service/IntroduceService.java new file mode 100644 index 00000000..bef31602 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/service/IntroduceService.java @@ -0,0 +1,4 @@ +package umc.kkijuk.server.introduce.service; + +public class IntroduceService { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8f718cb3..7a647987 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,7 +5,7 @@ spring.application.name=server spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul spring.datasource.username=root -spring.datasource.password=1234 +spring.datasource.password=zheldelqlWkd29 # spring jpa spring.jpa.database=mysql @@ -13,5 +13,4 @@ spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=create spring.jpa.properties.hibernate.format_sql=true - commit.hash=local \ No newline at end of file From 547656fc5040740dd07e0b36ee9aa22e8972060f Mon Sep 17 00:00:00 2001 From: Nellie Date: Thu, 18 Jul 2024 23:23:57 +0900 Subject: [PATCH 02/13] Delete src/main/resources/application-prod.properties --- src/main/resources/application-prod.properties | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 src/main/resources/application-prod.properties diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties deleted file mode 100644 index 57a5c698..00000000 --- a/src/main/resources/application-prod.properties +++ /dev/null @@ -1,11 +0,0 @@ -spring.application.name=server - - -# spring data source -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver - -# spring jpa -spring.jpa.database=mysql -spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=create -spring.jpa.properties.hibernate.format_sql=true From 5f1e1743ddfcb1fe3aa9b7973110489b7ed22df7 Mon Sep 17 00:00:00 2001 From: Nellie Date: Thu, 18 Jul 2024 23:24:05 +0900 Subject: [PATCH 03/13] Delete src/main/resources/application.properties --- src/main/resources/application.properties | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 src/main/resources/application.properties diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 7a647987..00000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1,16 +0,0 @@ -spring.application.name=server - - -# spring data source -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul -spring.datasource.username=root -spring.datasource.password=zheldelqlWkd29 - -# spring jpa -spring.jpa.database=mysql -spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=create -spring.jpa.properties.hibernate.format_sql=true - -commit.hash=local \ No newline at end of file From 08a14355ab21982837c0964e1b58c7e2c15eb2ab Mon Sep 17 00:00:00 2001 From: nanna29 Date: Thu, 18 Jul 2024 23:24:33 +0900 Subject: [PATCH 04/13] delete: add gitignore (#2) --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index c2065bc2..95b585e9 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ out/ ### VS Code ### .vscode/ + +application.properties +application-prod.properties \ No newline at end of file From acd2069867b51874675bf0755d6cf79c00c33228 Mon Sep 17 00:00:00 2001 From: nanna29 Date: Mon, 22 Jul 2024 05:09:59 +0900 Subject: [PATCH 05/13] feat: Add MasterIntroduce code (#2) --- .../controller/MasterIntroduceController.java | 79 +++++++++++++++++++ .../server/introduce/domain/Introduce.java | 11 +++ .../introduce/domain/IntroduceRepository.java | 4 +- .../introduce/domain/MasterIntroduce.java | 57 +++++++++++++ .../domain/MasterIntroduceRepository.java | 6 ++ .../introduce/dto/MasterIntroduceReqDto.java | 14 ++++ .../introduce/dto/MasterIntroduceResDto.java | 24 ++++++ .../BaseErrorResponse.java | 3 +- .../{error => response}/BaseException.java | 2 +- .../GlobalExceptionHandler.java | 2 +- .../service/MasterIntroduceService.java | 54 +++++++++++++ 11 files changed, 251 insertions(+), 5 deletions(-) create mode 100644 src/main/java/umc/kkijuk/server/introduce/controller/MasterIntroduceController.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/domain/MasterIntroduce.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/domain/MasterIntroduceRepository.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/dto/MasterIntroduceReqDto.java create mode 100644 src/main/java/umc/kkijuk/server/introduce/dto/MasterIntroduceResDto.java rename src/main/java/umc/kkijuk/server/introduce/{error => response}/BaseErrorResponse.java (81%) rename src/main/java/umc/kkijuk/server/introduce/{error => response}/BaseException.java (86%) rename src/main/java/umc/kkijuk/server/introduce/{error => response}/GlobalExceptionHandler.java (94%) create mode 100644 src/main/java/umc/kkijuk/server/introduce/service/MasterIntroduceService.java diff --git a/src/main/java/umc/kkijuk/server/introduce/controller/MasterIntroduceController.java b/src/main/java/umc/kkijuk/server/introduce/controller/MasterIntroduceController.java new file mode 100644 index 00000000..a2a7150d --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/controller/MasterIntroduceController.java @@ -0,0 +1,79 @@ +package umc.kkijuk.server.introduce.controller; + +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import umc.kkijuk.server.introduce.common.BaseResponse; +import umc.kkijuk.server.introduce.domain.MasterIntroduce; +import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto; +import umc.kkijuk.server.introduce.dto.MasterIntroduceResDto; +import umc.kkijuk.server.introduce.response.BaseErrorResponse; +import umc.kkijuk.server.introduce.response.BaseException; +import umc.kkijuk.server.introduce.service.MasterIntroduceService; + +import java.util.List; + +@Tag(name = "master", description = "마스터 자기소개서 API") +@RestController +@RequiredArgsConstructor +@RequestMapping("/history/intro/master") +public class MasterIntroduceController { + private final MasterIntroduceService masterIntroduceService; + + @PostMapping + public ResponseEntity save(@RequestBody MasterIntroduceReqDto masterIntroduceReqDto){ + try { + MasterIntroduceResDto masterIntroduceResDto = masterIntroduceService.saveMasterIntro(masterIntroduceReqDto); + return ResponseEntity + .status(HttpStatus.OK) + .body(new BaseResponse<>(HttpStatus.OK.value(), "마스터 자기소개서 생성 완료", masterIntroduceResDto)); + } catch (BaseException e) { + return ResponseEntity + .status(e.getCode()) + .body(new BaseErrorResponse(e.getCode(), e.getMessage())); + } catch (Exception e) { + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error")); + } + } + + @GetMapping + public ResponseEntity get(){ + try { + List masterIntroduce = masterIntroduceService.getMasterIntro(); + return ResponseEntity + .status(HttpStatus.OK) + .body(new BaseResponse<>(HttpStatus.OK.value(), "마스터 자기소개서 조회 완료", masterIntroduce)); + } catch (BaseException e) { + return ResponseEntity + .status(e.getCode()) + .body(new BaseErrorResponse(e.getCode(), e.getMessage())); + } catch (Exception e) { + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error")); + } + } + + @PatchMapping + public ResponseEntity update(Long id, @RequestBody MasterIntroduceReqDto masterIntroduceReqDto){ + try { + MasterIntroduceResDto masterIntroduceResDto = masterIntroduceService.updateMasterIntro(id, masterIntroduceReqDto); + return ResponseEntity + .status(HttpStatus.OK) + .body(new BaseResponse<>(HttpStatus.OK.value(), "마스터 자기소개서 수정 완료", masterIntroduceResDto)); + } catch (BaseException e) { + return ResponseEntity + .status(e.getCode()) + .body(new BaseErrorResponse(e.getCode(), e.getMessage())); + } catch (Exception e) { + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error")); + } + } + +} diff --git a/src/main/java/umc/kkijuk/server/introduce/domain/Introduce.java b/src/main/java/umc/kkijuk/server/introduce/domain/Introduce.java index 1686b474..38dde476 100644 --- a/src/main/java/umc/kkijuk/server/introduce/domain/Introduce.java +++ b/src/main/java/umc/kkijuk/server/introduce/domain/Introduce.java @@ -1,4 +1,15 @@ package umc.kkijuk.server.introduce.domain; +import jakarta.persistence.*; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; + +@Entity +@Table(name="introduce") +@Getter public class Introduce { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + } diff --git a/src/main/java/umc/kkijuk/server/introduce/domain/IntroduceRepository.java b/src/main/java/umc/kkijuk/server/introduce/domain/IntroduceRepository.java index 1811c2b1..d5b690b8 100644 --- a/src/main/java/umc/kkijuk/server/introduce/domain/IntroduceRepository.java +++ b/src/main/java/umc/kkijuk/server/introduce/domain/IntroduceRepository.java @@ -1,4 +1,6 @@ package umc.kkijuk.server.introduce.domain; -public class IntroduceRepository { +import org.springframework.data.jpa.repository.JpaRepository; + +public interface IntroduceRepository extends JpaRepository { } diff --git a/src/main/java/umc/kkijuk/server/introduce/domain/MasterIntroduce.java b/src/main/java/umc/kkijuk/server/introduce/domain/MasterIntroduce.java new file mode 100644 index 00000000..0ddb65e5 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/domain/MasterIntroduce.java @@ -0,0 +1,57 @@ +package umc.kkijuk.server.introduce.domain; + +import jakarta.persistence.*; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; +import org.springframework.data.annotation.CreatedDate; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; + +@Entity +@Table(name = "master_introduce") +@Getter +@NoArgsConstructor +public class MasterIntroduce { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Size(max = 24) + private String oneLiner; + + private String subTitle; + private String content; + + @CreationTimestamp + private LocalDateTime created_at; + + @UpdateTimestamp + private LocalDateTime updated_at; + + @Builder + public MasterIntroduce(String oneLiner, String subTitle, String content) { + this.oneLiner = oneLiner; + this.subTitle = subTitle; + this.content = content; + } + + public String getUpdated_at() { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + return updated_at != null ? updated_at.format(formatter) : null; + } + + public void update(String oneLiner, String subTitle, String content) { + this.oneLiner = oneLiner; + this.subTitle = subTitle; + this.content = content; + } + +} diff --git a/src/main/java/umc/kkijuk/server/introduce/domain/MasterIntroduceRepository.java b/src/main/java/umc/kkijuk/server/introduce/domain/MasterIntroduceRepository.java new file mode 100644 index 00000000..c7001108 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/domain/MasterIntroduceRepository.java @@ -0,0 +1,6 @@ +package umc.kkijuk.server.introduce.domain; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MasterIntroduceRepository extends JpaRepository { +} diff --git a/src/main/java/umc/kkijuk/server/introduce/dto/MasterIntroduceReqDto.java b/src/main/java/umc/kkijuk/server/introduce/dto/MasterIntroduceReqDto.java new file mode 100644 index 00000000..14433137 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/dto/MasterIntroduceReqDto.java @@ -0,0 +1,14 @@ +package umc.kkijuk.server.introduce.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Builder +@AllArgsConstructor +@Getter +public class MasterIntroduceReqDto { + private String oneLiner; + private String subTitle; + private String content; +} diff --git a/src/main/java/umc/kkijuk/server/introduce/dto/MasterIntroduceResDto.java b/src/main/java/umc/kkijuk/server/introduce/dto/MasterIntroduceResDto.java new file mode 100644 index 00000000..3ef6139a --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/dto/MasterIntroduceResDto.java @@ -0,0 +1,24 @@ +package umc.kkijuk.server.introduce.dto; + +import lombok.*; +import umc.kkijuk.server.introduce.domain.MasterIntroduce; + +import java.time.LocalDateTime; + +@Getter +@Setter +public class MasterIntroduceResDto { + private String oneLiner; + private String subTitle; + private String content; + private String updatedAt; + + @Builder + public MasterIntroduceResDto(MasterIntroduce masterIntroduce) { + this.oneLiner = masterIntroduce.getOneLiner(); + this.subTitle = masterIntroduce.getSubTitle(); + this.content = masterIntroduce.getContent(); + this.updatedAt = masterIntroduce.getUpdated_at(); + } + +} diff --git a/src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java b/src/main/java/umc/kkijuk/server/introduce/response/BaseErrorResponse.java similarity index 81% rename from src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java rename to src/main/java/umc/kkijuk/server/introduce/response/BaseErrorResponse.java index d9d32a33..650f2d68 100644 --- a/src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java +++ b/src/main/java/umc/kkijuk/server/introduce/response/BaseErrorResponse.java @@ -1,5 +1,4 @@ -package umc.kkijuk.server.introduce.error; -import umc.kkijuk.server.introduce.error.BaseException; +package umc.kkijuk.server.introduce.response; import lombok.Getter; @Getter diff --git a/src/main/java/umc/kkijuk/server/introduce/error/BaseException.java b/src/main/java/umc/kkijuk/server/introduce/response/BaseException.java similarity index 86% rename from src/main/java/umc/kkijuk/server/introduce/error/BaseException.java rename to src/main/java/umc/kkijuk/server/introduce/response/BaseException.java index a39c5c37..e0be75d3 100644 --- a/src/main/java/umc/kkijuk/server/introduce/error/BaseException.java +++ b/src/main/java/umc/kkijuk/server/introduce/response/BaseException.java @@ -1,4 +1,4 @@ -package umc.kkijuk.server.introduce.error; +package umc.kkijuk.server.introduce.response; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java b/src/main/java/umc/kkijuk/server/introduce/response/GlobalExceptionHandler.java similarity index 94% rename from src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java rename to src/main/java/umc/kkijuk/server/introduce/response/GlobalExceptionHandler.java index 275b0914..abddc066 100644 --- a/src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java +++ b/src/main/java/umc/kkijuk/server/introduce/response/GlobalExceptionHandler.java @@ -1,4 +1,4 @@ -package umc.kkijuk.server.introduce.error; +package umc.kkijuk.server.introduce.response; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; diff --git a/src/main/java/umc/kkijuk/server/introduce/service/MasterIntroduceService.java b/src/main/java/umc/kkijuk/server/introduce/service/MasterIntroduceService.java new file mode 100644 index 00000000..94cd67e0 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/introduce/service/MasterIntroduceService.java @@ -0,0 +1,54 @@ +package umc.kkijuk.server.introduce.service; + +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import umc.kkijuk.server.introduce.domain.MasterIntroduce; +import umc.kkijuk.server.introduce.domain.MasterIntroduceRepository; +import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto; +import umc.kkijuk.server.introduce.dto.MasterIntroduceResDto; +import umc.kkijuk.server.introduce.response.BaseException; + +import java.util.List; + +@RequiredArgsConstructor +@Service +public class MasterIntroduceService{ + private final MasterIntroduceRepository MasterIntroduceRepository; + private final MasterIntroduceRepository masterIntroduceRepository; + + @Transactional + public MasterIntroduceResDto saveMasterIntro(MasterIntroduceReqDto masterIntroduceReqDto) throws Exception{ + if( MasterIntroduceRepository.findAll().stream().count()>0 ){ + throw new BaseException(HttpStatus.CONFLICT.value(), "이미 마스터 자기소개가 존재합니다"); + } + + MasterIntroduce masterIntroduce=MasterIntroduce.builder() + .oneLiner(masterIntroduceReqDto.getOneLiner()) + .content(masterIntroduceReqDto.getContent()) + .subTitle(masterIntroduceReqDto.getSubTitle()) + .build(); + + MasterIntroduceRepository.save(masterIntroduce); + + return new MasterIntroduceResDto(masterIntroduce); + } + + @Transactional + public List getMasterIntro(){ + return MasterIntroduceRepository.findAll(); + } + + @Transactional + public MasterIntroduceResDto updateMasterIntro(Long id, MasterIntroduceReqDto masterIntroduceReqDto) throws Exception{ + MasterIntroduce masterIntroduce=masterIntroduceRepository.findById(id) + .orElseThrow(() -> new BaseException(HttpStatus.NOT_FOUND.value(), "아이디를 다시 확인해주세요")); + + masterIntroduce.update(masterIntroduceReqDto.getOneLiner(), + masterIntroduceReqDto.getSubTitle(), + masterIntroduceReqDto.getContent()); + + return new MasterIntroduceResDto(masterIntroduce); + } +} From ef6225da12cc56d092fbacdd5a3ec546cddbb646 Mon Sep 17 00:00:00 2001 From: nanna29 Date: Tue, 23 Jul 2024 02:47:46 +0900 Subject: [PATCH 06/13] Test: test code about MasterIntroduecController --- .../controller/MasterIntroduceController.java | 4 +- .../BaseErrorResponse.java | 2 +- .../{response => error}/BaseException.java | 2 +- .../GlobalExceptionHandler.java | 2 +- .../service/MasterIntroduceService.java | 2 +- .../MasterIntroduceControllerTest.java | 98 +++++++++++++++++++ src/test/resources/application.properties | 5 +- 7 files changed, 106 insertions(+), 9 deletions(-) rename src/main/java/umc/kkijuk/server/introduce/{response => error}/BaseErrorResponse.java (90%) rename src/main/java/umc/kkijuk/server/introduce/{response => error}/BaseException.java (86%) rename src/main/java/umc/kkijuk/server/introduce/{response => error}/GlobalExceptionHandler.java (94%) create mode 100644 src/test/java/umc/kkijuk/server/introduce/controller/MasterIntroduceControllerTest.java diff --git a/src/main/java/umc/kkijuk/server/introduce/controller/MasterIntroduceController.java b/src/main/java/umc/kkijuk/server/introduce/controller/MasterIntroduceController.java index a2a7150d..b742a926 100644 --- a/src/main/java/umc/kkijuk/server/introduce/controller/MasterIntroduceController.java +++ b/src/main/java/umc/kkijuk/server/introduce/controller/MasterIntroduceController.java @@ -9,8 +9,8 @@ import umc.kkijuk.server.introduce.domain.MasterIntroduce; import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto; import umc.kkijuk.server.introduce.dto.MasterIntroduceResDto; -import umc.kkijuk.server.introduce.response.BaseErrorResponse; -import umc.kkijuk.server.introduce.response.BaseException; +import umc.kkijuk.server.introduce.error.BaseErrorResponse; +import umc.kkijuk.server.introduce.error.BaseException; import umc.kkijuk.server.introduce.service.MasterIntroduceService; import java.util.List; diff --git a/src/main/java/umc/kkijuk/server/introduce/response/BaseErrorResponse.java b/src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java similarity index 90% rename from src/main/java/umc/kkijuk/server/introduce/response/BaseErrorResponse.java rename to src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java index 650f2d68..a2e015c4 100644 --- a/src/main/java/umc/kkijuk/server/introduce/response/BaseErrorResponse.java +++ b/src/main/java/umc/kkijuk/server/introduce/error/BaseErrorResponse.java @@ -1,4 +1,4 @@ -package umc.kkijuk.server.introduce.response; +package umc.kkijuk.server.introduce.error; import lombok.Getter; @Getter diff --git a/src/main/java/umc/kkijuk/server/introduce/response/BaseException.java b/src/main/java/umc/kkijuk/server/introduce/error/BaseException.java similarity index 86% rename from src/main/java/umc/kkijuk/server/introduce/response/BaseException.java rename to src/main/java/umc/kkijuk/server/introduce/error/BaseException.java index e0be75d3..a39c5c37 100644 --- a/src/main/java/umc/kkijuk/server/introduce/response/BaseException.java +++ b/src/main/java/umc/kkijuk/server/introduce/error/BaseException.java @@ -1,4 +1,4 @@ -package umc.kkijuk.server.introduce.response; +package umc.kkijuk.server.introduce.error; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/umc/kkijuk/server/introduce/response/GlobalExceptionHandler.java b/src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java similarity index 94% rename from src/main/java/umc/kkijuk/server/introduce/response/GlobalExceptionHandler.java rename to src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java index abddc066..275b0914 100644 --- a/src/main/java/umc/kkijuk/server/introduce/response/GlobalExceptionHandler.java +++ b/src/main/java/umc/kkijuk/server/introduce/error/GlobalExceptionHandler.java @@ -1,4 +1,4 @@ -package umc.kkijuk.server.introduce.response; +package umc.kkijuk.server.introduce.error; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; diff --git a/src/main/java/umc/kkijuk/server/introduce/service/MasterIntroduceService.java b/src/main/java/umc/kkijuk/server/introduce/service/MasterIntroduceService.java index 94cd67e0..974b8987 100644 --- a/src/main/java/umc/kkijuk/server/introduce/service/MasterIntroduceService.java +++ b/src/main/java/umc/kkijuk/server/introduce/service/MasterIntroduceService.java @@ -8,7 +8,7 @@ import umc.kkijuk.server.introduce.domain.MasterIntroduceRepository; import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto; import umc.kkijuk.server.introduce.dto.MasterIntroduceResDto; -import umc.kkijuk.server.introduce.response.BaseException; +import umc.kkijuk.server.introduce.error.BaseException; import java.util.List; diff --git a/src/test/java/umc/kkijuk/server/introduce/controller/MasterIntroduceControllerTest.java b/src/test/java/umc/kkijuk/server/introduce/controller/MasterIntroduceControllerTest.java new file mode 100644 index 00000000..fc51345f --- /dev/null +++ b/src/test/java/umc/kkijuk/server/introduce/controller/MasterIntroduceControllerTest.java @@ -0,0 +1,98 @@ +package umc.kkijuk.server.introduce.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import umc.kkijuk.server.introduce.domain.MasterIntroduce; +import umc.kkijuk.server.introduce.domain.MasterIntroduceRepository; +import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto; +import umc.kkijuk.server.introduce.service.MasterIntroduceService; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureMockMvc +@Transactional +class MasterIntroduceControllerTest { + @LocalServerPort + private int port; + @Autowired + private MasterIntroduceRepository masterIntroduceRepository; + @Autowired + private MockMvc mockMvc; + @Autowired + private MasterIntroduceService masterIntroduceService; + @Autowired + private ObjectMapper objectMapper; + + @Test + @DisplayName("마스터 자기소개서 생성 테스트") + public void postMaster() throws Exception{ + final String oneLiner= "one-liner-test"; + final String subTitle= "sub-title-test"; + final String content= "content-test"; + + MasterIntroduceReqDto masterIntroduceReqDto= MasterIntroduceReqDto.builder() + .oneLiner(oneLiner) + .subTitle(subTitle) + .content(content) + .build(); + + + mockMvc.perform(post("/history/intro/master") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(masterIntroduceReqDto))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data.oneLiner").value("one-liner-test")) + .andExpect(jsonPath("$.data.subTitle").value("sub-title-test")) + .andExpect(jsonPath("$.data.content").value("content-test")); + } + + @Test + @DisplayName("마스터 자기소개서 수정 테스트") + public void updateMaster() throws Exception { + final String oneLiner = "one-liner-test"; + final String subTitle = "sub-title-test"; + final String content = "content-test"; + + MasterIntroduce masterIntroduce = masterIntroduceRepository.save(MasterIntroduce.builder() + .oneLiner(oneLiner) + .subTitle(subTitle) + .content(content) + .build()); + + Long id = masterIntroduce.getId(); // Retrieve the ID of the saved entity + + String expectedOneLiner = "one-liner2"; + String expectedSubTitle = "sub-title2"; + String expectedContent = "content2"; + + MasterIntroduceReqDto masterIntroduceReqDto = MasterIntroduceReqDto.builder() + .oneLiner(expectedOneLiner) + .subTitle(expectedSubTitle) + .content(expectedContent) + .build(); + + mockMvc.perform(patch("/history/intro/master") + .param("id", id.toString()) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(masterIntroduceReqDto))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data.oneLiner").value(expectedOneLiner)) + .andExpect(jsonPath("$.data.subTitle").value(expectedSubTitle)) + .andExpect(jsonPath("$.data.content").value(expectedContent)); + } +} \ No newline at end of file diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index acf6314b..7a647987 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -4,8 +4,8 @@ spring.application.name=server # spring data source spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul -spring.datasource.username=test -spring.datasource.password=testPW +spring.datasource.username=root +spring.datasource.password=zheldelqlWkd29 # spring jpa spring.jpa.database=mysql @@ -13,5 +13,4 @@ spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=create spring.jpa.properties.hibernate.format_sql=true - commit.hash=local \ No newline at end of file From 31bee122b4a42e6b32e5fe3801ce5398b84decf9 Mon Sep 17 00:00:00 2001 From: Nellie Date: Tue, 23 Jul 2024 02:51:51 +0900 Subject: [PATCH 07/13] Delete src/test/resources/application.properties --- src/test/resources/application.properties | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 src/test/resources/application.properties diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties deleted file mode 100644 index 7a647987..00000000 --- a/src/test/resources/application.properties +++ /dev/null @@ -1,16 +0,0 @@ -spring.application.name=server - - -# spring data source -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul -spring.datasource.username=root -spring.datasource.password=zheldelqlWkd29 - -# spring jpa -spring.jpa.database=mysql -spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=create -spring.jpa.properties.hibernate.format_sql=true - -commit.hash=local \ No newline at end of file From 3e2d0e9c6a14d8a7b04cd6512521158ec574d18d Mon Sep 17 00:00:00 2001 From: nanna29 Date: Tue, 23 Jul 2024 02:58:02 +0900 Subject: [PATCH 08/13] chore: fix gitignore --- .gitignore | 3 +++ src/main/resources/application-prod.properties | 2 +- src/main/resources/application.properties | 4 ++-- src/test/resources/application.properties | 6 +++--- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index c2065bc2..95b585e9 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ out/ ### VS Code ### .vscode/ + +application.properties +application-prod.properties \ No newline at end of file diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 57a5c698..c1591efc 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -7,5 +7,5 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # spring jpa spring.jpa.database=mysql spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.format_sql=true diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8f718cb3..a2c6bf93 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,12 +5,12 @@ spring.application.name=server spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul spring.datasource.username=root -spring.datasource.password=1234 +spring.datasource.password=zheldelqlWkd29 # spring jpa spring.jpa.database=mysql spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.format_sql=true diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index acf6314b..a2c6bf93 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -4,13 +4,13 @@ spring.application.name=server # spring data source spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul -spring.datasource.username=test -spring.datasource.password=testPW +spring.datasource.username=root +spring.datasource.password=zheldelqlWkd29 # spring jpa spring.jpa.database=mysql spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.format_sql=true From 5ec86cd7b6bd778c2ef45375939623b682320b12 Mon Sep 17 00:00:00 2001 From: Nellie Date: Tue, 23 Jul 2024 03:04:43 +0900 Subject: [PATCH 09/13] Delete src/main/resources/application-prod.properties --- src/main/resources/application-prod.properties | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 src/main/resources/application-prod.properties diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties deleted file mode 100644 index c1591efc..00000000 --- a/src/main/resources/application-prod.properties +++ /dev/null @@ -1,11 +0,0 @@ -spring.application.name=server - - -# spring data source -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver - -# spring jpa -spring.jpa.database=mysql -spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=update -spring.jpa.properties.hibernate.format_sql=true From 400cfa5ae800684b781d95d29ff3584dbbd9bad7 Mon Sep 17 00:00:00 2001 From: Nellie Date: Tue, 23 Jul 2024 03:04:51 +0900 Subject: [PATCH 10/13] Delete src/main/resources/application.properties --- src/main/resources/application.properties | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 src/main/resources/application.properties diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index a2c6bf93..00000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1,17 +0,0 @@ -spring.application.name=server - - -# spring data source -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul -spring.datasource.username=root -spring.datasource.password=zheldelqlWkd29 - -# spring jpa -spring.jpa.database=mysql -spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=update -spring.jpa.properties.hibernate.format_sql=true - - -commit.hash=local \ No newline at end of file From 093e2233cce4d8ba299ff055ebd07c05886d51b7 Mon Sep 17 00:00:00 2001 From: Nellie Date: Tue, 23 Jul 2024 03:05:09 +0900 Subject: [PATCH 11/13] Delete src/test/resources/application.properties --- src/test/resources/application.properties | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 src/test/resources/application.properties diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties deleted file mode 100644 index f46d581e..00000000 --- a/src/test/resources/application.properties +++ /dev/null @@ -1,17 +0,0 @@ -spring.application.name=server - - -# spring data source -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul -spring.datasource.username=test -spring.datasource.password=zheldelqlWkd29 - -# spring jpa -spring.jpa.database=mysql -spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=update -spring.jpa.properties.hibernate.format_sql=true - - -commit.hash=local \ No newline at end of file From 01e80db00b27908833cdcb70d181f9dd50c419f4 Mon Sep 17 00:00:00 2001 From: nanna29 Date: Tue, 23 Jul 2024 03:06:22 +0900 Subject: [PATCH 12/13] chore: Add gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 95b585e9..ac0e9db7 100644 --- a/.gitignore +++ b/.gitignore @@ -36,5 +36,6 @@ out/ ### VS Code ### .vscode/ +### Properties ### application.properties application-prod.properties \ No newline at end of file From 4aad4cab07001c902a75df7b6e23f73714835b41 Mon Sep 17 00:00:00 2001 From: nanna29 Date: Tue, 23 Jul 2024 23:31:42 +0900 Subject: [PATCH 13/13] update: gitignore & application.properties --- .gitignore | 6 +----- src/main/resources/application-prod.properties | 11 +++++++++++ src/main/resources/application.properties | 17 +++++++++++++++++ .../MasterIntroduceControllerTest.java | 2 +- src/test/resources/application.properties | 17 +++++++++++++++++ 5 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/main/resources/application-prod.properties create mode 100644 src/main/resources/application.properties create mode 100644 src/test/resources/application.properties diff --git a/.gitignore b/.gitignore index ac0e9db7..e48b6be6 100644 --- a/.gitignore +++ b/.gitignore @@ -34,8 +34,4 @@ out/ /.nb-gradle/ ### VS Code ### -.vscode/ - -### Properties ### -application.properties -application-prod.properties \ No newline at end of file +.vscode/ \ No newline at end of file diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties new file mode 100644 index 00000000..bfdf7cf7 --- /dev/null +++ b/src/main/resources/application-prod.properties @@ -0,0 +1,11 @@ +spring.application.name=server + + +# spring data source +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver + +# spring jpa +spring.jpa.database=mysql +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=update +spring.jpa.properties.hibernate.format_sql=true \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 00000000..07a78ad5 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,17 @@ +spring.application.name=server + + +# spring data source +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul +spring.datasource.username=test +spring.datasource.password=testPW + +# spring jpa +spring.jpa.database=mysql +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=update +spring.jpa.properties.hibernate.format_sql=true + + +commit.hash=local \ No newline at end of file diff --git a/src/test/java/umc/kkijuk/server/introduce/controller/MasterIntroduceControllerTest.java b/src/test/java/umc/kkijuk/server/introduce/controller/MasterIntroduceControllerTest.java index fc51345f..b8059ca0 100644 --- a/src/test/java/umc/kkijuk/server/introduce/controller/MasterIntroduceControllerTest.java +++ b/src/test/java/umc/kkijuk/server/introduce/controller/MasterIntroduceControllerTest.java @@ -74,7 +74,7 @@ public void updateMaster() throws Exception { .content(content) .build()); - Long id = masterIntroduce.getId(); // Retrieve the ID of the saved entity + Long id = masterIntroduce.getId(); String expectedOneLiner = "one-liner2"; String expectedSubTitle = "sub-title2"; diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 00000000..07a78ad5 --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,17 @@ +spring.application.name=server + + +# spring data source +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul +spring.datasource.username=test +spring.datasource.password=testPW + +# spring jpa +spring.jpa.database=mysql +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=update +spring.jpa.properties.hibernate.format_sql=true + + +commit.hash=local \ No newline at end of file