Skip to content

Commit

Permalink
PIMS-1220, PIMS-1221, PIMS-1222 (#88)
Browse files Browse the repository at this point in the history
* PIMS-1220, PIMS-1221, PIMS-1222
Automap Evaluations
Move logic from controller to DAL
Rewrite DAL logic to be less verbose, support direct write of automapper
entities.

* code review comments

Co-authored-by: Smith <[email protected]>
  • Loading branch information
devinleighsmith and Smith authored Mar 27, 2020
1 parent 287e534 commit dc3b655
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 167 deletions.
129 changes: 47 additions & 82 deletions backend/api/Areas/Admin/Controllers/ParcelController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,6 @@ public IActionResult GetParcelByPid(string pid)
public IActionResult AddParcel([FromBody] ParcelModel model)
{
var entity = _mapper.Map<Entity.Parcel>(model);
entity.Evaluations.Add(new Entity.ParcelEvaluation(model.FiscalYear, entity) // TODO: Move this logic to AutoMapper.
{
EstimatedValue = model.EstimatedValue,
AssessedValue = model.AssessedValue,
NetBookValue = model.NetBookValue
});

foreach (var building in model.Buildings)
{
// We only allow adding buildings at this point. Can't include an existing one.
var b_entity = _mapper.Map<Entity.Building>(building);
b_entity.Evaluations.Add(new Entity.BuildingEvaluation(building.FiscalYear, b_entity) // TODO: Move this logic to AutoMapper.
{
EstimatedValue = building.EstimatedValue,
AssessedValue = building.AssessedValue,
NetBookValue = building.NetBookValue
});
entity.Buildings.Add(b_entity);
}

_pimsAdminService.Parcel.Add(entity);
var parcel = _mapper.Map<ParcelModel>(entity);
Expand All @@ -162,30 +143,6 @@ public IActionResult AddParcel([FromBody] ParcelModel model)
public IActionResult AddParcels([FromBody] ParcelModel[] models)
{
var entities = _mapper.Map<Entity.Parcel[]>(models);

for (var i = 0; i < models.Count(); i++)
{
var entity = entities[i];
entity.Evaluations.Add(new Entity.ParcelEvaluation(models[i].FiscalYear, entity) // TODO: Move this logic to AutoMapper.
{
EstimatedValue = models[i].EstimatedValue,
AssessedValue = models[i].AssessedValue,
NetBookValue = models[i].NetBookValue
});

foreach (var building in models[i].Buildings)
{
// We only allow adding buildings at this point. Can't include an existing one.
var b_entity = _mapper.Map<Entity.Building>(building);
b_entity.Evaluations.Add(new Entity.BuildingEvaluation(building.FiscalYear, b_entity) // TODO: Move this logic to AutoMapper.
{
EstimatedValue = building.EstimatedValue,
AssessedValue = building.AssessedValue,
NetBookValue = building.NetBookValue
});
entity.Buildings.Add(b_entity);
}
}
_pimsAdminService.Parcel.Add(entities);
var parcels = _mapper.Map<ParcelModel[]>(entities);

Expand All @@ -208,25 +165,28 @@ public IActionResult UpdateParcel([FromBody] ParcelModel model)

_mapper.Map(model, entity);

// Update evaluation.
var p_eval = entity.Evaluations.FirstOrDefault(e => e.FiscalYear == model.FiscalYear);
if (p_eval == null)
foreach (var evaluation in model.Evaluations)
{
entity.Evaluations.Add(new Entity.ParcelEvaluation(model.FiscalYear, entity) // TODO: Move this logic to AutoMapper.
// Update evaluation.
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 = model.EstimatedValue,
AssessedValue = model.AssessedValue,
NetBookValue = model.NetBookValue,
CreatedById = userId
EstimatedValue = evaluation.EstimatedValue,
AssessedValue = evaluation.AssessedValue,
NetBookValue = evaluation.NetBookValue,
CreatedById = userId
});
}
else
{
p_eval.EstimatedValue = model.EstimatedValue;
p_eval.AssessedValue = model.AssessedValue;
p_eval.NetBookValue = model.NetBookValue;
p_eval.UpdatedById = userId; // TODO: Move to DAL.
p_eval.UpdatedOn = DateTime.UtcNow;
}
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;
}
}

foreach (var building in model.Buildings)
Expand All @@ -236,14 +196,16 @@ public IActionResult UpdateParcel([FromBody] ParcelModel model)
// Add a new building to the parcel.
var b_entity = _mapper.Map<Entity.Building>(building);
b_entity.CreatedById = userId;

b_entity.Evaluations.Add(new Entity.BuildingEvaluation(building.FiscalYear, b_entity) // TODO: Move this logic to AutoMapper.
foreach (var evaluation in building.Evaluations)
{
b_entity.Evaluations.Add(new Entity.BuildingEvaluation(evaluation.FiscalYear, b_entity) // TODO: Move this logic to AutoMapper.
{
EstimatedValue = building.EstimatedValue,
AssessedValue = building.AssessedValue,
NetBookValue = building.NetBookValue,
CreatedById = userId
EstimatedValue = evaluation.EstimatedValue,
AssessedValue = evaluation.AssessedValue,
NetBookValue = evaluation.NetBookValue,
CreatedById = userId
});
}

entity.Buildings.Add(b_entity);
}
Expand All @@ -260,26 +222,29 @@ public IActionResult UpdateParcel([FromBody] ParcelModel model)
b_entity.UpdatedById = userId;
b_entity.UpdatedOn = DateTime.UtcNow;

// Update evaluation.
var b_eval = b_entity.Evaluations.FirstOrDefault(e => e.FiscalYear == building.FiscalYear);

if (b_eval == null)
foreach (var evaluation in building.Evaluations)
{
b_entity.Evaluations.Add(new Entity.BuildingEvaluation(building.FiscalYear, b_entity) // TODO: Move this logic to AutoMapper.
// 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 = building.EstimatedValue,
AssessedValue = building.AssessedValue,
NetBookValue = building.NetBookValue,
CreatedById = userId
EstimatedValue = evaluation.EstimatedValue,
AssessedValue = evaluation.AssessedValue,
NetBookValue = evaluation.NetBookValue,
CreatedById = userId
});
}
else
{
b_eval.EstimatedValue = building.EstimatedValue;
b_eval.AssessedValue = building.AssessedValue;
b_eval.NetBookValue = building.NetBookValue;
b_eval.UpdatedById = userId; // TODO: Move to DAL.
b_eval.UpdatedOn = DateTime.UtcNow;
}
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;
}
}

_pimsAdminService.Building.UpdateOne(b_entity);
Expand Down
9 changes: 1 addition & 8 deletions backend/api/Controllers/ParcelController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public IActionResult GetParcels()
/// <returns></returns>
[HttpPost]
[HasPermission(Permissions.PropertyView)]
[Route("/api/parcels/filter")]
public IActionResult GetParcels([FromBody]ParcelFilter filter)
{
filter.ThrowBadRequestIfNull($"The request must include a filter.");
Expand Down Expand Up @@ -102,14 +103,6 @@ public IActionResult AddParcel([FromBody] ParcelModel model)
var entity = _mapper.Map<Entity.Parcel>(model);
var userId = this.User.GetUserId();

foreach (var building in model.Buildings)
{
// We only allow adding buildings at this point. Can't include an existing one.
var b_entity = _mapper.Map<Entity.Building>(building);
b_entity.CreatedById = userId;
entity.Buildings.Add(b_entity);
}

_pimsService.Parcel.Add(entity);
var parcel = _mapper.Map<ParcelModel>(entity);

Expand Down
19 changes: 4 additions & 15 deletions backend/api/Models/BuildingModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Pims.Api.Models
{
Expand Down Expand Up @@ -35,13 +36,7 @@ public class BuildingModel : BaseModel, IEquatable<BuildingModel>

public float RentableArea { get; set; }

public int FiscalYear { get; set; }

public float EstimatedValue { get; set; }

public float AssessedValue { get; set; }

public float NetBookValue { get; set; }
public IEnumerable<EvaluationModel> Evaluations { get; set; } = new List<EvaluationModel>();

public override bool Equals(object obj)
{
Expand All @@ -65,10 +60,7 @@ public bool Equals([AllowNull] BuildingModel other)
BuildingPredominateUse == other.BuildingPredominateUse &&
BuildingTenancy == other.BuildingTenancy &&
RentableArea == other.RentableArea &&
FiscalYear == other.FiscalYear &&
EstimatedValue == other.EstimatedValue &&
AssessedValue == other.AssessedValue &&
NetBookValue == other.NetBookValue;
Enumerable.SequenceEqual(Evaluations, other.Evaluations);
}

public override int GetHashCode()
Expand All @@ -88,10 +80,7 @@ public override int GetHashCode()
hash.Add(BuildingPredominateUse);
hash.Add(BuildingTenancy);
hash.Add(RentableArea);
hash.Add(FiscalYear);
hash.Add(EstimatedValue);
hash.Add(AssessedValue);
hash.Add(NetBookValue);
hash.Add(Evaluations);
return hash.ToHashCode();
}
#endregion
Expand Down
49 changes: 49 additions & 0 deletions backend/api/Models/EvaluationModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace Pims.Api.Models
{
public class EvaluationModel : BaseModel, IEquatable<EvaluationModel>
{
#region Properties
public int PropertyId { get; set; }

public int FiscalYear { get; set; }

public float EstimatedValue { get; set; }

public float AssessedValue { get; set; }

public float NetBookValue { get; set; }

public override bool Equals(object obj)
{
return Equals(obj as EvaluationModel);
}

public bool Equals([AllowNull] EvaluationModel other)
{
return other != null &&
base.Equals(other) &&
PropertyId == other.PropertyId &&
FiscalYear == other.FiscalYear &&
EstimatedValue == other.EstimatedValue &&
AssessedValue == other.AssessedValue &&
NetBookValue == other.NetBookValue;
}

public override int GetHashCode()
{
var hash = new HashCode();
hash.Add(base.GetHashCode());
hash.Add(PropertyId);
hash.Add(FiscalYear);
hash.Add(EstimatedValue);
hash.Add(AssessedValue);
hash.Add(NetBookValue);
return hash.ToHashCode();
}

#endregion
}
}
21 changes: 4 additions & 17 deletions backend/api/Models/ParcelModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ public class ParcelModel : BaseModel, IEquatable<ParcelModel>

public AddressModel Address { get; set; }

public int FiscalYear { get; set; }

public float EstimatedValue { get; set; }

public float AssessedValue { get; set; }

public float NetBookValue { get; set; }

public double Latitude { get; set; }

public double Longitude { get; set; }
Expand All @@ -48,6 +40,7 @@ public class ParcelModel : BaseModel, IEquatable<ParcelModel>

public string LandLegalDescription { get; set; }

public IEnumerable<EvaluationModel> Evaluations { get; set; } = new List<EvaluationModel>();
public IEnumerable<Parts.ParcelBuildingModel> Buildings { get; set; } = new List<Parts.ParcelBuildingModel>();

public override bool Equals(object obj)
Expand All @@ -70,16 +63,13 @@ public bool Equals([AllowNull] ParcelModel other)
SubAgency == other.SubAgency &&
Agency == other.Agency &&
EqualityComparer<AddressModel>.Default.Equals(Address, other.Address) &&
FiscalYear == other.FiscalYear &&
EstimatedValue == other.EstimatedValue &&
AssessedValue == other.AssessedValue &&
NetBookValue == other.NetBookValue &&
Latitude == other.Latitude &&
Longitude == other.Longitude &&
LandArea == other.LandArea &&
Description == other.Description &&
LandLegalDescription == other.LandLegalDescription &&
Enumerable.SequenceEqual(Buildings, other.Buildings);
Enumerable.SequenceEqual(Buildings, other.Buildings) &&
Enumerable.SequenceEqual(Evaluations, other.Evaluations);
}

public override int GetHashCode()
Expand All @@ -97,16 +87,13 @@ public override int GetHashCode()
hash.Add(SubAgency);
hash.Add(Agency);
hash.Add(Address);
hash.Add(FiscalYear);
hash.Add(EstimatedValue);
hash.Add(AssessedValue);
hash.Add(NetBookValue);
hash.Add(Latitude);
hash.Add(Longitude);
hash.Add(LandArea);
hash.Add(Description);
hash.Add(LandLegalDescription);
hash.Add(Buildings);
hash.Add(Evaluations);
return hash.ToHashCode();
}

Expand Down
Loading

0 comments on commit dc3b655

Please sign in to comment.