Skip to content

Commit

Permalink
Merge pull request #15 from mixcore/develop/v2-hoang
Browse files Browse the repository at this point in the history
Update Infa
  • Loading branch information
nhathoang989 authored Aug 26, 2021
2 parents 8b24cc1 + bfaecf4 commit 8b7aae0
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 148 deletions.
Binary file added src/Mix.Example/mix-db-example.db-shm
Binary file not shown.
Empty file.
2 changes: 1 addition & 1 deletion src/Mix.Heart/Enums/MixDatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public enum MixDatabaseProvider
{
MSSQL,
SQLSERVER,
MySQL,
PostgreSQL,
SQLITE
Expand Down
10 changes: 8 additions & 2 deletions src/Mix.Heart/Exceptions/MixException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ public MixException(string message) : base(message)
public MixException(string message, Exception innerException) : base(message, innerException)
{
}

protected MixException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

public MixException(MixErrorStatus status, params object[] messages) : base()
public MixException(MixErrorStatus status, params object[] messages) : base(string.Join('\n', messages))
{
Status = status;
Value = messages;
}

public MixException(MixErrorStatus status, Exception ex) : base(ex.InnerException?.Message ?? ex.Message)
{
Status = status;
Value = ex.Data;
}

public MixErrorStatus Status { get; set; } = MixErrorStatus.ServerError;

public object Value { get; set; }
Expand Down
15 changes: 8 additions & 7 deletions src/Mix.Heart/Helpers/ReflectionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Mix.Heart.Enums;
using Mix.Heart.Exceptions;
using Mix.Heart.Extensions;
using Mix.Heart.Model;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -92,24 +93,24 @@ public static Expression<Func<T, bool>>

FieldInfo fieldInfo = type.GetField(propertyName.ToTitleCase());

if (fieldInfo == null)
if (fieldInfo != null)
{
fieldPropertyType = fieldInfo.FieldType;
fieldPropertyExpression = Expression.Field(par, fieldInfo);
}
else
{
PropertyInfo propertyInfo =
type.GetProperty(propertyName.ToTitleCase());

if (propertyInfo == null)
{
throw new Exception();
throw new MixException(MixErrorStatus.Badrequest, "Invalid Property Expression");
}

fieldPropertyType = propertyInfo.PropertyType;
fieldPropertyExpression = Expression.Property(par, propertyInfo);
}
else
{
fieldPropertyType = fieldInfo.FieldType;
fieldPropertyExpression = Expression.Field(par, fieldInfo);
}
object data2;
if (fieldPropertyType.IsGenericType &&
fieldPropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
Expand Down
2 changes: 1 addition & 1 deletion src/Mix.Heart/Infrastructure/Entity/EntityBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class EntityBase<TPrimaryKey> : IEntity<TPrimaryKey>
public TPrimaryKey Id { get; set; }
public DateTime CreatedDateTime { get; set; }
public DateTime? LastModified { get; set; }
public Guid CreatedBy { get; set; }
public string CreatedBy { get; set; }
public Guid? ModifiedBy { get; set; }
public int Priority { get; set; }
public MixContentStatus Status { get; set; }
Expand Down
122 changes: 103 additions & 19 deletions src/Mix.Heart/Infrastructure/Repository/QueryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ public class QueryRepository<TDbContext, TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
where TPrimaryKey : IComparable
{
public QueryRepository(UnitOfWorkInfo uowInfo) : base(uowInfo) { }
public QueryRepository(TDbContext dbContext) : base(dbContext) { }

public QueryRepository()
{
}

public QueryRepository(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkInfo)
{
}


#region IQueryable

Expand All @@ -38,17 +45,13 @@ public IQueryable<TEntity> GetListQuery(Expression<Func<TEntity, bool>> predicat
return GetAllQuery().Where(predicate);
}

public async Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> predicate)
{
return await GetAllQuery().SingleOrDefaultAsync(predicate);
}

public IQueryable<TEntity> GetPagingQuery(Expression<Func<TEntity, bool>> predicate,
IPagingModel paging)
{
var query = GetListQuery(predicate);
paging.Total = query.Count();
dynamic sortBy = GetLambda(paging.SortBy);

switch (paging.SortDirection)
{
case Enums.SortDirection.Asc:
Expand All @@ -58,14 +61,17 @@ public IQueryable<TEntity> GetPagingQuery(Expression<Func<TEntity, bool>> predic
query = Queryable.OrderByDescending(query, sortBy);
break;
}
query =
query.Skip(paging.PageIndex * paging.PageSize).Take(paging.PageSize);

if (paging.PageSize.HasValue)
{
query = query.Skip(paging.PageIndex * paging.PageSize.Value).Take(paging.PageSize.Value);
}

return query;
}

#endregion

#region Entity Async
public virtual bool CheckIsExists(TEntity entity)
{
return GetAllQuery().Any(e => e.Id.Equals(entity.Id));
Expand All @@ -76,6 +82,13 @@ public virtual bool CheckIsExists(Func<TEntity, bool> predicate)
return GetAllQuery().Any(predicate);
}

#region Entity Async

public async Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> predicate)
{
return await GetAllQuery().SingleOrDefaultAsync(predicate);
}

public virtual async Task<TEntity> GetByIdAsync(TPrimaryKey id)
{
return await Context.Set<TEntity>().FindAsync(id);
Expand All @@ -93,6 +106,62 @@ public Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> predicate)

#endregion

#region Entity Sync

public TEntity GetSingle(Expression<Func<TEntity, bool>> predicate)
{
try
{
return GetAllQuery().SingleOrDefault(predicate);
}
catch (Exception ex)
{
HandleException(ex);
return default;
}
}

public virtual TEntity GetById(TPrimaryKey id)
{
try
{
return Context.Set<TEntity>().Find(id);
}
catch (Exception ex)
{
HandleException(ex);
return default;
}
}

public virtual int Max(Func<TEntity, int> predicate)
{
try
{
return GetAllQuery().Max(predicate);
}
catch (Exception ex)
{
HandleException(ex);
return default;
}
}

public TEntity GetFirst(Expression<Func<TEntity, bool>> predicate)
{
try
{
return GetAllQuery().FirstOrDefault(predicate);
}
catch (Exception ex)
{
HandleException(ex);
return default;
}
}

#endregion

#region View Async

public virtual async Task<TView> GetSingleViewAsync<TView>(TPrimaryKey id)
Expand All @@ -106,12 +175,27 @@ public virtual async Task<TView> GetSingleViewAsync<TView>(TPrimaryKey id)
throw new MixException(MixErrorStatus.NotFound, id);
}

public virtual async Task<List<TView>> GetListViewAsync<TView>(Expression<Func<TEntity, bool>> predicate, UnitOfWorkInfo uowInfo = null)
public virtual async Task<TView> GetSingleViewAsync<TView>(Expression<Func<TEntity, bool>> predicate)
where TView : ViewModelBase<TDbContext, TEntity, TPrimaryKey>
{
var entity = await GetSingleAsync(predicate);
if (entity != null)
{
var result = await BuildViewModel<TView>(entity);
return result;
}
return null;
}

public virtual async Task<List<TView>> GetListViewAsync<TView>(
Expression<Func<TEntity, bool>> predicate, UnitOfWorkInfo uowInfo = null)
where TView : ViewModelBase<TDbContext, TEntity, TPrimaryKey>
{
BeginUow(uowInfo);
var query = GetListQuery(predicate);
return await ToListViewModelAsync<TView>(query);
var result = await ToListViewModelAsync<TView>(query);
await CloseUowAsync();
return result;
}

public virtual async Task<PagingResponseModel<TView>> GetPagingViewAsync<TView>(
Expand All @@ -130,11 +214,11 @@ public virtual async Task<PagingResponseModel<TView>> GetPagingViewAsync<TView>(
#region Helper
#region Private methods

protected virtual Task<TView> BuildViewModel<TView>(TEntity entity)
protected virtual Task<TView> BuildViewModel<TView>(TEntity entity, UnitOfWorkInfo uowInfo = null)
where TView : ViewModelBase<TDbContext, TEntity, TPrimaryKey>
{
ConstructorInfo classConstructor = typeof(TView).GetConstructor(new Type[] { typeof(TEntity) });
return Task.FromResult((TView)classConstructor.Invoke(new object[] { entity }));
ConstructorInfo classConstructor = typeof(TView).GetConstructor(new Type[] { typeof(TEntity), typeof(UnitOfWorkInfo) });
return Task.FromResult((TView)classConstructor.Invoke(new object[] { entity, uowInfo }));
}

public async Task<List<TView>> ToListViewModelAsync<TView>(
Expand All @@ -150,10 +234,10 @@ public async Task<List<TView>> ToListViewModelAsync<TView>(
var entities = await source.SelectMembers(members).ToListAsync();

List<TView> data = new List<TView>();
ConstructorInfo classConstructor = typeof(TView).GetConstructor(new Type[] { typeof(TEntity) });
ConstructorInfo classConstructor = typeof(TView).GetConstructor(new Type[] { typeof(TEntity), typeof(UnitOfWorkInfo) });
foreach (var entity in entities)
{
var view = await BuildViewModel<TView>(entity);
var view = await BuildViewModel<TView>(entity, UowInfo);
data.Add(view);
}

Expand Down Expand Up @@ -190,10 +274,10 @@ protected async Task<PagingResponseModel<TView>> ToPagingViewModelAsync<TView>(
var entities = await source.SelectMembers(members).ToListAsync();

List<TView> data = new List<TView>();
ConstructorInfo classConstructor = typeof(TView).GetConstructor(new Type[] { typeof(TEntity) });
ConstructorInfo classConstructor = typeof(TView).GetConstructor(new Type[] { typeof(TEntity), typeof(UnitOfWorkInfo) });
foreach (var entity in entities)
{
var view = (TView)classConstructor.Invoke(new object[] { entity });
var view = (TView)classConstructor.Invoke(new object[] { entity, UowInfo });
data.Add(view);
}

Expand Down Expand Up @@ -234,7 +318,7 @@ private List<TView> GetListCachedData<TView>(
return result;
}

private IViewModel<TPrimaryKey> GetCachedData(
private IViewModel GetCachedData(
TEntity entity, QueryRepository<TDbContext, TEntity, TPrimaryKey> repository, string[] keys)
{
throw new NotImplementedException();
Expand Down
Loading

0 comments on commit 8b7aae0

Please sign in to comment.