public FlightPropertyValue GetPropertyValue(string[] fields) { FlightPropertyValue value = new FlightPropertyValue { Property = Property, PropertyId = Property.Id }; switch (Property.DataType) { case FlightPropertyDataType.Date: value.DateValue = Get <DateTime>(fields); break; case FlightPropertyDataType.Number: value.NumberValue = Get <decimal>(fields); break; case FlightPropertyDataType.String: value.StringValue = fields[Index]; break; default: break; } return(value); }
/// <summary> /// Create a new flight property value /// </summary> /// <param name="flightId"></param> /// <param name="value"></param> /// <returns></returns> private async Task <FlightPropertyValue> AddNewPropertyValueAsync(int flightId, FlightPropertyValue value) { // Create a new property value FlightPropertyValue updated; switch (value.Property.DataType) { case FlightPropertyDataType.Date: // TODO Date value creation updated = value; break; case FlightPropertyDataType.Number: updated = await _properties.AddFlightPropertyNumberValueAsync( flightId, value.Property.Id, value.NumberValue); break; case FlightPropertyDataType.String: updated = await _properties.AddFlightPropertyStringValueAsync( flightId, value.Property.Id, value.StringValue); break; default: updated = value; break; } return(updated); }
/// <summary> /// Update the model's flight property values from the bound property /// values dictionary /// </summary> public void UpdatePropertiesFromBoundValues() { // Iterate over the bound values foreach (var kvp in BoundPropertyValues) { // Find the property value that is associated with the bound value property FlightPropertyValue value = Properties.FirstOrDefault(p => p.Property.Id == kvp.Key); // Update the value if either it corresponds to a value that's already // been saved (updating) or the associated bound value isn't empty (creating) if ((value != null) && ((value.Id > 0) || !string.IsNullOrEmpty(kvp.Value))) { switch (value.Property.DataType) { case FlightPropertyDataType.Date: // TODO : Add date parsing break; case FlightPropertyDataType.Number: value.NumberValue = decimal.Parse(kvp.Value); break; case FlightPropertyDataType.String: value.StringValue = kvp.Value; break; default: break; } } } }
/// <summary> /// Create flight property values from the specified record /// </summary> /// <param name="count"></param> /// <param name="record"></param> /// <returns></returns> private List <FlightPropertyValue> CreatePropertyValuesFromRecord(int count, string[] record) { List <FlightPropertyValue> values = new List <FlightPropertyValue>(); // Iterate over the fields that relate to flight properties foreach (FlightPropertyField field in _fields.Where(f => f is FlightPropertyField)) { // Check we have a value for this one if (!string.IsNullOrEmpty(record[field.Index])) { // Construct a property value object for it try { FlightPropertyValue value = field.GetPropertyValue(record); values.Add(value); } catch (Exception ex) { LastError = new CsvReaderError { Record = count, Message = $"Error getting value for field '{field.Name}' at record {count}", Exception = ex.Message }; throw; } } } return(values); }
public void UpdatePropertyValueTest() { _factory.Properties.UpdatePropertyValue(_propertyValueId, UpdatedPropertyValue); _factory.Context.SaveChanges(); FlightPropertyValue value = _factory.Properties.GetPropertyValues(_flightId).First(v => v.Id == _propertyValueId); Assert.AreEqual(UpdatedPropertyValue, value.NumberValue); }
public async Task GetPropertyValueAsyncTest() { FlightPropertyValue value = await _factory.Properties.GetPropertyValueAsync(_propertyValueId); Assert.AreEqual(_flightId, value.FlightId); Assert.AreEqual(_propertyId, value.PropertyId); Assert.AreEqual(PropertyValue, value.NumberValue); }
public void GetPropertyValueTest() { FlightPropertyValue value = _factory.Properties.GetPropertyValue(_propertyValueId); Assert.AreEqual(_flightId, value.FlightId); Assert.AreEqual(_propertyId, value.PropertyId); Assert.AreEqual(PropertyValue, value.NumberValue); }
private void ThrowIfPropertyValueFound(FlightPropertyValue propertyValue, string propertyName, int flightId) { if (propertyValue != null) { string message = $"Single instance property {propertyName} already has a value for flight Id {flightId}"; throw new ValueExistsException(message); } }
private void ThrowIfPropertyValueNotFound(FlightPropertyValue value, int id) { if (value == null) { string message = $"Flight property value with ID {id} not found"; throw new PropertyValueNotFoundException(message); } }
/// <summary> /// Return the specified flight property value /// </summary> /// <param name="id"></param> /// <returns></returns> public FlightPropertyValue GetPropertyValue(int id) { FlightPropertyValue value = _context.FlightPropertyValues .Include(v => v.Property) .FirstOrDefault(v => v.Id == id); ThrowIfPropertyValueNotFound(value, id); return(value); }
/// <summary> /// Return the specified flight property value /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <FlightPropertyValue> GetPropertyValueAsync(int id) { FlightPropertyValue value = await _context.FlightPropertyValues .Include(v => v.Property) .FirstOrDefaultAsync(v => v.Id == id); ThrowIfPropertyValueNotFound(value, id); return(value); }
public async Task UpdatePropertyValueAsyncTest() { await _factory.Properties.UpdatePropertyValueAsync(_propertyValueId, UpdatedPropertyValue); await _factory.Context.SaveChangesAsync(); FlightPropertyValue value = await _factory.Properties.GetPropertyValuesAsync(_flightId).FirstAsync(v => v.Id == _propertyValueId); Assert.AreEqual(UpdatedPropertyValue, value.NumberValue); }
/// <summary> /// Update an existing flight property definition /// </summary> /// <param name="id"></param> /// <param name="value"></param> /// <returns></returns> public async Task <FlightPropertyValue> UpdatePropertyValueAsync(int id, object value) { FlightPropertyValue propertyValue = await _context.FlightPropertyValues.FirstOrDefaultAsync(p => p.Id == id); ThrowIfPropertyValueNotFound(propertyValue, id); FlightProperty definition = await _context.FlightProperties.FirstAsync(p => p.Id == propertyValue.PropertyId); SetPropertyValue(definition, propertyValue, value); return(propertyValue); }
/// <summary> /// Update an existing flight property definition /// </summary> /// <param name="id"></param> /// <param name="value"></param> /// <returns></returns> public FlightPropertyValue UpdatePropertyValue(int id, object value) { FlightPropertyValue propertyValue = _context.FlightPropertyValues.FirstOrDefault(p => p.Id == id); ThrowIfPropertyValueNotFound(propertyValue, id); FlightProperty definition = _context.FlightProperties.First(p => p.Id == propertyValue.PropertyId); SetPropertyValue(definition, propertyValue, value); return(propertyValue); }
public void AddStringPropertyValueTest() { FlightProperty property = _factory.Properties.AddProperty(StringPropertyName, FlightPropertyDataType.String, true); _factory.Context.SaveChanges(); _factory.Properties.AddPropertyValue(_flightId, property.Id, StringPropertyValue); _factory.Context.SaveChanges(); FlightPropertyValue value = _factory.Properties.GetPropertyValues(_flightId).First(v => v.PropertyId == property.Id); Assert.AreEqual(value.StringValue, StringPropertyValue); }
public async Task AddPropertyValueAsyncTest() { // To allow this to work, we need to make the existing property multi-value _factory.Context.FlightProperties.First(p => p.Id == _propertyId).IsSingleInstance = false; await _factory.Context.SaveChangesAsync(); FlightPropertyValue value = await _factory.Properties.AddPropertyValueAsync(_flightId, _propertyId, AsyncPropertyValue); await _factory.Context.SaveChangesAsync(); Assert.AreEqual(2, _factory.Context.FlightPropertyValues.Count()); Assert.AreEqual(_flightId, value.FlightId); Assert.AreEqual(_propertyId, value.PropertyId); Assert.AreEqual(AsyncPropertyValue, value.NumberValue); }
public void AddDatePropertyValueTest() { FlightProperty property = _factory.Properties.AddProperty(DatePropertyName, FlightPropertyDataType.Date, true); _factory.Context.SaveChanges(); DateTime date = DateTime.Now; _factory.Properties.AddPropertyValue(_flightId, property.Id, date); _factory.Context.SaveChanges(); FlightPropertyValue value = _factory.Properties.GetPropertyValues(_flightId).First(v => v.PropertyId == property.Id); Assert.AreEqual(value.DateValue, date); }
/// <summary> /// Create a new string property value /// </summary> /// <param name="flightId"></param> /// <param name="propertyId"></param> /// <param name="propertyValue"></param> /// <returns></returns> public async Task <FlightPropertyValue> AddFlightPropertyStringValueAsync(int flightId, int propertyId, string propertyValue) { dynamic template = new { FlightId = flightId, PropertyId = propertyId, StringValue = propertyValue }; string data = JsonConvert.SerializeObject(template); string json = await SendIndirectAsync(ValuesRouteKey, data, HttpMethod.Post); FlightPropertyValue value = JsonConvert.DeserializeObject <FlightPropertyValue>(json); return(value); }
/// <summary> /// Update an existing property value /// </summary> /// <param name="id"></param> /// <param name="numberValue"></param> /// <param name="stringValue"></param> /// <param name="dateValue"></param> /// <returns></returns> public async Task <FlightPropertyValue> UpdateFlightPropertyValueAsync(int id, decimal?numberValue, string stringValue, DateTime?dateValue) { dynamic template = new { Id = id, NumberValue = numberValue, StringValue = stringValue, DateValue = dateValue }; string data = JsonConvert.SerializeObject(template); string json = await SendIndirectAsync(ValuesRouteKey, data, HttpMethod.Put); FlightPropertyValue value = JsonConvert.DeserializeObject <FlightPropertyValue>(json); return(value); }
public void TestInitialize() { DroneFlightLogDbContext context = new DroneFlightLogDbContextFactory().CreateDbContext(null); _factory = new DroneFlightLogFactory <DroneFlightLogDbContext>(context); Address address = _factory.Addresses.AddAddress(Number, Street, Town, County, Postcode, Country); _factory.Context.SaveChanges(); Operator op = _factory.Operators.AddOperator(FirstNames, Surname, DoB, FlyerNumber, OperatorNumber, address.Id); _factory.Context.SaveChanges(); Location location = _factory.Locations.AddLocation(LocationName); _factory.Context.SaveChanges(); Manufacturer manufacturer = _factory.Manufacturers.AddManufacturer(ManufacturerName); _factory.Context.SaveChanges(); Model model = _factory.Models.AddModel(ModelName, manufacturer.Id); _factory.Context.SaveChanges(); Drone drone = _factory.Drones.AddDrone(DroneName, DroneSerialNumber, model.Id); _factory.Context.SaveChanges(); Flight flight = _factory.Flights.AddFlight(op.Id, drone.Id, location.Id, DateTime.Now, DateTime.Now); _factory.Context.SaveChanges(); _flightId = flight.Id; FlightProperty property = _factory.Properties.AddProperty(PropertyName, PropertyType, true); _factory.Context.SaveChanges(); _propertyId = property.Id; FlightPropertyValue value = _factory.Properties.AddPropertyValue(_flightId, _propertyId, PropertyValue); _factory.Context.SaveChanges(); _propertyValueId = value.Id; }
private void SetPropertyValue(FlightProperty definition, FlightPropertyValue propertyValue, object value) { switch (definition.DataType) { case FlightPropertyDataType.Date: propertyValue.DateValue = (DateTime)value; break; case FlightPropertyDataType.Number: propertyValue.NumberValue = (decimal)value; break; case FlightPropertyDataType.String: propertyValue.StringValue = (string)value; break; default: break; } }
public async Task <IActionResult> Index(FlightDetailsViewModel model) { // Load the flight details Flight flight = await _flights.GetFlightAsync(model.Id); _mapper.Map <Flight, FlightDetailsViewModel>(flight, model); // The (complex) model properties will not be bound by the model binder // and so need to be loaded, here, and updated using the dictionary of // property values that are bound by the model binder await LoadModelPropertiesAsync(model); if (ModelState.IsValid) { // Apply property values from the bound collection model.UpdatePropertiesFromBoundValues(); // Save newly added property values for (int i = 0; i < model.Properties.Count; i++) { FlightPropertyValue value = model.Properties[i]; if (value.IsNewPropertyValue) { model.Properties[i] = await AddNewPropertyValueAsync(model.Id, model.Properties[i]); } else if (value.Id > 0) { model.Properties[i] = await _properties.UpdateFlightPropertyValueAsync( value.Id, value.NumberValue, value.StringValue, value.DateValue); } } } return(View(model)); }
private FlightPropertyValue CreatePropertyValue(FlightProperty definition, int flightId, int propertyId, object value) { FlightPropertyValue propertyValue = null; switch (definition.DataType) { case FlightPropertyDataType.Date: propertyValue = new FlightPropertyValue { FlightId = flightId, PropertyId = propertyId }; break; case FlightPropertyDataType.Number: propertyValue = new FlightPropertyValue { FlightId = flightId, PropertyId = propertyId }; break; case FlightPropertyDataType.String: propertyValue = new FlightPropertyValue { FlightId = flightId, PropertyId = propertyId }; break; default: break; } SetPropertyValue(definition, propertyValue, value); return(propertyValue); }
public async Task <ActionResult <FlightPropertyValue> > CreatePropertyValueAsync([FromBody] FlightPropertyValue template) { FlightPropertyValue value; try { FlightProperty property = await _factory.Properties .GetPropertiesAsync() .FirstOrDefaultAsync(p => p.Id == template.PropertyId); switch (property.DataType) { case FlightPropertyDataType.Date: value = await _factory.Properties.AddPropertyValueAsync(template.FlightId, template.PropertyId, template.DateValue); break; case FlightPropertyDataType.Number: value = await _factory.Properties.AddPropertyValueAsync(template.FlightId, template.PropertyId, template.NumberValue); break; case FlightPropertyDataType.String: value = await _factory.Properties.AddPropertyValueAsync(template.FlightId, template.PropertyId, template.StringValue); break; default: return(BadRequest()); } await _factory.Context.SaveChangesAsync(); } catch (ValueExistsException) { return(BadRequest()); } return(value); }
public async Task <ActionResult <FlightPropertyValue> > UpdatePropertyValueAsync([FromBody] FlightPropertyValue template) { FlightPropertyValue value; try { value = await _factory.Properties.GetPropertyValueAsync(template.Id); switch (value.Property.DataType) { case FlightPropertyDataType.Date: value = await _factory.Properties.UpdatePropertyValueAsync(template.Id, template.DateValue); break; case FlightPropertyDataType.Number: value = await _factory.Properties.UpdatePropertyValueAsync(template.Id, template.NumberValue); break; case FlightPropertyDataType.String: value = await _factory.Properties.UpdatePropertyValueAsync(template.Id, template.StringValue); break; default: return(BadRequest()); } await _factory.Context.SaveChangesAsync(); } catch (PropertyValueNotFoundException) { return(NotFound()); } return(value); }