Skip to content

conventions Coding Guide

Kimgyuilli edited this page Jan 15, 2026 · 2 revisions

코딩 가이드

목차

1. DTO 작성 규칙

Request/Response 모두 Java Record 사용

Request DTO

@Schema(description = "챌린지 생성 요청")
public record ChallengeCreateRequestDto(
	@Schema(description = "홈케어 루틴 ID (1-6)", example = "1", requiredMode = RequiredMode.REQUIRED)
	@NotNull(message = "홈케어 루틴 ID는 필수입니다")
	Integer homecareRoutineId,

	@Schema(description = "챌린지 제목", example = "7일 챌린지", requiredMode = RequiredMode.REQUIRED)
	@NotBlank(message = "챌린지 제목은 필수입니다")
	@Size(max = 100, message = "챌린지 제목은 100자를 초과할 수 없습니다")
	String title,

	@Schema(description = "루틴명 리스트 (1-10개, 각 100자 이하)",
		example = "[\"아침 세안\", \"토너 바르기\", \"크림 바르기\"]",
		requiredMode = RequiredMode.REQUIRED)
	@NotNull(message = "루틴명 리스트는 필수입니다")
	@Size(min = 1, max = 10, message = "루틴은 1개 이상 10개 이하여야 합니다")
	List<@NotBlank(message = "루틴명은 필수입니다")
	@Size(max = 100, message = "루틴명은 100자를 초과할 수 없습니다")
		String> routineNames
) {
}

Request DTO 규칙

  • Java Record 사용: 불변 객체, 간결한 코드
  • @Schema: Swagger 문서화 (description, example, requiredMode)
  • Validation 어노테이션: @NotBlank, @NotNull, @Size
  • 컬렉션 요소 검증: List<@NotBlank ... String> 형태로 요소별 검증

Response DTO

@Schema(description = "챌린지 루틴 응답")
public record ChallengeRoutineResponseDto(
	@Schema(description = "루틴 ID", example = "1")
	Long routineId,

	@Schema(description = "루틴명", example = "아침 세안")
	String name,

	@Schema(description = "예정일", example = "2025-01-05")
	@JsonFormat(pattern = "yyyy-MM-dd")
	LocalDate scheduledDate,

	@Schema(description = "완료 여부", example = "false")
	Boolean isComplete
) {
	// Entity -> DTO 변환 (정적 팩토리 메서드)
	public static ChallengeRoutineResponseDto from(ChallengeRoutine routine) {
		return new ChallengeRoutineResponseDto(
			routine.getId(),
			routine.getName(),
			routine.getScheduledDate(),
			routine.getIsComplete()
		);
	}
}

Response DTO 규칙

  • Java Record 사용: 불변 객체, Getter 자동 생성
  • @Schema: Swagger 문서화
  • from(Entity) 정적 팩토리 메서드: Entity → DTO 변환

중첩 Response DTO 예시

@Schema(description = "챌린지 상세 조회 응답")
public record ChallengeDetailResponseDto(
	@Schema(description = "챌린지 ID", example = "1")
	Long challengeId,

	@Schema(description = "챌린지 제목", example = "7일 보습 챌린지")
	String title,

	@Schema(description = "현재 일차", example = "3")
	int currentDay,

	@Schema(description = "전체 진행률 (%)", example = "37.5")
	double progressPercentage,

	@Schema(description = "오늘의 루틴 리스트")
	List<ChallengeRoutineResponseDto> todayRoutines,

	@Schema(description = "응원 메시지", example = "오늘도 힘내봐요!")
	String cheeringMessage
) {
	public static ChallengeDetailResponseDto from(
		Challenge challenge,
		int currentDay,
		ChallengeStatistics statistics,
		List<ChallengeRoutine> todayRoutines,
		String cheeringMessage
	) {
		List<ChallengeRoutineResponseDto> routineDtos = todayRoutines.stream()
			.map(ChallengeRoutineResponseDto::from)
			.toList();

		return new ChallengeDetailResponseDto(
			challenge.getId(),
			challenge.getTitle(),
			currentDay,
			statistics.getProgressPercentage(),
			routineDtos,
			cheeringMessage
		);
	}
}

DTO 네이밍 규칙

종류 네이밍 패턴 예시
Request {기능}RequestDto ChallengeCreateRequestDto, CustomRoutineAddRequestDto
Response {기능}ResponseDto ChallengeDetailResponseDto, ChallengeRoutineResponseDto

DTO 패키지 구조

domain/{도메인}/{서브도메인}/presentation/dto/
├── request/
│   └── {기능}RequestDto.java
└── response/
    └── {기능}ResponseDto.java

2. Entity 작성 규칙

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class User extends BaseTimeEntity {

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

    @Column(nullable = false)
    private String name;

    @Column(nullable = false, unique = true)
    private String email;

    @Builder
    private User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    // 비즈니스 로직은 Entity 내부에 작성
    public void updateEmail(String newEmail) {
        this.email = newEmail;
    }
}

Entity 규칙

  • @Setter 사용 금지 (불변성 보장)
  • @Getter 는 쓸 때 한번 고민해보기!
  • 생성자는 @Builder 사용
  • 비즈니스 로직은 Entity 내부에 작성

3. Configuration 작성

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // 설정 내용
        return http.build();
    }
}
  • @Configuration 클래스는 global/config 패키지에 위치
  • 각 설정은 목적별로 분리 (Security, Database, Redis 등)

Clone this wiki locally