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

[넬리] 마스터 자기소개서 CRUD #10

Merged
merged 20 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ out/
/.nb-gradle/

### VS Code ###
.vscode/


.vscode/
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package umc.kkijuk.server.introduce.common;

import lombok.Getter;

@Getter
public class BaseResponse<T> {

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package umc.kkijuk.server.introduce.controller;

public class IntroduceController {
}
Original file line number Diff line number Diff line change
@@ -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.error.BaseErrorResponse;
import umc.kkijuk.server.introduce.error.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<Object> 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<Object> get(){
try {
List<MasterIntroduce> 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<Object> 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"));
}
}

}
15 changes: 15 additions & 0 deletions src/main/java/umc/kkijuk/server/introduce/domain/Introduce.java
Original file line number Diff line number Diff line change
@@ -0,0 +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;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package umc.kkijuk.server.introduce.domain;

import org.springframework.data.jpa.repository.JpaRepository;

public interface IntroduceRepository extends JpaRepository<Introduce, Long> {
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package umc.kkijuk.server.introduce.domain;

import org.springframework.data.jpa.repository.JpaRepository;

public interface MasterIntroduceRepository extends JpaRepository<MasterIntroduce, Long> {
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package umc.kkijuk.server.introduce.error;
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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<BaseErrorResponse> 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<BaseErrorResponse> handleBaseException(BaseException e) {
return ResponseEntity.status(e.getCode()).body(new BaseErrorResponse(e));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package umc.kkijuk.server.introduce.service;

public class IntroduceService {
}
Original file line number Diff line number Diff line change
@@ -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.error.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<MasterIntroduce> 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);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spring.datasource.password=testPW
# 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


Expand Down
Loading