/// <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); }