diff --git a/backend/api/Controllers/InspectionFindingsController.cs b/backend/api/Controllers/InspectionFindingsController.cs new file mode 100644 index 000000000..4c3eabcb8 --- /dev/null +++ b/backend/api/Controllers/InspectionFindingsController.cs @@ -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; + + } + + /// <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); + 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}'"); + } + + } + +} diff --git a/backend/api/Controllers/Models/InspectionFindingsQuery.cs b/backend/api/Controllers/Models/InspectionFindingsQuery.cs new file mode 100644 index 000000000..e13d8d184 --- /dev/null +++ b/backend/api/Controllers/Models/InspectionFindingsQuery.cs @@ -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; } + + + } + +} + diff --git a/backend/api/Database/Models/InspectionFindings.cs b/backend/api/Database/Models/InspectionFindings.cs index 0243e00c5..b2d2e348c 100644 --- a/backend/api/Database/Models/InspectionFindings.cs +++ b/backend/api/Database/Models/InspectionFindings.cs @@ -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; } - 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"; + } } } diff --git a/backend/api/Program.cs b/backend/api/Program.cs index e66c224df..7265e0d5a 100644 --- a/backend/api/Program.cs +++ b/backend/api/Program.cs @@ -74,7 +74,6 @@ builder.Services.AddScoped<IPoseTimeseriesService, PoseTimeseriesService>(); builder.Services.AddScoped<ILastMissionRunService, LastMissionRunService>(); - bool useInMemoryDatabase = builder.Configuration .GetSection("Database") .GetValue<bool>("UseInMemoryDatabase"); diff --git a/backend/api/Services/InspectionService.cs b/backend/api/Services/InspectionService.cs index ea1559a51..2ca1488aa 100644 --- a/backend/api/Services/InspectionService.cs +++ b/backend/api/Services/InspectionService.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Api.Controllers.Models; using Api.Database.Context; using Api.Database.Models; using Api.Services.Models; @@ -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( @@ -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)); } @@ -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; + } } }