public CarMaintenance Create(CarMaintenanceViewModel toCreate) { Validate(toCreate); // If the mileage is less than 1, pull the mileage from the car if (toCreate.Mileage < 1) { toCreate.Mileage = _db.Cars.First(c => c.CarId == toCreate.CarId).Mileage ?? 0; } var maint = new CarMaintenance() { CarId = toCreate.CarId, Date = toCreate.Date, Mileage = toCreate.Mileage, Type = toCreate.Type, Notes = toCreate.Notes, Active = true }; _db.CarMaintenances.Add(maint); _db.SaveChanges(); return(maint); }
private void Validate(CarMaintenanceViewModel vm) { if (!_db.Cars.Any(c => c.CarId == vm.CarId)) { throw new EntityValidationException("No car with the given id exists."); } if (vm.Mileage < 0) { throw new EntityValidationException("Mileage cannot be less than 0."); } }
public static CarMaintenanceViewModel ToViewModel(this CarMaintenance carMaint) { var vm = new CarMaintenanceViewModel() { Type = carMaint.Type, Date = carMaint.Date, CarId = carMaint.CarId, Mileage = carMaint.Mileage, CarMaintenanceId = carMaint.CarMaintenanceId, Notes = carMaint.Notes }; return(vm); }
public CarMaintenance Update(CarMaintenanceViewModel toUpdate) { var maint = Get(toUpdate.CarMaintenanceId); if (null == maint) { throw new EntityValidationException("No Car Maintenance with the given id exists."); } Validate(toUpdate); maint.CarId = toUpdate.CarId; maint.Date = toUpdate.Date; maint.Mileage = toUpdate.Mileage; maint.Notes = toUpdate.Notes; maint.Type = toUpdate.Type; _db.SaveChanges(); return(maint); }
public IActionResult Update([FromBody] CarMaintenanceViewModel vm) { return(Ok(_carMaintenanceService.Update(vm).ToViewModel())); }