Esempio n. 1
0
        public async Task <IActionResult> PostQueueEntryModel([FromBody] QueueEntryModel queueEntryModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.QueueEntries.Add(queueEntryModel);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (QueueEntryModelExists(queueEntryModel.QueueId))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetQueueEntryModel", new { id = queueEntryModel.QueueId }, queueEntryModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> ResolveEntryByName(string entryName, int queueId)
        {
            QueueEntryModel entryToDelete = _context.QueueEntries.Where(entry => entry.IdentificationCode == entryName &&
                                                                        entry.QueueId == queueId).Single();


            if (entryToDelete == null)
            {
                return(BadRequest("Entry not found"));
            }
            else
            {
                if (queueHelper.EntryIsAtTheReady(entryToDelete))
                {
                    _context.QueueEntries.Remove(entryToDelete);
                    await _context.SaveChangesAsync();

                    return(Ok()); // TODO: Check/Update ETA?
                }
                else
                {
                    return(BadRequest("Entry is not ready yet"));
                }
            }
        }
        public async Task <IActionResult> GetReadyState(int queueEntryId)
        {
            QueueEntryModel queueEntry = await _context.QueueEntries.FindAsync(queueEntryId);

            if (queueEntry == null)
            {
                return(BadRequest("Entry not found"));
            }

            QueueModel queue = await _context.Queues.FindAsync(queueEntry.QueueId);

            if (queueEntry == null)
            {
                return(BadRequest("Queue not found"));
            }

            int entriesBefore = _context.QueueEntries.Where(entry => entry.EntryTime < queueEntry.EntryTime &&
                                                            entry.QueueId == queueEntry.QueueId).Count();

            if (entriesBefore < queue.AtTheReadyCount)
            {
                return(Ok(true));
            }
            else
            {
                return(Ok(false));
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> PutQueueEntryModel([FromRoute] int id, [FromBody] QueueEntryModel queueEntryModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != queueEntryModel.QueueId)
            {
                return(BadRequest());
            }

            _context.Entry(queueEntryModel).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!QueueEntryModelExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> GetEstimatedWaitingTime(int queueEntryId)
        {
            QueueEntryModel queueEntry = await _context.QueueEntries.FindAsync(queueEntryId);

            if (queueEntry == null)
            {
                return(BadRequest("Entry not found"));
            }

            QueueModel queue = await _context.Queues.FindAsync(queueEntry.QueueId);

            if (queueEntry == null)
            {
                return(BadRequest("Queue not found"));
            }

            int averageWaitTime = queue.AverageWaitTimeSeconds;

            int entriesBefore = _context.QueueEntries.Where(entry => entry.EntryTime < queueEntry.EntryTime &&
                                                            entry.QueueId == queueEntry.QueueId).Count();

            int entriesUntilReady = entriesBefore - queue.AtTheReadyCount;

            if (entriesUntilReady < 0)
            {
                entriesUntilReady = 0;
            }

            return(Ok(entriesUntilReady * averageWaitTime));
        }
        public bool EntryIsAtTheReady(QueueEntryModel entry)
        {
            int entriesBefore        = _context.QueueEntries.Where(item => item.QueueId == entry.QueueId && item.EntryTime < entry.EntryTime).Count();
            int queueAtTheReadyCount = _context.Queues.Where(queue => queue.Id == entry.QueueId).Single().AtTheReadyCount;

            return(entriesBefore < queueAtTheReadyCount);
        }
Esempio n. 7
0
        public async Task <IActionResult> DeleteEntry([FromRoute] int entryId)
        {
            QueueEntryModel entryToDelete = await _context.QueueEntries.FindAsync(entryId);

            if (entryToDelete == null)
            {
                return(BadRequest("Entry not found"));
            }
            else
            {
                int queueId = entryToDelete.QueueId;
                _context.QueueEntries.Remove(entryToDelete);
                await _context.SaveChangesAsync();

                await queueHelper.RemoveTimedoutQueueEntries(queueId);

                await queueHelper.UpdateAtTheReady(queueId);

                return(Ok()); // TODO: Check/Update ETA
            }
        }
        public async Task UpdateAtTheReady(int queueId)
        {
            IEnumerable <QueueEntryModel> queueEntries = _context.QueueEntries.Where(entry => entry.QueueId == queueId).OrderBy(entry => entry.EntryTime);

            QueueModel queue = await _context.Queues.FindAsync(queueId);

            int atTheReadyCount = queue.AtTheReadyCount;

            for (int i = 0; i < atTheReadyCount; i++)
            {
                QueueEntryModel entry = queueEntries.ElementAtOrDefault(i);
                if (entry == null)
                {
                    break;
                }
                if (entry.WasReadyAt != null)
                {
                    entry.WasReadyAt = DateTime.Now;
                }
            }
        }
Esempio n. 9
0
        public async Task <IActionResult> AddEntry(int queueId, int?userId = null)
        {
            if (_context.Queues.Where(queue => queue.Id == queueId).Count() != 1)
            {
                return(BadRequest("Failed to find queue"));
            }

            if (queueHelper.QueueIsFull(queueId))
            {
                return(BadRequest("Queue is full"));
            }

            if (!queueHelper.QueueIsOpen(queueId))
            {
                return(BadRequest("Queue is closed"));
            }

            QueueEntryModel entry = new QueueEntryModel()
            {
                UserId             = userId,
                QueueId            = queueId,
                EntryTime          = DateTime.Now,
                Priority           = 0, // TODO
                IdentificationCode = QueueHelper.GenerateQueueIdentification(queueId, _context)
            };

            _context.QueueEntries.Add(entry);

            try
            {
                await _context.SaveChangesAsync();

                return(Ok(entry.Id));
            }
            catch (Exception ex)
            {
                ExceptionHandler.LogException(ex.StackTrace, ExceptionHandler.ErrorLevel.WARNING, _context);
                return(BadRequest("Failed to insert entry"));
            }
        }
Esempio n. 10
0
        public async Task <IActionResult> ResolveEntry(int entryId)
        {
            QueueEntryModel entryToDelete = await _context.QueueEntries.FindAsync(entryId);

            if (entryToDelete == null)
            {
                return(BadRequest("Entry not found"));
            }
            else
            {
                if (queueHelper.EntryIsAtTheReady(entryToDelete))
                {
                    _context.QueueEntries.Remove(entryToDelete);
                    await _context.SaveChangesAsync();

                    return(Ok()); // TODO: Check/Update ETA?
                }
                else
                {
                    return(BadRequest("Entry is not ready yet"));
                }
            }
        }
        public async Task <IActionResult> GetPositionInQueue(int queueEntryId)
        {
            QueueEntryModel queueEntry = await _context.QueueEntries.FindAsync(queueEntryId);

            if (queueEntry == null)
            {
                return(BadRequest("Entry not found"));
            }

            QueueModel queue = await _context.Queues.FindAsync(queueEntry.QueueId);

            if (queueEntry == null)
            {
                return(BadRequest("Queue not found"));
            }

            int entriesBefore = _context.QueueEntries.Where(entry => entry.EntryTime < queueEntry.EntryTime &&
                                                            entry.QueueId == queueEntry.QueueId).Count();

            int position = entriesBefore - queue.AtTheReadyCount;

            return(Ok(position < 0 ? 1 : ++position));
        }
 public QueueEntryErrorableModel(QueueEntryModel qem) : base(qem)
 {
     HasError     = false;
     ErrorMessage = "";
 }