From 4332edcd8da31272e4c564ddf4363ea9d803704d Mon Sep 17 00:00:00 2001 From: itbeard Date: Tue, 9 Nov 2021 02:37:55 +0300 Subject: [PATCH] added basic person update --- .../Person/EditPersonRequest.cs | 2 +- .../Person/EditPersonResponse.cs | 9 ++++ Pds/Pds.Api.Contracts/Person/PersonDto.cs | 2 + Pds/Pds.Api/Controllers/PersonController.cs | 27 +++++++++++ .../Exceptions/Person/PersonEditException.cs | 26 ++++++++++ Pds/Pds.Data/Repositories/PersonRepository.cs | 1 + Pds/Pds.Mappers/ApiMappingProfile.cs | 6 +++ .../Person/EditPersonModel.cs | 27 +++++++++++ Pds/Pds.Services/Interfaces/IPersonService.cs | 3 ++ Pds/Pds.Services/Services/ContentService.cs | 1 + Pds/Pds.Services/Services/PersonService.cs | 47 +++++++++++++++++++ Pds/Pds.Web/Pages/Persons/Edit.razor | 20 ++++---- 12 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 Pds/Pds.Api.Contracts/Person/EditPersonResponse.cs create mode 100644 Pds/Pds.Core/Exceptions/Person/PersonEditException.cs create mode 100644 Pds/Pds.Services.Models/Person/EditPersonModel.cs diff --git a/Pds/Pds.Api.Contracts/Person/EditPersonRequest.cs b/Pds/Pds.Api.Contracts/Person/EditPersonRequest.cs index e34666f2..d3ab8b26 100644 --- a/Pds/Pds.Api.Contracts/Person/EditPersonRequest.cs +++ b/Pds/Pds.Api.Contracts/Person/EditPersonRequest.cs @@ -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 { diff --git a/Pds/Pds.Api.Contracts/Person/EditPersonResponse.cs b/Pds/Pds.Api.Contracts/Person/EditPersonResponse.cs new file mode 100644 index 00000000..af3f061e --- /dev/null +++ b/Pds/Pds.Api.Contracts/Person/EditPersonResponse.cs @@ -0,0 +1,9 @@ +using System; + +namespace Pds.Api.Contracts.Person +{ + public class EditPersonResponse + { + public Guid Id { get; set; } + } +} \ No newline at end of file diff --git a/Pds/Pds.Api.Contracts/Person/PersonDto.cs b/Pds/Pds.Api.Contracts/Person/PersonDto.cs index 54cf4191..0a3908aa 100644 --- a/Pds/Pds.Api.Contracts/Person/PersonDto.cs +++ b/Pds/Pds.Api.Contracts/Person/PersonDto.cs @@ -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; } diff --git a/Pds/Pds.Api/Controllers/PersonController.cs b/Pds/Pds.Api/Controllers/PersonController.cs index 26fc8835..c0fe373e 100644 --- a/Pds/Pds.Api/Controllers/PersonController.cs +++ b/Pds/Pds.Api/Controllers/PersonController.cs @@ -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 { @@ -109,6 +110,32 @@ public async Task Create(CreatePersonRequest request) } } + /// + /// Edit person + /// + /// + [HttpPut] + [ProducesResponseType(typeof(EditPersonResponse), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] + public async Task Edit(EditPersonRequest request) + { + try + { + if (ModelState.IsValid) + { + var editCostModel = mapper.Map(request); + var costId = await personService.EditAsync(editCostModel); + return Ok(new EditPersonResponse{Id = costId}); + } + + return BadRequest(); + } + catch (Exception e) + { + return ExceptionResult(e); + } + } + /// /// Delete specified person /// diff --git a/Pds/Pds.Core/Exceptions/Person/PersonEditException.cs b/Pds/Pds.Core/Exceptions/Person/PersonEditException.cs new file mode 100644 index 00000000..79e8a89b --- /dev/null +++ b/Pds/Pds.Core/Exceptions/Person/PersonEditException.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; + +namespace Pds.Core.Exceptions.Person +{ + public class PersonEditException : Exception, IApiException + { + public List Errors { get; } + + public PersonEditException(List errors) + { + Errors = errors; + } + + public PersonEditException(string message) + : base(message) + { + Errors = new List { message }; + } + + public PersonEditException(string message, Exception inner) + : base(message, inner) + { + } + } +} \ No newline at end of file diff --git a/Pds/Pds.Data/Repositories/PersonRepository.cs b/Pds/Pds.Data/Repositories/PersonRepository.cs index ed898e44..940d09aa 100644 --- a/Pds/Pds.Data/Repositories/PersonRepository.cs +++ b/Pds/Pds.Data/Repositories/PersonRepository.cs @@ -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; diff --git a/Pds/Pds.Mappers/ApiMappingProfile.cs b/Pds/Pds.Mappers/ApiMappingProfile.cs index d517b7e5..379782a3 100644 --- a/Pds/Pds.Mappers/ApiMappingProfile.cs +++ b/Pds/Pds.Mappers/ApiMappingProfile.cs @@ -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 @@ -133,6 +134,11 @@ public ApiMappingProfile() CreateMap(); CreateMap(); CreateMap(); + CreateMap() + .ForMember( + dest => dest.BrandsIds, + opt => opt + .MapFrom(p => p.Brands.Where(b=>b.IsSelected).Select(b=>b.Id))); CreateMap(); CreateMap() .ForMember( diff --git a/Pds/Pds.Services.Models/Person/EditPersonModel.cs b/Pds/Pds.Services.Models/Person/EditPersonModel.cs new file mode 100644 index 00000000..9b082887 --- /dev/null +++ b/Pds/Pds.Services.Models/Person/EditPersonModel.cs @@ -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 BrandsIds { get; set; } + } +} \ No newline at end of file diff --git a/Pds/Pds.Services/Interfaces/IPersonService.cs b/Pds/Pds.Services/Interfaces/IPersonService.cs index 9a785f66..9bafd4b0 100644 --- a/Pds/Pds.Services/Interfaces/IPersonService.cs +++ b/Pds/Pds.Services/Interfaces/IPersonService.cs @@ -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 { @@ -13,6 +14,8 @@ public interface IPersonService Task CreateAsync(Person person); + Task EditAsync(EditPersonModel model); + Task ArchiveAsync(Guid personId); Task UnarchiveAsync(Guid personId); diff --git a/Pds/Pds.Services/Services/ContentService.cs b/Pds/Pds.Services/Services/ContentService.cs index ddd9e9a7..c6d89455 100644 --- a/Pds/Pds.Services/Services/ContentService.cs +++ b/Pds/Pds.Services/Services/ContentService.cs @@ -108,6 +108,7 @@ public async Task 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; diff --git a/Pds/Pds.Services/Services/PersonService.cs b/Pds/Pds.Services/Services/PersonService.cs index 76cb1dfc..da529ed0 100644 --- a/Pds/Pds.Services/Services/PersonService.cs +++ b/Pds/Pds.Services/Services/PersonService.cs @@ -7,6 +7,7 @@ using Pds.Data; using Pds.Data.Entities; using Pds.Services.Interfaces; +using Pds.Services.Models.Person; namespace Pds.Services.Services { @@ -60,6 +61,52 @@ public async Task CreateAsync(Person person) return result.Id; } + public async Task 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(); + 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); diff --git a/Pds/Pds.Web/Pages/Persons/Edit.razor b/Pds/Pds.Web/Pages/Persons/Edit.razor index c816abe5..7edca3e0 100644 --- a/Pds/Pds.Web/Pages/Persons/Edit.razor +++ b/Pds/Pds.Web/Pages/Persons/Edit.razor @@ -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 @@ -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); - + } @@ -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); } @@ -114,6 +109,7 @@ else if (isValid) { isSaveButtonDisabled = true; + person.Brands = brands; // save new state of brands var result = await ApiClient.Put(TokenProvider, "persons", person); isSaveButtonDisabled = false; @@ -128,9 +124,15 @@ else } } - private async Task> GetBrandsAsync() + private async Task> GetBrandsAsync(List personBrands) { - return await ApiClient.Get>(TokenProvider, "persons/get-brands"); + var allBrands = await ApiClient.Get>(TokenProvider, "persons/get-brands"); + foreach (var brand in allBrands) + { + brand.IsSelected = personBrands.Select(b => b.Id).Contains(brand.Id); + } + + return allBrands; } private async Task GetPerson()