Skip to content

Commit

Permalink
Merge pull request #30 from davewalker5/FR-120-Flight-Editing-Bug
Browse files Browse the repository at this point in the history
Fixup flight editing interaction with IATA Code validation
  • Loading branch information
davewalker5 committed Dec 11, 2023
2 parents 0c2381c + 17d42f3 commit fd5a076
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docker/ui/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/aspnet:latest AS runtime
COPY flightrecorder.mvc-1.8.0.0 /opt/flightrecorder.mvc-1.8.0.0
WORKDIR /opt/flightrecorder.mvc-1.8.0.0/bin
COPY flightrecorder.mvc-1.9.0.0 /opt/flightrecorder.mvc-1.9.0.0
WORKDIR /opt/flightrecorder.mvc-1.9.0.0/bin
ENTRYPOINT [ "./FlightRecorder.Mvc" ]
25 changes: 13 additions & 12 deletions src/FlightRecorder.Mvc/Attributes/IATACodeAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using FlightRecorder.Mvc.Api;
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using FlightRecorder.Mvc.Models;

namespace FlightRecorder.Mvc.Attributes
{
Expand All @@ -14,17 +10,22 @@ public class IATACodeAttribute : ValidationAttribute

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
FlightDetailsViewModel model = (FlightDetailsViewModel)validationContext.ObjectInstance;
List<string> codes = _client.GetAirportsAsync(1, int.MaxValue).Result.Select(x => x.Code).ToList();
// Get a list of airport IATA codes to validate against
var codes = _client.GetAirportsAsync(1, int.MaxValue).Result.Select(x => x.Code).ToList();

if ((validationContext.MemberName == "Embarkation") && !codes.Contains(model.Embarkation.ToUpper()))
{
return new ValidationResult($"{model.Embarkation} is not a valid IATA code for the point of embarkation");
}
// Get the properties of the object instance being validated
var properties = validationContext.ObjectInstance.GetType().GetProperties();

if ((validationContext.MemberName == "Destination") && !codes.Contains(model.Destination.ToUpper()))
// See if the object has the named property (at this point, it should have)
var property = properties.FirstOrDefault(x => x.Name == validationContext.MemberName);
if (property != null)
{
return new ValidationResult($"{model.Destination} is not a valid IATA code for the destination");
// It does, so get the value and check it's in the code list
var propertyValue = ((string)property.GetValue(validationContext.ObjectInstance) ?? "").ToUpper();
if (!codes.Contains(propertyValue))
{
return new ValidationResult($"{propertyValue} is not a valid IATA code for {validationContext.MemberName}");
}
}

return ValidationResult.Success;
Expand Down
6 changes: 3 additions & 3 deletions src/FlightRecorder.Mvc/FlightRecorder.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ReleaseVersion>1.8.0.0</ReleaseVersion>
<FileVersion>1.8.0.0</FileVersion>
<ProductVersion>1.8.0</ProductVersion>
<ReleaseVersion>1.9.0.0</ReleaseVersion>
<FileVersion>1.9.0.0</FileVersion>
<ProductVersion>1.9.0</ProductVersion>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/FlightRecorder.Mvc/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"AllowedHosts": "*",
"AppSettings": {
"Secret": "e2b6e7fe16ef469d9862d43eb76d00e2802ab769b85848048cc9387743ca2cc38c0f4fd8a0de46798f347bedf676bc31",
"ApiUrl": "https://localhost:5001",
"ApiUrl": "http://localhost:8093",
"ApiDateFormat": "yyyy-MM-dd H:mm:ss",
"ApiRoutes": [
{
Expand Down

0 comments on commit fd5a076

Please sign in to comment.