From 6054db2a480a40333bd9ac9d89a9c9d04190b240 Mon Sep 17 00:00:00 2001 From: Dalton Fury Date: Sun, 24 Apr 2022 22:53:08 +0530 Subject: [PATCH 1/3] Add and use queue joining timestamp --- .../model/queue/QueueDetailsResponse.java | 2 +- simplq/src/main/java/me/simplq/dao/Token.java | 13 +++++++++++-- simplq/src/main/resources/db/changeset.log.xml | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/simplq/src/main/java/me/simplq/controller/model/queue/QueueDetailsResponse.java b/simplq/src/main/java/me/simplq/controller/model/queue/QueueDetailsResponse.java index ce65cad..2cb9eb2 100644 --- a/simplq/src/main/java/me/simplq/controller/model/queue/QueueDetailsResponse.java +++ b/simplq/src/main/java/me/simplq/controller/model/queue/QueueDetailsResponse.java @@ -66,7 +66,7 @@ public static QueueDetailsResponse fromEntity(Queue queue) { queue.isSelfJoinAllowed(), queue.isNotifyByEmail()); queue.getTokens().stream() - .sorted(Comparator.comparing(me.simplq.dao.Token::getTokenCreationTimestamp)) + .sorted(Comparator.comparing(me.simplq.dao.Token::getQueueJoiningTimestamp)) .forEach(response::addToken); return response; } diff --git a/simplq/src/main/java/me/simplq/dao/Token.java b/simplq/src/main/java/me/simplq/dao/Token.java index 443a470..662e1c2 100644 --- a/simplq/src/main/java/me/simplq/dao/Token.java +++ b/simplq/src/main/java/me/simplq/dao/Token.java @@ -39,7 +39,11 @@ public class Token { @Temporal(TemporalType.TIMESTAMP) Date tokenCreationTimestamp; - @Column(updatable = true) + @Column() + @Temporal(TemporalType.TIMESTAMP) + Date queueJoiningTimestamp; + + @Column() @Temporal(TemporalType.TIMESTAMP) Date tokenDeletionTimestamp; @@ -58,11 +62,16 @@ public Long getAheadCount() { return this.getQueue().getTokens().stream() .filter( fellowUser -> - fellowUser.getTokenCreationTimestamp().before(this.getTokenCreationTimestamp()) + fellowUser.getQueueJoiningTimestamp().before(this.getQueueJoiningTimestamp()) && !fellowUser.getStatus().equals(TokenStatus.REMOVED)) .count(); } + public void setQueue(Queue queue) { + this.queue = queue; + this.queueJoiningTimestamp = new Date(); + } + public void delete() { this.status = TokenStatus.REMOVED; this.tokenDeletionTimestamp = new Date(); diff --git a/simplq/src/main/resources/db/changeset.log.xml b/simplq/src/main/resources/db/changeset.log.xml index c617fb8..eaa8ff2 100644 --- a/simplq/src/main/resources/db/changeset.log.xml +++ b/simplq/src/main/resources/db/changeset.log.xml @@ -131,5 +131,10 @@ + + + + + From aadc50b41403e2ac4d5092b431ff01ff39ebbe58 Mon Sep 17 00:00:00 2001 From: Dalton Fury Date: Sun, 24 Apr 2022 23:13:28 +0530 Subject: [PATCH 2/3] Add patch queue to move token to another queue --- .../me/simplq/controller/TokenController.java | 7 ++++ .../model/token/PatchTokenRequest.java | 8 ++++ .../java/me/simplq/service/TokenService.java | 38 +++++++++++++++++-- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 simplq/src/main/java/me/simplq/controller/model/token/PatchTokenRequest.java diff --git a/simplq/src/main/java/me/simplq/controller/TokenController.java b/simplq/src/main/java/me/simplq/controller/TokenController.java index 0b1d7ea..3f0fcb5 100644 --- a/simplq/src/main/java/me/simplq/controller/TokenController.java +++ b/simplq/src/main/java/me/simplq/controller/TokenController.java @@ -3,6 +3,7 @@ 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; @@ -10,6 +11,7 @@ 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; @@ -57,4 +59,9 @@ public ResponseEntity deleteToken(@PathVariable("tokenId") public ResponseEntity notifyToken(@PathVariable("tokenId") String tokenId) { return ResponseEntity.ok(tokenService.notifyToken(tokenId)); } + + @PatchMapping(path = "/token/{tokenId}") + public ResponseEntity patchToken(@PathVariable("tokenId") String tokenId, @RequestBody PatchTokenRequest patchTokenRequest) { + return ResponseEntity.ok(tokenService.patchToken(tokenId, patchTokenRequest)); + } } diff --git a/simplq/src/main/java/me/simplq/controller/model/token/PatchTokenRequest.java b/simplq/src/main/java/me/simplq/controller/model/token/PatchTokenRequest.java new file mode 100644 index 0000000..b5ca4af --- /dev/null +++ b/simplq/src/main/java/me/simplq/controller/model/token/PatchTokenRequest.java @@ -0,0 +1,8 @@ +package me.simplq.controller.model.token; + +import lombok.Data; + +@Data +public class PatchTokenRequest { + String queueId; +} diff --git a/simplq/src/main/java/me/simplq/service/TokenService.java b/simplq/src/main/java/me/simplq/service/TokenService.java index dbbb4de..ffd4301 100644 --- a/simplq/src/main/java/me/simplq/service/TokenService.java +++ b/simplq/src/main/java/me/simplq/service/TokenService.java @@ -1,6 +1,7 @@ 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; @@ -8,6 +9,7 @@ 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; @@ -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 */ @@ -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)); + } } From ff8ad0445d7d835b11b7d5b20ca7856fbab51897 Mon Sep 17 00:00:00 2001 From: github-actions <> Date: Sun, 24 Apr 2022 17:44:13 +0000 Subject: [PATCH 3/3] Google Java Format --- simplq/src/main/java/me/simplq/controller/TokenController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/simplq/src/main/java/me/simplq/controller/TokenController.java b/simplq/src/main/java/me/simplq/controller/TokenController.java index 3f0fcb5..36d978f 100644 --- a/simplq/src/main/java/me/simplq/controller/TokenController.java +++ b/simplq/src/main/java/me/simplq/controller/TokenController.java @@ -61,7 +61,8 @@ public ResponseEntity notifyToken(@PathVariable("tokenId") } @PatchMapping(path = "/token/{tokenId}") - public ResponseEntity patchToken(@PathVariable("tokenId") String tokenId, @RequestBody PatchTokenRequest patchTokenRequest) { + public ResponseEntity patchToken( + @PathVariable("tokenId") String tokenId, @RequestBody PatchTokenRequest patchTokenRequest) { return ResponseEntity.ok(tokenService.patchToken(tokenId, patchTokenRequest)); } }