model and Dto
This commit is contained in:
124
Infrastructure/Persistence/GenericRepository.cs
Normal file
124
Infrastructure/Persistence/GenericRepository.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using Hushian.Application.Contracts.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Security.Principal;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hushian.Persistence.Repositories
|
||||
{
|
||||
public class GenericRepository<T> : IGenericRepository<T> where T : class
|
||||
{
|
||||
private readonly HushianDbContext _context;
|
||||
private readonly DbSet<T> _query;
|
||||
|
||||
public GenericRepository(HushianDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
_query = _context.Set<T>();
|
||||
}
|
||||
public async Task<T?> ADD(T entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _context.AddAsync(entity);
|
||||
if ((await _context.SaveChangesAsync() == 1))
|
||||
return entity;
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public async Task<bool> ADDBool(T entity)
|
||||
{
|
||||
await _context.AddAsync(entity);
|
||||
return (await _context.SaveChangesAsync() == 1);
|
||||
}
|
||||
public async Task<ICollection<T>> ADD(ICollection<T> entities)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _context.AddRangeAsync(entities);
|
||||
if ((await _context.SaveChangesAsync() == entities.Count()))
|
||||
return entities;
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public async Task<T?> UPDATE(T entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Entry(entity).State = EntityState.Modified;
|
||||
if ((await _context.SaveChangesAsync() == 1))
|
||||
return entity;
|
||||
return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public async Task<bool> UPDATEBool(T entity)
|
||||
{
|
||||
_context.Entry(entity).State = EntityState.Modified;
|
||||
return (await _context.SaveChangesAsync() == 1);
|
||||
}
|
||||
public async Task<ICollection<T>> UPDATE(ICollection<T> entities)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
_context.Entry(entity).State = EntityState.Modified;
|
||||
|
||||
if ((await _context.SaveChangesAsync()) == entities.Count())
|
||||
return entities;
|
||||
return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public async Task<bool> DELETE(T entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Set<T>().Remove(entity);
|
||||
return (await _context.SaveChangesAsync() == 1);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public async Task<bool> DELETE(ICollection<T> entities)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Set<T>().RemoveRange(entities);
|
||||
return (await _context.SaveChangesAsync() == entities.Count());
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public IQueryable<T> Get()
|
||||
{
|
||||
//No track
|
||||
return _query.AsNoTracking().AsQueryable();
|
||||
}
|
||||
}
|
||||
}
|
21
Infrastructure/Persistence/Hushian.Persistence.csproj
Normal file
21
Infrastructure/Persistence/Hushian.Persistence.csproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Hushian.Application\Hushian.Application.csproj" />
|
||||
<ProjectReference Include="..\..\Hushian.Domain\Hushian.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
36
Infrastructure/Persistence/HushianDbContext.cs
Normal file
36
Infrastructure/Persistence/HushianDbContext.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Hushian.Domain.Entites;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
||||
namespace Hushian.Persistence
|
||||
{
|
||||
public class HushianDbContext : DbContext
|
||||
{
|
||||
public HushianDbContext(DbContextOptions<HushianDbContext> options)
|
||||
:base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#region Table
|
||||
public DbSet<Company> Companies { get; set; }
|
||||
public DbSet<Conversation> Conversations { get; set; }
|
||||
public DbSet<ConversationItem> ConversationItems { get; set; }
|
||||
public DbSet<Group> Groups { get; set; }
|
||||
public DbSet<Exper> Expers { get; set; }
|
||||
public DbSet<ExperGroup> EG { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
#endregion
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<ExperGroup>().HasKey(c => new { c.ExperID, c.GroupID });
|
||||
|
||||
modelBuilder
|
||||
.ApplyConfigurationsFromAssembly(typeof(HushianDbContext).Assembly);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
|
||||
|
||||
using Hushian.Application.Contracts.Persistence;
|
||||
using Hushian.Persistence.Repositories;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Reflection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Hushian.Persistence
|
||||
{
|
||||
public static class PersistenceServicesRegistration
|
||||
{
|
||||
public static IServiceCollection ConfigurePersistenceServices(this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
services.AddDbContext<HushianDbContext>(options =>
|
||||
{
|
||||
options.UseSqlServer(configuration
|
||||
.GetConnectionString("MainConnectionString"));
|
||||
});
|
||||
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user