/// <summary> /// Parse the header field collection to create a set of fields that allow /// for parsing of row values when processing CSV rows into flight records /// </summary> /// <param name="headers"></param> private void ParseHeaders(string[] headers) { // Get a list of all available flight properties IEnumerable <FlightProperty> properties = _factory.Properties.GetProperties(); // Create an empty field list _fields = new List <CsvField>(); // Iterate over the CSV headers for (int i = 0; i < headers.Length; i++) { // See if this header name matches a flight property. If it does, // add a field at this position that maps to that property. If not, // just add a simple field at this position FlightProperty property = properties.FirstOrDefault(p => p.Name.Equals(headers[i], StringComparison.OrdinalIgnoreCase)); if (property != null) { _fields.Add(new FlightPropertyField { Name = headers[i], Index = i, Property = property }); } else { _fields.Add(new CsvField { Name = headers[i], Index = i }); } } }
public async Task <IActionResult> Edit(int id) { FlightProperty property = await _client.GetFlightPropertyAsync(id); EditFlightPropertyViewModel model = _mapper.Map <EditFlightPropertyViewModel>(property); return(View(model)); }
private void ThrowIfPropertyFound(FlightProperty property, string name) { if (property != null) { string message = $"Property {name} already exists"; throw new PropertyExistsException(message); } }
private void ThrowIfPropertyNotFound(FlightProperty property, int id) { if (property == null) { string message = $"Flight property with ID {id} not found"; throw new PropertyNotFoundException(message); } }
public void UpdatePropertyTest() { _factory.Properties.UpdateProperty(_propertyId, UpdatedPropertyName); _factory.Context.SaveChanges(); FlightProperty property = _factory.Properties.GetProperties().First(p => p.Id == _propertyId); Assert.AreEqual(UpdatedPropertyName, property.Name); }
/// <summary> /// Return the flight property with the specified Id /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <FlightProperty> GetFlightPropertyAsync(int id) { // TODO : This needs to be replaced with a call to retrieve a single // property by Id. For now, retrieve them all then pick the one required List <FlightProperty> poperties = await GetFlightPropertiesAsync(); FlightProperty property = poperties.First(l => l.Id == id); return(property); }
public async Task UpdatePropertyAsyncTest() { await _factory.Properties.UpdatePropertyAsync(_propertyId, UpdatedPropertyName); await _factory.Context.SaveChangesAsync(); FlightProperty property = _factory.Properties.GetProperties().First(p => p.Id == _propertyId); Assert.AreEqual(UpdatedPropertyName, property.Name); }
public async Task AddPropertyAsyncTest() { FlightProperty property = await _factory.Properties.AddPropertyAsync(AsyncPropertyName, PropertyType, true); await _factory.Context.SaveChangesAsync(); Assert.AreEqual(2, _factory.Context.FlightProperties.Count()); Assert.AreEqual(AsyncPropertyName, property.Name); Assert.AreEqual(PropertyType, property.DataType); Assert.IsTrue(property.IsSingleInstance); }
/// <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); }
/// <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 /// </summary> /// <param name="name"></param> /// <param name="dataType"></param> /// <param name="isSingleInstance"></param> /// <returns></returns> public async Task <FlightProperty> UpdateFlightPropertyAsync(int id, string name) { _cache.Remove(CacheKey); string data = $"\"{name}\""; string baseRoute = _settings.Value.ApiRoutes.First(r => r.Name == PropertiesRouteKey).Route; string route = $"{baseRoute}/{id}"; string json = await SendDirectAsync(route, data, HttpMethod.Put); FlightProperty property = JsonConvert.DeserializeObject <FlightProperty>(json); return(property); }
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 <IActionResult> Add(AddFlightPropertyViewModel model) { if (ModelState.IsValid) { FlightProperty property = await _client.AddFlightPropertyAsync(model.Name, model.DataTypeValue, model.IsSingleInstance); ModelState.Clear(); model.Clear(); model.Message = $"Flight property '{property.Name}' added successfully"; } return(View(model)); }
public static string ToString(this Flight flight, FlightProperty property) { switch (property) { case FlightProperty.Date: return flight != null ? flight.Date.ToLongDateString () : string.Empty; case FlightProperty.Aircraft: return flight != null ? flight.Aircraft : string.Empty; case FlightProperty.AirportDeparted: return flight != null ? flight.AirportDeparted : string.Empty; case FlightProperty.AirportVisited: return flight != null ? flight.AirportVisited : null; case FlightProperty.AirportArrived: return flight != null ? flight.AirportArrived : string.Empty; case FlightProperty.FlightTime: return flight != null ? FormatFlightTime (flight.FlightTime, true) : string.Empty; case FlightProperty.CrossCountry: return flight != null ? FormatFlightTime (flight.CrossCountry, false) : null; case FlightProperty.CertifiedFlightInstructor: return flight != null ? FormatFlightTime (flight.CertifiedFlightInstructor, false) : null; case FlightProperty.DualReceived: return flight != null ? FormatFlightTime (flight.DualReceived, false) : null; case FlightProperty.PilotInCommand: return flight != null ? FormatFlightTime (flight.PilotInCommand, false) : null; case FlightProperty.SecondInCommand: return flight != null ? FormatFlightTime (flight.SecondInCommand, false) : null; case FlightProperty.Day: return flight != null ? FormatFlightTime (flight.Day, false) : null; case FlightProperty.Night: return flight != null ? FormatFlightTime (flight.Night, false) : null; case FlightProperty.DayLandings: return flight != null && flight.DayLandings > 0 ? flight.DayLandings.ToString () : null; case FlightProperty.NightLandings: return flight != null && flight.NightLandings > 0 ? flight.NightLandings.ToString () : null; case FlightProperty.InstrumentActual: return flight != null ? FormatFlightTime (flight.InstrumentActual, false) : null; case FlightProperty.InstrumentHood: return flight != null ? FormatFlightTime (flight.InstrumentHood, false) : null; case FlightProperty.InstrumentSimulator: return flight != null ? FormatFlightTime (flight.InstrumentSimulator, false) : null; case FlightProperty.InstrumentApproaches: return flight != null && flight.InstrumentApproaches > 0 ? flight.InstrumentApproaches.ToString () : null; case FlightProperty.InstrumentHoldingProcedures: return flight != null && flight.InstrumentHoldingProcedures > 0 ? flight.InstrumentHoldingProcedures.ToString () : null; case FlightProperty.InstrumentSafetyPilot: return flight != null && !string.IsNullOrEmpty (flight.InstrumentSafetyPilot) ? flight.InstrumentSafetyPilot : null; case FlightProperty.Remarks: return flight != null && !string.IsNullOrEmpty (flight.Remarks) ? flight.Remarks : null; default: throw new ArgumentOutOfRangeException (); } }
public void AddMultiInstancePropertyValuesTest() { FlightProperty property = _factory.Properties.AddProperty(MultiInstancePropertyName, PropertyType, false); _factory.Context.SaveChanges(); _factory.Properties.AddPropertyValue(_flightId, property.Id, 1.0M); _factory.Properties.AddPropertyValue(_flightId, property.Id, 2.0M); _factory.Context.SaveChanges(); IEnumerable <FlightPropertyValue> values = _factory.Properties.GetPropertyValues(_flightId); Assert.AreEqual(3, values.Count()); }
/// <summary> /// Add a value for a property to a flight /// </summary> /// <param name="flightId"></param> /// <param name="propertyId"></param> /// <param name="value"></param> public FlightPropertyValue AddPropertyValue(int flightId, int propertyId, object value) { FlightProperty definition = _context.FlightProperties.First(p => p.Id == propertyId); FlightPropertyValue propertyValue; if (definition.IsSingleInstance) { propertyValue = _context.FlightPropertyValues.FirstOrDefault(v => (v.FlightId == flightId) && (v.PropertyId == propertyId)); ThrowIfPropertyValueFound(propertyValue, definition.Name, flightId); } propertyValue = CreatePropertyValue(definition, flightId, propertyId, value); _context.FlightPropertyValues.Add(propertyValue); return(propertyValue); }
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> /// Update an existing flight property definition /// </summary> /// <param name="id"></param> /// <param name="name"></param> /// <returns></returns> public async Task <FlightProperty> UpdatePropertyAsync(int id, string name) { name = name.CleanString(); FlightProperty existing = await _context.FlightProperties.FirstOrDefaultAsync(p => p.Name == name); ThrowIfPropertyFound(existing, name); FlightProperty property = await _context.FlightProperties.FirstOrDefaultAsync(p => p.Id == id); ThrowIfPropertyNotFound(property, id); // The only property that can be updated without running the risk of invalidating // existing data is the name property.Name = name; return(property); }
/// <summary> /// Add a new flight property definition /// </summary> /// <param name="name"></param> /// <param name="type"></param> /// <param name="isSingleInstance"></param> public FlightProperty AddProperty(string name, FlightPropertyDataType type, bool isSingleInstance) { FlightProperty property = _context.FlightProperties.FirstOrDefault(p => p.Name == name); ThrowIfPropertyFound(property, name); property = new FlightProperty { Name = name, DataType = type, IsSingleInstance = isSingleInstance }; _context.FlightProperties.Add(property); return(property); }
/// <summary> /// Add a new flight property definition /// </summary> /// <param name="name"></param> /// <param name="type"></param> /// <param name="isSingleInstance"></param> /// <returns></returns> public async Task <FlightProperty> AddPropertyAsync(string name, FlightPropertyDataType type, bool isSingleInstance) { FlightProperty property = await _context.FlightProperties.FirstOrDefaultAsync(p => p.Name == name); ThrowIfPropertyFound(property, name); property = new FlightProperty { Name = name, DataType = type, IsSingleInstance = isSingleInstance }; await _context.FlightProperties.AddAsync(property); return(property); }
public async Task <ActionResult <FlightProperty> > CreatePropertyAsync([FromBody] FlightProperty template) { FlightProperty property; try { property = await _factory.Properties.AddPropertyAsync(template.Name, template.DataType, template.IsSingleInstance); await _factory.Context.SaveChangesAsync(); } catch (PropertyExistsException) { return(BadRequest()); } return(property); }
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; }
/// <summary> /// Create a new flight property /// </summary> /// <param name="name"></param> /// <param name="dataType"></param> /// <param name="isSingleInstance"></param> /// <returns></returns> public async Task <FlightProperty> AddFlightPropertyAsync(string name, int dataType, bool isSingleInstance) { _cache.Remove(CacheKey); dynamic template = new { Name = name, DataType = dataType, IsSingleInstance = isSingleInstance }; string data = JsonConvert.SerializeObject(template); string json = await SendIndirectAsync(PropertiesRouteKey, data, HttpMethod.Post); FlightProperty property = JsonConvert.DeserializeObject <FlightProperty>(json); return(property); }
public int GetFlightTime(FlightProperty property) { switch (property) { case FlightProperty.FlightTime: return(FlightTime); case FlightProperty.Day: return(Day); case FlightProperty.Night: return(Night); case FlightProperty.CrossCountry: return(CrossCountry); case FlightProperty.CrossCountryPIC: return(Math.Min(CrossCountry, PilotInCommand)); case FlightProperty.CertifiedFlightInstructor: return(CertifiedFlightInstructor); case FlightProperty.DualReceived: return(DualReceived); case FlightProperty.PilotInCommand: return(PilotInCommand); case FlightProperty.SecondInCommand: return(SecondInCommand); case FlightProperty.InstrumentActual: return(InstrumentActual); case FlightProperty.InstrumentHood: return(InstrumentHood); case FlightProperty.InstrumentSimulator: return(InstrumentSimulator); default: throw new ArgumentOutOfRangeException(); } }
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 <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); }
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 int GetFlightTime(FlightProperty property) { switch (property) { case FlightProperty.FlightTime: return FlightTime; case FlightProperty.Day: return Day; case FlightProperty.Night: return Night; case FlightProperty.CrossCountry: return CrossCountry; case FlightProperty.CrossCountryPIC: return Math.Min (CrossCountry, PilotInCommand); case FlightProperty.CertifiedFlightInstructor: return CertifiedFlightInstructor; case FlightProperty.DualReceived: return DualReceived; case FlightProperty.PilotInCommand: return PilotInCommand; case FlightProperty.SecondInCommand: return SecondInCommand; case FlightProperty.InstrumentActual: return InstrumentActual; case FlightProperty.InstrumentHood: return InstrumentHood; case FlightProperty.InstrumentSimulator: return InstrumentSimulator; default: throw new ArgumentOutOfRangeException (); } }
public static string ToString(this Flight flight, FlightProperty property) { switch (property) { case FlightProperty.Date: return(flight != null?flight.Date.ToLongDateString() : string.Empty); case FlightProperty.Aircraft: return(flight != null ? flight.Aircraft : string.Empty); case FlightProperty.AirportDeparted: return(flight != null ? flight.AirportDeparted : string.Empty); case FlightProperty.AirportVisited: return(flight != null ? flight.AirportVisited : null); case FlightProperty.AirportArrived: return(flight != null ? flight.AirportArrived : string.Empty); case FlightProperty.FlightTime: return(flight != null?FormatFlightTime(flight.FlightTime, true) : string.Empty); case FlightProperty.CrossCountry: return(flight != null?FormatFlightTime(flight.CrossCountry, false) : null); case FlightProperty.CertifiedFlightInstructor: return(flight != null?FormatFlightTime(flight.CertifiedFlightInstructor, false) : null); case FlightProperty.DualReceived: return(flight != null?FormatFlightTime(flight.DualReceived, false) : null); case FlightProperty.PilotInCommand: return(flight != null?FormatFlightTime(flight.PilotInCommand, false) : null); case FlightProperty.SecondInCommand: return(flight != null?FormatFlightTime(flight.SecondInCommand, false) : null); case FlightProperty.Day: return(flight != null?FormatFlightTime(flight.Day, false) : null); case FlightProperty.Night: return(flight != null?FormatFlightTime(flight.Night, false) : null); case FlightProperty.DayLandings: return(flight != null && flight.DayLandings > 0 ? flight.DayLandings.ToString() : null); case FlightProperty.NightLandings: return(flight != null && flight.NightLandings > 0 ? flight.NightLandings.ToString() : null); case FlightProperty.InstrumentActual: return(flight != null?FormatFlightTime(flight.InstrumentActual, false) : null); case FlightProperty.InstrumentHood: return(flight != null?FormatFlightTime(flight.InstrumentHood, false) : null); case FlightProperty.InstrumentSimulator: return(flight != null?FormatFlightTime(flight.InstrumentSimulator, false) : null); case FlightProperty.InstrumentApproaches: return(flight != null && flight.InstrumentApproaches > 0 ? flight.InstrumentApproaches.ToString() : null); case FlightProperty.InstrumentHoldingProcedures: return(flight != null && flight.InstrumentHoldingProcedures > 0 ? flight.InstrumentHoldingProcedures.ToString() : null); case FlightProperty.InstrumentSafetyPilot: return(flight != null && !string.IsNullOrEmpty(flight.InstrumentSafetyPilot) ? flight.InstrumentSafetyPilot : null); case FlightProperty.Remarks: return(flight != null && !string.IsNullOrEmpty(flight.Remarks) ? flight.Remarks : null); default: throw new ArgumentOutOfRangeException(); } }