From 7a027b3cba3ed686da090d1f25dbc5b5e8c479c4 Mon Sep 17 00:00:00 2001 From: Piotr Wasko Date: Fri, 15 Oct 2021 15:32:50 +0200 Subject: [PATCH 1/2] Add API with queue history in CSV --- simplq/pom.xml | 9 ++- .../me/simplq/controller/QueueController.java | 12 ++++ .../QueueEventsCsvResponseConverter.java | 59 +++++++++++++++++++ .../model/queue/QueueEventsCsvResponse.java | 14 +++++ .../me/simplq/service/QueueEventsService.java | 27 +++++++-- .../java/me/simplq/service/QueueService.java | 7 +++ 6 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 simplq/src/main/java/me/simplq/controller/converters/QueueEventsCsvResponseConverter.java create mode 100644 simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsCsvResponse.java diff --git a/simplq/pom.xml b/simplq/pom.xml index 19f893b..44041a5 100644 --- a/simplq/pom.xml +++ b/simplq/pom.xml @@ -106,6 +106,11 @@ liquibase-core 4.3.1 + + org.apache.commons + commons-csv + 1.8 + @@ -155,9 +160,9 @@ - postgresql + org.postgresql postgresql - 9.1-901.jdbc4 + 42.2.24 diff --git a/simplq/src/main/java/me/simplq/controller/QueueController.java b/simplq/src/main/java/me/simplq/controller/QueueController.java index b304938..20a792a 100644 --- a/simplq/src/main/java/me/simplq/controller/QueueController.java +++ b/simplq/src/main/java/me/simplq/controller/QueueController.java @@ -1,6 +1,7 @@ package me.simplq.controller; import javax.validation.Valid; + import lombok.RequiredArgsConstructor; import me.simplq.controller.model.queue.CreateQueueRequest; import me.simplq.controller.model.queue.CreateQueueResponse; @@ -9,6 +10,7 @@ import me.simplq.controller.model.queue.PatchQueueResponse; import me.simplq.controller.model.queue.PauseQueueRequest; import me.simplq.controller.model.queue.QueueDetailsResponse; +import me.simplq.controller.model.queue.QueueEventsCsvResponse; import me.simplq.controller.model.queue.QueueEventsResponse; import me.simplq.controller.model.queue.QueueStatusResponse; import me.simplq.controller.model.queue.UpdateQueueStatusResponse; @@ -22,6 +24,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @@ -86,4 +89,13 @@ public ResponseEntity getQueueEvents( @PathVariable("queueId") String queueId) { return ResponseEntity.ok(queueService.getQueueEvents(queueId)); } + + @GetMapping(path = "/queue/{queueId}/history", produces = "text/csv") + @ResponseBody + public QueueEventsCsvResponse getQueueHistoryCSV( + @PathVariable("queueId") String queueId) { + return queueService.getQueueEventsInCsv(queueId); + } + + } diff --git a/simplq/src/main/java/me/simplq/controller/converters/QueueEventsCsvResponseConverter.java b/simplq/src/main/java/me/simplq/controller/converters/QueueEventsCsvResponseConverter.java new file mode 100644 index 0000000..fc1ff82 --- /dev/null +++ b/simplq/src/main/java/me/simplq/controller/converters/QueueEventsCsvResponseConverter.java @@ -0,0 +1,59 @@ +package me.simplq.controller.converters; + +import me.simplq.controller.model.queue.QueueEventsCsvResponse; +import me.simplq.controller.model.queue.QueueEventsResponse; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.springframework.http.ContentDisposition; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.converter.AbstractHttpMessageConverter; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.http.converter.HttpMessageNotWritableException; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; + +@Component +public class QueueEventsCsvResponseConverter extends AbstractHttpMessageConverter { + + private static final MediaType TEXT_CSV = new MediaType("text", "csv", StandardCharsets.UTF_8); + + QueueEventsCsvResponseConverter() { + super(TEXT_CSV); + } + + @Override + protected boolean supports(Class aClass) { + return QueueEventsCsvResponse.class.equals(aClass); + } + + @Override + protected QueueEventsCsvResponse readInternal(Class aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException { + throw new HttpMessageNotReadableException("Reading history from CSV not supported", httpInputMessage); + } + + @Override + protected void writeInternal(QueueEventsCsvResponse response, HttpOutputMessage output) throws IOException, HttpMessageNotWritableException { + output.getHeaders().setContentType(TEXT_CSV); + output.getHeaders().setContentDisposition(ContentDisposition.builder("attachment").filename(response.getFileName()).build()); + CSVPrinter csvPrinter = new CSVPrinter(new OutputStreamWriter(output.getBody()), CSVFormat.DEFAULT); + for (QueueEventsResponse.Event event : response.getEventResponse().getEvents()) { + printEvent(csvPrinter, event); + } + csvPrinter.flush(); + csvPrinter.close(); + } + + private void printEvent(CSVPrinter csvPrinter, QueueEventsResponse.Event event) throws IOException { + csvPrinter.printRecord( + event.getTokenNumber(), + event.getTokenName(), + event.getEventType(), + event.getEventTimestamp() + ); + } +} diff --git a/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsCsvResponse.java b/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsCsvResponse.java new file mode 100644 index 0000000..c801ca3 --- /dev/null +++ b/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsCsvResponse.java @@ -0,0 +1,14 @@ +package me.simplq.controller.model.queue; + +import lombok.Builder; +import lombok.Data; + +import java.util.Date; +import java.util.List; + +@Data +@Builder +public class QueueEventsCsvResponse { + String fileName; + QueueEventsResponse eventResponse; +} diff --git a/simplq/src/main/java/me/simplq/service/QueueEventsService.java b/simplq/src/main/java/me/simplq/service/QueueEventsService.java index 551f87a..76169ce 100644 --- a/simplq/src/main/java/me/simplq/service/QueueEventsService.java +++ b/simplq/src/main/java/me/simplq/service/QueueEventsService.java @@ -4,7 +4,10 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; + +import lombok.val; import me.simplq.controller.model.queue.QueueDetailsResponse; +import me.simplq.controller.model.queue.QueueEventsCsvResponse; import me.simplq.controller.model.queue.QueueEventsResponse; import me.simplq.controller.model.queue.QueueEventsResponse.Event; import me.simplq.controller.model.queue.QueueEventsResponse.Event.EventType; @@ -14,6 +17,23 @@ public class QueueEventsService { public QueueEventsResponse getQueueEvents(QueueDetailsResponse queueDetails) { + return QueueEventsResponse.builder() + .events( + getTokenEventsStream(queueDetails) + .sorted(Comparator.comparing(Event::getEventTimestamp).reversed()) + .collect(Collectors.toList())) + .build(); + } + + public QueueEventsCsvResponse getQueueEventsInCsv(QueueDetailsResponse queueDetails) { + + return QueueEventsCsvResponse.builder() + .fileName(String.format("%s.history.csv", queueDetails.getQueueName())) + .eventResponse(getQueueEvents(queueDetails)) + .build(); + } + + private Stream getTokenEventsStream(QueueDetailsResponse queueDetails) { // Make creation events for active tokens. var activeTokenEventStream = queueDetails.getTokens().stream() @@ -46,11 +66,6 @@ public QueueEventsResponse getQueueEvents(QueueDetailsResponse queueDetails) { .build())) .flatMap(List::stream); - return QueueEventsResponse.builder() - .events( - Stream.concat(activeTokenEventStream, removedTokenEventStream) - .sorted(Comparator.comparing(Event::getEventTimestamp).reversed()) - .collect(Collectors.toList())) - .build(); + return Stream.concat(activeTokenEventStream, removedTokenEventStream); } } diff --git a/simplq/src/main/java/me/simplq/service/QueueService.java b/simplq/src/main/java/me/simplq/service/QueueService.java index cda3a7d..e8a95d7 100644 --- a/simplq/src/main/java/me/simplq/service/QueueService.java +++ b/simplq/src/main/java/me/simplq/service/QueueService.java @@ -14,6 +14,7 @@ import me.simplq.controller.model.queue.PatchQueueResponse; import me.simplq.controller.model.queue.PauseQueueRequest; import me.simplq.controller.model.queue.QueueDetailsResponse; +import me.simplq.controller.model.queue.QueueEventsCsvResponse; import me.simplq.controller.model.queue.QueueEventsResponse; import me.simplq.controller.model.queue.QueueStatusResponse; import me.simplq.controller.model.queue.UpdateQueueStatusResponse; @@ -22,6 +23,7 @@ import me.simplq.exceptions.SQInvalidRequestException; import me.simplq.utils.predicates.QueueThrowingPredicate; import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @Service @@ -149,6 +151,11 @@ public QueueEventsResponse getQueueEvents(String queueId) { return queueEventsService.getQueueEvents(this.getQueueDetailsResponseInternal(queueId)); } + @Transactional + public QueueEventsCsvResponse getQueueEventsInCsv(String queueId) { + return queueEventsService.getQueueEventsInCsv(this.getQueueDetailsResponseInternal(queueId)); + } + private QueueDetailsResponse getQueueDetailsResponseInternal(String queueId) { return queueRepository .findById(queueId) From 6f8da31809ef00b75e2e34874dc90ba203a07af0 Mon Sep 17 00:00:00 2001 From: Piotr Wasko Date: Sun, 17 Oct 2021 19:02:59 +0200 Subject: [PATCH 2/2] Add queue ID and name to events response and use it in CSV --- .../me/simplq/controller/QueueController.java | 34 +++-- ...java => QueueEventsResponseConverter.java} | 22 ++-- .../model/queue/QueueEventsCsvResponse.java | 14 --- .../model/queue/QueueEventsResponse.java | 33 ++--- .../me/simplq/service/QueueEventsService.java | 84 ++++++------- .../java/me/simplq/service/QueueService.java | 116 +++++++++--------- 6 files changed, 141 insertions(+), 162 deletions(-) rename simplq/src/main/java/me/simplq/controller/converters/{QueueEventsCsvResponseConverter.java => QueueEventsResponseConverter.java} (66%) delete mode 100644 simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsCsvResponse.java diff --git a/simplq/src/main/java/me/simplq/controller/QueueController.java b/simplq/src/main/java/me/simplq/controller/QueueController.java index 20a792a..ac0eb19 100644 --- a/simplq/src/main/java/me/simplq/controller/QueueController.java +++ b/simplq/src/main/java/me/simplq/controller/QueueController.java @@ -1,7 +1,5 @@ package me.simplq.controller; -import javax.validation.Valid; - import lombok.RequiredArgsConstructor; import me.simplq.controller.model.queue.CreateQueueRequest; import me.simplq.controller.model.queue.CreateQueueResponse; @@ -10,11 +8,11 @@ import me.simplq.controller.model.queue.PatchQueueResponse; import me.simplq.controller.model.queue.PauseQueueRequest; import me.simplq.controller.model.queue.QueueDetailsResponse; -import me.simplq.controller.model.queue.QueueEventsCsvResponse; import me.simplq.controller.model.queue.QueueEventsResponse; import me.simplq.controller.model.queue.QueueStatusResponse; import me.simplq.controller.model.queue.UpdateQueueStatusResponse; import me.simplq.service.QueueService; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -24,9 +22,10 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; +import javax.validation.Valid; + @RestController @RequestMapping("/v1") @RequiredArgsConstructor @@ -36,7 +35,7 @@ public class QueueController { @PostMapping(path = "/queue") public ResponseEntity createQueue( - @Valid @RequestBody CreateQueueRequest createQueueRequest) { + @Valid @RequestBody CreateQueueRequest createQueueRequest) { return ResponseEntity.ok(queueService.createQueue(createQueueRequest)); } @@ -47,34 +46,34 @@ public ResponseEntity getMyQueues() { @PatchMapping(path = "/queue/{queueId}") public ResponseEntity patchQueue( - @Valid @RequestBody PatchQueueRequest patchRequest, @PathVariable("queueId") String queueId) { + @Valid @RequestBody PatchQueueRequest patchRequest, @PathVariable("queueId") String queueId) { return ResponseEntity.ok(queueService.patchQueue(queueId, patchRequest)); } @GetMapping(path = "/queue/{queueId}") public ResponseEntity getQueueDetails( - @PathVariable("queueId") String queueId) { + @PathVariable("queueId") String queueId) { return ResponseEntity.ok(queueService.getQueueDetails(queueId)); } @PostMapping(path = "/queue/{queueId}") public ResponseEntity pauseQueueRequest( - @Valid @RequestBody PauseQueueRequest pauseQueueRequest, - @PathVariable("queueId") String queueId) { + @Valid @RequestBody PauseQueueRequest pauseQueueRequest, + @PathVariable("queueId") String queueId) { return ResponseEntity.ok(queueService.pauseQueue(pauseQueueRequest, queueId)); } @DeleteMapping(path = "/queue/{queueId}") public ResponseEntity deleteQueueRequest( - @PathVariable("queueId") String queueId) { + @PathVariable("queueId") String queueId) { return ResponseEntity.ok(queueService.deleteQueue(queueId)); } @GetMapping(path = "/queue/status") public ResponseEntity getQueueStatus( - @RequestParam(required = false) String queueId, - @RequestParam(required = false) String queueName) { + @RequestParam(required = false) String queueId, + @RequestParam(required = false) String queueName) { if (queueId != null) { return ResponseEntity.ok(queueService.getQueueStatus(queueId)); } else if (queueName != null) { @@ -84,17 +83,16 @@ public ResponseEntity getQueueStatus( } } - @GetMapping(path = "/queue/{queueId}/events") + @GetMapping(path = "/queue/{queueId}/events", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getQueueEvents( - @PathVariable("queueId") String queueId) { + @PathVariable("queueId") String queueId) { return ResponseEntity.ok(queueService.getQueueEvents(queueId)); } - @GetMapping(path = "/queue/{queueId}/history", produces = "text/csv") - @ResponseBody - public QueueEventsCsvResponse getQueueHistoryCSV( + @GetMapping(path = "/queue/{queueId}/events", produces = "text/csv") + public ResponseEntity getQueueEventsCsv( @PathVariable("queueId") String queueId) { - return queueService.getQueueEventsInCsv(queueId); + return ResponseEntity.ok(queueService.getQueueEvents(queueId)); } diff --git a/simplq/src/main/java/me/simplq/controller/converters/QueueEventsCsvResponseConverter.java b/simplq/src/main/java/me/simplq/controller/converters/QueueEventsResponseConverter.java similarity index 66% rename from simplq/src/main/java/me/simplq/controller/converters/QueueEventsCsvResponseConverter.java rename to simplq/src/main/java/me/simplq/controller/converters/QueueEventsResponseConverter.java index fc1ff82..2df0787 100644 --- a/simplq/src/main/java/me/simplq/controller/converters/QueueEventsCsvResponseConverter.java +++ b/simplq/src/main/java/me/simplq/controller/converters/QueueEventsResponseConverter.java @@ -1,6 +1,5 @@ package me.simplq.controller.converters; -import me.simplq.controller.model.queue.QueueEventsCsvResponse; import me.simplq.controller.model.queue.QueueEventsResponse; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; @@ -18,30 +17,37 @@ import java.nio.charset.StandardCharsets; @Component -public class QueueEventsCsvResponseConverter extends AbstractHttpMessageConverter { +public class QueueEventsResponseConverter extends AbstractHttpMessageConverter { private static final MediaType TEXT_CSV = new MediaType("text", "csv", StandardCharsets.UTF_8); - QueueEventsCsvResponseConverter() { + QueueEventsResponseConverter() { super(TEXT_CSV); } @Override protected boolean supports(Class aClass) { - return QueueEventsCsvResponse.class.equals(aClass); + return QueueEventsResponse.class.equals(aClass); } @Override - protected QueueEventsCsvResponse readInternal(Class aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException { + protected QueueEventsResponse readInternal(Class aClass, HttpInputMessage httpInputMessage) throws HttpMessageNotReadableException { throw new HttpMessageNotReadableException("Reading history from CSV not supported", httpInputMessage); } @Override - protected void writeInternal(QueueEventsCsvResponse response, HttpOutputMessage output) throws IOException, HttpMessageNotWritableException { + protected void writeInternal(QueueEventsResponse response, HttpOutputMessage output) throws IOException, HttpMessageNotWritableException { output.getHeaders().setContentType(TEXT_CSV); - output.getHeaders().setContentDisposition(ContentDisposition.builder("attachment").filename(response.getFileName()).build()); + output.getHeaders().setContentDisposition( + ContentDisposition.builder("attachment") + .filename(String.format("%s.history.csv", response.getQueueName())) + .build()); + printEventsToCsv(response, output); + } + + private void printEventsToCsv(QueueEventsResponse response, HttpOutputMessage output) throws IOException { CSVPrinter csvPrinter = new CSVPrinter(new OutputStreamWriter(output.getBody()), CSVFormat.DEFAULT); - for (QueueEventsResponse.Event event : response.getEventResponse().getEvents()) { + for (QueueEventsResponse.Event event : response.getEvents()) { printEvent(csvPrinter, event); } csvPrinter.flush(); diff --git a/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsCsvResponse.java b/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsCsvResponse.java deleted file mode 100644 index c801ca3..0000000 --- a/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsCsvResponse.java +++ /dev/null @@ -1,14 +0,0 @@ -package me.simplq.controller.model.queue; - -import lombok.Builder; -import lombok.Data; - -import java.util.Date; -import java.util.List; - -@Data -@Builder -public class QueueEventsCsvResponse { - String fileName; - QueueEventsResponse eventResponse; -} diff --git a/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsResponse.java b/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsResponse.java index 5787890..e8e973b 100644 --- a/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsResponse.java +++ b/simplq/src/main/java/me/simplq/controller/model/queue/QueueEventsResponse.java @@ -1,26 +1,29 @@ package me.simplq.controller.model.queue; -import java.util.Date; -import java.util.List; import lombok.Builder; import lombok.Data; +import java.util.Date; +import java.util.List; + @Data @Builder public class QueueEventsResponse { - @Data - @Builder - public static class Event { - public enum EventType { - TOKEN_ADDED, - TOKEN_REMOVED - } + @Data + @Builder + public static class Event { + public enum EventType { + TOKEN_ADDED, + TOKEN_REMOVED + } - EventType eventType; - String tokenName; - Integer tokenNumber; - Date eventTimestamp; - } + EventType eventType; + String tokenName; + Integer tokenNumber; + Date eventTimestamp; + } - List events; + String queueId; + String queueName; + List events; } diff --git a/simplq/src/main/java/me/simplq/service/QueueEventsService.java b/simplq/src/main/java/me/simplq/service/QueueEventsService.java index 76169ce..0ea4dad 100644 --- a/simplq/src/main/java/me/simplq/service/QueueEventsService.java +++ b/simplq/src/main/java/me/simplq/service/QueueEventsService.java @@ -1,70 +1,62 @@ package me.simplq.service; -import java.util.Comparator; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import lombok.val; import me.simplq.controller.model.queue.QueueDetailsResponse; -import me.simplq.controller.model.queue.QueueEventsCsvResponse; import me.simplq.controller.model.queue.QueueEventsResponse; import me.simplq.controller.model.queue.QueueEventsResponse.Event; import me.simplq.controller.model.queue.QueueEventsResponse.Event.EventType; import org.springframework.stereotype.Component; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + @Component public class QueueEventsService { public QueueEventsResponse getQueueEvents(QueueDetailsResponse queueDetails) { return QueueEventsResponse.builder() - .events( - getTokenEventsStream(queueDetails) - .sorted(Comparator.comparing(Event::getEventTimestamp).reversed()) - .collect(Collectors.toList())) - .build(); - } - - public QueueEventsCsvResponse getQueueEventsInCsv(QueueDetailsResponse queueDetails) { - - return QueueEventsCsvResponse.builder() - .fileName(String.format("%s.history.csv", queueDetails.getQueueName())) - .eventResponse(getQueueEvents(queueDetails)) - .build(); + .queueId(queueDetails.getQueueId()) + .queueName(queueDetails.getQueueName()) + .events( + getTokenEventsStream(queueDetails) + .sorted(Comparator.comparing(Event::getEventTimestamp).reversed()) + .collect(Collectors.toList())) + .build(); } private Stream getTokenEventsStream(QueueDetailsResponse queueDetails) { // Make creation events for active tokens. var activeTokenEventStream = - queueDetails.getTokens().stream() - .map( - token -> - Event.builder() - .eventType(EventType.TOKEN_ADDED) - .tokenName(token.getName()) - .tokenNumber(token.getTokenNumber()) - .eventTimestamp(token.getTokenCreationTimestamp()) - .build()); + queueDetails.getTokens().stream() + .map( + token -> + Event.builder() + .eventType(EventType.TOKEN_ADDED) + .tokenName(token.getName()) + .tokenNumber(token.getTokenNumber()) + .eventTimestamp(token.getTokenCreationTimestamp()) + .build()); // Make creation and removal events for removed tokens. var removedTokenEventStream = - queueDetails.getRemovedTokens().stream() - .map( - token -> - List.of( - Event.builder() - .eventType(EventType.TOKEN_ADDED) - .tokenName(token.getName()) - .tokenNumber(token.getTokenNumber()) - .eventTimestamp(token.getTokenCreationTimestamp()) - .build(), - Event.builder() - .eventType(EventType.TOKEN_REMOVED) - .tokenName(token.getName()) - .tokenNumber(token.getTokenNumber()) - .eventTimestamp(token.getTokenDeletionTimestamp()) - .build())) - .flatMap(List::stream); + queueDetails.getRemovedTokens().stream() + .map( + token -> + List.of( + Event.builder() + .eventType(EventType.TOKEN_ADDED) + .tokenName(token.getName()) + .tokenNumber(token.getTokenNumber()) + .eventTimestamp(token.getTokenCreationTimestamp()) + .build(), + Event.builder() + .eventType(EventType.TOKEN_REMOVED) + .tokenName(token.getName()) + .tokenNumber(token.getTokenNumber()) + .eventTimestamp(token.getTokenDeletionTimestamp()) + .build())) + .flatMap(List::stream); return Stream.concat(activeTokenEventStream, removedTokenEventStream); } diff --git a/simplq/src/main/java/me/simplq/service/QueueService.java b/simplq/src/main/java/me/simplq/service/QueueService.java index e8a95d7..e14f506 100644 --- a/simplq/src/main/java/me/simplq/service/QueueService.java +++ b/simplq/src/main/java/me/simplq/service/QueueService.java @@ -1,9 +1,5 @@ package me.simplq.service; -import java.util.Comparator; -import java.util.function.Function; -import java.util.stream.Collectors; -import javax.transaction.Transactional; import lombok.RequiredArgsConstructor; import me.simplq.constants.QueueStatus; import me.simplq.controller.advices.LoggedInUserInfo; @@ -14,7 +10,6 @@ import me.simplq.controller.model.queue.PatchQueueResponse; import me.simplq.controller.model.queue.PauseQueueRequest; import me.simplq.controller.model.queue.QueueDetailsResponse; -import me.simplq.controller.model.queue.QueueEventsCsvResponse; import me.simplq.controller.model.queue.QueueEventsResponse; import me.simplq.controller.model.queue.QueueStatusResponse; import me.simplq.controller.model.queue.UpdateQueueStatusResponse; @@ -23,9 +18,13 @@ import me.simplq.exceptions.SQInvalidRequestException; import me.simplq.utils.predicates.QueueThrowingPredicate; import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import javax.transaction.Transactional; +import java.util.Comparator; +import java.util.function.Function; +import java.util.stream.Collectors; + @Service @RequiredArgsConstructor public class QueueService { @@ -41,8 +40,8 @@ public CreateQueueResponse createQueue(CreateQueueRequest createQueueRequest) { var owner = ownerService.getOwnerOrElseCreate(); try { var queue = - queueRepository.saveAndFlush( - new Queue(createQueueRequest.getQueueName(), owner, QueueStatus.ACTIVE)); + queueRepository.saveAndFlush( + new Queue(createQueueRequest.getQueueName(), owner, QueueStatus.ACTIVE)); return new CreateQueueResponse(queue.getQueueName(), queue.getQueueId()); } catch (DataIntegrityViolationException de) { throw SQInvalidRequestException.queueNameNotUniqueException(); @@ -52,37 +51,37 @@ public CreateQueueResponse createQueue(CreateQueueRequest createQueueRequest) { @Transactional public UpdateQueueStatusResponse pauseQueue(PauseQueueRequest pauseQueueRequest, String queueId) { return queueRepository - .findById(queueId) - .map( - queue1 -> { - queueThrowingPredicate - .isNotDeleted() - .and(queueThrowingPredicate.currentUserOwnsQueue()) - .test(queue1); - if (pauseQueueRequest.getStatus().equals(QueueStatus.DELETED)) { - throw SQInvalidRequestException.queueDeletedNotAllowedException(); - } - queue1.setStatus(pauseQueueRequest.getStatus()); - return queueRepository.save(queue1); - }) - .map(UpdateQueueStatusResponse::fromEntity) - .orElseThrow(SQInvalidRequestException::queueNotFoundException); + .findById(queueId) + .map( + queue1 -> { + queueThrowingPredicate + .isNotDeleted() + .and(queueThrowingPredicate.currentUserOwnsQueue()) + .test(queue1); + if (pauseQueueRequest.getStatus().equals(QueueStatus.DELETED)) { + throw SQInvalidRequestException.queueDeletedNotAllowedException(); + } + queue1.setStatus(pauseQueueRequest.getStatus()); + return queueRepository.save(queue1); + }) + .map(UpdateQueueStatusResponse::fromEntity) + .orElseThrow(SQInvalidRequestException::queueNotFoundException); } @Transactional public UpdateQueueStatusResponse deleteQueue(String queueId) { return queueRepository - .findById(queueId) - .filter( - queueThrowingPredicate - .isNotDeleted() - .and(queueThrowingPredicate.currentUserOwnsQueue())) - .map( - queue1 -> { - queue1.setStatus(QueueStatus.DELETED); - return UpdateQueueStatusResponse.fromEntity(queueRepository.save(queue1)); - }) - .orElseThrow(SQInvalidRequestException::tokenNotFoundException); + .findById(queueId) + .filter( + queueThrowingPredicate + .isNotDeleted() + .and(queueThrowingPredicate.currentUserOwnsQueue())) + .map( + queue1 -> { + queue1.setStatus(QueueStatus.DELETED); + return UpdateQueueStatusResponse.fromEntity(queueRepository.save(queue1)); + }) + .orElseThrow(SQInvalidRequestException::tokenNotFoundException); } @Transactional @@ -93,36 +92,36 @@ public QueueDetailsResponse getQueueDetails(String queueId) { @Transactional public QueueStatusResponse getQueueStatus(String queueId) { return queueRepository - .findById(queueId) - .map(QueueStatusResponse::fromEntity) - .orElseThrow(SQInvalidRequestException::queueNotFoundException); + .findById(queueId) + .map(QueueStatusResponse::fromEntity) + .orElseThrow(SQInvalidRequestException::queueNotFoundException); } @Transactional public MyQueuesResponse getMyQueues() { return new MyQueuesResponse( - queueRepository - .findByOwnerId(loggedInUserInfo.getUserId()) - .filter(queue -> !QueueStatus.DELETED.equals(queue.getStatus())) - .sorted(Comparator.comparing(Queue::getQueueCreationTimestamp)) - .map(MyQueuesResponse.Queue::fromEntity) - .collect(Collectors.toList())); + queueRepository + .findByOwnerId(loggedInUserInfo.getUserId()) + .filter(queue -> !QueueStatus.DELETED.equals(queue.getStatus())) + .sorted(Comparator.comparing(Queue::getQueueCreationTimestamp)) + .map(MyQueuesResponse.Queue::fromEntity) + .collect(Collectors.toList())); } @Transactional public QueueStatusResponse getQueueStatusByName(String queueName) { return queueRepository - .findByQueueName(queueName) - .map(QueueStatusResponse::fromEntity) - .orElseThrow(SQInvalidRequestException::queueNotFoundException); + .findByQueueName(queueName) + .map(QueueStatusResponse::fromEntity) + .orElseThrow(SQInvalidRequestException::queueNotFoundException); } @Transactional public PatchQueueResponse patchQueue(String queueId, PatchQueueRequest patchRequest) { return queueRepository - .findById(queueId) - .map(patchQueue(patchRequest)) - .orElseThrow(SQInvalidRequestException::queueNotFoundException); + .findById(queueId) + .map(patchQueue(patchRequest)) + .orElseThrow(SQInvalidRequestException::queueNotFoundException); } private Function patchQueue(PatchQueueRequest patchQueueRequest) { @@ -140,9 +139,9 @@ private Function patchQueue(PatchQueueRequest patchQu var updatedQueue = queueRepository.save(queue); return response - .queueName(updatedQueue.getQueueName()) - .queueId(updatedQueue.getQueueId()) - .build(); + .queueName(updatedQueue.getQueueName()) + .queueId(updatedQueue.getQueueId()) + .build(); }; } @@ -151,16 +150,11 @@ public QueueEventsResponse getQueueEvents(String queueId) { return queueEventsService.getQueueEvents(this.getQueueDetailsResponseInternal(queueId)); } - @Transactional - public QueueEventsCsvResponse getQueueEventsInCsv(String queueId) { - return queueEventsService.getQueueEventsInCsv(this.getQueueDetailsResponseInternal(queueId)); - } - private QueueDetailsResponse getQueueDetailsResponseInternal(String queueId) { return queueRepository - .findById(queueId) - .filter(queueThrowingPredicate.currentUserOwnsQueue()) - .map(QueueDetailsResponse::fromEntity) - .orElseThrow(SQInvalidRequestException::queueNotFoundException); + .findById(queueId) + .filter(queueThrowingPredicate.currentUserOwnsQueue()) + .map(QueueDetailsResponse::fromEntity) + .orElseThrow(SQInvalidRequestException::queueNotFoundException); } }