Skip to content

Commit

Permalink
Include automatic incrementing counter to missionRun model
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadUsama-afk-equinor authored and UsamaEquinorAFK committed Feb 5, 2024
1 parent e37c570 commit ab2685b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions backend/api/Database/Context/FlotillaDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.Property(m => m.InspectionFrequency)
.HasConversion(new TimeSpanToTicksConverter());

modelBuilder.Entity<MissionRun>().Property(m => m.MissionRunCount).ValueGeneratedOnAdd();
modelBuilder.Entity<MissionRun>().OwnsOne(m => m.Map).OwnsOne(t => t.TransformationMatrices);
modelBuilder.Entity<MissionRun>().OwnsOne(m => m.Map).OwnsOne(b => b.Boundary);
modelBuilder.Entity<Robot>().OwnsOne(r => r.Pose).OwnsOne(p => p.Orientation);
Expand Down Expand Up @@ -98,6 +99,30 @@ protected override void ConfigureConventions(ModelConfigurationBuilder configura
configurationBuilder.Properties(typeof(Enum)).HaveConversion<string>();
}

public static void CreateTriggerForMissionRunsInsert(FlotillaDbContext dbContext)
{
string sql = @"CREATE TRIGGER IF NOT EXISTS tr_insert_mission_runs_inspection_id
AFTER INSERT ON MissionRuns
BEGIN
UPDATE MissionRuns
SET InspectionId = (SELECT MAX(Id) FROM Inspections)
WHERE Id = NEW.Id;
END";

if (dbContext.Database.IsSqlite())
{
dbContext.Database.ExecuteSqlRaw(sql);
}
else if (dbContext.Database.IsSqlServer())
{
throw new NotImplementedException();
}
else
{
throw new InvalidOperationException("Unknown database provider.");
}
}

private static void AddConverterForDateTimeOffsets<T>(ref EntityTypeBuilder<T> entity)
where T : class
{
Expand Down
7 changes: 7 additions & 0 deletions backend/api/Database/Context/InitDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ private static List<MissionRun> GetMissionRuns()
var missionRun1 = new MissionRun
{
Name = "Placeholder Mission 1",
MissionRunCount = 1,
Robot = robots[0],
InstallationCode = areas[0].Installation!.InstallationCode,
Area = areas[0],
Expand All @@ -580,6 +581,7 @@ private static List<MissionRun> GetMissionRuns()
var missionRun2 = new MissionRun
{
Name = "Placeholder Mission 2",
MissionRunCount = 2,
Robot = robots[1],
InstallationCode = areas[1].Installation!.InstallationCode,
Area = areas[1],
Expand All @@ -594,6 +596,7 @@ private static List<MissionRun> GetMissionRuns()
var missionRun3 = new MissionRun
{
Name = "Placeholder Mission 3",
MissionRunCount = 3,
Robot = robots[2],
InstallationCode = areas[1].Installation!.InstallationCode,
Area = areas[1],
Expand All @@ -607,6 +610,7 @@ private static List<MissionRun> GetMissionRuns()
var missionRun4 = new MissionRun
{
Name = "Placeholder Mission 4",
MissionRunCount = 4,
Robot = robots[2],
InstallationCode = areas[1].Installation.InstallationCode,
Area = areas[1],
Expand All @@ -624,6 +628,7 @@ private static List<MissionRun> GetMissionRuns()
var missionRun5 = new MissionRun
{
Name = "Placeholder Mission 5",
MissionRunCount = 5,
Robot = robots[2],
InstallationCode = areas[1].Installation.InstallationCode,
Area = areas[1],
Expand All @@ -641,6 +646,7 @@ private static List<MissionRun> GetMissionRuns()
var missionRun6 = new MissionRun
{
Name = "Placeholder Mission 6",
MissionRunCount = 6,
Robot = robots[2],
InstallationCode = areas[1].Installation.InstallationCode,
Area = areas[1],
Expand All @@ -658,6 +664,7 @@ private static List<MissionRun> GetMissionRuns()
var missionRun7 = new MissionRun
{
Name = "Some failed tasks",
MissionRunCount = 7,
Robot = robots[2],
InstallationCode = areas[1].Installation.InstallationCode,
Area = areas[1],
Expand Down
3 changes: 3 additions & 0 deletions backend/api/Database/Models/MissionRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace Api.Database.Models
{
public class MissionRun : SortableRecord
{
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int MissionRunCount { get; set; } = 1;

private MissionStatus _status;

Expand Down

0 comments on commit ab2685b

Please sign in to comment.