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

Queue join timestamp #185

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -3,13 +3,15 @@
import lombok.RequiredArgsConstructor;
import me.simplq.controller.model.token.CreateTokenRequest;
import me.simplq.controller.model.token.MyTokensResponse;
import me.simplq.controller.model.token.PatchTokenRequest;
import me.simplq.controller.model.token.TokenDeleteResponse;
import me.simplq.controller.model.token.TokenDetailResponse;
import me.simplq.controller.model.token.TokenNotifyResponse;
import me.simplq.service.TokenService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand Down Expand Up @@ -57,4 +59,10 @@ public ResponseEntity<TokenDeleteResponse> deleteToken(@PathVariable("tokenId")
public ResponseEntity<TokenNotifyResponse> notifyToken(@PathVariable("tokenId") String tokenId) {
return ResponseEntity.ok(tokenService.notifyToken(tokenId));
}

@PatchMapping(path = "/token/{tokenId}")
public ResponseEntity<TokenDetailResponse> patchToken(
@PathVariable("tokenId") String tokenId, @RequestBody PatchTokenRequest patchTokenRequest) {
return ResponseEntity.ok(tokenService.patchToken(tokenId, patchTokenRequest));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.simplq.controller.model.token;

import lombok.Data;

@Data
public class PatchTokenRequest {
String queueId;
}
38 changes: 34 additions & 4 deletions simplq/src/main/java/me/simplq/service/TokenService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package me.simplq.service;

import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import me.simplq.constants.QueueStatus;
import me.simplq.constants.TokenStatus;
import me.simplq.controller.advices.LoggedInUserInfo;
import me.simplq.controller.model.token.CreateTokenRequest;
import me.simplq.controller.model.token.MyTokensResponse;
import me.simplq.controller.model.token.PatchTokenRequest;
import me.simplq.controller.model.token.TokenDeleteResponse;
import me.simplq.controller.model.token.TokenDetailResponse;
import me.simplq.controller.model.token.TokenNotifyResponse;
Expand Down Expand Up @@ -35,10 +37,7 @@ public class TokenService {

@Transactional
public TokenDetailResponse getToken(String tokenId) {
return TokenDetailResponse.fromEntity(
tokenRepository
.findById(tokenId)
.orElseThrow(SQInvalidRequestException::tokenNotFoundException));
return getTokenDetailInternal(tokenId);
}

/** Get token by queueId and the contact number */
Expand Down Expand Up @@ -142,4 +141,35 @@ public MyTokensResponse getMyTokens() {
token.getTokenCreationTimestamp()))
.collect(Collectors.toList()));
}

@Transactional
public TokenDetailResponse patchToken(String tokenId, PatchTokenRequest patchTokenRequest) {
Optional.ofNullable(patchTokenRequest.getQueueId())
.ifPresent(newQueueId -> addTokenToQueue(tokenId, newQueueId));
return getTokenDetailInternal(tokenId);
}

private void addTokenToQueue(String tokenId, String newQueueId) {
queueRepository
.findById(newQueueId)
.map(
newQueue ->
tokenRepository
.findById(tokenId)
.map(
token -> {
token.setQueue(newQueue);
tokenRepository.save(token);
return token;
})
.orElseThrow(SQInvalidRequestException::tokenNotFoundException))
.orElseThrow(SQInvalidRequestException::queueNotFoundException);
}

private TokenDetailResponse getTokenDetailInternal(String tokenId) {
return TokenDetailResponse.fromEntity(
tokenRepository
.findById(tokenId)
.orElseThrow(SQInvalidRequestException::tokenNotFoundException));
}
}