fix next queue logic
This commit is contained in:
@@ -1,13 +1,17 @@
|
|||||||
package id.luxic.mai_queue.controller;
|
package id.luxic.mai_queue.controller;
|
||||||
|
|
||||||
import id.luxic.mai_queue.request.queue.QueueInsertSoloRequest;
|
import id.luxic.mai_queue.model.Queue;
|
||||||
import id.luxic.mai_queue.request.queue.ReorderQueueRequest;
|
import id.luxic.mai_queue.request.queue.QueueInsertPairRequest;
|
||||||
|
import id.luxic.mai_queue.request.queue.QueueInsertSingleRequest;
|
||||||
|
import id.luxic.mai_queue.request.queue.QueueReorderRequest;
|
||||||
import id.luxic.mai_queue.response.ResponseMessage;
|
import id.luxic.mai_queue.response.ResponseMessage;
|
||||||
import id.luxic.mai_queue.service.QueueService;
|
import id.luxic.mai_queue.service.QueueService;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/queue")
|
@RequestMapping("/api/v1/queue")
|
||||||
public class QueueController {
|
public class QueueController {
|
||||||
@@ -18,14 +22,15 @@ public class QueueController {
|
|||||||
this.queueService = queueService;
|
this.queueService = queueService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/insert/solo")
|
@PostMapping("/insert/single")
|
||||||
public ResponseEntity<ResponseMessage> insertSolo(@RequestBody QueueInsertSoloRequest request) {
|
public ResponseEntity<ResponseMessage> insertSingle(@RequestBody QueueInsertSingleRequest request) {
|
||||||
return ResponseMessage.of(HttpStatus.OK, queueService.insertSingle(request.getLocationId(), request.getUserId()));
|
return ResponseMessage.of(HttpStatus.OK, queueService.insertSingle(request.getLocationId(), request.getUserId(), request.isSolo()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/insert/pair")
|
@PostMapping("/insert/pair")
|
||||||
public ResponseEntity<ResponseMessage> insertPair() {
|
public ResponseEntity<ResponseMessage> insertPair(@RequestBody QueueInsertPairRequest request) {
|
||||||
return ResponseMessage.of(HttpStatus.OK, null, "Insert group");
|
List<Queue> queue = queueService.insertPair(request.getLocationId(), request.getUserId1(), request.getUserId2());
|
||||||
|
return ResponseMessage.of(HttpStatus.OK, queue, "Insert pair success");
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/getByLocation/{id}")
|
@GetMapping("/getByLocation/{id}")
|
||||||
@@ -35,7 +40,7 @@ public class QueueController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/reorder")
|
@PostMapping("/reorder")
|
||||||
public ResponseEntity<ResponseMessage> reorderQueue(@RequestBody ReorderQueueRequest request) {
|
public ResponseEntity<ResponseMessage> reorderQueue(@RequestBody QueueReorderRequest request) {
|
||||||
|
|
||||||
queueService.reorderQueue(request.getLocationId(), request.getFrom(), request.getTo());
|
queueService.reorderQueue(request.getLocationId(), request.getFrom(), request.getTo());
|
||||||
|
|
||||||
|
|||||||
@@ -32,4 +32,7 @@ public interface QueueRepository extends JpaRepository<Queue, Integer> {
|
|||||||
@Query("SELECT q FROM Queue q WHERE q.location.id = :locationId AND order = :order")
|
@Query("SELECT q FROM Queue q WHERE q.location.id = :locationId AND order = :order")
|
||||||
Queue findByLocationIdAndOrder(@Param("locationId") String locationId, @Param("order") Integer order);
|
Queue findByLocationIdAndOrder(@Param("locationId") String locationId, @Param("order") Integer order);
|
||||||
|
|
||||||
|
@Query(value = "SELECT q.\"order\" FROM queue q WHERE q.location_id = :locationId AND \"order\" > :order ORDER BY \"order\" ASC LIMIT 1", nativeQuery = true)
|
||||||
|
Integer getNextOrderInLocation(@Param("locationId") String locationId, @Param("order") Integer order);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package id.luxic.mai_queue.request.queue;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class QueueInsertPairRequest {
|
||||||
|
private String locationId;
|
||||||
|
private String userId1;
|
||||||
|
private String userId2;
|
||||||
|
}
|
||||||
@@ -1,11 +1,15 @@
|
|||||||
package id.luxic.mai_queue.request.queue;
|
package id.luxic.mai_queue.request.queue;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
@Data
|
@Getter
|
||||||
public class QueueInsertSoloRequest {
|
@Setter
|
||||||
|
public class QueueInsertSingleRequest {
|
||||||
|
|
||||||
private String userId;
|
private String userId;
|
||||||
private String locationId;
|
private String locationId;
|
||||||
|
private boolean solo;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ import lombok.Setter;
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class ReorderQueueRequest {
|
public class QueueReorderRequest {
|
||||||
|
|
||||||
private String locationId;
|
private String locationId;
|
||||||
private Integer from;
|
private Integer from;
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package id.luxic.mai_queue.scheduler;
|
|
||||||
|
|
||||||
public class Cleanup {
|
|
||||||
}
|
|
||||||
@@ -66,7 +66,7 @@ public class LocationService {
|
|||||||
Integer nextOrder = queueRepository.nextQueueByLocation(locationId, offset);
|
Integer nextOrder = queueRepository.nextQueueByLocation(locationId, offset);
|
||||||
Integer lastOrder = queueRepository.findLastOrderByLocationId(locationId);
|
Integer lastOrder = queueRepository.findLastOrderByLocationId(locationId);
|
||||||
|
|
||||||
if (nextOrder == null || nextOrder.equals(lastOrder)) nextOrder = lastOrder + 1;
|
if (nextOrder == null) nextOrder = lastOrder + 1;
|
||||||
|
|
||||||
location.setCurrentQueue(nextOrder);
|
location.setCurrentQueue(nextOrder);
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import id.luxic.mai_queue.model.Queue;
|
|||||||
import id.luxic.mai_queue.model.User;
|
import id.luxic.mai_queue.model.User;
|
||||||
import id.luxic.mai_queue.repository.LocationRepository;
|
import id.luxic.mai_queue.repository.LocationRepository;
|
||||||
import id.luxic.mai_queue.repository.QueueRepository;
|
import id.luxic.mai_queue.repository.QueueRepository;
|
||||||
|
import id.luxic.mai_queue.tools.IdGenerator;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class QueueService {
|
public class QueueService {
|
||||||
@@ -21,8 +23,39 @@ public class QueueService {
|
|||||||
this.locationRepository = locationRepository;
|
this.locationRepository = locationRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Queue> insertPair(String locationId, String userId1, String userId2) {
|
||||||
|
Location location = locationRepository.findById(locationId).orElse(null);
|
||||||
|
|
||||||
|
if (location == null) {
|
||||||
|
throw new IllegalArgumentException("Location not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer lastOrder = queueRepository.findLastOrderByLocationId(locationId);
|
||||||
|
Integer nextOrder = lastOrder == null ? 1 : lastOrder + 1;
|
||||||
|
|
||||||
|
String pairId = IdGenerator.generateUUID();
|
||||||
|
|
||||||
|
Queue queue1 = queueRepository.save(Queue.builder()
|
||||||
|
.location(location)
|
||||||
|
.player(User.builder().id(userId1).build())
|
||||||
|
.order(nextOrder)
|
||||||
|
.pairId(pairId)
|
||||||
|
.solo(false)
|
||||||
|
.build());
|
||||||
|
|
||||||
|
Queue queue2 = queueRepository.save(Queue.builder()
|
||||||
|
.location(location)
|
||||||
|
.player(User.builder().id(userId2).build())
|
||||||
|
.order(nextOrder+1)
|
||||||
|
.pairId(pairId)
|
||||||
|
.solo(false)
|
||||||
|
.build());
|
||||||
|
|
||||||
|
return List.of(queue1, queue2);
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Queue insertSingle(String locationId, String userId) {
|
public Queue insertSingle(String locationId, String userId, Boolean solo) {
|
||||||
Integer lastOrder = queueRepository.findLastOrderByLocationId(locationId);
|
Integer lastOrder = queueRepository.findLastOrderByLocationId(locationId);
|
||||||
Integer nextOrder = lastOrder == null ? 1 : lastOrder + 1;
|
Integer nextOrder = lastOrder == null ? 1 : lastOrder + 1;
|
||||||
|
|
||||||
@@ -37,6 +70,7 @@ public class QueueService {
|
|||||||
.player(User.builder().id(userId).build())
|
.player(User.builder().id(userId).build())
|
||||||
.order(nextOrder)
|
.order(nextOrder)
|
||||||
.pairId(null)
|
.pairId(null)
|
||||||
|
.solo(solo != null && solo)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
// Auto Reorder (Pending)
|
// Auto Reorder (Pending)
|
||||||
@@ -61,6 +95,10 @@ public class QueueService {
|
|||||||
// From must be higher order than target
|
// From must be higher order than target
|
||||||
Location location = locationRepository.findById(locationId).orElse(null);
|
Location location = locationRepository.findById(locationId).orElse(null);
|
||||||
|
|
||||||
|
if (location == null) {
|
||||||
|
throw new IllegalArgumentException("Location with id " + locationId + " not found");
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure from is higher order than target
|
// Make sure from is higher order than target
|
||||||
if (to > from) {
|
if (to > from) {
|
||||||
int temp = to;
|
int temp = to;
|
||||||
@@ -68,8 +106,6 @@ public class QueueService {
|
|||||||
from = temp;
|
from = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("to: " + to + ", from: " + from);
|
|
||||||
|
|
||||||
if (to < location.getCurrentQueue()) {
|
if (to < location.getCurrentQueue()) {
|
||||||
throw new IllegalArgumentException("Target queue is lower than current queue");
|
throw new IllegalArgumentException("Target queue is lower than current queue");
|
||||||
}
|
}
|
||||||
@@ -87,6 +123,19 @@ public class QueueService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void deleteQueue(Integer id) {
|
public void deleteQueue(Integer id) {
|
||||||
|
|
||||||
|
Queue queue = queueRepository.findById(id).orElse(null);
|
||||||
|
|
||||||
|
if (queue == null) {
|
||||||
|
throw new IllegalArgumentException("Queue not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Objects.equals(queue.getLocation().getCurrentQueue(), queue.getOrder())) {
|
||||||
|
Integer nextQueue = queueRepository.getNextOrderInLocation(queue.getLocation().getId(), queue.getOrder());
|
||||||
|
queue.getLocation().setCurrentQueue(nextQueue);
|
||||||
|
locationRepository.save(queue.getLocation());
|
||||||
|
}
|
||||||
|
|
||||||
queueRepository.deleteById(id);
|
queueRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user