Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            CarRentInformation = await _context.CarRentInformation.FindAsync(id);

            if (CarRentInformation != null)
            {
                _context.CarRentInformation.Remove(CarRentInformation);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            CarRentInformation = await _context.CarRentInformation
                                 .Include(c => c.Client)
                                 .Include(c => c.Employee)
                                 .Include(c => c.Vehicle).FirstOrDefaultAsync(m => m.Id == id);

            if (CarRentInformation == null)
            {
                return(NotFound());
            }
            return(Page());
        }
        public async Task <bool> Create(CarRentInformation entity)
        {
            if (entity == null)
            {
                return(false);
            }
            entity.RentSequence = Guid.NewGuid().ToString().ToUpper();
            entity.RentStateId  = (int)RentStateEnum.CheckWaiting;
            entity.CreationDate = DateTime.Now;
            entity.UpdateDate   = DateTime.Now;
            entity.Active       = true;

            await using var contextTransaction = await _dbContext.Database.BeginTransactionAsync();

            try
            {
                await _dbContext.CarRentInformation.AddAsync(entity);

                var result = await _dbContext.SaveChangesAsync();

                if (result > 0)
                {
                    var resultVehicle = await _vehicleService.UpdateState(entity.VehicleId, VehicleStateEnum.EsperaCheck);

                    if (resultVehicle)
                    {
                        await contextTransaction.CommitAsync();

                        return(true);
                    }

                    await contextTransaction.RollbackAsync();
                }
            }
            catch (Exception e)
            {
                await contextTransaction.RollbackAsync();

                return(false);
            }

            return(true);
        }
        public async Task <bool> Update(CarRentInformation entity)
        {
            if (entity == null)
            {
                return(false);
            }

            var entityUpdate = await GetSingleById(entity.Id);

            if (entityUpdate == null)
            {
                return(false);
            }

            entityUpdate.RentStateId  = entity.RentStateId;
            entityUpdate.AmountPerDay = entity.AmountPerDay;
            entityUpdate.ClientId     = entity.ClientId;
            entityUpdate.Comment      = entity.Comment;
            entityUpdate.RentDays     = entity.RentDays;
            entityUpdate.ReturnDay    = entity.ReturnDay;
            entityUpdate.UpdateDate   = DateTime.Now;
            entityUpdate.Active       = entity.Active;

            using (var contextTransaction = await _dbContext.Database.BeginTransactionAsync())
            {
                _dbContext.CarRentInformation.Update(entityUpdate);

                try
                {
                    var result = await _dbContext.SaveChangesAsync();

                    if (result > 0)
                    {
                        var vehicleResult = (RentStateEnum)entity.RentStateId switch
                        {
                            RentStateEnum.CheckWaiting => await _vehicleService.UpdateState(entity.VehicleId,
                                                                                            VehicleStateEnum.EsperaCheck),

                            RentStateEnum.Active => await _vehicleService.UpdateState(entity.VehicleId,
                                                                                      VehicleStateEnum.Rentado),

                            RentStateEnum.Canceled => await _vehicleService.UpdateState(entity.VehicleId,
                                                                                        VehicleStateEnum.Disponible),

                            RentStateEnum.Delivered => await _vehicleService.UpdateState(entity.VehicleId,
                                                                                         VehicleStateEnum.Disponible),

                            RentStateEnum.WaitingDelivery => await _vehicleService.UpdateState(entity.VehicleId,
                                                                                               VehicleStateEnum.EnEsperaEntrega),

                            RentStateEnum.CheckComplete => await _vehicleService.UpdateState(entity.VehicleId,
                                                                                             VehicleStateEnum.Chequeado),
                            _ => true
                        };

                        if (!vehicleResult)
                        {
                            await contextTransaction.RollbackAsync();
                        }
                    }
                    await contextTransaction.CommitAsync();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    await contextTransaction.RollbackAsync();

                    return(false);
                }

                return(true);
            }
        }