2021-09-10 11:28:57 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
2021-08-10 14:36:35 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-09-10 11:28:57 +05:00
|
|
|
|
using System;
|
2021-08-10 14:36:35 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
2021-09-17 16:24:01 +05:00
|
|
|
|
public class CrudServiceBase<TDto, TModel> : ICrudService<TDto>, IConverter<TDto, TModel>
|
|
|
|
|
where TDto : AsbCloudApp.Data.IId
|
2021-08-10 14:36:35 +05:00
|
|
|
|
where TModel : class, AsbCloudDb.Model.IId
|
|
|
|
|
{
|
|
|
|
|
protected readonly IAsbCloudDbContext context;
|
|
|
|
|
protected readonly DbSet<TModel> dbSet;
|
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
public List<string> Includes { get; } = new List<string>();
|
2021-09-10 11:28:57 +05:00
|
|
|
|
|
2021-08-10 14:36:35 +05:00
|
|
|
|
public CrudServiceBase(IAsbCloudDbContext context)
|
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
|
|
|
|
dbSet = context.Set<TModel>();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
public virtual async Task<PaginationContainer<TDto>> GetPageAsync(int skip = 0, int take = 32, CancellationToken token = default)
|
2021-09-10 11:28:57 +05:00
|
|
|
|
{
|
|
|
|
|
var query = GetQueryWithIncludes();
|
|
|
|
|
var count = await query
|
|
|
|
|
.CountAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
var container = new PaginationContainer<TDto>
|
2021-09-10 11:28:57 +05:00
|
|
|
|
{
|
|
|
|
|
Skip = skip,
|
|
|
|
|
Take = take,
|
|
|
|
|
Count = count,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (skip >= count)
|
|
|
|
|
return container;
|
|
|
|
|
|
2021-09-29 09:57:11 +05:00
|
|
|
|
query = query
|
|
|
|
|
.OrderBy(e => e.Id);
|
|
|
|
|
|
|
|
|
|
if (skip > 0)
|
|
|
|
|
query = query.Skip(skip);
|
|
|
|
|
|
|
|
|
|
query = query.Take(take);
|
|
|
|
|
|
2021-09-10 11:28:57 +05:00
|
|
|
|
var entities = await query
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
container.Items = entities
|
|
|
|
|
.Select(entity => Convert(entity))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return container;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
public virtual async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token = default)
|
2021-09-10 11:28:57 +05:00
|
|
|
|
{
|
|
|
|
|
var query = GetQueryWithIncludes();
|
|
|
|
|
var entities = await query
|
2021-09-29 09:57:11 +05:00
|
|
|
|
.OrderBy(e => e.Id)
|
2021-09-10 11:28:57 +05:00
|
|
|
|
.ToListAsync(token).ConfigureAwait(false);
|
2021-12-21 11:52:53 +05:00
|
|
|
|
var dto = entities.Select(Convert).ToList();
|
2021-09-10 11:28:57 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
public virtual async Task<TDto> GetAsync(int id, CancellationToken token = default)
|
2021-08-10 14:36:35 +05:00
|
|
|
|
{
|
2021-09-10 11:28:57 +05:00
|
|
|
|
var query = GetQueryWithIncludes();
|
|
|
|
|
var entity = await query
|
2021-08-11 10:16:01 +05:00
|
|
|
|
.FirstOrDefaultAsync(e => e.Id == id, token).ConfigureAwait(false);
|
2021-09-10 11:28:57 +05:00
|
|
|
|
var dto = Convert(entity);
|
2021-08-10 14:36:35 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 11:55:52 +05:00
|
|
|
|
public virtual async Task<int> InsertAsync(TDto item, CancellationToken token = default)
|
2021-08-10 14:36:35 +05:00
|
|
|
|
{
|
2021-09-10 11:28:57 +05:00
|
|
|
|
var entity = Convert(item);
|
2021-10-14 17:13:57 +05:00
|
|
|
|
entity.Id = 0;
|
2021-09-10 11:28:57 +05:00
|
|
|
|
dbSet.Add(entity);
|
2021-11-25 11:55:52 +05:00
|
|
|
|
await context.SaveChangesAsync(token);
|
|
|
|
|
return entity.Id;
|
2021-08-10 14:36:35 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
public virtual Task<int> InsertRangeAsync(IEnumerable<TDto> items, CancellationToken token = default)
|
2021-08-10 14:36:35 +05:00
|
|
|
|
{
|
2021-10-14 17:13:57 +05:00
|
|
|
|
var entities = items.Select(i => {
|
|
|
|
|
var entity = Convert(i);
|
|
|
|
|
entity.Id = 0;
|
|
|
|
|
return entity;
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-10 11:28:57 +05:00
|
|
|
|
dbSet.AddRange(entities);
|
|
|
|
|
return context.SaveChangesAsync(token);
|
2021-08-10 14:36:35 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 15:03:33 +05:00
|
|
|
|
public virtual async Task<int> UpdateAsync(int id, TDto item, CancellationToken token = default)
|
2021-08-10 14:36:35 +05:00
|
|
|
|
{
|
2021-12-07 11:34:06 +05:00
|
|
|
|
var existingEntity = await dbSet.AsNoTracking().FirstOrDefaultAsync(e => e.Id == id, token).ConfigureAwait(false);
|
2021-12-03 15:03:33 +05:00
|
|
|
|
if (existingEntity is null)
|
|
|
|
|
return 0;
|
2021-09-10 11:28:57 +05:00
|
|
|
|
var entity = Convert(item);
|
2021-12-03 15:03:33 +05:00
|
|
|
|
entity.Id = id;
|
2021-09-10 11:28:57 +05:00
|
|
|
|
dbSet.Update(entity);
|
2021-12-03 15:03:33 +05:00
|
|
|
|
return await context.SaveChangesAsync(token);
|
2021-08-10 14:36:35 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Task<int> DeleteAsync(int id, CancellationToken token = default)
|
|
|
|
|
{
|
2021-08-11 10:16:01 +05:00
|
|
|
|
var entity = dbSet.AsNoTracking()
|
|
|
|
|
.FirstOrDefault(e => e.Id == id);
|
2021-08-10 14:36:35 +05:00
|
|
|
|
if (entity == default)
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
dbSet.Remove(entity);
|
|
|
|
|
return context.SaveChangesAsync(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
|
|
|
|
|
{
|
2021-08-11 10:16:01 +05:00
|
|
|
|
var entities = dbSet.Where(e => ids.Contains(e.Id)).AsNoTracking();
|
2021-08-10 14:36:35 +05:00
|
|
|
|
if (entities == default)
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
dbSet.RemoveRange(entities);
|
|
|
|
|
return context.SaveChangesAsync(token);
|
|
|
|
|
}
|
2021-09-10 11:28:57 +05:00
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
public virtual TDto Convert(TModel src) => src.Adapt<TDto>();
|
2021-09-10 11:28:57 +05:00
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
public virtual TModel Convert(TDto src) => src.Adapt<TModel>();
|
2021-09-10 11:28:57 +05:00
|
|
|
|
|
|
|
|
|
private IQueryable<TModel> GetQueryWithIncludes()
|
|
|
|
|
{
|
|
|
|
|
IQueryable<TModel> query = dbSet;
|
2021-12-07 18:27:52 +05:00
|
|
|
|
foreach (var include in Includes)
|
2021-09-10 11:28:57 +05:00
|
|
|
|
query = query.Include(include);
|
|
|
|
|
return query;
|
|
|
|
|
}
|
2021-08-10 14:36:35 +05:00
|
|
|
|
}
|
|
|
|
|
}
|