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

PIMS-1284, PIMS-1227 Resolving AutoMapper issues #96

Merged
merged 1 commit into from
Mar 31, 2020
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
30 changes: 16 additions & 14 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ nupkg/
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn
**/[Dd]ebug/
**/[Dd]ebugPublic/
**/[Rr]elease/
**/[Rr]eleases/
**/x64/
**/x86/
**/build/
**/bld/
**/[Bb]in/
**/[Oo]bj/
**/[Oo]ut/
**/msbuild.log
**/msbuild.err
**/msbuild.wrn

# Visual Studio 2015
.vs/
Expand All @@ -50,3 +50,5 @@ msbuild.wrn

# NET Core Healthchecks UI
healthchecksdb
healthchecksdb-shm
healthchecksdb-wal
60 changes: 56 additions & 4 deletions backend/Pims.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,43 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pims.Api.Test", "test\Pims.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pims.Dal", "dal\Pims.Dal.csproj", "{6DFFF5E1-1B87-403B-99D0-A9E03D8A8EB3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pims.Dal.Entities", "entities\Pims.Dal.Entities.csproj", "{1C724CD5-CD24-46CD-835A-A83F673F97B5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pims.Dal.Entities", "entities\Pims.Dal.Entities.csproj", "{1C724CD5-CD24-46CD-835A-A83F673F97B5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pims.Keycloak", "keycloak\Pims.Keycloak.csproj", "{970903E9-BC53-436F-BA77-C62349546425}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pims.Keycloak", "keycloak\Pims.Keycloak.csproj", "{970903E9-BC53-436F-BA77-C62349546425}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pims.Core", "core\Pims.Core.csproj", "{768D5D0A-A3B7-4599-B5CF-E32FC407AD9A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pims.Core", "core\Pims.Core.csproj", "{768D5D0A-A3B7-4599-B5CF-E32FC407AD9A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pims.Dal.Keycloak", "dal.keycloak\Pims.Dal.Keycloak.csproj", "{5697DD19-62CC-4377-ABA8-1E192376F4F6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pims.Dal.Keycloak", "dal.keycloak\Pims.Dal.Keycloak.csproj", "{5697DD19-62CC-4377-ABA8-1E192376F4F6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{E5C0D5F1-5433-42D8-A9FB-A887A42F5C06}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "libraries", "libraries", "{1CDE14FF-8E56-45D0-BA1D-ED4C49BDE0B2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{6F7A19B7-123A-44AF-9737-BBBD8CBD0E55}"
ProjectSection(SolutionItems) = preProject
docs\API.md = docs\API.md
docs\DAL.md = docs\DAL.md
docs\DATABASE.md = docs\DATABASE.md
docs\SETUP.md = docs\SETUP.md
docs\TOOLS.md = docs\TOOLS.md
docs\VERSIONING.md = docs\VERSIONING.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "root", "root", "{BBBAAD5E-6C48-4B44-A35D-70571BC90C41}"
ProjectSection(SolutionItems) = preProject
.dockerignore = .dockerignore
.editorconfig = .editorconfig
.gitignore = .gitignore
Dockerfile = Dockerfile
entrypoint.sh = entrypoint.sh
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{8610ED92-BF22-4A27-963A-F27C961EE333}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "import", "import", "{C3863C8F-4B5B-4990-AE5D-D6E63D5214DE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pims.Tools.Import", "tools\import\Pims.Tools.Import.csproj", "{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -111,10 +141,32 @@ Global
{5697DD19-62CC-4377-ABA8-1E192376F4F6}.Release|x64.Build.0 = Release|Any CPU
{5697DD19-62CC-4377-ABA8-1E192376F4F6}.Release|x86.ActiveCfg = Release|Any CPU
{5697DD19-62CC-4377-ABA8-1E192376F4F6}.Release|x86.Build.0 = Release|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Debug|x64.ActiveCfg = Debug|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Debug|x64.Build.0 = Debug|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Debug|x86.ActiveCfg = Debug|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Debug|x86.Build.0 = Debug|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Release|Any CPU.Build.0 = Release|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Release|x64.ActiveCfg = Release|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Release|x64.Build.0 = Release|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Release|x86.ActiveCfg = Release|Any CPU
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{19A6829C-20D8-4F66-9889-55C553ACE2B2} = {E5C0D5F1-5433-42D8-A9FB-A887A42F5C06}
{6DFFF5E1-1B87-403B-99D0-A9E03D8A8EB3} = {1CDE14FF-8E56-45D0-BA1D-ED4C49BDE0B2}
{1C724CD5-CD24-46CD-835A-A83F673F97B5} = {1CDE14FF-8E56-45D0-BA1D-ED4C49BDE0B2}
{970903E9-BC53-436F-BA77-C62349546425} = {1CDE14FF-8E56-45D0-BA1D-ED4C49BDE0B2}
{768D5D0A-A3B7-4599-B5CF-E32FC407AD9A} = {1CDE14FF-8E56-45D0-BA1D-ED4C49BDE0B2}
{5697DD19-62CC-4377-ABA8-1E192376F4F6} = {1CDE14FF-8E56-45D0-BA1D-ED4C49BDE0B2}
{C3863C8F-4B5B-4990-AE5D-D6E63D5214DE} = {8610ED92-BF22-4A27-963A-F27C961EE333}
{13BF0B7D-1BA6-4B62-BA01-6150059B81EC} = {C3863C8F-4B5B-4990-AE5D-D6E63D5214DE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3433C5DD-DC49-4A96-A1AE-90C1A1EBA87C}
EndGlobalSection
Expand Down
139 changes: 40 additions & 99 deletions backend/api/Areas/Admin/Controllers/ParcelController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using System.Linq;
using AutoMapper;
using BModel = Pims.Api.Models;
using Entity = Pims.Dal.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Pims.Dal.Helpers.Extensions;
using Entity = Pims.Dal.Entities;
using Model = Pims.Api.Models;
using Model = Pims.Api.Areas.Admin.Models.Parcel;
using Pims.Api.Helpers.Exceptions;
using Pims.Api.Policies;
using Pims.Core.Helpers;
using Pims.Dal.Helpers.Extensions;
using Pims.Dal.Security;
using Pims.Dal.Services.Admin;
using Pims.Core.Helpers;
using System.Collections.Generic;
using Pims.Api.Helpers.Exceptions;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Pims.Api.Areas.Admin.Controllers
{
Expand Down Expand Up @@ -59,17 +60,17 @@ public ParcelController(ILogger<ParcelController> logger, IPimsAdminService pims
[HttpGet]
[HasPermission(Permissions.PropertyView)]
[Produces("application/json")]
[ProducesResponseType(typeof(Entity.Models.Paged<Model.Parts.ParcelModel>), 200)]
[ProducesResponseType(typeof(Entity.Models.Paged<Model.PartialParcelModel>), 200)]
[SwaggerOperation(Tags = new[] { "admin-parcel" })]
public IActionResult GetParcels(int page = 1, int quantity = 10, string sort = null) // TODO: sort and filter.
{
if (page < 1) page = 1;
if (quantity < 1) quantity = 1;
if (quantity > 50) quantity = 50;

var result = _pimsAdminService.Parcel.GetNoTracking(page, quantity, sort);
var entities = _mapper.Map<Model.Parts.ParcelModel[]>(result.Items);
var paged = new Entity.Models.Paged<Model.Parts.ParcelModel>(entities, page, quantity, result.Total);
var result = _pimsAdminService.Parcel.Get(page, quantity, sort);
var entities = _mapper.Map<Model.PartialParcelModel[]>(result.Items);
var paged = new Entity.Models.Paged<Model.PartialParcelModel>(entities, page, quantity, result.Total);

return new JsonResult(paged);
}
Expand All @@ -83,11 +84,11 @@ public IActionResult GetParcels(int page = 1, int quantity = 10, string sort = n
[HasPermission(Permissions.PropertyView)]
[Produces("application/json")]
[ProducesResponseType(typeof(Model.ParcelModel), 200)]
[ProducesResponseType(typeof(Model.ErrorResponseModel), 400)]
[ProducesResponseType(typeof(BModel.ErrorResponseModel), 400)]
[SwaggerOperation(Tags = new[] { "admin-parcel" })]
public IActionResult GetParcel(int id)
{
var entity = _pimsAdminService.Parcel.GetNoTracking(id);
var entity = _pimsAdminService.Parcel.Get(id);

var parcel = _mapper.Map<Model.ParcelModel>(entity);

Expand All @@ -107,7 +108,7 @@ public IActionResult GetParcel(int id)
[SwaggerOperation(Tags = new[] { "admin-parcel" })]
public IActionResult GetParcelByPid(int id)
{
var entity = ExceptionHelper.HandleKeyNotFound(() => _pimsAdminService.Parcel.GetByPidNoTracking(id));
var entity = ExceptionHelper.HandleKeyNotFound(() => _pimsAdminService.Parcel.GetByPid(id));
if (entity == null) return NoContent();

var parcel = _mapper.Map<Model.ParcelModel>(entity);
Expand All @@ -125,13 +126,13 @@ public IActionResult GetParcelByPid(int id)
[Produces("application/json")]
[ProducesResponseType(typeof(Model.ParcelModel), 200)]
[ProducesResponseType(204)]
[ProducesResponseType(typeof(Model.ErrorResponseModel), 400)]
[ProducesResponseType(typeof(BModel.ErrorResponseModel), 400)]
[SwaggerOperation(Tags = new[] { "admin-parcel" })]
public IActionResult GetParcelByPid(string pid)
{
if (!int.TryParse(pid.Replace("-", ""), out int id)) throw new BadRequestException("PID is invalid");

var entity = ExceptionHelper.HandleKeyNotFound(() => _pimsAdminService.Parcel.GetByPidNoTracking(id));
var entity = ExceptionHelper.HandleKeyNotFound(() => _pimsAdminService.Parcel.GetByPid(id));
if (entity == null) return NoContent();

var parcel = _mapper.Map<Model.ParcelModel>(entity);
Expand All @@ -148,7 +149,7 @@ public IActionResult GetParcelByPid(string pid)
[HasPermission(Permissions.PropertyAdd)]
[Produces("application/json")]
[ProducesResponseType(typeof(Model.ParcelModel), 201)]
[ProducesResponseType(typeof(Model.ErrorResponseModel), 400)]
[ProducesResponseType(typeof(BModel.ErrorResponseModel), 400)]
[SwaggerOperation(Tags = new[] { "admin-parcel" })]
public IActionResult AddParcel([FromBody] Model.ParcelModel model)
{
Expand All @@ -157,7 +158,7 @@ public IActionResult AddParcel([FromBody] Model.ParcelModel model)
_pimsAdminService.Parcel.Add(entity);
var parcel = _mapper.Map<Model.ParcelModel>(entity);

return new CreatedAtActionResult(nameof(GetParcel), nameof(ParcelController), new { id = parcel.Id }, parcel);
return CreatedAtAction(nameof(GetParcel), new { id = parcel.Id }, parcel);
}

/// <summary>
Expand All @@ -169,7 +170,7 @@ public IActionResult AddParcel([FromBody] Model.ParcelModel model)
[HasPermission(Permissions.PropertyAdd)]
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<Model.ParcelModel>), 200)]
[ProducesResponseType(typeof(Model.ErrorResponseModel), 400)]
[ProducesResponseType(typeof(BModel.ErrorResponseModel), 400)]
[SwaggerOperation(Tags = new[] { "admin-parcel" })]
public IActionResult AddParcels([FromBody] Model.ParcelModel[] models)
{
Expand All @@ -182,6 +183,7 @@ public IActionResult AddParcels([FromBody] Model.ParcelModel[] models)

/// <summary>
/// PUT - Update the parcel in the datasource.
/// This function will not delete evaluations, only add or update.
/// </summary>
/// <param name="id"></param>
/// <param name="model">The parcel model.</param>
Expand All @@ -190,17 +192,12 @@ public IActionResult AddParcels([FromBody] Model.ParcelModel[] models)
[HasPermission(Permissions.PropertyEdit)]
[Produces("application/json")]
[ProducesResponseType(typeof(Model.ParcelModel), 200)]
[ProducesResponseType(typeof(Model.ErrorResponseModel), 400)]
[ProducesResponseType(typeof(BModel.ErrorResponseModel), 400)]
[SwaggerOperation(Tags = new[] { "admin-parcel" })]
public IActionResult UpdateParcel(int id, [FromBody] Model.ParcelModel model)
{

var entity = _pimsAdminService.Parcel.Get(model.Id);

if (entity == null) return BadRequest("Item does not exist");
var userId = this.User.GetUserId();
var address = entity.Address?.ToString();

_mapper.Map(model, entity);

foreach (var evaluation in model.Evaluations)
Expand All @@ -209,106 +206,50 @@ public IActionResult UpdateParcel(int id, [FromBody] Model.ParcelModel model)
var p_eval = entity.Evaluations.FirstOrDefault(e => e.FiscalYear == evaluation.FiscalYear);
if (p_eval == null)
{
entity.Evaluations.Add(new Entity.ParcelEvaluation(evaluation.FiscalYear, entity) // TODO: Move this logic to AutoMapper.
{
EstimatedValue = evaluation.EstimatedValue,
AssessedValue = evaluation.AssessedValue,
NetBookValue = evaluation.NetBookValue,
CreatedById = userId
});
entity.Evaluations.Add(new Entity.ParcelEvaluation(evaluation.FiscalYear, entity, evaluation.EstimatedValue, evaluation.AppraisedValue, evaluation.AssessedValue, evaluation.NetBookValue));
}
else
{
p_eval.EstimatedValue = evaluation.EstimatedValue;
p_eval.AssessedValue = evaluation.AssessedValue;
p_eval.NetBookValue = evaluation.NetBookValue;
p_eval.UpdatedById = userId; // TODO: Move to DAL.
p_eval.UpdatedOn = DateTime.UtcNow;
_mapper.Map(evaluation, p_eval);
}
}

foreach (var building in model.Buildings)
{
if (building.Id == 0)

var b_entity = entity.Buildings.FirstOrDefault(b => b.Id == building.Id);
if (b_entity == null)
{
// Add a new building to the parcel.
var b_entity = _mapper.Map<Entity.Building>(building);
b_entity.CreatedById = userId;
b_entity = _mapper.Map<Entity.Building>(building);
foreach (var evaluation in building.Evaluations)
{
b_entity.Evaluations.Add(new Entity.BuildingEvaluation(evaluation.FiscalYear, b_entity) // TODO: Move this logic to AutoMapper.
{
EstimatedValue = evaluation.EstimatedValue,
AssessedValue = evaluation.AssessedValue,
NetBookValue = evaluation.NetBookValue,
CreatedById = userId
});
b_entity.Evaluations.Add(new Entity.BuildingEvaluation(evaluation.FiscalYear, b_entity, evaluation.EstimatedValue, evaluation.AppraisedValue, evaluation.AssessedValue, evaluation.NetBookValue));
}

entity.Buildings.Add(b_entity);
}
else
{
// Update existing building on the parcel.
var b_entity = entity.Buildings.FirstOrDefault(b => b.Id == building.Id);
_mapper.Map(building, b_entity);

// We will ignore building Ids that don't match.
if (b_entity != null)
foreach (var evaluation in building.Evaluations)
{
var b_address = b_entity.Address?.ToString();
_mapper.Map(building, b_entity);
b_entity.UpdatedById = userId;
b_entity.UpdatedOn = DateTime.UtcNow;

foreach (var evaluation in building.Evaluations)
// Update evaluation.
var b_eval = b_entity.Evaluations.FirstOrDefault(e => e.FiscalYear == evaluation.FiscalYear);
if (b_eval == null)
{
// Update evaluation.
var b_eval = b_entity.Evaluations.FirstOrDefault(e => e.FiscalYear == evaluation.FiscalYear);

if (b_eval == null)
{
b_entity.Evaluations.Add(new Entity.BuildingEvaluation(evaluation.FiscalYear, b_entity) // TODO: Move this logic to AutoMapper.
{
EstimatedValue = evaluation.EstimatedValue,
AssessedValue = evaluation.AssessedValue,
NetBookValue = evaluation.NetBookValue,
CreatedById = userId
});
}
else
{
b_eval.EstimatedValue = evaluation.EstimatedValue;
b_eval.AssessedValue = evaluation.AssessedValue;
b_eval.NetBookValue = evaluation.NetBookValue;
b_eval.UpdatedById = userId; // TODO: Move to DAL.
b_eval.UpdatedOn = DateTime.UtcNow;
}
b_entity.Evaluations.Add(new Entity.BuildingEvaluation(evaluation.FiscalYear, b_entity, evaluation.EstimatedValue, evaluation.AppraisedValue, evaluation.AssessedValue, evaluation.NetBookValue));
}

_pimsAdminService.Building.UpdateOne(b_entity);

// Check if the address was updated.
if (b_address != b_entity.Address.ToString())
else
{
b_entity.Address.UpdatedById = userId;
b_entity.Address.UpdatedOn = DateTime.UtcNow;
_pimsAdminService.Address.UpdateOne(b_entity.Address);
_mapper.Map(evaluation, b_eval);
}
}
else
{
_logger.LogDebug($"Invalid - Attempting to update parcel with invalid building - PID:{entity.PID}, BuildingId:{building.Id}");
}
}
}

// Check if the address was updated.
if (address != entity.Address.ToString())
{
entity.Address.UpdatedById = userId;
entity.Address.UpdatedOn = DateTime.UtcNow;
_pimsAdminService.Address.UpdateOne(entity.Address);
}
_pimsAdminService.Parcel.Update(entity);
var parcel = _mapper.Map<Model.ParcelModel>(entity);

Expand All @@ -325,9 +266,9 @@ public IActionResult UpdateParcel(int id, [FromBody] Model.ParcelModel model)
[HasPermission(Permissions.PropertyAdd)]
[Produces("application/json")]
[ProducesResponseType(typeof(Model.ParcelModel), 200)]
[ProducesResponseType(typeof(Model.ErrorResponseModel), 400)]
[ProducesResponseType(typeof(BModel.ErrorResponseModel), 400)]
[SwaggerOperation(Tags = new[] { "admin-parcel" })]
public IActionResult DeleteParcel(Guid id, [FromBody] Model.ParcelModel model)
public IActionResult DeleteParcel(int id, [FromBody] Model.ParcelModel model)
{
var parcel = _mapper.Map<Entity.Parcel>(model);
_pimsAdminService.Parcel.Remove(parcel);
Expand Down
Loading