Exemplo n.º 1
0
        public async Task <Option <Exception, Unit> > Handle(VehicleUpdateCommand request, CancellationToken cancellationToken)
        {
            Option <Exception, Vehicle> callback = await _repository.GetByPlateAsync(request.Plate);

            if (callback.IsFailure)
            {
                return(callback.Failure);
            }

            var priceCallback = await _priceRepository.GetByDateAsync(callback.Success.CameIn);

            if (priceCallback.IsFailure)
            {
                return(new NotAllowedException("Operação não permitida devido a não havar um preço para o periodo ao qual o carro entou no estacionamento."));
            }

            var timeInParking = DateTime.Now - callback.Success.CameIn;

            var logic = _priceLogicService.AmountPaid(timeInParking, priceCallback.Success);

            if (logic.IsFailure)
            {
                return(logic.Failure);
            }

            callback.Success.Price              = priceCallback.Success.Value;
            callback.Success.Exited             = DateTime.Now;
            callback.Success.AmountPaid         = $"R$ {logic.Success.Item1}";
            callback.Success.TotalTimePaid      = $"{logic.Success.Item2} {(timeInParking.TotalMinutes <= 30 ? "min" : "hrs")}";
            callback.Success.TotalTimeInParking = $"{logic.Success.Item3} hrs";

            return(await _repository.UpdateAsync(callback.Success));
        }
        protected override Option <Exception, IQueryable <VehicleAmountViewModel> > Handle(VehicleAmountQuery request)
        {
            var callback = _repository.GetAll();

            if (callback.IsFailure)
            {
                return(new NotFoundException("Não foi encontrado nenhum usuário com o id informado."));
            }

            List <VehicleAmountViewModel> list = new List <VehicleAmountViewModel>();

            foreach (var item in callback.Success.Where(c => c.CameIn.Date == DateTime.Now.Date || !c.Exited.HasValue).ToList())
            {
                var priceCallback = _priceRepository.GetByDateAsync(item.CameIn).GetAwaiter().GetResult();

                if (priceCallback.IsFailure)
                {
                    return(new NotAllowedException("Operação não permitida devido a não havar um preço para o periodo ao qual o carro entou no estacionamento."));
                }

                var timeInParking = DateTime.Now - item.CameIn;

                var logic = _priceLogicService.AmountPaid(timeInParking, priceCallback.Success);

                if (logic.IsFailure)
                {
                    return(logic.Failure);
                }

                list.Add(new VehicleAmountViewModel
                {
                    Id                 = item.Id,
                    Price              = priceCallback.Success.Value,
                    AmountPaid         = item.AmountPaid ?? $"R$ {logic.Success.Item1}",
                    TotalTimePaid      = item.TotalTimePaid ?? $"{logic.Success.Item2} {(timeInParking.TotalMinutes <= 30 ? "min" : "hrs")}",
                    TotalTimeInParking = item.TotalTimeInParking ?? $"{logic.Success.Item3} hrs",
                    Plate              = item.Plate,
                    CameIn             = item.CameIn,
                    Exited             = item.Exited
                });
            }

            return(Option <Exception, IQueryable <VehicleAmountViewModel> > .Of(list.AsQueryable <VehicleAmountViewModel>()));
        }