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 InspectionFindings endpoint to Flotilla API #1150

Merged
merged 8 commits into from
Nov 15, 2023
95 changes: 95 additions & 0 deletions backend/api/Controllers/InspectionFindingsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Api.Controllers.Models;
using Api.Database.Models;
using Api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
[ApiController]
[Route("inspection-findings")]
public class InspectionFindingsController : ControllerBase
{
private readonly IInspectionService _inspectionService;
private readonly ILogger<InspectionFindingsController> _logger;
public InspectionFindingsController(
ILogger<InspectionFindingsController> logger,
IInspectionService inspectionService
)
{
_logger = logger;
_inspectionService = inspectionService;

UsamaEquinorAFK marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Associate a new inspection finding with the inspection corresponding to isarStepId
/// </summary>
/// <remarks>
/// </remarks>
[HttpPost("add-findings")]
[Authorize(Roles = Role.Admin)]
[ProducesResponseType(typeof(InspectionFindings), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<InspectionFindings>> AddFindings([FromBody] InspectionFindingsQuery inspectionFinding)
{
_logger.LogInformation("Updating inspection findings for inspection with isarStepId '{Id}'", inspectionFinding.IsarStepId);
try
{
var inspection = await _inspectionService.AddFindings(inspectionFinding);

if (inspection != null)
{
return Ok(inspection.InspectionFindings);
}

}
catch (Exception e)
{
_logger.LogError(e, "Error while adding findings to inspection with IsarStepId '{Id}'", inspectionFinding.IsarStepId);
return StatusCode(StatusCodes.Status500InternalServerError);
}
return NotFound($"Could not find any inspection with the provided '{inspectionFinding.IsarStepId}'");
}

/// <summary>
/// Get the full inspection against an isarStepId
/// </summary>
/// <remarks>
/// </remarks>
[HttpGet]
[Authorize(Roles = Role.Admin)]
[Route("{id}")]
[ProducesResponseType(typeof(Inspection), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<Inspection>> GetInspections([FromRoute] string id)
{
_logger.LogInformation("Get inspection by ID '{id}'", id);
try
{
var inspection = await _inspectionService.ReadByIsarStepId(id);
UsamaEquinorAFK marked this conversation as resolved.
Show resolved Hide resolved
if (inspection != null)
{
return Ok(inspection);
}

}
catch (Exception e)
{
_logger.LogError(e, "Error while finding an inspection with inspection id '{id}'", id);
return StatusCode(StatusCodes.Status500InternalServerError);
}
return NotFound("Could not find any inspection with the provided '{id}'");
}

}

}
18 changes: 18 additions & 0 deletions backend/api/Controllers/Models/InspectionFindingsQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Api.Controllers.Models
{
public struct InspectionFindingsQuery
{

public DateTime InspectionDate { get; set; }

public string Area { get; set; }

public string IsarStepId { get; set; }

public string Findings { get; set; }


}

}

36 changes: 26 additions & 10 deletions backend/api/Database/Models/InspectionFindings.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
using Microsoft.EntityFrameworkCore;

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Api.Controllers.Models;
#pragma warning disable CS8618
namespace Api.Database.Models
{
[Owned]
public class InspectionFindings
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }

UsamaEquinorAFK marked this conversation as resolved.
Show resolved Hide resolved
public string RobotName { get; set; }

public string InspectionDate { get; set; }
public DateTime InspectionDate { get; set; }

public string Area { get; set; }

public string InspectionId { get; set; }

public string FindingsTag { get; set; }

public string IsarStepId { get; set; }

public string Findings { get; set; }

public InspectionFindings(InspectionFindingsQuery createInspectionFindingQuery)
{
InspectionDate = createInspectionFindingQuery.InspectionDate;
Area = createInspectionFindingQuery.Area;
IsarStepId = createInspectionFindingQuery.IsarStepId;
Findings = createInspectionFindingQuery.Findings;
}

public InspectionFindings()
{
InspectionDate = DateTime.UtcNow;
Area = "string";
IsarStepId = "string";
Findings = "string";
}
}

}
1 change: 0 additions & 1 deletion backend/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
builder.Services.AddScoped<IPoseTimeseriesService, PoseTimeseriesService>();
builder.Services.AddScoped<ILastMissionRunService, LastMissionRunService>();


bool useInMemoryDatabase = builder.Configuration
.GetSection("Database")
.GetValue<bool>("UseInMemoryDatabase");
Expand Down
30 changes: 29 additions & 1 deletion backend/api/Services/InspectionService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Api.Controllers.Models;
using Api.Database.Context;
using Api.Database.Models;
using Api.Services.Models;
Expand All @@ -9,6 +10,9 @@ namespace Api.Services
public interface IInspectionService
{
public Task<Inspection> UpdateInspectionStatus(string isarStepId, IsarStepStatus isarStepStatus);
public Task<Inspection?> ReadByIsarStepId(string id);
public Task<Inspection?> AddFindings(InspectionFindingsQuery inspectionFindingsQuery);

}

[SuppressMessage(
Expand Down Expand Up @@ -52,7 +56,7 @@ private async Task<Inspection> Update(Inspection inspection)
return entry.Entity;
}

private async Task<Inspection?> ReadByIsarStepId(string id)
public async Task<Inspection?> ReadByIsarStepId(string id)
{
return await GetInspections().FirstOrDefaultAsync(inspection => inspection.IsarStepId != null && inspection.IsarStepId.Equals(id));
}
Expand All @@ -61,5 +65,29 @@ private IQueryable<Inspection> GetInspections()
{
return _context.Inspections.Include(inspection => inspection.InspectionFindings);
}

public async Task<Inspection?> AddFindings(InspectionFindingsQuery inspectionFindingsQuery)
{

var inspection = await ReadByIsarStepId(inspectionFindingsQuery.IsarStepId);

if (inspection is null)
{
return null;
}

var inspectionFindings = new InspectionFindings
{
InspectionDate = inspectionFindingsQuery.InspectionDate,
Area = inspectionFindingsQuery.Area,
IsarStepId = inspectionFindingsQuery.IsarStepId,
Findings = inspectionFindingsQuery.Findings
};

inspection.InspectionFindings.Add(inspectionFindings);
inspection = await Update(inspection);
_ = _signalRService.SendMessageAsync("Inspection findings added", inspection);
return inspection;
}
}
}
Loading