Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DefaultLocalizationArea to deck #1003

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
aeshub marked this conversation as resolved.
Show resolved Hide resolved
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
Loading
Loading