Created
September 25, 2019 21:52
-
-
Save baio/a7afd4c1b084a28a77ff8758f73a4d32 to your computer and use it in GitHub Desktop.
C# 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UIDef.Lists.Data.Core; | |
using System.Linq; | |
using Microsoft.EntityFrameworkCore; | |
using UIDef.Core.Domain; | |
using System; | |
using UIDef.Lists.Data.Core.Domain; | |
using CSharpFunctionalExtensions; | |
using UIDef.Lists.Requests.GetFromatters; | |
using UIDef.Core.MediatR.CRUDHandlers.GetList; | |
namespace UIDef.Lists.Domain.Requests.Formatters.GetFromatters | |
{ | |
public class GetFormattersHandler : GetListHandler<GetFormattersRequest, Formatter, GetFormattersResultItem, GetFormattersFilterDTO, SortFieldsDTO> | |
{ | |
private readonly IUnitOfWork _unitOfWork; | |
public GetFormattersHandler(IUnitOfWork unitOfWork, ICheckProjectPermission checkProjectPermission) | |
: base(checkProjectPermission) | |
{ | |
_unitOfWork = unitOfWork; | |
} | |
protected override IQueryable<Formatter> OnGetQuery(int projectId) => | |
_unitOfWork.Formatters.GetAll().Where(p => p.ProjectId == projectId); | |
protected override Func<Formatter, dynamic> OnOrder(SortFieldsDTO sort) => sort switch | |
{ | |
SortFieldsDTO.Name => x => x.Name, | |
SortFieldsDTO.ValueType => x => x.ValueType, | |
{ } => throw new ArgumentOutOfRangeException() | |
}; | |
protected override IQueryable<Formatter> OnFilter(IQueryable<Formatter> q, GetFormattersFilterDTO filter) => filter switch | |
{ | |
{ Name: var name } when name != null => q.Where(p => EF.Functions.Like(p.Name, $"%{name}%")), | |
_ => q | |
}; | |
protected override GetFormattersResultItem OnMapItem(Formatter item) => new GetFormattersResultItem( | |
item.Id, | |
item.Name, | |
item.ValueType, | |
item.Description | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment