Skip to content

Commit

Permalink
single 엔티티에 RunningTime 추가 (#57)
Browse files Browse the repository at this point in the history
클라이언트에서 싱글모드 정지, 실행 기능이 추가되면서 runningTIme을 저장해야했습니다.
따라서 single 엔티티에 runningTime을 추가했습니다.

직전의 직렬화 이슈 말고는 크게 다룰 내용은 없습니다.
  • Loading branch information
seong-wooo authored Sep 13, 2023
1 parent a5fa53e commit e9e206a
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package online.partyrun.partyrunbattleservice.domain.single.dto;

import online.partyrun.partyrunbattleservice.domain.single.entity.RunningTime;

public record RunningTimeRequest(int hours, int minutes, int seconds) {

public RunningTime toRunningTime() {
return new RunningTime(hours, minutes, seconds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

import jakarta.validation.constraints.NotNull;
import online.partyrun.partyrunbattleservice.domain.runner.entity.record.RunnerRecord;
import online.partyrun.partyrunbattleservice.domain.single.entity.RunningTime;

import java.util.List;

public record SingleRunnerRecordsRequest(@NotNull List<SingleRunnerRecordRequest> records) {
public List<RunnerRecord> toRunnerRecords() {
public record SingleRunnerRecordsRequest(
@NotNull RunningTimeRequest runningTime,
@NotNull List<SingleRunnerRecordRequest> records) {

public RunningTime getRunningTime() {
return runningTime.toRunningTime();
}

public List<RunnerRecord> runnerRecords() {
return this.records.stream()
.map(SingleRunnerRecordRequest::toRunnerRecord)
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package online.partyrun.partyrunbattleservice.domain.single.entity;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;
import online.partyrun.partyrunbattleservice.domain.single.exception.IllegalRunningTimeException;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class RunningTime {

private static final int MIN_TIME = 0;
private static final int MAX_TIME = 59;

int hours;
int minutes;
int seconds;

public RunningTime(int hours, int minutes, int seconds) {
validateNoTime(hours, minutes, seconds);
validateIsCorrectTime(hours, minutes, seconds);


this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}

private void validateNoTime(int hours, int minutes, int seconds) {
if (hours == MIN_TIME && minutes == MIN_TIME && seconds == MIN_TIME) {
throw new IllegalRunningTimeException(hours, minutes, seconds);
}
}

private void validateIsCorrectTime(int hours, int minutes, int seconds) {
if (isNotCorrectHours(hours) || isNotCorrectMinutes(minutes) || isNotCorrectSeconds(seconds)) {
throw new IllegalRunningTimeException(hours, minutes, seconds);
}
}

private boolean isNotCorrectHours(int hours) {
return MIN_TIME > hours;
}

private boolean isNotCorrectMinutes(int minutes) {
return minutes < MIN_TIME || minutes > MAX_TIME;
}

private boolean isNotCorrectSeconds(int seconds) {
return seconds < MIN_TIME || seconds > MAX_TIME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public class Single {
@Id
String id;
String runnerId;
RunningTime runningTime;
List<RunnerRecord> runnerRecords;

public Single(String runnerId, List<RunnerRecord> runnerRecords) {
public Single(String runnerId, RunningTime runningTime, List<RunnerRecord> runnerRecords) {
validateRunnerRecords(runnerRecords);
this.runnerId = runnerId;
this.runningTime = runningTime;
this.runnerRecords = runnerRecords;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package online.partyrun.partyrunbattleservice.domain.single.exception;

import online.partyrun.partyrunbattleservice.global.exception.BadRequestException;

public class IllegalRunningTimeException extends BadRequestException {

public IllegalRunningTimeException(int hours, int minutes, int seconds) {
super(String.format("RunningTime은 %d시 %d분 %d초 일 수 없습니다.", hours, minutes, seconds));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import online.partyrun.partyrunbattleservice.domain.runner.entity.record.RunnerRecord;
import online.partyrun.partyrunbattleservice.domain.single.dto.SingleIdResponse;
import online.partyrun.partyrunbattleservice.domain.single.dto.SingleRunnerRecordsRequest;
import online.partyrun.partyrunbattleservice.domain.single.entity.RunningTime;
import online.partyrun.partyrunbattleservice.domain.single.entity.Single;
import online.partyrun.partyrunbattleservice.domain.single.repository.SingleRepository;
import org.springframework.stereotype.Service;
Expand All @@ -19,8 +20,9 @@ public class SingleService {
SingleRepository singleRepository;

public SingleIdResponse create(String runnerId, SingleRunnerRecordsRequest request) {
final List<RunnerRecord> records = request.toRunnerRecords();
final Single newSingleRecord = singleRepository.save(new Single(runnerId, records));
final RunningTime runningTime = request.getRunningTime();
final List<RunnerRecord> records = request.runnerRecords();
final Single newSingleRecord = singleRepository.save(new Single(runnerId, runningTime, records));
return new SingleIdResponse(newSingleRecord.getId());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package online.partyrun.partyrunbattleservice.domain.single.controller;

import online.partyrun.partyrunbattleservice.domain.single.dto.RunningTimeRequest;
import online.partyrun.partyrunbattleservice.domain.single.dto.SingleIdResponse;
import online.partyrun.partyrunbattleservice.domain.single.dto.SingleRunnerRecordRequest;
import online.partyrun.partyrunbattleservice.domain.single.dto.SingleRunnerRecordsRequest;
Expand Down Expand Up @@ -43,6 +44,7 @@ class 러너의_기록이_주어지면 {
@DisplayName("싱글을_생성한다")
void createSingle() throws Exception {
SingleRunnerRecordsRequest request = new SingleRunnerRecordsRequest(
new RunningTimeRequest(0,0,1),
List.of(
new SingleRunnerRecordRequest(0, 0, 0, now, 0),
new SingleRunnerRecordRequest(0.0001, 0.0001, 0.0001, now, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package online.partyrun.partyrunbattleservice.domain.single.entity;

import online.partyrun.partyrunbattleservice.domain.single.exception.IllegalRunningTimeException;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@DisplayName("RunningTime")
class RunningTimeTest {

@Nested
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class RunningTime을_생성할_때 {

@ParameterizedTest
@DisplayName("잘못된 시간이 들어오면 예외를 던진다.")
@CsvSource(value = {"-1,1,1", "1,-1,1", "1,60,1", "1,59,-1", "1,59,60","0,0,0"})
void throwExceptionForCorrectTime(int hours, int minutes, int seconds) {
assertThatThrownBy(() -> new RunningTime(hours, minutes, seconds))
.isInstanceOf(IllegalRunningTimeException.class);
}

@ParameterizedTest
@DisplayName("올바른 시간이 들어오면 RunningTime을 생성한다.")
@CsvSource(value = {"0,0,1", "0,1,0", "1,0,0"})
void create(int hours, int minutes, int seconds) {
assertThatCode(() -> new RunningTime(hours, minutes, seconds))
.doesNotThrowAnyException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Single_기록을_생성할_때 {
@NullAndEmptySource
@ParameterizedTest
void throwException(List<RunnerRecord> records) {
assertThatThrownBy(() -> new Single("runnerId", records))
assertThatThrownBy(() -> new Single("runnerId", new RunningTime(1, 1, 1), records))
.isInstanceOf(SingleRunnerRecordEmptyException.class);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package online.partyrun.partyrunbattleservice.domain.single.service;

import online.partyrun.partyrunbattleservice.domain.single.dto.RunningTimeRequest;
import online.partyrun.partyrunbattleservice.domain.single.dto.SingleIdResponse;
import online.partyrun.partyrunbattleservice.domain.single.dto.SingleRunnerRecordRequest;
import online.partyrun.partyrunbattleservice.domain.single.dto.SingleRunnerRecordsRequest;
Expand Down Expand Up @@ -31,6 +32,7 @@ void create() {
final SingleIdResponse response = singleService.create(
runnerId,
new SingleRunnerRecordsRequest(
new RunningTimeRequest(1,1,1),
List.of(
new SingleRunnerRecordRequest(0, 0, 0, now, 0),
new SingleRunnerRecordRequest(0.0001, 0.0001, 0.0001, now, 1)
Expand Down

0 comments on commit e9e206a

Please sign in to comment.