Skip to content

Commit

Permalink
Merge pull request #140 from Team-B1ND/fix/#139
Browse files Browse the repository at this point in the history
Fix/#139
  • Loading branch information
GayeongKimm authored Nov 22, 2024
2 parents 631ce02 + 28ed191 commit 44c07a4
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dodam-application/dodam-rest-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ dependencies {
implementation project(':dodam-in-system-available:dodam-token-client')
implementation project(':dodam-in-system-available:dodam-youtube-video-client')
implementation project(':dodam-in-system-available:dodam-firebase-client')
implementation project(':dodam-in-system-available:dodam-simple-storage-service-client')

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-validation'

implementation 'org.springframework:spring-tx:6.0.6'
implementation project(path: ':dodam-in-system-available:dodam-simple-storage-service-client')

runtimeOnly 'io.micrometer:micrometer-registry-prometheus'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Component
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
Expand All @@ -24,6 +26,52 @@ public class BusApplicationUseCase {
private final StudentRepository studentRepository;
private final MemberAuthenticationHolder memberAuthenticationHolder;

public Response modifyStatus(int busId) {
Student student = studentRepository.getByMember(memberAuthenticationHolder.current());
Optional<BusApplication> busApplication = busApplicationRepository.findByStudentAndBus_LeaveTimeAfter(student, ZonedDateTimeUtil.nowToLocalDateTime());

return busApplication
.map(application -> handleExistingApplication(busId, application))
.orElseGet(() -> applyForNewBus(busId, student));
}

private Response handleExistingApplication(int busId, BusApplication currentApplication) {
return currentApplication.getBus().getId() == busId
? cancelCurrentApplication(currentApplication)
: updateToNewBus(busId, currentApplication);
}

private Response applyForNewBus(int busId, Student student) {
Bus bus = adjustApplicationCount(busId, true);
busApplicationRepository.save(
BusApplication.builder()
.bus(bus)
.student(student)
.build()
);
return Response.created("버스 신청 성공");
}

private Response updateToNewBus(int busId, BusApplication currentApplication) {
cancelCurrentApplication(currentApplication);
Bus newBus = adjustApplicationCount(busId, true);
currentApplication.updateBus(newBus);
return Response.noContent("버스 신청 수정 성공");
}

private Response cancelCurrentApplication(BusApplication currentApplication) {
adjustApplicationCount(currentApplication.getBus().getId(), false);
busApplicationRepository.delete(currentApplication);
return Response.noContent("버스 신청 취소 성공");
}

private Bus adjustApplicationCount(int busId, boolean increment) {
Bus bus = busRepository.getByIdForUpdate(busId);
if (increment) bus.increaseApplyCount();
else bus.decreaseApplyCount();
return bus;
}

public Response apply(int busId) {
Student student = studentRepository.getByMember(memberAuthenticationHolder.current());
checkIfTheApplicationExists(student);
Expand Down Expand Up @@ -71,4 +119,4 @@ private void decreaseApplicationCount(int id) {
bus.decreaseApplyCount();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public ResponseData<List<BusRes>> getByDate(
return busUseCase.getByDate(year, month, day);
}

@PatchMapping("/apply/status/{id}")
public Response modifyStatus(@PathVariable int id){
return busApplicationUseCase.modifyStatus(id);
}

@GetMapping("/apply")
public ResponseData<Bus> getMy() {
return busUseCase.getMy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import b1nd.dodam.restapi.member.application.data.req.*;
import b1nd.dodam.restapi.support.data.Response;
import b1nd.dodam.restapi.support.encrypt.Sha512PasswordEncoder;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.ApplicationEventPublisher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void send(Exception e, RequestInfo request) {
+ "```\n"
+ getStackTrace(e)
+ "\n```";

discordWebHookClient.notice("", title, description);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;
import java.util.function.Consumer;

@Getter
@Entity(name = "bus")
Expand Down Expand Up @@ -56,21 +57,20 @@ public Bus(String busName, String description, int peopleLimit, LocalDateTime le

public void updateBus(String busName, String description, LocalDateTime leaveTime, LocalTime timeRequired, int peopleLimit) {
checkIfTheBusHasDeparted();
if(Objects.nonNull(busName)) {
this.busName = busName;
}
if(Objects.nonNull(description)) {
this.description = description;
}
if(Objects.nonNull(leaveTime)) {
this.leaveTime = leaveTime;
}
if(Objects.nonNull(timeRequired)) {
this.timeRequired = timeRequired;
}

updateApplyIfNotEmpty(busName, value -> this.busName = value);
updateApplyIfNotEmpty(description, value -> this.description = value);
updateApplyIfNotEmpty(leaveTime, value -> this.leaveTime = value);
updateApplyIfNotEmpty(timeRequired, value -> this.timeRequired = value);
this.peopleLimit = peopleLimit;
}

private <T> void updateApplyIfNotEmpty(T newValue, Consumer<T> parameter) {
if (newValue instanceof String ? StringUtils.isNotEmpty((String) newValue) : newValue != null) {
parameter.accept(newValue);
}
}

public void increaseApplyCount() {
checkIfTheBusHasDeparted();
checkIfThereAreSeatsAvailable();
Expand All @@ -79,7 +79,6 @@ public void increaseApplyCount() {

public void decreaseApplyCount() {
checkIfTheBusHasDeparted();
checkIfThereAreSeatsAvailable();
this.applyCount -= 1;
}

Expand Down

0 comments on commit 44c07a4

Please sign in to comment.