Skip to content

Commit

Permalink
🔀 :: (#157) 버그 해결
Browse files Browse the repository at this point in the history
🔀 :: (#157) 버그 해결
  • Loading branch information
leeseojune53 authored Jun 11, 2021
2 parents 7bd00ae + 96e7d08 commit 900fb70
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum ErrorCode {

GSPASS_ALREADY_APPLY(400, "GsPass Already Apply."),
GSPASS_NOT_FOUND(404, "GsPass Not Found."),
NOT_GSPASS_APPLY_TIME(400, "Not GsPass Apply Time"),
NOT_GSPASS_APPLY_TIME(400, "Not GsPass Apply Time."),
GSPASS_ALREADY_USE(400, "GsPass Already Use."),

SCHOOL_NOT_FOUND(404, "School Not Found."),
GRADE_NOT_FOUND(404, "Grade Not Found."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.semicolon.gspass.exception;

import com.semicolon.gspass.error.exception.ErrorCode;
import com.semicolon.gspass.error.exception.GsException;

public class GsPassAlreadyUseException extends GsException {

public GsPassAlreadyUseException() {
super(ErrorCode.GSPASS_ALREADY_USE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface UserFacade {
School findByRandomCode(String randomCode);
School findById(Integer id);
GsPass save(GsPass gsPass);
void useAndSave(GsPass gsPass);
Optional<Grade> findByIdAndSchool(int id, School school);
int unUsedPassCount(Grade grade, int id);
int PassCount(Grade grade, int id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public GsPass save(GsPass gsPass) {
return gsPassRepository.save(gsPass);
}

@Override
public void useAndSave(GsPass gsPass) {
gsPassRepository.save(gsPass.use());
}

@Override
public Optional<Grade> findByIdAndSchool(int id, School school) {
return gradeRepository.findByIdAndSchool(id, school);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -156,24 +155,28 @@ public GsPassResponse getPassInfo() {
if(gsPass.isUsed()) throw new GsPassNotFoundException();
int count = userFacade.unUsedPassCount(grade, gsPass.getId());
int allCount = userFacade.PassCount(grade, gsPass.getId());
if (school.getDinnerPeriod() != null && grade.getDinner() != null && school.getDinnerPeriod().toLocalTime().isBefore(LocalTime.now())) {

if (school.getDinnerPeriod() != null && grade.getDinner() != null &&
(school.getDinnerPeriod().toLocalTime().isBefore(LocalTime.now()) ||
grade.getDinner().toLocalTime().plusHours(1).isAfter(LocalTime.now()))) {

Duration duration = Duration.between(LocalTime.now(), grade.getDinner().toLocalTime().plusSeconds(5 * (allCount+1)));
int hours = (int)duration.getSeconds() / 3600;
int minutes = (int)(duration.getSeconds() % 3600) / 60;
int seconds = (int)duration.getSeconds() % 60;
return new GsPassResponse(count, LocalTime.of(hours, minutes, seconds));
} else if (school.getLunchPeriod() != null && grade.getDinner() != null && school.getLunchPeriod().toLocalTime().isBefore(LocalTime.now())) {
return calculate(duration, count);

} else if (school.getLunchPeriod() != null && grade.getDinner() != null &&
school.getLunchPeriod().toLocalTime().isBefore(LocalTime.now()) ||
grade.getLunch().toLocalTime().plusHours(1).isAfter(LocalTime.now())) {

Duration duration = Duration.between(LocalTime.now(), grade.getLunch().toLocalTime().plusSeconds(5 * (allCount+1)));
int hours = (int)duration.getSeconds() / 3600;
int minutes = (int)(duration.getSeconds() % 3600) / 60;
int seconds = (int)duration.getSeconds() % 60;
return new GsPassResponse(count, LocalTime.of(hours, minutes, seconds));
} else if (school.getBreakfastPeriod() != null && grade.getDinner() != null && school.getBreakfastPeriod().toLocalTime().isBefore(LocalTime.now())) {
return calculate(duration, count);

} else if (school.getBreakfastPeriod() != null && grade.getDinner() != null &&
school.getBreakfastPeriod().toLocalTime().isBefore(LocalTime.now()) ||
grade.getBreakfast().toLocalTime().plusHours(1).isAfter(LocalTime.now())) {

Duration duration = Duration.between(LocalTime.now(), grade.getBreakfast().toLocalTime().plusSeconds(5 * (allCount+1)));
int hours = (int)duration.getSeconds() / 3600;
int minutes = (int)(duration.getSeconds() % 3600) / 60;
int seconds = (int)duration.getSeconds() % 60;
return new GsPassResponse(count, LocalTime.of(hours, minutes, seconds));
return calculate(duration, count);

} else throw new GsPassNotFoundException();
}

Expand Down Expand Up @@ -202,7 +205,10 @@ public void useGsPass() {
GsPass gsPass = userFacade.findByUser(user)
.orElseThrow(GsPassNotFoundException::new);

userFacade.save(gsPass.use());
if(gsPass.isUsed())
throw new GsPassAlreadyUseException();

userFacade.useAndSave(gsPass);
}

private TokenResponse generateToken(String id) {
Expand All @@ -223,4 +229,11 @@ public void deleteGsPass() {
userFacade.deleteAll();
}

private GsPassResponse calculate(Duration duration, int count) {
int hours = (int)duration.getSeconds() / 3600;
int minutes = (int)(duration.getSeconds() % 3600) / 60;
int seconds = (int)duration.getSeconds() % 60;
return new GsPassResponse(count, LocalTime.of(hours, minutes, seconds));
}

}

0 comments on commit 900fb70

Please sign in to comment.