Beispiel #1
0
        public async Task <SimulateRent> simulate(SimulateRent rent)
        {
            foreach (var item in rent.items)
            {
                if (!(await _validateItemStock.validate(item)))
                {
                    return(null);
                }
                item.Laptop = (await _laptopRepository.findByIdAsync(item.LaptopId)).Value;
            }
            int daysAmount = (
                DateTime
                .ParseExact(
                    rent.rentExpirationDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces)
                .Subtract(
                    DateTime
                    .ParseExact(
                        rent.rentDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces))).Days;

            rent.customer = await _customerRepository.findByIdAsync(rent.customerId);

            rent.items
            .ToList()
            .ForEach(item =>
            {
                rent.fullPrice += (item.Laptop.DailyPrice * item.Quantity * daysAmount);
            });


            return(rent);
        }
Beispiel #2
0
        public async Task <ActionResult <LaptopOutput> > findByIdAsync(int id)
        {
            var laptop = (await _laptopRepository.findByIdAsync(id)).Value;

            if (laptop != null)
            {
                return(new LaptopOutput(laptop));
            }
            return(null);
        }
Beispiel #3
0
        public async Task <bool> validate(RentItemInput item)
        {
            var laptop = (await _laptopRepository.findByIdAsync(item.LaptopId)).Value;

            if (laptop.StockAmount > 0 && laptop.StockAmount >= item.Quantity)
            {
                laptop.StockAmount -= 1;
                await _laptopRepository.update(laptop);
            }
            else
            {
                return(false);
            }
            return(true);
        }
Beispiel #4
0
        public async Task <ActionResult <RentOutput> > create(RentInput rent)
        {
            foreach (var item in rent.Items)
            {
                if (!(await _validateItemStock.validate(new RentItemInput(item))))
                {
                    return(null);
                }
                item.laptop = (await _laptopRepository.findByIdAsync(item.laptopId)).Value;
            }
            rent.Status = "efetuado";
            await _rentRepository.addAsync(new Rent(rent));

            rent.Customer = await _customerRepository.findByIdAsync(rent.CustomerId);

            return(new RentOutput(new Rent(rent)));
        }
Beispiel #5
0
        public async Task refund(int id)
        {
            var rent = await _rentRepository.findByIdAsync(id);

            if (rent.Status != "devolvido")
            {
                foreach (var item in rent.Items)
                {
                    var laptop = (await _laptopRepository.findByIdAsync(item.laptopId)).Value;
                    laptop.StockAmount += item.quantity;
                    await _laptopRepository.update(laptop);
                }

                rent.Status = "devolvido";

                await _rentRepository.update(rent);
            }
        }