Skip to content

Commit

Permalink
Add DefaultLocalizationArea to deck
Browse files Browse the repository at this point in the history
  • Loading branch information
oysand committed Sep 8, 2023
1 parent af12978 commit 03ae689
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
50 changes: 50 additions & 0 deletions backend/api/Controllers/DeckController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ 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
)
{
_logger = logger;
_mapService = mapService;
_areaService = areaService;
_deckService = deckService;
_installationService = installationService;
_plantService = plantService;
Expand Down Expand Up @@ -141,6 +144,53 @@ 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: 5 additions & 2 deletions backend/api/Database/Context/FlotillaDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
poseBuilder.OwnsOne(pose => pose.Position);
poseBuilder.OwnsOne(pose => pose.Orientation);
});
modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithMany();
modelBuilder.Entity<Area>().HasOne(a => a.Deck);
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 @@ -89,11 +91,12 @@ 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).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithOne().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: 5 additions & 0 deletions backend/api/Database/Models/Deck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ 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
2 changes: 1 addition & 1 deletion backend/api/Services/DeckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<IEnumerable<Deck>> ReadAll()

private IQueryable<Deck> GetDecks()
{
return _context.Decks.Include(p => p.Plant).Include(i => i.Installation);
return _context.Decks.Include(p => p.Plant).Include(i => i.Installation).Include(d => d.DefaultLocalizationArea);
}

public async Task<Deck?> ReadById(string id)
Expand Down

0 comments on commit 03ae689

Please sign in to comment.