/// <summary> /// Checking the item in to the earliest subscriber to have placed a hold /// </summary> /// <param name="assetId"></param> /// <param name="currentHolds"></param> private void CheckoutToEarliestHold(int assetId, IEnumerable <Hold> currentHolds) { Hold earliestHold = currentHolds.OrderBy(a => a.HoldPlaced).FirstOrDefault(); CMSLibraryCard card = earliestHold.LibraryCard; _context.Remove(earliestHold); _context.SaveChanges(); CheckoutItem(assetId, card.Id); }
/// <summary> /// Checks out an CMSLibraryAsset to a library card id and fills the entry in history /// </summary> /// <param name="assetId"></param> /// <param name="libraryCardId"></param> public string CheckoutItem(int assetId, int libraryCardId) { if (IsCheckedOut(assetId)) { return("Double Checked Out!"); // don't double checkout } DateTime now = DateTime.Now; CMSLibraryAsset item = _context.CMSLibraryAsset .Include(a => a.Status) .FirstOrDefault(a => a.Id == assetId); CMSLibraryCard libraryCard = _context.CMSLibraryCard .Include(c => c.Checkouts) .FirstOrDefault(a => a.Id == libraryCardId); if (libraryCard == null) { return("Invalid card!"); // make sure the library card is valid } IEnumerable <Hold> currentHolds = GetCurrentHolds(assetId); if (!currentHolds.Any()) { // Changes the status of the asset to "Checked Out" if the Hold queue is empty UpdateStatus(assetId, "Checked Out"); } Checkout checkout = new Checkout // creating the checkout { LibraryAsset = item, LibraryCard = libraryCard, Since = now, Until = GetDefaultCheckoutTime(now, assetId) }; _context.Add(checkout); CheckoutHistory checkoutHistory = new CheckoutHistory // creating the checkout history { CheckedOut = now, LibraryAsset = item, LibraryCard = libraryCard }; _context.Add(checkoutHistory); _context.SaveChanges(); return(null); }
/// <summary> /// Places a Hold to the item linked to a card id and subscriber /// </summary> /// <param name="assetId"></param> /// <param name="libraryCardId"></param> /// <returns></returns> public string PlaceHold(int assetId, int libraryCardId) { DateTime now = DateTime.Now; CMSLibraryAsset asset = _context.CMSLibraryAsset .Include(a => a.Status) .FirstOrDefault(a => a.Id == assetId); CMSLibraryCard libraryCard = _context.CMSLibraryCard .FirstOrDefault(a => a.Id == libraryCardId); if (libraryCard == null) // when the card is not valid { return("Invalid card!"); } else if (GetLatestCheckout(assetId).LibraryCard.Id == libraryCardId) // when the checkouter is trying to place a hold { return("You cannot place a hold on an item which you have checked out!"); } else if (GetCurrentHolds(assetId).Any(a => a.LibraryCard.Id == libraryCardId)) // when you are trying to place a second hold on an item { return("You have allready placed a hold on that item!"); } _context.Update(asset); if (asset.Status.Name == "Checked Out") { UpdateStatus(assetId, "On Hold"); } Hold hold = new Hold { HoldPlaced = now, LibraryAsset = asset, LibraryCard = libraryCard }; _context.Add(hold); _context.SaveChanges(); return(null); }