Skip to content

Commit

Permalink
Refactor: 기수 이름 대신 숫자로 변경 (#28)
Browse files Browse the repository at this point in the history
* refactor: 기수 이름 대신 숫자로 변경

- generation_name 대신 generation_number로 수정
- 관련 예외 처리도 변경

* refactor: dto 객체 정적 팩토리 메서드 패턴으로 변경

- class -> record로 변경
- 서비스에 buildGenerationInfo 메서드 삭제
- record에 static 메서드 추가

* refactor: 코드 길이 최적화

---------

Co-authored-by: Youth <[email protected]>
  • Loading branch information
yunhacandy and Youthhing authored Feb 8, 2024
1 parent 563a70a commit 0ecc90f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@Getter
public class AddGenerationRequest {
private String generationName;
private int generationNumber;
private int startYear;
private int startMonth;
private int startDay;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cotato.csquiz.domain.dto.generation;

import cotato.csquiz.domain.entity.Generation;

public record GenerationInfoResponse(
Long generationId,
int generationNumber
) {

public static GenerationInfoResponse from(Generation generation) {
return new GenerationInfoResponse(
generation.getId(),
generation.getNumber()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class MemberEnrollInfoResponse {
private Long id;
private String name;
private MemberPosition position;
private String generationName;
private int generationNumber;

public static MemberEnrollInfoResponse from(Member member) {
return new MemberEnrollInfoResponse(
member.getId(),
member.getName(),
member.getPosition(),
member.getGeneration().getName()
member.getGeneration().getNumber()
);
}
}
8 changes: 4 additions & 4 deletions src/main/java/cotato/csquiz/domain/entity/Generation.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class Generation extends BaseTimeEntity {
@Column(name = "generation_id")
private Long id;

@Column(name = "generation_name", unique = true, nullable = false)
private String name;
@Column(name = "generation_number", unique = true, nullable = false)
private int number;

@Column(name = "generation_start_date")
private LocalDate startDate;
Expand All @@ -35,8 +35,8 @@ public class Generation extends BaseTimeEntity {
private Boolean isRecruit;

@Builder
public Generation(String name, LocalDate startDate, LocalDate endDate) {
this.name = name;
public Generation(int number, LocalDate startDate, LocalDate endDate) {
this.number = number;
this.startDate = startDate;
this.endDate = endDate;
isRecruit = false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cotato/csquiz/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public enum ErrorCode {

WEBSOCKET_SEND_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, "소캣 메세지 전송 실패"),
GENERATION_NAME_EXIST(HttpStatus.CONFLICT, "이미 이름이 있습니다"),
GENERATION_NUMBER_EXIST(HttpStatus.CONFLICT, "같은 숫자의 기수가 있습니다"),
EDUCATION_EXIST(HttpStatus.CONFLICT, "이미 교육이 존재합니다"),
IMAGE_PROCESSING_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "이미지 처리에 실패했습니다."),
IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "변경할 이미지가 없습니다"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import cotato.csquiz.domain.entity.Generation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface GenerationRepository extends JpaRepository<Generation, Long> {
Optional<Generation> findByName(String name);
Optional<Generation> findByNumber(int number);
}
35 changes: 13 additions & 22 deletions src/main/java/cotato/csquiz/service/GenerationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
import cotato.csquiz.domain.dto.generation.AddGenerationResponse;
import cotato.csquiz.domain.dto.generation.ChangePeriodRequest;
import cotato.csquiz.domain.dto.generation.ChangeRecruitingRequest;
import cotato.csquiz.domain.dto.generation.GenerationInfo;
import cotato.csquiz.domain.dto.generation.GenerationInfoResponse;
import cotato.csquiz.domain.entity.Generation;
import cotato.csquiz.exception.AppException;
import cotato.csquiz.exception.ErrorCode;
import cotato.csquiz.repository.GenerationRepository;
import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -31,9 +29,9 @@ public AddGenerationResponse addGeneration(AddGenerationRequest request) {
LocalDate startDate = LocalDate.of(request.getStartYear(), request.getStartMonth(), request.getStartDay());
LocalDate endDate = LocalDate.of(request.getEndYear(), request.getEndMonth(), request.getEndDay());
checkPeriodValid(startDate, endDate);
checkNameValid(request.getGenerationName());
checkNumberValid(request.getGenerationNumber());
Generation generation = Generation.builder()
.name(request.getGenerationName())
.number(request.getGenerationNumber())
.startDate(startDate)
.endDate(endDate)
.build();
Expand All @@ -59,35 +57,28 @@ public void changePeriod(ChangePeriodRequest request) {
Generation generation = generationRepository.findById(request.getGenerationId()).orElseThrow(
() -> new AppException(ErrorCode.DATA_NOTFOUND));
generation.changePeriod(startDate, endDate);
log.info("change date "+startDate+ " " + endDate);
log.info("change date " + startDate + " " + endDate);
}

//기수 목록 알려주기
public List<GenerationInfo> getGenerations(){
public List<GenerationInfoResponse> getGenerations() {
List<Generation> generations = generationRepository.findAll();
return generations.stream()
.map(this::buildGenerationInfo).toList();
.map(GenerationInfoResponse::from)
.toList();
}

//시작 날짜가 끝나는 날짜보다 뒤면 오류 처리
private void checkPeriodValid(LocalDate startDate, LocalDate endDate) {
if(endDate.isBefore(startDate)){
if (endDate.isBefore(startDate)) {
log.info("날짜 오류");
throw new AppException(ErrorCode.DATE_INVALID);
}
}

private void checkNameValid(String generationName) {
Optional<Generation> generation = generationRepository.findByName(generationName);
if(generation.isPresent()){
throw new AppException(ErrorCode.GENERATION_NAME_EXIST);
private void checkNumberValid(int generationNumber) {
if (generationRepository.findByNumber(generationNumber).isPresent()) {
throw new AppException(ErrorCode.GENERATION_NUMBER_EXIST);
}
}

private GenerationInfo buildGenerationInfo(Generation generation) {
return GenerationInfo.builder()
.generationId(generation.getId())
.generationName(generation.getName())
.build();
}
}

0 comments on commit 0ecc90f

Please sign in to comment.