Exemplo n.º 1
0
        /// <summary>
        /// Allows to add copies of given cartridge
        /// </summary>
        /// <param name="amountAll">Amout of all copies to add</param>
        /// <param name="amountAvaible">Amount of copies avaible. Rest will be unavaible.</param>
        /// <param name="catridgeId">Cartridge id</param>
        private async Task AddCopies(int amountAll, int amountAvaible, Guid catridgeId)
        {
            var copies = new List <CartridgeCopy>(amountAll);

            for (int i = 0; i < amountAll; ++i)
            {
                copies.Add(new CartridgeCopy
                {
                    CartridgeId = catridgeId,
                    Avaible     = i < amountAvaible
                });
            }

            //var tasks = new List<Task>(amountAll);
            foreach (var c in copies)
            {
                await CartridgeCopyRepo.CreateAsync(c);

                await CartridgeCopyRepo.SaveChangesAsync();

                //tasks.Add(CartridgeCopyRepo.CreateAsync(c));
            }
            //await Task.WhenAll(tasks);
            //await CartridgeCopyRepo.SaveChangesAsync();
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <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();
        }
Exemplo n.º 4
0
        /// <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);
        }