Skip to content

Commit

Permalink
added basic person update
Browse files Browse the repository at this point in the history
  • Loading branch information
itbeard committed Nov 8, 2021
1 parent 375a941 commit 4332edc
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Pds/Pds.Api.Contracts/Person/EditPersonRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.ComponentModel.DataAnnotations;
using Pds.Api.Contracts.Person;

namespace Pds.Api.Contracts.Cost
namespace Pds.Api.Contracts.Person
{
public class EditPersonRequest
{
Expand Down
9 changes: 9 additions & 0 deletions Pds/Pds.Api.Contracts/Person/EditPersonResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Pds.Api.Contracts.Person
{
public class EditPersonResponse
{
public Guid Id { get; set; }
}
}
2 changes: 2 additions & 0 deletions Pds/Pds.Api.Contracts/Person/PersonDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class PersonDto

public string FullName { get; set; }

public string Country { get; set; }

public string City { get; set; }

public string Location { get; set; }
Expand Down
27 changes: 27 additions & 0 deletions Pds/Pds.Api/Controllers/PersonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Pds.Api.Contracts.Person;
using Pds.Data.Entities;
using Pds.Services.Interfaces;
using Pds.Services.Models.Person;

namespace Pds.Api.Controllers
{
Expand Down Expand Up @@ -109,6 +110,32 @@ public async Task<IActionResult> Create(CreatePersonRequest request)
}
}

/// <summary>
/// Edit person
/// </summary>
/// <returns></returns>
[HttpPut]
[ProducesResponseType(typeof(EditPersonResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Edit(EditPersonRequest request)
{
try
{
if (ModelState.IsValid)
{
var editCostModel = mapper.Map<EditPersonModel>(request);
var costId = await personService.EditAsync(editCostModel);
return Ok(new EditPersonResponse{Id = costId});
}

return BadRequest();
}
catch (Exception e)
{
return ExceptionResult(e);
}
}

/// <summary>
/// Delete specified person
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions Pds/Pds.Core/Exceptions/Person/PersonEditException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;

namespace Pds.Core.Exceptions.Person
{
public class PersonEditException : Exception, IApiException
{
public List<string> Errors { get; }

public PersonEditException(List<string> errors)
{
Errors = errors;
}

public PersonEditException(string message)
: base(message)
{
Errors = new List<string> { message };
}

public PersonEditException(string message, Exception inner)
: base(message, inner)
{
}
}
}
1 change: 1 addition & 0 deletions Pds/Pds.Data/Repositories/PersonRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Pds.Core.Enums;
using Pds.Core.Exceptions;
using Pds.Data.Entities;
using Pds.Data.Repositories.Interfaces;

Expand Down
6 changes: 6 additions & 0 deletions Pds/Pds.Mappers/ApiMappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Pds.Services.Models.Client;
using Pds.Services.Models.Content;
using Pds.Services.Models.Cost;
using Pds.Services.Models.Person;
using Pds.Web.Models.Content;

namespace Pds.Mappers
Expand Down Expand Up @@ -133,6 +134,11 @@ public ApiMappingProfile()
CreateMap<EditClientRequest, EditClientModel>();
CreateMap<EditCostRequest, EditCostModel>();
CreateMap<EditBillRequest, EditBillModel>();
CreateMap<EditPersonRequest, EditPersonModel>()
.ForMember(
dest => dest.BrandsIds,
opt => opt
.MapFrom(p => p.Brands.Where(b=>b.IsSelected).Select(b=>b.Id)));
CreateMap<CreateContentBillDto, CreateContentBillModel>();
CreateMap<CreateContentRequest, CreateContentModel>()
.ForMember(
Expand Down
27 changes: 27 additions & 0 deletions Pds/Pds.Services.Models/Person/EditPersonModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;

namespace Pds.Services.Models.Person
{
public class EditPersonModel
{
public Guid Id { get; set; }
public string FirstName { get; set; }

public string LastName { get; set; }

public string ThirdName { get; set; }

public string Country { get; set; }

public string City { get; set; }

public string Topics { get; set; }

public string Info { get; set; }

public int? Rate { get; set; }

public List<Guid> BrandsIds { get; set; }
}
}
3 changes: 3 additions & 0 deletions Pds/Pds.Services/Interfaces/IPersonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Pds.Data.Entities;
using Pds.Services.Models.Person;

namespace Pds.Services.Interfaces
{
Expand All @@ -13,6 +14,8 @@ public interface IPersonService

Task<Guid> CreateAsync(Person person);

Task<Guid> EditAsync(EditPersonModel model);

Task ArchiveAsync(Guid personId);

Task UnarchiveAsync(Guid personId);
Expand Down
1 change: 1 addition & 0 deletions Pds/Pds.Services/Services/ContentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public async Task<Guid> EditAsync(EditContentModel model)
content.ReleaseDate = model.ReleaseDate.Date;
content.EndDate = model.EndDate?.Date;
content.PersonId = model.PersonId != null && model.PersonId.Value == Guid.Empty ? null : model.PersonId;

if (model.Bill != null && content.Bill != null) // Just update existed bill
{
content.Bill.ClientId = model.Bill.ClientId;
Expand Down
47 changes: 47 additions & 0 deletions Pds/Pds.Services/Services/PersonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Pds.Data;
using Pds.Data.Entities;
using Pds.Services.Interfaces;
using Pds.Services.Models.Person;

namespace Pds.Services.Services
{
Expand Down Expand Up @@ -60,6 +61,52 @@ public async Task<Guid> CreateAsync(Person person)
return result.Id;
}

public async Task<Guid> EditAsync(EditPersonModel model)
{
if (model == null)
{
throw new PersonEditException($"Модель запроса пуста.");
}

if (model.BrandsIds.Count == 0)
{
throw new PersonEditException("Персону нельзя создать без бренда.");
}

var person = await unitOfWork.Persons.GetFullByIdAsync(model.Id);

if (person == null)
{
throw new PersonEditException($"Персона с id {model.Id} не найдена.");
}

if (person.Status == PersonStatus.Archived)
{
throw new PersonEditException($"Нельзя редактировать архивную персону.");
}

person.UpdatedAt = DateTime.UtcNow;
person.FirstName = model.FirstName;
person.LastName = model.LastName;
person.ThirdName = model.ThirdName;
person.Country = model.Country;
person.City = model.City;
person.Rate = model.Rate;
person.Topics = model.Topics;
person.Info = model.Info;

person.Brands = new List<Brand>();
foreach (var brandId in model.BrandsIds)
{
var brand = await unitOfWork.Brands.GetFirstWhereAsync(b => b.Id == brandId);
person.Brands.Add(brand);
}

var result = await unitOfWork.Persons.UpdateAsync(person);

return result.Id;
}

public async Task ArchiveAsync(Guid personId)
{
var person = await unitOfWork.Persons.GetFirstWhereAsync(p => p.Id == personId);
Expand Down
20 changes: 11 additions & 9 deletions Pds/Pds.Web/Pages/Persons/Edit.razor
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
@page "/persons/{personId}/edit"
@inherits BasePageComponent
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Pds.Api.Contracts
@using Pds.Api.Contracts.Cost
@using Pds.Api.Contracts.Person
@using Pds.Web.Common
@using static Pds.Web.Common.TitleExtension
Expand Down Expand Up @@ -35,10 +33,7 @@ else
@foreach (var brand in brands)
{
var id = $"brand_{brand.Id.ToString()}";
brand.IsSelected = person.Brands.Select(b=>b.Id).Contains(brand.Id);
<InputCheckbox name="@brand.Id"
class="btn-check" @bind-Value="brand.IsSelected"
Id="@id" />
<InputCheckbox name="@brand.Id" class="btn-check" @bind-Value="brand.IsSelected" Id="@id" />
<label class="btn btn-secondary" for="@id">@brand.Name</label>
}
</div>
Expand Down Expand Up @@ -101,7 +96,7 @@ else
{
isSaveButtonDisabled = false;
person = await GetPerson();
brands = await GetBrandsAsync();
brands = await GetBrandsAsync(person.Brands);
editContext = new EditContext(person);
msgStore = new ValidationMessageStore(editContext);
}
Expand All @@ -114,6 +109,7 @@ else
if (isValid)
{
isSaveButtonDisabled = true;
person.Brands = brands; // save new state of brands
var result =
await ApiClient.Put<object, EditPersonRequest>(TokenProvider, "persons", person);
isSaveButtonDisabled = false;
Expand All @@ -128,9 +124,15 @@ else
}
}

private async Task<List<BrandForCheckboxesDto>> GetBrandsAsync()
private async Task<List<BrandForCheckboxesDto>> GetBrandsAsync(List<BrandForCheckboxesDto> personBrands)
{
return await ApiClient.Get<List<BrandForCheckboxesDto>>(TokenProvider, "persons/get-brands");
var allBrands = await ApiClient.Get<List<BrandForCheckboxesDto>>(TokenProvider, "persons/get-brands");
foreach (var brand in allBrands)
{
brand.IsSelected = personBrands.Select(b => b.Id).Contains(brand.Id);
}

return allBrands;
}

private async Task<EditPersonRequest> GetPerson()
Expand Down

0 comments on commit 4332edc

Please sign in to comment.