Don't seperately parse status, let the JSON decoder do this for now
This commit is contained in:
@@ -26,8 +26,7 @@ public class CreateMissionLaunch(MissionControlContext dbContext)
|
|||||||
var missionLaunch = new MissionLaunch
|
var missionLaunch = new MissionLaunch
|
||||||
{
|
{
|
||||||
ScheduledFor = req.ScheduledFor,
|
ScheduledFor = req.ScheduledFor,
|
||||||
// Validated in `CreateMissionLaunchRequestValidator`
|
Status = req.Status,
|
||||||
Status = Enum.Parse<MissionLaunchStatus>(req.Status, true),
|
|
||||||
MissionId = req.MissionId,
|
MissionId = req.MissionId,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -43,9 +42,7 @@ public record CreateMissionLaunchRequest
|
|||||||
public Guid MissionId { get; init; }
|
public Guid MissionId { get; init; }
|
||||||
public required DateTimeOffset ScheduledFor { get; init; }
|
public required DateTimeOffset ScheduledFor { get; init; }
|
||||||
|
|
||||||
// Define as a string here so it can be safely deserialized into its enum type, with a validation message when unable to do so
|
public required MissionLaunchStatus Status { get; init; }
|
||||||
// Otherwise, we return a serialization error that is not assigned to a specific field
|
|
||||||
public required string Status { get; init; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class CreateMissionLaunchRequestValidator : Validator<CreateMissionLaunchRequest>
|
public class CreateMissionLaunchRequestValidator : Validator<CreateMissionLaunchRequest>
|
||||||
@@ -55,6 +52,5 @@ public class CreateMissionLaunchRequestValidator : Validator<CreateMissionLaunch
|
|||||||
RuleFor(req => req.MissionId)
|
RuleFor(req => req.MissionId)
|
||||||
.NotEqual(Guid.Empty)
|
.NotEqual(Guid.Empty)
|
||||||
.WithMessage("Please provide a non-zero GUID.");
|
.WithMessage("Please provide a non-zero GUID.");
|
||||||
RuleFor(req => req.Status).IsEnumName(typeof(MissionLaunchStatus), false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-1
@@ -18,7 +18,7 @@ public class CreateMissionLaunchTests
|
|||||||
{
|
{
|
||||||
MissionId = Guid.NewGuid(),
|
MissionId = Guid.NewGuid(),
|
||||||
ScheduledFor = DateTimeOffset.UtcNow,
|
ScheduledFor = DateTimeOffset.UtcNow,
|
||||||
Status = "Pending",
|
Status = MissionLaunchStatus.Pending,
|
||||||
};
|
};
|
||||||
|
|
||||||
await endpoint.HandleAsync(request, CancellationToken.None);
|
await endpoint.HandleAsync(request, CancellationToken.None);
|
||||||
@@ -27,4 +27,21 @@ public class CreateMissionLaunchTests
|
|||||||
|
|
||||||
(await database.Context.MissionLaunches.AnyAsync()).ShouldBeFalse();
|
(await database.Context.MissionLaunches.AnyAsync()).ShouldBeFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Rejects_empty_mission_id_guid()
|
||||||
|
{
|
||||||
|
var validator = new CreateMissionLaunchRequestValidator();
|
||||||
|
|
||||||
|
var result = validator.Validate(new CreateMissionLaunchRequest
|
||||||
|
{
|
||||||
|
MissionId = Guid.Empty,
|
||||||
|
ScheduledFor = default,
|
||||||
|
Status = MissionLaunchStatus.Pending,
|
||||||
|
});
|
||||||
|
|
||||||
|
result.IsValid.ShouldBeFalse();
|
||||||
|
result.Errors.ShouldContain(e => e.PropertyName == nameof(CreateMissionLaunchRequest.MissionId));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user