Ensure mission exists before inserting mission launch entity
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using MissionControl.Data;
|
using MissionControl.Data;
|
||||||
using MissionControl.Domain.Entities;
|
using MissionControl.Domain.Entities;
|
||||||
|
|
||||||
@@ -15,6 +16,13 @@ public class CreateMissionLaunch(MissionControlContext dbContext)
|
|||||||
|
|
||||||
public override async Task HandleAsync(CreateMissionLaunchRequest req, CancellationToken ct)
|
public override async Task HandleAsync(CreateMissionLaunchRequest req, CancellationToken ct)
|
||||||
{
|
{
|
||||||
|
var missionExists = await dbContext.Missions.AnyAsync(mission => mission.Id == req.MissionId, ct);
|
||||||
|
if (!missionExists)
|
||||||
|
{
|
||||||
|
await Send.NotFoundAsync(ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var missionLaunch = new MissionLaunch
|
var missionLaunch = new MissionLaunch
|
||||||
{
|
{
|
||||||
ScheduledFor = req.ScheduledFor,
|
ScheduledFor = req.ScheduledFor,
|
||||||
|
|||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
using FastEndpoints;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using MissionControl.Domain.Entities;
|
||||||
|
using MissionControl.Server.Endpoints.Missions.MissionLaunches;
|
||||||
|
using Shouldly;
|
||||||
|
|
||||||
|
namespace MissionControl.Server.Tests.Endpoints.Missions.MissionLaunches;
|
||||||
|
|
||||||
|
public class CreateMissionLaunchTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Returns_not_found_when_requested_mission_does_not_exist()
|
||||||
|
{
|
||||||
|
using var database = new TestDatabase();
|
||||||
|
var endpoint = Factory.Create<CreateMissionLaunch>(database.Context);
|
||||||
|
var request = new CreateMissionLaunchRequest
|
||||||
|
{
|
||||||
|
MissionId = Guid.NewGuid(),
|
||||||
|
ScheduledFor = DateTimeOffset.UtcNow,
|
||||||
|
Status = "Pending",
|
||||||
|
};
|
||||||
|
|
||||||
|
await endpoint.HandleAsync(request, CancellationToken.None);
|
||||||
|
|
||||||
|
endpoint.HttpContext.Response.StatusCode.ShouldBe(StatusCodes.Status404NotFound);
|
||||||
|
|
||||||
|
(await database.Context.MissionLaunches.AnyAsync()).ShouldBeFalse();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user