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

feat: laps 필드 0.5 단위 입력 가능하도록 데이터 타입 수정 #123

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public class Stroke {
private Long id;
private Memory memory;
private String name;
private Short laps;
private Float laps;
ywonchae1 marked this conversation as resolved.
Show resolved Hide resolved
private Integer meter;

@Builder
public Stroke(Long id, Memory memory, String name, Short laps, Integer meter) {
public Stroke(Long id, Memory memory, String name, Float laps, Integer meter) {
this.id = id;
this.memory = memory;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.depromeet.memory.port.in.command;

public record CreateStrokeCommand(String name, Short laps, Integer meter) {}
public record CreateStrokeCommand(String name, Float laps, Integer meter) {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.depromeet.memory.port.in.command;

public record UpdateStrokeCommand(Long id, String name, Short laps, Integer meter) {}
public record UpdateStrokeCommand(Long id, String name, Float laps, Integer meter) {}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public Memory save(Member writer, CreateMemoryCommand command) {
if (memoryDetail != null) {
memoryDetail = memoryDetailPersistencePort.save(memoryDetail);
}
Pool pool = poolPersistencePort.findById(command.poolId()).orElse(null);

Pool pool = null;
if (command.poolId() != null) {
pool = poolPersistencePort.findById(command.poolId()).orElse(null);
}
Memory memory =
Memory.builder()
.member(writer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ void init() {
void 회원은_영법을_수정할_수_있다() {
// given
UpdateStrokeCommand firstCommand =
new UpdateStrokeCommand((Long) null, "자유형", (short) 4, null);
new UpdateStrokeCommand((Long) null, "자유형", 4F, null);
UpdateStrokeCommand secondCommand =
new UpdateStrokeCommand((Long) null, "접영", (short) 2, null);
new UpdateStrokeCommand((Long) null, "접영", 2F, null);

List<UpdateStrokeCommand> commands = List.of(firstCommand, secondCommand);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void init() {
// given
List<CreateStrokeCommand> commands = new ArrayList<>();
for (int i = 0; i < STROKE_NAME_LIST.size(); i++) {
Short laps = 2;
Float laps = 2F;
commands.add(new CreateStrokeCommand(STROKE_NAME_LIST.get(i), laps, 50));
}
Memory memory =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ List<Stroke> saveMeterStroke() {

List<Stroke> saveLapsStroke() {
List<CreateStrokeCommand> commands = new ArrayList<>();
Short laps = 2;
Float laps = 2F;
for (int i = 0; i < STROKE_NAME_LIST.size(); i++) {
commands.add(new CreateStrokeCommand(STROKE_NAME_LIST.get(i), laps, null));
expectedTotalMeter += laps * lane;
expectedTotalMeter += (int) (laps * 2) * lane;
}

return strokeService.saveAll(memory, commands);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public class StrokeEntity {

private String name;

private Short laps;
private Float laps;

private Integer meter;

@Builder
public StrokeEntity(Long id, MemoryEntity memory, String name, Short laps, Integer meter) {
public StrokeEntity(Long id, MemoryEntity memory, String name, Float laps, Integer meter) {
this.id = id;
this.memory = memory;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class StrokeFixture {
public static Stroke mockLapsStroke(Memory memory) {
return Stroke.builder().memory(memory).name("자유형").laps((short) 5).build();
return Stroke.builder().memory(memory).name("자유형").laps(5F).build();
}

public static Stroke mockMeterStroke(Memory memory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.depromeet.memory.annotation;

import com.depromeet.memory.validator.HalfFloatValidator;
import jakarta.validation.Constraint;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = HalfFloatValidator.class)
public @interface HalfFloatCheck {
String message() default "바퀴 수는 0.5 단위로만 입력 가능합니다";

Class[] groups() default {};

Class[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.depromeet.memory.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalTime;
Expand Down Expand Up @@ -51,6 +52,7 @@ public class MemoryCreateRequest {
private String diary;

// Stroke
@Valid
@Schema(description = "영법 목록")
private List<StrokeCreateRequest> strokes;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.depromeet.memory.dto.request;

import com.depromeet.memory.annotation.HalfFloatCheck;
import io.swagger.v3.oas.annotations.media.Schema;

public record StrokeCreateRequest(
@Schema(description = "영법 이름", example = "자유형") String name,
@Schema(description = "바퀴 수", example = "3") Short laps,
@HalfFloatCheck @Schema(description = "바퀴 수", example = "3.5") Float laps,
@Schema(description = "미터", example = "150") Integer meter) {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.depromeet.memory.dto.request;

import com.depromeet.memory.annotation.HalfFloatCheck;
import io.swagger.v3.oas.annotations.media.Schema;

public record StrokeUpdateRequest(
@Schema(description = "영법 Id", example = "1") Long id,
@Schema(description = "영법 이름", example = "자유형") String name,
@Schema(description = "바퀴 수", example = "3") Short laps,
@HalfFloatCheck @Schema(description = "바퀴 수", example = "3.5") Float laps,
@Schema(description = "미터", example = "150") Integer meter) {}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static List<StrokeResponse> getStrokeResponses(Memory memoryDomain) {
.name(it.getName())
.meter(
it.getMeter() == null
? it.getLaps()
? (int) (it.getLaps() * 2)
* memoryDomain.getPool().getLane()
: it.getMeter())
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class MemoryResponse {
private LocalTime endTime;
private LocalTime duration;
private Short lane;
private Integer totalLap;
private Float totalLap;
private Integer totalMeter;
private String diary;

Expand All @@ -51,7 +51,7 @@ public MemoryResponse(
List<StrokeResponse> resultStrokes = getResultStrokes(strokes, lane);

// 기록 전체 바퀴 수와 미터 수를 계산
Integer totalLap = 0;
Float totalLap = 0F;
Integer totalMeter = 0;
for (StrokeResponse stroke : resultStrokes) {
totalLap += stroke.laps();
Expand Down Expand Up @@ -99,13 +99,13 @@ private static List<StrokeResponse> getResultStrokes(List<Stroke> strokes, Short
.strokeId(stroke.getId())
.name(stroke.getName())
.laps(stroke.getLaps())
.meter(stroke.getLaps() * lane)
.meter((short) (stroke.getLaps() * 2) * lane)
.build();
} else {
return StrokeResponse.builder()
.strokeId(stroke.getId())
.name(stroke.getName())
.laps((short) (stroke.getMeter() / lane))
.laps((float) stroke.getMeter() / (lane * 2))
.meter(stroke.getMeter())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Builder;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record StrokeResponse(Long strokeId, String name, Short laps, Integer meter) {
public record StrokeResponse(Long strokeId, String name, Float laps, Integer meter) {
@Builder
public StrokeResponse {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private static Long getMemoryDetailId(Memory memory) {
: null;
}

private static Short getLapsFromStroke(Stroke stroke) {
private static Float getLapsFromStroke(Stroke stroke) {
return stroke.getLaps() != null ? stroke.getLaps() : null;
}

Expand Down Expand Up @@ -126,7 +126,7 @@ private static Integer calculateTotalMeter(List<Stroke> strokes, Short lane) {
totalMeter += stroke.getMeter();
} else {
if (lane != null) {
totalMeter += (int) stroke.getLaps() * lane;
totalMeter += (int) (stroke.getLaps() * 2) * lane;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public void create(Long memberId, MemoryCreateRequest request) {
imageUploadUseCase.changeImageStatusAndAddMemoryIdToImages(
newMemory, request.getImageIdList());

poolSearchLogUseCase.createSearchLog(writer, request.getPoolId());
if (request.getPoolId() != null) {
poolSearchLogUseCase.createSearchLog(writer, request.getPoolId());
}
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.depromeet.memory.validator;

import com.depromeet.memory.annotation.HalfFloatCheck;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class HalfFloatValidator implements ConstraintValidator<HalfFloatCheck, Float> {
@Override
public boolean isValid(Float value, ConstraintValidatorContext constraintValidatorContext) {
if (value == null) {
return false;
}
return value % 0.5 == 0F;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,45 @@ public class MemoryControllerTest extends ControllerTestConfig {
.andDo(print());
}

@Test
@WithCustomMockMember
void 소수점_바퀴_수는_5만_가능합니다() throws Exception {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("poolId", 1);
requestBody.put("item", "오리발");
requestBody.put("heartRate", 115);
requestBody.put("pace", "05:00:00");
requestBody.put("kcal", 300);
requestBody.put("recordAt", "2024-07-24");
requestBody.put("startTime", "11:00:00");
requestBody.put("endTime", "11:50:00");
requestBody.put("lane", 25);
requestBody.put("diary", "일기를 기록한다");

List<Map<String, Object>> strokes = new ArrayList<>();
Map<String, Object> stroke = new HashMap<>();
stroke.put("name", "자유형");
stroke.put("laps", 3.3); // Validation Error
stroke.put("meter", 150);
strokes.add(stroke);

requestBody.put("strokes", strokes);

List<Long> imageIdList = new ArrayList<>();
requestBody.put("imageIdList", imageIdList);

mockMvc.perform(
post("/memory")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("COMMON_1"))
.andExpect(jsonPath("$.message").value("입력 값 검증에 실패하였습니다"))
.andExpect(
jsonPath("$.data.fieldErrors[0].reason").value("바퀴 수는 0.5 단위로만 입력 가능합니다"))
.andDo(print());
}

@Test
@WithCustomMockMember
public void 수영기록을_조회합니다() throws Exception {
Expand Down
Loading