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

psp-7909 take ui refactor. #3980

Merged
merged 5 commits into from
May 1, 2024
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
115 changes: 108 additions & 7 deletions source/backend/api/Areas/Takes/Controllers/TakeController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MapsterMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Pims.Api.Helpers.Exceptions;
using Pims.Api.Models.Concepts.Take;
using Pims.Api.Policies;
using Pims.Api.Services;
using Pims.Core.Extensions;
using Pims.Core.Json;
using Pims.Dal.Entities;
using Pims.Dal.Exceptions;
using Pims.Dal.Security;
using Swashbuckle.AspNetCore.Annotations;

Expand Down Expand Up @@ -104,28 +107,98 @@ public IActionResult GetTakesByPropertyId([FromRoute] long fileId, [FromRoute] l
}

/// <summary>
/// Update the list of takes associated to a property within an acquisition file.
/// Add the passed take to the acquisition property with the given id.
/// </summary>
/// <returns></returns>
[HttpPut("acquisition/property/{acquisitionFilePropertyId:long}")]
[HttpPost("acquisition/property/{acquisitionFilePropertyId:long}/takes")]
[HasPermission(Permissions.AcquisitionFileEdit, Permissions.PropertyEdit)]
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<TakeModel>), 200)]
[ProducesResponseType(typeof(TakeModel), 201)]
[SwaggerOperation(Tags = new[] { "take" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult AddAcquisitionPropertyTake(long acquisitionFilePropertyId, [FromBody] TakeModel take)
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
nameof(TakeController),
nameof(AddAcquisitionPropertyTake),
User.GetUsername(),
DateTime.Now);

if (acquisitionFilePropertyId != take.PropertyAcquisitionFileId)
{
throw new BadRequestException("Invalid acquisition file property id.");
}

_logger.LogInformation("Dispatching to service: {Service}", _takeService.GetType());

var addedTake = _takeService.AddAcquisitionPropertyTake(acquisitionFilePropertyId, _mapper.Map<PimsTake>(take));
return new JsonResult(_mapper.Map<TakeModel>(addedTake));
}

/// <summary>
/// Update a take with the given take and acquisition file property id with the passed take.
/// </summary>
/// <returns></returns>
[HttpPut("acquisition/property/{acquisitionFilePropertyId:long}/takes/{takeId:long}")]
[HasPermission(Permissions.AcquisitionFileEdit, Permissions.PropertyEdit)]
[Produces("application/json")]
[ProducesResponseType(typeof(TakeModel), 200)]
[SwaggerOperation(Tags = new[] { "take" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult UpdateAcquisitionPropertyTakes(long acquisitionFilePropertyId, [FromBody] IEnumerable<TakeModel> takes)
public IActionResult UpdateAcquisitionPropertyTake(long acquisitionFilePropertyId, long takeId, [FromBody] TakeModel take)
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
nameof(TakeController),
nameof(UpdateAcquisitionPropertyTakes),
nameof(UpdateAcquisitionPropertyTake),
User.GetUsername(),
DateTime.Now);

if (acquisitionFilePropertyId != take.PropertyAcquisitionFileId)
{
throw new BadRequestException("Invalid acquisition file property id.");
}
else if (takeId != take.Id)
{
throw new BadRequestException("Invalid take id.");
}

_logger.LogInformation("Dispatching to service: {Service}", _takeService.GetType());

var updatedTakes = _takeService.UpdateAcquisitionPropertyTakes(acquisitionFilePropertyId, _mapper.Map<IEnumerable<PimsTake>>(takes));
return new JsonResult(_mapper.Map<IEnumerable<TakeModel>>(updatedTakes));
var updatedTake = _takeService.UpdateAcquisitionPropertyTake(acquisitionFilePropertyId, _mapper.Map<PimsTake>(take));
return new JsonResult(_mapper.Map<TakeModel>(updatedTake));
}

/// <summary>
/// Delete a take with the given take id and acquisition file property id.
/// </summary>
[HttpDelete("acquisition/property/{acquisitionFilePropertyId:long}/takes/{takeId:long}")]
[HasPermission(Permissions.AcquisitionFileEdit, Permissions.PropertyEdit)]
[Produces("application/json")]
[ProducesResponseType(typeof(void), 200)]
[SwaggerOperation(Tags = new[] { "take" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public void DeleteAcquisitionPropertyTake(long acquisitionFilePropertyId, long takeId, [FromQuery] string[] userOverrideCodes)
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
nameof(TakeController),
nameof(DeleteAcquisitionPropertyTake),
User.GetUsername(),
DateTime.Now);

_logger.LogInformation("Dispatching to service: {Service}", _takeService.GetType());
var existingTake = _takeService.GetById(takeId);
if (existingTake.PropertyAcquisitionFileId != acquisitionFilePropertyId)
{
throw new BadRequestException("Invalid acquisition file property id.");
}
var deleted = _takeService.DeleteAcquisitionPropertyTake(takeId, userOverrideCodes.Select(oc => UserOverrideCode.Parse(oc)));
if (!deleted)
{
throw new InvalidOperationException($"Failed to delete take {takeId}.");
}
}

/// <summary>
Expand All @@ -152,6 +225,34 @@ public IActionResult GetTakesCountByPropertyId([FromRoute] long propertyId)
return new JsonResult(count);
}

/// <summary>
/// GGet a take by id.
/// </summary>
/// <returns></returns>
[HttpGet("acquisition/property/{acquisitionFilePropertyId:long}/takes/{takeId:long}")]
[HasPermission(Permissions.AcquisitionFileView, Permissions.PropertyView)]
[Produces("application/json")]
[ProducesResponseType(typeof(int), 200)]
[SwaggerOperation(Tags = new[] { "take" })]
public IActionResult GetTakeByPropertyFileId(long acquisitionFilePropertyId, long takeId)
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
nameof(TakeController),
nameof(GetTakesCountByPropertyId),
User.GetUsername(),
DateTime.Now);

_logger.LogInformation("Dispatching to service: {Service}", _takeService.GetType());

var take = _takeService.GetById(takeId);
if(take.PropertyAcquisitionFileId != acquisitionFilePropertyId)
{
throw new BadRequestException("Invalid acquisition file property id.");
}
return new JsonResult(_mapper.Map<TakeModel>(take));
}

#endregion
}
}
12 changes: 10 additions & 2 deletions source/backend/api/Services/ITakeService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
using System.Collections.Generic;
using Pims.Dal.Entities;
using Pims.Dal.Exceptions;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not needed?


namespace Pims.Api.Services
{
public interface ITakeService
{

PimsTake GetById(long takeId);

PimsTake AddAcquisitionPropertyTake(long acquisitionFilePropertyId, PimsTake take);

PimsTake UpdateAcquisitionPropertyTake(long acquisitionFilePropertyId, PimsTake take);

bool DeleteAcquisitionPropertyTake(long takeId, IEnumerable<UserOverrideCode> userOverrides);

IEnumerable<PimsTake> GetByFileId(long fileId);

IEnumerable<PimsTake> GetByPropertyId(long fileId, long acquisitionFilePropertyId);

int GetCountByPropertyId(long propertyId);

IEnumerable<PimsTake> UpdateAcquisitionPropertyTakes(long acquisitionFilePropertyId, IEnumerable<PimsTake> takes);
}
}
Loading
Loading