Skip to content

Commit

Permalink
Avoid double-tracking robot entry
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Jul 30, 2024
1 parent 59834f6 commit eeb8531
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions backend/api/Controllers/MissionSchedulingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ [FromBody] ScheduleMissionQuery scheduledMissionQuery
)
{
Robot robot;
try { robot = await robotService.GetRobotWithPreCheck(scheduledMissionQuery.RobotId); }
try { robot = await robotService.GetRobotWithPreCheck(scheduledMissionQuery.RobotId, readOnly: true); }
catch (Exception e) when (e is RobotNotFoundException) { return NotFound(e.Message); }
catch (Exception e) when (e is RobotPreCheckFailedException) { return BadRequest(e.Message); }

var missionRun = await missionRunService.ReadById(missionRunId);
var missionRun = await missionRunService.ReadByIdAsReadOnly(missionRunId);
if (missionRun == null) return NotFound("Mission run not found");

var missionTasks = missionRun.Tasks.Where((t) => t.Status != Database.Models.TaskStatus.Successful && t.Status != Database.Models.TaskStatus.PartiallySuccessful).Select((t) => new MissionTask(t, Database.Models.TaskStatus.NotStarted)).ToList();
Expand Down
11 changes: 11 additions & 0 deletions backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface IMissionRunService

public Task<MissionRun?> ReadById(string id);

public Task<MissionRun?> ReadByIdAsReadOnly(string id);

public Task<MissionRun?> ReadByIsarMissionId(string isarMissionId);

public Task<IList<MissionRun>> ReadMissionRunQueue(string robotId);
Expand Down Expand Up @@ -133,6 +135,12 @@ public async Task<PagedList<MissionRun>> ReadAll(MissionRunQueryStringParameters
.FirstOrDefaultAsync(missionRun => missionRun.Id.Equals(id));
}

public async Task<MissionRun?> ReadByIdAsReadOnly(string id)
{
return await GetMissionRunsWithSubModels().AsNoTracking()
.FirstOrDefaultAsync(missionRun => missionRun.Id.Equals(id));
}

public async Task<IList<MissionRun>> ReadMissionRunQueue(string robotId)
{
return await GetMissionRunsWithSubModels()
Expand Down Expand Up @@ -321,6 +329,9 @@ private IQueryable<MissionRun> GetMissionRunsWithSubModels()
.ThenInclude(deck => deck != null ? deck.Plant : null)
.ThenInclude(plant => plant != null ? plant.Installation : null)
.Include(missionRun => missionRun.Area)
.ThenInclude(area => area != null ? area.Plant : null)
.ThenInclude(plant => plant != null ? plant.Installation : null)
.Include(missionRun => missionRun.Area)
.ThenInclude(area => area != null ? area.Installation : null)
.Include(missionRun => missionRun.Area)
.ThenInclude(area => area != null ? area.Deck : null)
Expand Down
8 changes: 5 additions & 3 deletions backend/api/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IRobotService
public Task<Robot> Create(Robot newRobot);
public Task<Robot> CreateFromQuery(CreateRobotQuery robotQuery);

public Task<Robot> GetRobotWithPreCheck(string robotId);
public Task<Robot> GetRobotWithPreCheck(string robotId, bool readOnly = false);
public Task<IEnumerable<Robot>> ReadAll();
public Task<IEnumerable<string>> ReadAllActivePlants();
public Task<Robot?> ReadById(string id);
Expand Down Expand Up @@ -97,9 +97,9 @@ public async Task<Robot> CreateFromQuery(CreateRobotQuery robotQuery)
throw new DbUpdateException("Could not create new robot in database as robot model does not exist");
}

public async Task<Robot> GetRobotWithPreCheck(string robotId)
public async Task<Robot> GetRobotWithPreCheck(string robotId, bool readOnly = false)
{
var robot = await ReadById(robotId);
var robot = readOnly ? await ReadByIdAsReadOnly(robotId) : await ReadById(robotId);

if (robot is null)
{
Expand Down Expand Up @@ -287,6 +287,8 @@ public async Task<Robot> UpdateFlotillaStatus(string robotId, RobotFlotillaStatu

public async Task<Robot?> ReadById(string id) { return await GetRobotsWithSubModels().FirstOrDefaultAsync(robot => robot.Id.Equals(id)); }

public async Task<Robot?> ReadByIdAsReadOnly(string id) { return await GetRobotsWithSubModels().AsNoTracking().FirstOrDefaultAsync(robot => robot.Id.Equals(id)); }

public async Task<Robot?> ReadByIsarId(string isarId)
{
return await GetRobotsWithSubModels()
Expand Down

0 comments on commit eeb8531

Please sign in to comment.