....
This commit is contained in:
67
Back/Data/Infrastructure/Persistence/SqlDbContext.cs
Normal file
67
Back/Data/Infrastructure/Persistence/SqlDbContext.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Back.Data.Models;
|
||||
|
||||
namespace TaxPayer.Infrastructure.Persistence
|
||||
{
|
||||
public class SqlDbContext : DbContext
|
||||
{
|
||||
public SqlDbContext(DbContextOptions<SqlDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
#region Table
|
||||
public DbSet<BillType> BillTypes { get; set; }
|
||||
public DbSet<Fild> Filds { get; set; }
|
||||
public DbSet<FildMode> FildModes { get; set; }
|
||||
public DbSet<FildModeInPattern> FildModeInPattern { get; set; }
|
||||
public DbSet<Pattern> Patterns { get; set; }
|
||||
public DbSet<Coding> Codings { get; set; }
|
||||
public DbSet<SpecialCondition> SpecialConditions { get; set; }
|
||||
public DbSet<Company> Companies { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<CalculationType> CalculationTypes { get; set; }
|
||||
public DbSet<Permission> Permissions { get; set; }
|
||||
public DbSet<PermissionPeriod> PermissionPeriods { get; set; }
|
||||
public DbSet<RolUser> RolUsers { get; set; }
|
||||
public DbSet<Customer> Customers { get; set; }
|
||||
public DbSet<CODUnit> CODUnits { get; set; }
|
||||
public DbSet<CODItem> CODItems { get; set; }
|
||||
public DbSet<Invoice> Invoices { get; set; }
|
||||
public DbSet<InvoiceItem> InvoiceItems { get; set; }
|
||||
public DbSet<InvoicePayment> InvoicePayments { get; set; }
|
||||
public DbSet<PermissionUser> PermissionUsers { get; set; }
|
||||
public DbSet<InvoiceStatusChang> InvoiceStatusChangs { get; set; }
|
||||
public DbSet<VerificationCode> VerificationCodes { get; set; }
|
||||
public DbSet<Pricing> Pricing { get; set; }
|
||||
public DbSet<Order> Orders { get; set; }
|
||||
public DbSet<OrderItem> OrderItems { get; set; }
|
||||
public DbSet<Notification> Notifications { get; set; }
|
||||
public DbSet<Question> Questions { get; set; }
|
||||
public DbSet<QuestionCategory> QuestionCategories { get; set; }
|
||||
public DbSet<Blog> Blogs { get; set; }
|
||||
public DbSet<Ticket> Tickets { get; set; }
|
||||
public DbSet<SubTicket> SubTickets { get; set; }
|
||||
public DbSet<OrderDiscountCode> OrderDiscountCodes { get; set; }
|
||||
public DbSet<TiceketUnknownPeople> TiceketUnknownPeoples { get; set; }
|
||||
public DbSet<SaleLead> SaleLeads { get; set; }
|
||||
#endregion
|
||||
//public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||
//{
|
||||
// foreach (var entry in ChangeTracker.Entries<EntityBase>())
|
||||
// {
|
||||
// switch (entry.State)
|
||||
// {
|
||||
// case EntityState.Added:
|
||||
// entry.Entity.CreateDate = "";
|
||||
// entry.Entity.CreatedBy = "mohammad";
|
||||
// break;
|
||||
// case EntityState.Modified:
|
||||
// entry.Entity.ModifiedDate = "";
|
||||
// entry.Entity.LastModifiedBy = "mohammad";
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return base.SaveChangesAsync(cancellationToken);
|
||||
//}
|
||||
}
|
||||
}
|
235
Back/Data/Infrastructure/Repository/RepositoryBase.cs
Normal file
235
Back/Data/Infrastructure/Repository/RepositoryBase.cs
Normal file
@@ -0,0 +1,235 @@
|
||||
using Back.Data.Contracts;
|
||||
using Back.Data.Contracts;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TaxPayer.Infrastructure.Persistence;
|
||||
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
||||
|
||||
namespace Back.Data.Infrastructure.Repository
|
||||
{
|
||||
public class RepositoryBase<T> : IAsyncRepository<T> where T : class
|
||||
{
|
||||
protected readonly SqlDbContext _dbContext;
|
||||
private DbSet<T> _query;
|
||||
public RepositoryBase(SqlDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_query = _dbContext.Set<T>();
|
||||
}
|
||||
public IQueryable<T> GetAll()
|
||||
{
|
||||
return _query.AsQueryable();
|
||||
}
|
||||
public IQueryable<T> Get(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
var query = _query.AsQueryable();
|
||||
query = query.AsNoTracking();
|
||||
return query.Where(predicate).AsQueryable();
|
||||
}
|
||||
|
||||
public IQueryable<T> Get(
|
||||
Expression<Func<T, bool>> predicate = null,
|
||||
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
|
||||
string[] includeStrings = null,
|
||||
bool disableTracking = true)
|
||||
{
|
||||
var query = _query.AsQueryable();
|
||||
|
||||
if (disableTracking) query = query.AsNoTracking();
|
||||
if (includeStrings != null)
|
||||
{
|
||||
foreach (var includeString in includeStrings)
|
||||
if (!string.IsNullOrWhiteSpace(includeString)) query = query.Include(includeString);
|
||||
}
|
||||
|
||||
|
||||
if (predicate != null) query = query.Where(predicate);
|
||||
|
||||
if (orderBy != null)
|
||||
return orderBy(query).AsQueryable();
|
||||
|
||||
return query.AsQueryable();
|
||||
}
|
||||
|
||||
public IQueryable<T> Get(
|
||||
Expression<Func<T, bool>> predicate = null,
|
||||
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
|
||||
List<Expression<Func<T, object>>> includes = null,
|
||||
bool disableTracking = true)
|
||||
{
|
||||
var query = _query.AsQueryable();
|
||||
|
||||
if (disableTracking) query = query.AsNoTracking();
|
||||
|
||||
if (includes != null) query = includes.Aggregate(query, (current, include) => current.Include(include));
|
||||
|
||||
if (predicate != null) query = query.Where(predicate);
|
||||
|
||||
if (orderBy != null)
|
||||
return orderBy(query).AsQueryable();
|
||||
|
||||
return query.AsQueryable();
|
||||
}
|
||||
|
||||
public async Task<T> AddAsync(T entity)
|
||||
{
|
||||
await _query.AddAsync(entity);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
public T? Add(T entity)
|
||||
{
|
||||
_query.Add(entity);
|
||||
var res = _dbContext.SaveChanges();
|
||||
return res > 0 ? entity : null;
|
||||
//await _dbContext.SaveChangesAsync();
|
||||
//return entity;
|
||||
}
|
||||
public async Task<bool> AddRangeAsync(ICollection<T> entites)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _query.AddRangeAsync(entites);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public async Task<bool> UpdateAsync(T entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
_dbContext.Entry(entity).State = EntityState.Modified;
|
||||
var result = await _dbContext.SaveChangesAsync();
|
||||
return result > 0;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
public bool Update(T entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Update(entity);
|
||||
var result = _dbContext.SaveChanges();
|
||||
return result > 0;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
public async Task<bool> DeleteAsync(T entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
_query.Remove(entity);
|
||||
var res = await _dbContext.SaveChangesAsync();
|
||||
return res > 0;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
public async Task<bool> DeleteRangeAsync(ICollection<T> entites)
|
||||
{
|
||||
try
|
||||
{
|
||||
_query.RemoveRange(entites);
|
||||
var res = await _dbContext.SaveChangesAsync();
|
||||
return res > 0;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
public async Task<List<SqlParameter>> RunSP(string query, List<SqlParameter> parameters)
|
||||
{
|
||||
await _dbContext.Database.ExecuteSqlRawAsync(query, parameters);
|
||||
return parameters;
|
||||
}
|
||||
public async Task ExecuteSql(string query)
|
||||
{
|
||||
await _dbContext.Database.ExecuteSqlRawAsync(query);
|
||||
}
|
||||
public bool Delete(T entity)
|
||||
{
|
||||
_query.Remove(entity);
|
||||
var res = _dbContext.SaveChanges();
|
||||
return res > 0;
|
||||
}
|
||||
public async Task<bool> AddBoolResultAsync(T entity)
|
||||
{
|
||||
int a = 0;
|
||||
await _query.AddAsync(entity);
|
||||
try
|
||||
{
|
||||
a = await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
return a > 0;
|
||||
}
|
||||
public bool AddBoolResult(T entity)
|
||||
{
|
||||
_query.Add(entity);
|
||||
var res = _dbContext.SaveChanges();
|
||||
return res > 0;
|
||||
}
|
||||
public async Task<bool> UpdateRangeAsync(ICollection<T> entites)
|
||||
{
|
||||
try
|
||||
{
|
||||
//_dbContext.Entry(entites).State = EntityState.Modified;
|
||||
_dbContext.UpdateRange(entites);
|
||||
var result = await _dbContext.SaveChangesAsync();
|
||||
return result > 0;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public bool UpdateRange(ICollection<T> entites)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.UpdateRange(entites);
|
||||
var result = _dbContext.SaveChanges();
|
||||
return result > 0;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//Sample Inclucde List
|
||||
//new List<Expression<Func<AdmUsersPermission, object>>>
|
||||
// { x => x.System }
|
Reference in New Issue
Block a user