/// <summary> /// Edits the queue details in the database. /// </summary> /// <param name="id"></param> /// <param name="request"></param> /// <returns></returns> public bool UpdateQueueItem(int id, UpdateQueueItemRequest request) { using (helpdesksystemContext context = new helpdesksystemContext()) { Queueitem item = context.Queueitem.FirstOrDefault(p => p.ItemId == id); if (item == null) { throw new NotFoundException($"Unable to find queue item with id [{id}]"); } Topic topic = context.Topic.FirstOrDefault(t => t.TopicId == request.TopicID); // Check that the topic the queue item wants to update to actually exists. if (topic == null) { throw new NotFoundException($"Unable to find topic with id [{request.TopicID}]"); } item.TopicId = request.TopicID; item.Description = request.Description; context.SaveChanges(); return(true); } }
/// <summary> /// Converts the queue item DTO to a DAO to interact with the database /// </summary> /// <param name="queueItemDTO">The DTO for the queue item</param> /// <returns>The DAO for the queue item</returns> private Queueitem DTO2DAO(QueueItemDTO queueItemDTO) { Queueitem queueItem = new Queueitem { ItemId = queueItemDTO.ItemId, StudentId = queueItemDTO.StudentId, TopicId = queueItemDTO.TopicId, Description = queueItemDTO.Description, TimeAdded = queueItemDTO.TimeAdded, TimeHelped = queueItemDTO.TimeHelped, TimeRemoved = queueItemDTO.TimeRemoved }; return(queueItem); }
/// <summary> /// Converts the queue item DAO to a DTO to send to the front end /// </summary> /// <param name="queueItem">The DAO for the queue item</param> /// <returns>The DTO for the queue item</returns> private QueueItemDTO DAO2DTO(Queueitem queueItem) { QueueItemDTO queueItemDTO = null; queueItemDTO = new QueueItemDTO(); queueItemDTO.ItemId = queueItem.ItemId; queueItemDTO.StudentId = queueItem.StudentId; queueItemDTO.Nickname = queueItem.Student.NickName; queueItemDTO.TopicId = queueItem.TopicId; queueItemDTO.Topic = queueItem.Topic.Name; queueItemDTO.Unit = queueItem.Topic.Unit.Name; queueItemDTO.TimeAdded = queueItem.TimeAdded; queueItemDTO.TimeHelped = queueItem.TimeHelped; queueItemDTO.TimeRemoved = queueItem.TimeRemoved; queueItemDTO.Description = queueItem.Description; return(queueItemDTO); }
/// <summary> /// Used to add a queue item to the database /// </summary> /// <param name="request">The request with the information to be added</param> /// <returns>The id of the queue item or null if not successful</returns> public int AddToQueue(AddToQueueRequest request) { int?id = null; using (helpdesksystemContext context = new helpdesksystemContext()) { Queueitem item = new Queueitem() { StudentId = request.StudentID.Value, TimeAdded = DateTime.Now, TopicId = request.TopicID, Description = request.Description, }; context.Queueitem.Add(item); context.SaveChanges(); id = item.ItemId; if (request.CheckInID.HasValue) { Checkinhistory checkinhistory = context.Checkinhistory.FirstOrDefault(ci => ci.CheckInId == request.CheckInID.Value); if (checkinhistory == null) { throw new NotFoundException($"Unable to find queue item with id [{request.CheckInID.Value}]"); } Checkinqueueitem checkinqueueitem = new Checkinqueueitem() { CheckInId = request.CheckInID.Value, QueueItemId = item.ItemId, }; context.Checkinqueueitem.Add(checkinqueueitem); } context.SaveChanges(); } return(id.Value); }
/// <summary> /// Used to update a queue item status in the database. /// </summary> /// <param name="request"></param> /// <returns></returns> public bool UpdateQueueItemStatus(int id, UpdateQueueItemStatusRequest request) { using (helpdesksystemContext context = new helpdesksystemContext()) { Queueitem item = context.Queueitem.FirstOrDefault(p => p.ItemId == id); if (item == null) { throw new NotFoundException($"Unable to find queue item with id [{id}]"); } if (request.TimeHelped != null && request.TimeRemoved == null && item.TimeHelped == null) { // Update TimeHelped item.TimeHelped = request.TimeHelped; context.SaveChanges(); return(true); } if (request.TimeRemoved != null && request.TimeHelped == null && item.TimeRemoved == null) { if (request.TimeRemoved <= item.TimeHelped) { // TimeRemoved date is earlier than TimeHelped date - this can not happen. return(false); } // Update TimeRemoved item.TimeRemoved = request.TimeRemoved; context.SaveChanges(); return(true); } // something went wrong. Shouldn't happen if the facade validation did its job. // Means both Helped and Removed values are assigned or null. // Could also mean a TimeHelped or TimeRemoved was provided twice. // E.g. you tried to provide TimeHelped when TimeHelped was alrady set for that queue item. return(false); } }
public void CheckOutWithQueueItems() { Unit unit = new Unit() { Code = AlphaNumericStringGenerator.GetString(8), IsDeleted = false, Name = AlphaNumericStringGenerator.GetString(10), }; Topic topic = new Topic() { Name = AlphaNumericStringGenerator.GetString(10), IsDeleted = false }; using (helpdesksystemContext context = new helpdesksystemContext()) { context.Unit.Add(unit); topic.UnitId = unit.UnitId; context.Topic.Add(topic); context.SaveChanges(); } CheckInRequest request = new CheckInRequest() { UnitID = unit.UnitId, Nickname = AlphaNumericStringGenerator.GetString(10), SID = AlphaNumericStringGenerator.GetStudentIDString() }; CheckInFacade facade = new CheckInFacade(); CheckInResponse response = facade.CheckIn(request); Assert.AreEqual(HttpStatusCode.OK, response.Status); Assert.IsTrue(response.CheckInID > 0); Queueitem queueitem = new Queueitem() { StudentId = response.StudentID, TopicId = topic.TopicId, Description = "Check In Unit Test", TimeAdded = DateTime.Now }; using (helpdesksystemContext context = new helpdesksystemContext()) { context.Queueitem.Add(queueitem); Checkinqueueitem checkinqueueitem = new Checkinqueueitem() { CheckInId = response.CheckInID, QueueItemId = queueitem.ItemId }; context.Checkinqueueitem.Add(checkinqueueitem); context.SaveChanges(); } CheckOutRequest coRequest = new CheckOutRequest() { ForcedCheckout = false }; CheckOutResponse coResponse = facade.CheckOut(coRequest, response.CheckInID); Assert.AreEqual(HttpStatusCode.OK, coResponse.Status); Assert.IsTrue(coResponse.Result); using (helpdesksystemContext context = new helpdesksystemContext()) { Checkinhistory checkOut = context.Checkinhistory.FirstOrDefault(co => co.CheckInId == response.CheckInID); Queueitem item = context.Queueitem.FirstOrDefault(qi => qi.ItemId == queueitem.ItemId); Assert.IsNotNull(checkOut); var baseTime = DateTime.Now.AddMinutes(-1); var addTime = checkOut.CheckoutTime; var timeDiff = baseTime.CompareTo(addTime); Assert.IsTrue(timeDiff == -1); Assert.AreEqual(coRequest.ForcedCheckout, checkOut.ForcedCheckout); Assert.IsNotNull(item); var baseQueueTime = DateTime.Now.AddMinutes(-1); var removeQueueTime = item.TimeRemoved; var queueTimeDiff = baseQueueTime.CompareTo(removeQueueTime); Assert.IsTrue(queueTimeDiff == -1); } }
public void JoinQueueNoCheckInNewStudent() { Helpdesksettings helpdesk = new Helpdesksettings() { HasQueue = true, HasCheckIn = false, IsDeleted = false, Name = AlphaNumericStringGenerator.GetString(10), }; Unit unit = new Unit() { Code = AlphaNumericStringGenerator.GetString(8), IsDeleted = false, Name = AlphaNumericStringGenerator.GetString(10), }; Topic topic = new Topic() { IsDeleted = false, Name = AlphaNumericStringGenerator.GetString(10), }; using (helpdesksystemContext context = new helpdesksystemContext()) { context.Helpdesksettings.Add(helpdesk); context.Unit.Add(unit); context.SaveChanges(); Helpdeskunit helpdeskunit = new Helpdeskunit() { HelpdeskId = helpdesk.HelpdeskId, UnitId = unit.UnitId }; context.SaveChanges(); topic.UnitId = unit.UnitId; context.Topic.Add(topic); context.SaveChanges(); } AddToQueueRequest request = new AddToQueueRequest() { Nickname = AlphaNumericStringGenerator.GetString(10), SID = AlphaNumericStringGenerator.GetStudentIDString(), TopicID = topic.TopicId, Description = "JoinQueueNoCheckInNewStudent Test" }; QueueFacade facade = new QueueFacade(); AddToQueueResponse response = facade.AddToQueue(request); Assert.AreEqual(HttpStatusCode.OK, response.Status); Assert.IsTrue(response.ItemId > 0); using (helpdesksystemContext context = new helpdesksystemContext()) { Queueitem queueitem = context.Queueitem.FirstOrDefault(qi => qi.ItemId == response.ItemId); var baseTime = DateTime.Now.AddMinutes(-1); var addTime = queueitem.TimeAdded; Assert.AreEqual(request.TopicID, queueitem.TopicId); var timeDiff = baseTime.CompareTo(addTime); Assert.IsTrue(timeDiff == -1); Nicknames nicknames = context.Nicknames.FirstOrDefault(n => n.StudentId == queueitem.StudentId); Assert.AreEqual(request.Nickname, nicknames.NickName); Assert.AreEqual(request.SID, nicknames.Sid); } }