MVC (layered variant)
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
#if POSTGRES
|
||||
using MVC_SimpleCRUD_Layered.Data.Data.Postgres;
|
||||
#else
|
||||
using MVC_SimpleCRUD_Layered.Data.Data.MSSQL;
|
||||
#endif
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MVC_SimpleCRUD_Layered.Application.Models;
|
||||
using MVC_SimpleCRUD_Layered.Application.Util.Extensions;
|
||||
using MVC_SimpleCRUD_Layered.Data.Models;
|
||||
using Sieve.Models;
|
||||
using Sieve.Services;
|
||||
|
||||
namespace MVC_SimpleCRUD_Layered.Application.People;
|
||||
|
||||
public class PeopleService : IPeopleService
|
||||
{
|
||||
private readonly EventsContext ctx;
|
||||
private readonly ISieveProcessor sieveProcessor;
|
||||
private readonly PagingSettings pagingSettings;
|
||||
|
||||
public PeopleService(EventsContext ctx, IOptionsSnapshot<PagingSettings> pagingSettings, ISieveProcessor sieveProcessor)
|
||||
{
|
||||
this.ctx = ctx;
|
||||
this.sieveProcessor = sieveProcessor;
|
||||
this.pagingSettings = pagingSettings.Value;
|
||||
}
|
||||
|
||||
public async Task<List<PersonInfo>> GetAllForSimpleListAsync()
|
||||
{
|
||||
return await ProjectPeople
|
||||
.OrderBy(p => p.LastNameTranscription)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<PagedList<PersonInfo>> GetPagedListAsync(PeopleIndexRequest request)
|
||||
{
|
||||
var page = request.Page < 1 ? 1 : request.Page;
|
||||
var pageSize = request.PageSize <= 0 ? pagingSettings.PageSize : request.PageSize;
|
||||
var sorts = request.Sorts.NullIfWhiteSpace() ?? nameof(PersonInfo.LastNameTranscription);
|
||||
|
||||
var sieveModel = new SieveModel
|
||||
{
|
||||
Page = page,
|
||||
PageSize = pageSize,
|
||||
Sorts = sorts
|
||||
};
|
||||
|
||||
var totalCount = await ctx.People.CountAsync();
|
||||
var query = ProjectPeople;
|
||||
|
||||
int filteredCount;
|
||||
var searchText = request.SearchText.NullIfWhiteSpace();
|
||||
if (searchText is not null)
|
||||
{
|
||||
sieveModel.Filters = $"({nameof(PersonInfo.FirstNameTranscription)}|{nameof(PersonInfo.LastNameTranscription)}|{nameof(PersonInfo.CountryName)})@=*{searchText}";
|
||||
filteredCount = await sieveProcessor
|
||||
.Apply(sieveModel, query, applyPagination: false, applySorting: false)
|
||||
.CountAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
filteredCount = totalCount;
|
||||
}
|
||||
|
||||
var pagingInfo = new PagingInfo
|
||||
{
|
||||
FilteredItemsCount = filteredCount,
|
||||
TotalItemsCount = totalCount,
|
||||
ItemsPerPage = pageSize,
|
||||
CurrentPage = page,
|
||||
Sorts = sorts,
|
||||
SearchText = searchText
|
||||
};
|
||||
|
||||
if (pagingInfo.CurrentPage > pagingInfo.TotalPages)
|
||||
{
|
||||
pagingInfo.CurrentPage = pagingInfo.TotalPages;
|
||||
sieveModel.Page = pagingInfo.CurrentPage;
|
||||
}
|
||||
|
||||
var people = filteredCount > 0
|
||||
? await sieveProcessor.Apply(sieveModel, query).ToListAsync()
|
||||
: [];
|
||||
|
||||
return new PagedList<PersonInfo>(people, pagingInfo);
|
||||
}
|
||||
|
||||
public async Task<PersonForm?> GetFormForEditAsync(int id, PeopleIndexRequest returnRequest)
|
||||
{
|
||||
var person = await ctx.People.AsNoTracking().FirstOrDefaultAsync(p => p.Id == id);
|
||||
return person is null ? null : CreateForm(person, returnRequest);
|
||||
}
|
||||
|
||||
public async Task<List<CountryOption>> GetCountryOptionsAsync()
|
||||
{
|
||||
return await ctx.Countries
|
||||
.AsNoTracking()
|
||||
.OrderBy(d => d.Name)
|
||||
.Select(d => new CountryOption(d.Code, d.Name))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<SavePersonResult> CreateAsync(PersonForm form)
|
||||
{
|
||||
var person = new Person();
|
||||
CopyToPerson(form, person);
|
||||
ctx.People.Add(person);
|
||||
|
||||
try
|
||||
{
|
||||
await ctx.SaveChangesAsync();
|
||||
return SavePersonResult.Ok(GetDisplayName(person));
|
||||
}
|
||||
catch (DbUpdateException exc)
|
||||
{
|
||||
return SavePersonResult.Failed(GetErrorMessage(exc));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<SavePersonResult> UpdateAsync(int id, PersonForm form)
|
||||
{
|
||||
var person = await ctx.People.FindAsync(id);
|
||||
if (person is null)
|
||||
{
|
||||
return SavePersonResult.Failed($"Person with id {id} was not found.");
|
||||
}
|
||||
|
||||
CopyToPerson(form, person);
|
||||
|
||||
try
|
||||
{
|
||||
await ctx.SaveChangesAsync();
|
||||
return SavePersonResult.Ok(GetDisplayName(person));
|
||||
}
|
||||
catch (DbUpdateException exc)
|
||||
{
|
||||
return SavePersonResult.Failed(GetErrorMessage(exc));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DeletePersonResult> DeleteAsync(int id)
|
||||
{
|
||||
var person = await ctx.People.FindAsync(id);
|
||||
if (person is null)
|
||||
{
|
||||
return DeletePersonResult.NotFound();
|
||||
}
|
||||
|
||||
var personName = GetDisplayName(person);
|
||||
ctx.People.Remove(person);
|
||||
|
||||
try
|
||||
{
|
||||
await ctx.SaveChangesAsync();
|
||||
return DeletePersonResult.Deleted(personName);
|
||||
}
|
||||
catch (DbUpdateException exc)
|
||||
{
|
||||
return DeletePersonResult.Failed(personName, GetErrorMessage(exc));
|
||||
}
|
||||
}
|
||||
|
||||
private IQueryable<PersonInfo> ProjectPeople => ctx.People
|
||||
.Select(p => new PersonInfo
|
||||
{
|
||||
Id = p.Id,
|
||||
FirstName = p.FirstName,
|
||||
LastName = p.LastName,
|
||||
FirstNameTranscription = p.FirstNameTranscription,
|
||||
LastNameTranscription = p.LastNameTranscription,
|
||||
BirthDate = p.BirthDate,
|
||||
CountryName = p.CountryCodeNavigation.Name,
|
||||
});
|
||||
|
||||
private static PersonForm CreateForm(Person person, PeopleIndexRequest returnRequest)
|
||||
{
|
||||
return new PersonForm
|
||||
{
|
||||
Id = person.Id,
|
||||
FirstName = person.FirstName,
|
||||
LastName = person.LastName,
|
||||
FirstNameTranscription = person.FirstNameTranscription,
|
||||
LastNameTranscription = person.LastNameTranscription,
|
||||
AddressLine = person.AddressLine,
|
||||
PostalCode = person.PostalCode,
|
||||
City = person.City,
|
||||
AddressCountry = person.AddressCountry,
|
||||
Email = person.Email,
|
||||
ContactPhone = person.ContactPhone,
|
||||
BirthDate = person.BirthDate,
|
||||
DocumentNumber = person.DocumentNumber,
|
||||
CountryCode = person.CountryCode,
|
||||
Page = returnRequest.Page,
|
||||
PageSize = returnRequest.PageSize,
|
||||
Sorts = returnRequest.Sorts,
|
||||
SearchText = returnRequest.SearchText
|
||||
};
|
||||
}
|
||||
|
||||
private static void CopyToPerson(PersonForm form, Person person)
|
||||
{
|
||||
person.FirstName = form.FirstName.NullIfWhiteSpace();
|
||||
person.LastName = form.LastName.NullIfWhiteSpace();
|
||||
person.FirstNameTranscription = form.FirstNameTranscription;
|
||||
person.LastNameTranscription = form.LastNameTranscription;
|
||||
person.AddressLine = form.AddressLine.NullIfWhiteSpace();
|
||||
person.PostalCode = form.PostalCode.NullIfWhiteSpace();
|
||||
person.City = form.City.NullIfWhiteSpace();
|
||||
person.AddressCountry = form.AddressCountry.NullIfWhiteSpace();
|
||||
person.Email = form.Email.NullIfWhiteSpace();
|
||||
person.ContactPhone = form.ContactPhone.NullIfWhiteSpace();
|
||||
person.BirthDate = form.BirthDate!.Value;
|
||||
person.DocumentNumber = form.DocumentNumber;
|
||||
person.CountryCode = form.CountryCode;
|
||||
}
|
||||
|
||||
private static string GetDisplayName(Person person) => $"{person.FirstNameTranscription} {person.LastNameTranscription}";
|
||||
|
||||
private static string GetErrorMessage(DbUpdateException exc) => exc.InnerException?.Message ?? exc.Message;
|
||||
}
|
||||
Reference in New Issue
Block a user