Skip to content

Commit

Permalink
Remove default localization area from deck
Browse files Browse the repository at this point in the history
  • Loading branch information
oysand committed Sep 20, 2023
1 parent 40022fe commit cd80713
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 1,411 deletions.
50 changes: 0 additions & 50 deletions backend/api/Controllers/DeckController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ public class DeckController : ControllerBase
private readonly IPlantService _plantService;

private readonly IMapService _mapService;
private readonly IAreaService _areaService;

private readonly ILogger<DeckController> _logger;

public DeckController(
ILogger<DeckController> logger,
IMapService mapService,
IAreaService areaService,
IDeckService deckService,
IInstallationService installationService,
IPlantService plantService,
Expand All @@ -32,7 +30,6 @@ IMissionDefinitionService missionDefinitionService
{
_logger = logger;
_mapService = mapService;
_areaService = areaService;
_deckService = deckService;
_installationService = installationService;
_plantService = plantService;
Expand Down Expand Up @@ -177,53 +174,6 @@ public async Task<ActionResult<Deck>> Create([FromBody] CreateDeckQuery deck)
}
}

/// <summary>
/// Set default localization area for a deck
/// </summary>
/// <remarks>
/// <para> This query sets the default localization area for a deck </para>
/// </remarks>
[HttpPost]
[Authorize(Roles = Role.Admin)]
[Route("{deckId}/{areaId}/set-default-localization-area")]
[ProducesResponseType(typeof(AreaResponse), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<Deck>> SetDefaultLocalizationArea(
[FromRoute] string deckId,
[FromRoute] string areaId
)
{
_logger.LogInformation(@"Setting default localization area {AreaId} to deck {DeckId}", areaId, deckId);
var deck = await _deckService.ReadById(deckId);
if (deck == null)
return NotFound($"Could not find deck with id {deckId}");

var area = await _areaService.ReadById(areaId);
if (area == null)
return NotFound($"Could not find area with id {areaId}");

if (area.Deck == null)
return NotFound($"Area {areaId} is not linked to any deck");

if (area.Deck.Id != deckId)
return NotFound($"Area {areaId} is not linked to deck {deckId}");

try
{
deck.DefaultLocalizationArea = area;
var updatedDeck = await _deckService.Update(deck);
return Ok(updatedDeck);
}
catch (Exception e)
{
_logger.LogError(e, "Error while setting a default localization area");
return StatusCode(StatusCodes.Status500InternalServerError);
}
}

/// <summary>
/// Deletes the deck with the specified id from the database.
/// </summary>
Expand Down
7 changes: 2 additions & 5 deletions backend/api/Database/Context/FlotillaDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
poseBuilder.OwnsOne(pose => pose.Position);
poseBuilder.OwnsOne(pose => pose.Orientation);
});
modelBuilder.Entity<Area>().HasOne(a => a.Deck);
modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithMany();
modelBuilder.Entity<Area>().HasOne(a => a.Installation).WithMany();
modelBuilder.Entity<Area>().HasOne(a => a.Plant).WithMany();
modelBuilder.Entity<Deck>().HasOne(d => d.Plant).WithMany();

modelBuilder.Entity<Deck>().HasOne(d => d.Installation).WithMany();
modelBuilder.Entity<Deck>().HasOne(d => d.DefaultLocalizationArea).WithOne(a => a.Deck);
modelBuilder.Entity<Plant>().HasOne(a => a.Installation).WithMany();

modelBuilder.Entity<SafePosition>().OwnsOne(s => s.Pose, poseBuilder =>
Expand All @@ -95,12 +93,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Installation>().HasIndex(a => new { a.InstallationCode }).IsUnique();
modelBuilder.Entity<Plant>().HasIndex(a => new { a.PlantCode }).IsUnique();

modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithOne().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Plant).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Deck>().HasOne(d => d.Plant).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Deck>().HasOne(d => d.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Deck>().HasOne(d => d.DefaultLocalizationArea).WithOne().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Plant>().HasOne(p => p.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
}

Expand Down
5 changes: 0 additions & 5 deletions backend/api/Database/Models/Deck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ public class Deck

public virtual Installation Installation { get; set; }

public string? DefaultLocalizationAreaId { get; set; }

[ForeignKey("DefaultLocalizationAreaId")]
public virtual Area? DefaultLocalizationArea { get; set; }

[Required]
[MaxLength(200)]
public string Name { get; set; }
Expand Down
Loading

0 comments on commit cd80713

Please sign in to comment.