Skip to content
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
@@ -1,21 +1,25 @@
package com.example.wini.domain.member.dto.common;

import static com.example.wini.global.common.constant.StatusReserveTimeConstants.INDEFINITE_HOUR;
import static com.example.wini.global.common.constant.StatusReserveTimeConstants.INDEFINITE_MINUTE;
import static com.example.wini.global.common.constant.StatusReserveTimeConstants.INDEFINITE_SECONDS;
import static com.example.wini.global.common.constant.StatusReserveTimeConstants.SECONDS_PER_HOUR;
import static com.example.wini.global.common.constant.StatusReserveTimeConstants.SECONDS_PER_MINUTE;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import java.time.Duration;

public record ReservedTimeInfo(
@NotNull @Min(value = -1) @Max(value = 12) Long hour, @NotNull @Min(value = -1) @Max(value = 59) Long minute) {
public static ReservedTimeInfo of(long hour, long minute) {
return new ReservedTimeInfo(hour, minute);
}

public Duration toDuration() {
if (this.hour == -1 && this.minute == -1) {
return Duration.ofDays(365L * 100);
public long toSeconds() {
if (this.hour == INDEFINITE_HOUR && this.minute == INDEFINITE_MINUTE) {
return INDEFINITE_SECONDS;
}

return Duration.ofHours(this.hour).plusMinutes(this.minute);
return this.hour * SECONDS_PER_HOUR + this.minute * SECONDS_PER_MINUTE;
}
Comment on lines +19 to 24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

무기한(-1/-1) 조합 불일치 시 음수 초 산출 가능성 — 방어 필요.

hour=-1, minute=30 같은 케이스에서 음수 초가 계산됩니다. 무기한은 양쪽이 동시에 -1이어야만 유효하도록 강제하세요(레코드 compact 생성자 사용 권장).

 public record ReservedTimeInfo(
         @NotNull @Min(value = -1) @Max(value = 12) Long hour, @NotNull @Min(value = -1) @Max(value = 59) Long minute) {
+    public ReservedTimeInfo {
+        boolean hourIndef = hour != null && hour == INDEFINITE_HOUR;
+        boolean minIndef  = minute != null && minute == INDEFINITE_MINUTE;
+        if (hourIndef ^ minIndef) {
+            throw new IllegalArgumentException("Indefinite must be (-1, -1).");
+        }
+        if (!hourIndef) {
+            if (hour < 0 || hour > 12 || minute < 0 || minute > 59) {
+                throw new IllegalArgumentException("hour/minute out of range.");
+            }
+        }
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public long toSeconds() {
if (this.hour == INDEFINITE_HOUR && this.minute == INDEFINITE_MINUTE) {
return INDEFINITE_SECONDS;
}
return Duration.ofHours(this.hour).plusMinutes(this.minute);
return this.hour * SECONDS_PER_HOUR + this.minute * SECONDS_PER_MINUTE;
}
public record ReservedTimeInfo(
@NotNull @Min(value = -1) @Max(value = 12) Long hour,
@NotNull @Min(value = -1) @Max(value = 59) Long minute) {
// Enforce that “indefinite” is only allowed when both hour and minute are –1,
// otherwise both must be within their respective valid ranges.
public ReservedTimeInfo {
boolean hourIndef = hour != null && hour == INDEFINITE_HOUR;
boolean minIndef = minute != null && minute == INDEFINITE_MINUTE;
if (hourIndef ^ minIndef) {
throw new IllegalArgumentException("Indefinite must be (-1, -1).");
}
if (!hourIndef) {
if (hour < 0 || hour > 12 || minute < 0 || minute > 59) {
throw new IllegalArgumentException("hour/minute out of range.");
}
}
}
public long toSeconds() {
if (this.hour == INDEFINITE_HOUR && this.minute == INDEFINITE_MINUTE) {
return INDEFINITE_SECONDS;
}
return this.hour * SECONDS_PER_HOUR + this.minute * SECONDS_PER_MINUTE;
}
}
🤖 Prompt for AI Agents
In src/main/java/com/example/wini/domain/member/dto/common/ReservedTimeInfo.java
around lines 19 to 24, the toSeconds() method can produce negative results when
only one field is INDEFINITE (e.g. hour = -1, minute = 30); change the logic to
treat indefinite only when both hour and minute equal INDEFINITE constants, and
defensively validate the fields otherwise: if one is INDEFINITE and the other is
not, throw an IllegalArgumentException (or IllegalStateException) to prevent
computing negative seconds, and ensure normal path validates non-negative
hour/minute before computing seconds; additionally enforce this invariant in the
record compact constructor so instances cannot be created with a single -1.

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.wini.domain.member.service;

import static com.example.wini.global.common.constant.StatusReserveTimeConstants.INDEFINITE_HOUR;
import static com.example.wini.global.common.constant.StatusReserveTimeConstants.INDEFINITE_MINUTE;
import static com.example.wini.global.common.constant.StatusReserveTimeConstants.INDEFINITE_SECONDS;
import static com.example.wini.global.error.exception.ErrorCode.MATE_NOT_FOUND;
import static com.example.wini.global.error.exception.ErrorCode.STATUS_NOT_FOUND;

Expand Down Expand Up @@ -72,6 +75,10 @@ private boolean isStatusValid(Member member) {
}

private ReservedTimeInfo createReservedTimeInfo(long durationSeconds) {
if (durationSeconds == INDEFINITE_SECONDS) {
return ReservedTimeInfo.of(INDEFINITE_HOUR, INDEFINITE_MINUTE);
}

Duration duration = Duration.ofSeconds(durationSeconds);
return ReservedTimeInfo.of(duration.toHours(), duration.toMinutes() % 60);
}
Expand All @@ -83,8 +90,7 @@ public MemberStatusResponse updateStatus(MemberStatusUpdateRequest request) {
Status status =
statusRepository.findById(request.statusId()).orElseThrow(() -> new CustomException(STATUS_NOT_FOUND));

Duration statusDuration = request.reservedTimeInfo().toDuration();
Long statusDurationSeconds = statusDuration.getSeconds();
Long statusDurationSeconds = request.reservedTimeInfo().toSeconds();

member.updateStatus(status, request.startedAt(), statusDurationSeconds);
notifyRoommateOfStatusUpdate(member.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.wini.global.common.constant;

public class StatusReserveTimeConstants {

public static final int SECONDS_PER_HOUR = 3600;
public static final int SECONDS_PER_MINUTE = 60;

public static final long INDEFINITE_SECONDS = 365L * 100 * 24 * 60 * 60;
public static final long INDEFINITE_HOUR = -1L;
public static final long INDEFINITE_MINUTE = -1L;

private StatusReserveTimeConstants() {}
}
Loading