public async Task EditCarAsync(int id, DeliveryCarViewModel model) { try { var car = this.carRepository.All().FirstOrDefault(e => e.Id == id); if (car == null) { throw new ApplicationException(string.Format(ErrorMessages.CarDoesNotExist, id)); } car.Model = model.Model; car.Mileage = model.Mileage; car.ProductionDate = model.ProductionDate; car.TankCapacity = model.TankCapacity; if (model.RestaurantId != null) { car.RestaurantId = int.Parse(model.RestaurantId); } this.carRepository.Update(car); await this.carRepository.SaveChangesAsync(); this.logger.LogInformation(string.Format(LogMessages.DeliveryCarUpdated, car.Id)); } catch (Exception e) { throw new ApplicationException(e.Message); } }
public async Task <IActionResult> Add(DeliveryCarViewModel carModel) { if (ModelState.IsValid) { await this.carService.AddCarAsync(carModel); TempData[TempDataKeys.TempDataSuccessCarKey] = string.Format(SuccessMessages.CarSuccessMessage, carModel.Model); } return(RedirectToAction(StringConstants.Manage, StringConstants.Manage, new { area = StringConstants.Manager })); }
public async Task <IActionResult> Edit(int id) { DeliveryCarViewModel model = null; try { model = await this.carService.MapCarAsync(id); } catch (Exception) { TempData[TempDataKeys.TempDataNonExistentCarKey] = string.Format(ErrorMessages.CarDoesNotExist, id); return(RedirectToAction(StringConstants.Manage, StringConstants.Manage, new { area = StringConstants.Manager })); } return(this.View(model)); }
public async Task <IActionResult> Edit(int id, DeliveryCarViewModel model) { try { if (ModelState.IsValid) { await this.carService.EditCarAsync(id, model); TempData[TempDataKeys.TempDataSuccessEditCarKey] = string.Format(SuccessMessages.CarEditSuccessMessage, model.Model); } } catch (Exception) { TempData[TempDataKeys.TempDataNonExistentCarKey] = string.Format(ErrorMessages.CarDoesNotExist, id); return(RedirectToAction(StringConstants.Manage, StringConstants.Manage, new { area = StringConstants.Manager })); } return(RedirectToAction(StringConstants.Cars, StringConstants.Car, new { area = StringConstants.Manager })); }
public async Task AddCarAsync(DeliveryCarViewModel model) { try { var car = this.mapper.Map <DeliveryCar>(model); if (model.RestaurantId != null) { car.RestaurantId = int.Parse(model.RestaurantId); } await this.carRepository.AddAsync(car); await this.carRepository.SaveChangesAsync(); this.logger.LogInformation(string.Format(LogMessages.DeliveryCarAdded, car.Id)); } catch (Exception e) { throw new ApplicationException(e.Message); } }