Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLID task done #12

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed Photo.db
Binary file not shown.
Binary file removed Photo.db-shm
Binary file not shown.
Empty file removed Photo.db-wal
Empty file.
11 changes: 4 additions & 7 deletions src/Spg.AloMalo.Api/Controllers/PhotosCqsController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Spg.AloMalo.Application.Services.PhotoUseCases.Query;
using Spg.AloMalo.DomainModel.Commands;
using Spg.AloMalo.DomainModel.Dtos;
using Spg.AloMalo.DomainModel.Exceptions;
using Spg.AloMalo.DomainModel.Queries;
using static Bogus.DataSets.Name;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace Spg.AloMalo.Api.Controllers
{
Expand All @@ -26,9 +22,10 @@ public PhotosCqsController(IMediator mediator)
[HttpGet()]
public IActionResult GetFiltered([FromQuery] GetPhotosQuery query)
{
var result = _mediator.Send(
new GetPhotosQueryModel(query)
);
List<PhotoDto> result = _mediator
.Send(new GetPhotosQueryModel(query))
.Result
.ToList();
return Ok(result);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Spg.AloMalo.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
options.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
});
builder.Services.AddTransient<IRequestHandler<CreatePhotoCommandModel, CreatePhotoReponseDto>, CreatePhotoCommandHandler>();
builder.Services.AddTransient<IRequestHandler<GetPhotosQueryModel, List<PhotoDto>>, GetPhotosQueryHandler>();
builder.Services.AddTransient<IRequestHandler<GetPhotosQueryModel, IQueryable<PhotoDto>>, GetPhotosQueryHandler>();
//
// Build App
var app = builder.Build();
Expand Down
69 changes: 69 additions & 0 deletions src/Spg.AloMalo.Application/ExpressionMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Spg.AloMalo.DomainModel.Interfaces.Repositories;
using System.Reflection;

namespace Spg.AloMalo.Application
{
public class ExpressionMapper<TEntity>
where TEntity : class
{
private readonly PropertyInfo _propertyInfo;
private readonly string? _propertyName;
private readonly string? _value1;
private readonly string? _value2;

public ExpressionMapper(PropertyInfo propertyInfo, string? propertyName = null
, string? value1 = null, string? value2 = null)
{
_propertyInfo = propertyInfo;
_propertyName = propertyName;
_value1 = value1;
_value2 = value2;
}


public void Use<T>(Func<T, IEntityFilterBuilder<TEntity>>? action)
{
if (string.IsNullOrEmpty(_value1))
{
return;
}
if (action is null)
{
return;
}

T target1 = TConverter.ChangeType<T>(_value1);

if ((_propertyInfo?.Name?.Trim()?.ToLower() ?? string.Empty)
== (_propertyName?.Trim()?.ToLower() ?? string.Empty))
{
action(target1);
}
}

public void Use<T1, T2>(Func<T1, T2, IEntityFilterBuilder<TEntity>>? action)
{
if (string.IsNullOrEmpty(_value1))
{
return;
}
if (string.IsNullOrEmpty(_value2))
{
return;
}
if (action is null)
{
return;
}

T1 target1 = TConverter.ChangeType<T1>(_value1);
T2 target2 = TConverter.ChangeType<T2>(_value2);

if ((_propertyInfo?.Name?.Trim()?.ToLower() ?? string.Empty)
== (_propertyName?.Trim()?.ToLower() ?? string.Empty))
{
action(target1, target2);
}
}
}
}
54 changes: 54 additions & 0 deletions src/Spg.AloMalo.Application/InterpretParameterBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Linq.Expressions;
using System.Reflection;

namespace Spg.AloMalo.Application
{
public abstract class InterpretParameterBase<TEntity>
where TEntity : class
{
private readonly string _operator;

public InterpretParameterBase(string @operator)
{
_operator = @operator;
}

public ExpressionMapper<TEntity> ForProperty<TProperty>(
string? queryParameter,
Expression<Func<TEntity, TProperty>> propertyExpression)
{
MemberExpression? member = propertyExpression.Body as MemberExpression;
if (member == null)
{
throw new ArgumentException($"{propertyExpression} is a Method!");
}
PropertyInfo? propInfo = member.Member as PropertyInfo;
if (propInfo == null)
{
throw new ArgumentException($"{propertyExpression} is a Field!");
}

if (string.IsNullOrEmpty(queryParameter))
{
return new ExpressionMapper<TEntity>(propInfo);
}
string[] parts = queryParameter.Split(' ');
if (parts.Length < 3 || parts.Length > 4)
{
throw new ArgumentException($"Incorrect Expression ('xxxx ct xxxx' or 'xxxx bw xxxx xxxx')");
}
if (parts[1].Trim().ToLower() == _operator.Trim().ToLower())
{
if (parts.Length == 3)
{
return new ExpressionMapper<TEntity>(propInfo, parts[0], parts[2]);
}
if (parts.Length == 4)
{
return new ExpressionMapper<TEntity>(propInfo, parts[0], parts[2], parts[3]);
}
}
return new ExpressionMapper<TEntity>(propInfo);
}
}
}
2 changes: 1 addition & 1 deletion src/Spg.AloMalo.Application/Services/PhotoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public IQueryable<PhotoDto> GetPhotos()
{
IQueryable<PhotoDto> result = _readOnlyPhotoRepository
.FilterBuilder
.ApplyNameContainsFilter("My")
//.ApplyNameContainsFilter("My")
.Build()
.Select(p =>
new PhotoDto(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Spg.AloMalo.Application.Services.PhotoUseCases.Query;
using Spg.AloMalo.DomainModel.Filters;
using Spg.AloMalo.DomainModel.Interfaces.Repositories;
using Spg.AloMalo.DomainModel.Model;

namespace Spg.AloMalo.Application.Services.PhotoUseCases.Filters
{
public class NameContainsParameter : IQueryParameter
{
private readonly IPhotoFilterBuilder _photoFilterBuilder;

public NameContainsParameter(IPhotoFilterBuilder photoFilterBuilder)
{
_photoFilterBuilder = photoFilterBuilder;
}

public IPhotoFilterBuilder Compile(string queryParameter)
{
string[] parts = queryParameter.Split(' ');
if (parts.Length == 3 && parts[0]?.Trim().ToLower() == "name" && parts[1]?.Trim().ToLower() == "contains")
{
return _photoFilterBuilder.ApplyFilter(new ContainsFilter<Photo>(p => p.Name, parts[2]));
}
return _photoFilterBuilder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Spg.AloMalo.Application.Services.PhotoUseCases.Query;
using Spg.AloMalo.DomainModel.Filters;
using Spg.AloMalo.DomainModel.Interfaces.Repositories;
using Spg.AloMalo.DomainModel.Model;

namespace Spg.AloMalo.Application.Services.PhotoUseCases.Filters
{
public class NameEndsWithParameter : IQueryParameter
{
private readonly IPhotoFilterBuilder _photoFilterBuilder;

public NameEndsWithParameter(IPhotoFilterBuilder photoFilterBuilder)
{
_photoFilterBuilder = photoFilterBuilder;
}

public IPhotoFilterBuilder Compile(string queryParameter)
{
string[] parts = queryParameter.Split(' ');
if (parts.Length == 3 && parts[0]?.Trim().ToLower() == "name" && parts[1]?.Trim().ToLower() == "endswith")
{
return _photoFilterBuilder.ApplyFilter(new EndsWithFilter<Photo>(p => p.Name, parts[2]));
}
return _photoFilterBuilder;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Spg.AloMalo.Application.Services.PhotoUseCases.Query;
using Spg.AloMalo.DomainModel.Filters;
using Spg.AloMalo.DomainModel.Interfaces.Repositories;
using Spg.AloMalo.DomainModel.Model;

namespace Spg.AloMalo.Application.Services.PhotoUseCases.Filters
{
public class NameEqualsParameter : IQueryParameter
{
private readonly IPhotoFilterBuilder _photoFilterBuilder;

public NameEqualsParameter(IPhotoFilterBuilder photoFilterBuilder)
{
_photoFilterBuilder = photoFilterBuilder;
}

public IPhotoFilterBuilder Compile(string queryParameter)
{
string[] parts = queryParameter.Split(' ');
if (parts.Length == 3 && parts[0]?.Trim().ToLower() == "name" && parts[1]?.Trim().ToLower() == "equals")
{
return _photoFilterBuilder.ApplyFilter(new EqualsFilter<Photo, string>(p => p.Name, parts[2]));
}
return _photoFilterBuilder;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Spg.AloMalo.Application.Services.PhotoUseCases.Query;
using Spg.AloMalo.DomainModel.Filters;
using Spg.AloMalo.DomainModel.Interfaces.Repositories;
using Spg.AloMalo.DomainModel.Model;

namespace Spg.AloMalo.Application.Services.PhotoUseCases.Filters
{
public class NameRegexParameter : IQueryParameter
{
private readonly IPhotoFilterBuilder _photoFilterBuilder;

public NameRegexParameter(IPhotoFilterBuilder photoFilterBuilder)
{
_photoFilterBuilder = photoFilterBuilder;
}

public IPhotoFilterBuilder Compile(string queryParameter)
{
string[] parts = queryParameter.Split(' ');
if (parts.Length == 3 && parts[0]?.Trim().ToLower() == "name" && parts[1]?.Trim().ToLower() == "regex")
{
return _photoFilterBuilder.ApplyFilter(new RegexFilter<Photo>(p => p.Name, parts[2]));
}
return _photoFilterBuilder;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Spg.AloMalo.Application.Services.PhotoUseCases.Query;
using Spg.AloMalo.DomainModel.Filters;
using Spg.AloMalo.DomainModel.Interfaces.Repositories;
using Spg.AloMalo.DomainModel.Model;

namespace Spg.AloMalo.Application.Services.PhotoUseCases.Filters
{
public class NameStartsWithParameter : IQueryParameter
{
private readonly IPhotoFilterBuilder _photoFilterBuilder;

public NameStartsWithParameter(IPhotoFilterBuilder photoFilterBuilder)
{
_photoFilterBuilder = photoFilterBuilder;
}

public IPhotoFilterBuilder Compile(string queryParameter)
{
string[] parts = queryParameter.Split(' ');
if (parts.Length == 3 && parts[0]?.Trim().ToLower() == "name" && parts[1]?.Trim().ToLower() == "startswith")
{
return _photoFilterBuilder.ApplyFilter(new StartsWithFilter<Photo>(p => p.Name, parts[2]));
}
return _photoFilterBuilder;
}
}
}
Loading
Loading