/// <summary> /// Gets cartridge for rent by id /// </summary> /// <param name="id">Cartridge id</param> /// <returns>Cartridge for rent</returns> public async Task <CartridgeForRent> CartridgeForRent(Guid id) { var cartridgeDb = await CartridgeRepo .GetAll() .Include(c => c.Movie) .ThenInclude(m => m.Images) .ThenInclude(i => i.Image) .AsNoTracking() .FirstOrDefaultAsync(c => c.Id == id); if (cartridgeDb == null) { return(null); } var result = Mapper.Map <CartridgeForRent>(cartridgeDb); result.Avaible = await CartridgeCopyRepo .GetAll(cc => cc.CartridgeId == result.Id) .Where(cc => cc.Avaible) .CountAsync(); result.OtherLanguages = await CartridgeRepo .GetAll(c => c.MovieId == cartridgeDb.MovieId && c.Id != id) .Select(c => new LanguageLink { CartridgeId = c.Id, Language = c.Language }) .ToListAsync(); return(result); }
/// <summary> /// Gets cartridges for rent list /// </summary> /// <returns>List of cartridges for rent</returns> public async Task <IEnumerable <CartridgeForRentList> > CartridgesForRent() { var cartridgesDb = await CartridgeRepo .GetAll() .Include(c => c.Movie) .AsNoTracking() .ToListAsync(); var result = new List <CartridgeForRentList>(cartridgesDb.Count); foreach (var c in cartridgesDb) { var cartridgeResult = Mapper.Map <CartridgeForRentList>(c); cartridgeResult.MovieCoverUrl = await MovieService.GetMovieCoverUrl(c.MovieId); cartridgeResult.CopiesAvaible = await CartridgeCopyRepo .GetAll(cc => cc.CartridgeId == c.Id) .Where(cc => cc.Avaible) .CountAsync(); cartridgeResult.Language = c.Language; result.Add(cartridgeResult); } return(result); }
/// <summary> /// Updates return date of given rental /// </summary> /// <param name="rentalId">Rental id</param> /// <param name="request">Update request with date to set</param> /// <returns>True if successful</returns> public async Task <bool> UpdateReturnDate(Guid rentalId, RentalUpdateDateRequest request) { var rental = await RentalRepo .GetAll() .FirstOrDefaultAsync(r => r.Id == rentalId); var cartridgeCopy = await CartridgeCopyRepo .GetAll() .FirstOrDefaultAsync(cc => cc.Id == rental.CartridgeCopyId); if (rental == null) { return(false); } rental.Returned = request.Date; if (request.Date == null) { cartridgeCopy.Avaible = false; } else { cartridgeCopy.Avaible = true; } CartridgeCopyRepo.Update(cartridgeCopy); await CartridgeCopyRepo.SaveChangesAsync(); await RentalRepo.SaveChangesAsync(); return(true); }
/// <summary> /// Helper for creating copies of cartridge /// </summary> /// <param name="amount">Amount to create</param> /// <param name="val">True to create avaible cartridge copies</param> /// <param name="cartridgeId">Cartridge id to create copies</param> private async Task MakeCopiesAvaOrUnava(int amount, bool val, Guid cartridgeId) { var copies = await CartridgeCopyRepo .GetAll(c => c.CartridgeId == cartridgeId) .ToListAsync(); var copiesAva = copies.Where(c => c.Avaible == !val).ToList(); for (int i = 0; i < amount; ++i) { copiesAva[i].Avaible = val; } await CartridgeCopyRepo.SaveChangesAsync(); }
/// <summary> /// Gets data for given cartridge rent form /// </summary> /// <param name="cartridgeId">Cartridge to rent id</param> /// <param name="userId">User that wants to rent it</param> /// <returns>Data for cartridge rental form</returns> public async Task <CartridgeRental> CartridgeForRentForm(Guid cartridgeId, Guid userId) { var userDb = await ShopUserRepo .GetAll() .AsNoTracking() .FirstOrDefaultAsync(u => u.UserId == userId.ToString()); if (userDb == null) { return(null); } var unsettledRentals = await RentalRepo .GetAll(r => r.ShopUserId == userDb.Id) .Where(r => r.Returned == null) .ToListAsync(); var result = new CartridgeRental(); if (unsettledRentals.Count >= 3) { result.UserCanBorrow = false; return(result); } if (unsettledRentals.Any(r => DateTime.UtcNow > r.Rented.AddDays(r.DaysToReturn))) { result.UserCanBorrow = false; return(result); } result.UserCanBorrow = true; var cartridgeDb = await CartridgeRepo .GetAll() .AsNoTracking() .Include(c => c.Movie) .FirstOrDefaultAsync(c => c.Id == cartridgeId); if (cartridgeDb == null) { return(null); } var copiesAvaible = await CartridgeCopyRepo .GetAll(c => c.CartridgeId == cartridgeId) .CountAsync(c => c.Avaible); result.Avaible = copiesAvaible; if (copiesAvaible <= 0) { return(result); } Mapper.Map(cartridgeDb, result); var userAddressesDb = await AddressRepo .GetAll(a => a.UserId == userId.ToString() && a.IsActive) .ToListAsync(); result.Addresses = Mapper.Map <IEnumerable <AddressDto> >(userAddressesDb); return(result); }
/// <summary> /// Rents 1st of avaible catridge copies by given cartridge id to given user /// Adds new Rental to database /// </summary> /// <param name="cartridgeId">Cartridge id</param> /// <param name="userId">User that wants to rent id</param> /// <param name="request">Rental request</param> /// <returns>Null if cartridge or user is not found. True if user could rent given cartridge, false otherwise.</returns> public async Task <bool?> RentCartridge(Guid cartridgeId, Guid userId, CartridgeRentRequest request) { var cartridge = await CartridgeRepo .GetAll() .AsNoTracking() .FirstOrDefaultAsync(c => c.Id == cartridgeId); if (cartridge == null) { return(null); } var copy = await CartridgeCopyRepo .GetAll() .FirstOrDefaultAsync(c => c.CartridgeId == cartridgeId && c.Avaible); if (copy == null) { return(false); } var address = new Address(); if (request.Delivery == collectionInPerson) { address = null; } else if (request.AddAddress) { request.NewAddress.UserId = userId.ToString(); address = await AddressService.CreateAddress(request.NewAddress); } else { address = await AddressRepo .GetAll() .FirstOrDefaultAsync(a => a.Id == new Guid(request.AddressId)); if (address == null) { return(null); } } var shopUser = await ShopUserRepo .GetAll() .AsNoTracking() .FirstOrDefaultAsync(su => su.UserId == userId.ToString()); var rental = new Rental { DaysToReturn = cartridge.DaysToReturn, RentPrice = cartridge.RentPrice, Rented = DateTime.UtcNow, AddressId = address?.Id, CartridgeCopyId = copy.Id, Delivery = request.Delivery, ShopUserId = shopUser.Id, }; copy.Avaible = false; await CartridgeCopyRepo.SaveChangesAsync(); await RentalRepo.CreateAsync(rental); await RentalRepo.SaveChangesAsync(); return(true); }