fix next queue logic
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
package id.luxic.mai_queue.controller;
|
||||
|
||||
import id.luxic.mai_queue.request.queue.QueueInsertSoloRequest;
|
||||
import id.luxic.mai_queue.request.queue.ReorderQueueRequest;
|
||||
import id.luxic.mai_queue.model.Queue;
|
||||
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.service.QueueService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/queue")
|
||||
public class QueueController {
|
||||
@@ -18,14 +22,15 @@ public class QueueController {
|
||||
this.queueService = queueService;
|
||||
}
|
||||
|
||||
@PostMapping("/insert/solo")
|
||||
public ResponseEntity<ResponseMessage> insertSolo(@RequestBody QueueInsertSoloRequest request) {
|
||||
return ResponseMessage.of(HttpStatus.OK, queueService.insertSingle(request.getLocationId(), request.getUserId()));
|
||||
@PostMapping("/insert/single")
|
||||
public ResponseEntity<ResponseMessage> insertSingle(@RequestBody QueueInsertSingleRequest request) {
|
||||
return ResponseMessage.of(HttpStatus.OK, queueService.insertSingle(request.getLocationId(), request.getUserId(), request.isSolo()));
|
||||
}
|
||||
|
||||
@PostMapping("/insert/pair")
|
||||
public ResponseEntity<ResponseMessage> insertPair() {
|
||||
return ResponseMessage.of(HttpStatus.OK, null, "Insert group");
|
||||
public ResponseEntity<ResponseMessage> insertPair(@RequestBody QueueInsertPairRequest request) {
|
||||
List<Queue> queue = queueService.insertPair(request.getLocationId(), request.getUserId1(), request.getUserId2());
|
||||
return ResponseMessage.of(HttpStatus.OK, queue, "Insert pair success");
|
||||
}
|
||||
|
||||
@GetMapping("/getByLocation/{id}")
|
||||
@@ -35,7 +40,7 @@ public class QueueController {
|
||||
}
|
||||
|
||||
@PostMapping("/reorder")
|
||||
public ResponseEntity<ResponseMessage> reorderQueue(@RequestBody ReorderQueueRequest request) {
|
||||
public ResponseEntity<ResponseMessage> reorderQueue(@RequestBody QueueReorderRequest request) {
|
||||
|
||||
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")
|
||||
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;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Data
|
||||
public class QueueInsertSoloRequest {
|
||||
@Getter
|
||||
@Setter
|
||||
public class QueueInsertSingleRequest {
|
||||
|
||||
private String userId;
|
||||
private String locationId;
|
||||
private boolean solo;
|
||||
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class ReorderQueueRequest {
|
||||
public class QueueReorderRequest {
|
||||
|
||||
private String locationId;
|
||||
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 lastOrder = queueRepository.findLastOrderByLocationId(locationId);
|
||||
|
||||
if (nextOrder == null || nextOrder.equals(lastOrder)) nextOrder = lastOrder + 1;
|
||||
if (nextOrder == null) nextOrder = lastOrder + 1;
|
||||
|
||||
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.repository.LocationRepository;
|
||||
import id.luxic.mai_queue.repository.QueueRepository;
|
||||
import id.luxic.mai_queue.tools.IdGenerator;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
public class QueueService {
|
||||
@@ -21,8 +23,39 @@ public class QueueService {
|
||||
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
|
||||
public Queue insertSingle(String locationId, String userId) {
|
||||
public Queue insertSingle(String locationId, String userId, Boolean solo) {
|
||||
Integer lastOrder = queueRepository.findLastOrderByLocationId(locationId);
|
||||
Integer nextOrder = lastOrder == null ? 1 : lastOrder + 1;
|
||||
|
||||
@@ -37,6 +70,7 @@ public class QueueService {
|
||||
.player(User.builder().id(userId).build())
|
||||
.order(nextOrder)
|
||||
.pairId(null)
|
||||
.solo(solo != null && solo)
|
||||
.build());
|
||||
|
||||
// Auto Reorder (Pending)
|
||||
@@ -61,6 +95,10 @@ public class QueueService {
|
||||
// From must be higher order than target
|
||||
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
|
||||
if (to > from) {
|
||||
int temp = to;
|
||||
@@ -68,8 +106,6 @@ public class QueueService {
|
||||
from = temp;
|
||||
}
|
||||
|
||||
System.out.println("to: " + to + ", from: " + from);
|
||||
|
||||
if (to < location.getCurrentQueue()) {
|
||||
throw new IllegalArgumentException("Target queue is lower than current queue");
|
||||
}
|
||||
@@ -87,6 +123,19 @@ public class QueueService {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user