Add project files.
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
namespace MyOffice.Data.Repositories;
|
||||
|
||||
using DbContext;
|
||||
|
||||
public class AppRepository<TEntity> : RepositoryBase<TEntity> where TEntity : class
|
||||
{
|
||||
public AppRepository() : base(AppDbContextFactory.Instance.CreateDbContext())
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="AppDbContext.cs" />
|
||||
<Compile Remove="AppDbContextFactory.cs" />
|
||||
<Compile Remove="RepositoryInitializer.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MyOffice.Data.Models\MyOffice.Data.Models.csproj" />
|
||||
<ProjectReference Include="..\MyOffice.DbContext\MyOffice.DbContext.csproj" />
|
||||
<ProjectReference Include="..\MyOffice.Migration.Sqlite\MyOffice.Migrations.Sqlite.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,78 @@
|
||||
namespace MyOffice.Data.Repositories;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DbContext;
|
||||
|
||||
public class RepositoryBase<TEntity> where TEntity : class
|
||||
{
|
||||
protected readonly AppDbContext _context;
|
||||
|
||||
protected RepositoryBase(
|
||||
AppDbContext context
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
protected async Task<int> SaveChangesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException updateException)
|
||||
{
|
||||
throw updateException;
|
||||
}
|
||||
}
|
||||
|
||||
protected int SaveChanges()
|
||||
{
|
||||
try
|
||||
{
|
||||
return _context.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException updateException)
|
||||
{
|
||||
throw updateException;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected async Task<TEntity?> GetAsync(Guid id)
|
||||
{
|
||||
return await _context.Set<TEntity>().FindAsync(id);
|
||||
}
|
||||
|
||||
protected async Task<int> AddAsync(TEntity entity)
|
||||
{
|
||||
await _context.Set<TEntity>().AddAsync(entity);
|
||||
return await SaveChangesAsync();
|
||||
}
|
||||
|
||||
protected int Add(TEntity entity)
|
||||
{
|
||||
_context.Set<TEntity>().Add(entity);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
protected int Remove(TEntity entity)
|
||||
{
|
||||
_context.Set<TEntity>().Remove(entity);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
protected async Task<int> UpdateAsync(TEntity entity)
|
||||
{
|
||||
_context.Attach(entity);
|
||||
_context.Entry(entity).State = EntityState.Modified;
|
||||
|
||||
return await SaveChangesAsync();
|
||||
}
|
||||
|
||||
protected int Update(TEntity entity)
|
||||
{
|
||||
_context.Attach(entity);
|
||||
return SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace MyOffice.Data.Repositories.Users;
|
||||
|
||||
using Models.Users;
|
||||
|
||||
public interface IUserExternalRepository
|
||||
{
|
||||
Task<int> AddUserExternalAsync(UserExternal external);
|
||||
int AddUserExternal(UserExternal external);
|
||||
Task<int> AddUserExternalsAsync(IEnumerable<UserExternal> claims);
|
||||
|
||||
Task<List<UserExternal>> GetUserExternalsByUserIdAsync(Guid userId);
|
||||
|
||||
Task<UserExternal?> GetByUserIdAsync(Guid userId, string provider);
|
||||
UserExternal? GetByUserId(Guid userId, string provider);
|
||||
|
||||
UserExternal? GetByExternalId(string externalId, string provider);
|
||||
Task<UserExternal?> GetByExternalIdAsync(string externalId, string provider);
|
||||
|
||||
void RemoveUserExternal(UserExternal userExternal);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace MyOffice.Data.Repositories.Users;
|
||||
|
||||
using MyOffice.Data.Models.Users;
|
||||
|
||||
public interface IUserRepository
|
||||
{
|
||||
Task<User?> GetUserAsync(Guid id);
|
||||
|
||||
Task<User?> GetByUserUserNameAsync(string userName);
|
||||
User? GetByUserUserName(string userName);
|
||||
|
||||
Task<int> AddUserAsync(User user);
|
||||
Task<int> UpdateUserAsync(User user);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
namespace MyOffice.Data.Repositories.Users;
|
||||
|
||||
using MyOffice.Data.Models.Users;
|
||||
using Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
public class UserExternalRepository : AppRepository<UserExternal>, IUserExternalRepository
|
||||
{
|
||||
public async Task<int> AddUserExternalAsync(UserExternal userExternal)
|
||||
{
|
||||
return await AddAsync(userExternal);
|
||||
}
|
||||
|
||||
public int AddUserExternal(UserExternal userExternal)
|
||||
{
|
||||
return Add(userExternal);
|
||||
}
|
||||
|
||||
public async Task<int> AddUserExternalsAsync(IEnumerable<UserExternal> claims)
|
||||
{
|
||||
_context.AddRange(claims);
|
||||
return await SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<UserExternal>> GetUserExternalsByUserNameAsync(string userName)
|
||||
{
|
||||
return await _context
|
||||
.UserClaims
|
||||
.Where(x => x.User.UserName == userName)
|
||||
.Include(x => x.User)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<UserExternal>> GetUserExternalsByUserIdAsync(Guid userId)
|
||||
{
|
||||
return await _context
|
||||
.UserClaims
|
||||
.Where(x => x.UserId == userId)
|
||||
.Include(x => x.User)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public List<UserExternal> GetUserExternalsByUser(Guid userId)
|
||||
{
|
||||
return _context
|
||||
.UserClaims
|
||||
.Where(x => x.UserId == userId)
|
||||
.Include(x => x.User)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<UserExternal> GetUserExternalsByUserName(string userName)
|
||||
{
|
||||
return _context
|
||||
.UserClaims
|
||||
.Where(x => x.User.UserName == userName)
|
||||
.Include(x => x.User)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<UserExternal?> GetByUserIdAsync(Guid userId, string provider)
|
||||
{
|
||||
return await _context
|
||||
.UserClaims
|
||||
.Include(x => x.User)
|
||||
.FirstOrDefaultAsync(x => x.UserId == userId && x.Provider == provider);
|
||||
}
|
||||
|
||||
public UserExternal? GetByUserId(Guid userId, string provider)
|
||||
{
|
||||
return _context
|
||||
.UserClaims
|
||||
.Include(x => x.User)
|
||||
.FirstOrDefault(x => x.UserId == userId && x.Provider == provider);
|
||||
}
|
||||
|
||||
public UserExternal? GetByExternalId(string externalId, string provider)
|
||||
{
|
||||
return _context
|
||||
.UserClaims
|
||||
.Include(x => x.User)
|
||||
.FirstOrDefault(x => x.ExternalId == externalId && x.Provider == provider);
|
||||
}
|
||||
|
||||
public Task<UserExternal?> GetByExternalIdAsync(string externalId, string provider)
|
||||
{
|
||||
return _context
|
||||
.UserClaims
|
||||
.Include(x => x.User)
|
||||
.FirstOrDefaultAsync(x => x.ExternalId == externalId && x.Provider == provider);
|
||||
}
|
||||
|
||||
public void RemoveUserExternal(UserExternal userExternal)
|
||||
{
|
||||
Remove(userExternal);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace MyOffice.Data.Repositories.Users;
|
||||
|
||||
using MyOffice.Data.Models.Users;
|
||||
using Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
public class UserRepository : AppRepository<User>, IUserRepository
|
||||
{
|
||||
public async Task<User?> GetUserAsync(Guid id)
|
||||
{
|
||||
return await GetAsync(id);
|
||||
}
|
||||
|
||||
public async Task<User?> GetByUserUserNameAsync(string userName)
|
||||
{
|
||||
return await _context.Users.FirstOrDefaultAsync(x => x.UserName == userName);
|
||||
}
|
||||
|
||||
public User? GetByUserUserName(string userName)
|
||||
{
|
||||
return _context.Users.FirstOrDefault(x => x.UserName == userName);
|
||||
}
|
||||
|
||||
public async Task<int> AddUserAsync(User user)
|
||||
{
|
||||
return await AddAsync(user);
|
||||
}
|
||||
|
||||
public async Task<int> UpdateUserAsync(User user)
|
||||
{
|
||||
return await UpdateAsync(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user