private void SetAttributes(VehicleIncident newVehicleIncident) { VINAttributes attributes; if (_dbContext.ContainsAttributes(newVehicleIncident.VIN)) { attributes = _dbContext.VINAttributes.FirstOrDefault(x => x.VIN == newVehicleIncident.VIN); } else { attributes = _vinDecodeService.GetAttributes(newVehicleIncident.VIN).Result; _dbContext.VINAttributes.Add(attributes); _dbContext.SaveChanges(); } newVehicleIncident.Make = attributes.Make; newVehicleIncident.Model = attributes.Model; newVehicleIncident.Year = attributes.Year; }
public void TestAddIncident() { using (var _dbContext = new VehicleIncidentContext(CreateNewContextOptions())) { var newVehicleIncident = new VehicleIncident { VIN = "1FTNW21P04EB82562", DateTime = DateTime.Today, Note = "Hit a pole", Model = "F-250", Make = "FORD", Year = 2004 }; _dbContext.VehicleIncidents.Add(newVehicleIncident); _dbContext.SaveChanges(); var incidents = _dbContext.VehicleIncidents.ToList(); incidents.Should().ContainSingle(x => x.VIN == "1FTNW21P04EB82562"); } }
public IActionResult CreateVehicleIncident([FromBody] CreateVehicleIncidentForm payload) { var newVehicleIncident = new VehicleIncident { VIN = payload.VIN, DateTime = Convert.ToDateTime(payload.DateTime), Note = payload.Note }; if (_dbContext.ContainsIncident(newVehicleIncident)) { return(BadRequest()); } SetAttributes(newVehicleIncident); _dbContext.VehicleIncidents.Add(newVehicleIncident); _dbContext.SaveChanges(); return(CreatedAtAction(nameof(VehicleIncident), new { vin = newVehicleIncident.VIN }, newVehicleIncident)); }
public bool ContainsIncident(VehicleIncident incident) { return(this.VehicleIncidents.Any(x => x.VIN == incident.VIN && x.DateTime == incident.DateTime)); }