initial commit from an older template
This commit is contained in:
50
Database/ApplicationContext.cs
Executable file
50
Database/ApplicationContext.cs
Executable file
@@ -0,0 +1,50 @@
|
||||
using GamificationService.Models.Database;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GamificationService.Database;
|
||||
|
||||
|
||||
public class ApplicationContext : IdentityDbContext<ApplicationUser, ApplicationRole, long>
|
||||
{
|
||||
public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public DbSet<Right> Rights { get; set; }
|
||||
public DbSet<RefreshToken> RefreshTokens { get; set; }
|
||||
public DbSet<ApplicationUser> Users { get; set; }
|
||||
public DbSet<ApplicationRole> Roles { get; set; }
|
||||
public DbSet<UserRole> UserRoles { get; set; }
|
||||
public DbSet<RoleRight> RoleRights { get; set; }
|
||||
public DbSet<UserProfile> UserProfiles { get; set; }
|
||||
public DbSet<Instruction> Instructions { get; set; }
|
||||
public DbSet<InstructionCategory> InstructionCategories { get; set; }
|
||||
public DbSet<InstructionParagraph> InstructionParagraphs { get; set; }
|
||||
public DbSet<InstructionTest> InstructionTests { get; set; }
|
||||
public DbSet<InstructionTestQuestion> InstructionTestQuestions { get; set; }
|
||||
public DbSet<InstructionTestResult> InstructionTestResults { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<UserRole>()
|
||||
.HasKey(ur => new { ur.UserId, ur.RoleId });
|
||||
|
||||
modelBuilder.Entity<RoleRight>()
|
||||
.HasKey(rr => new { rr.RoleId, rr.RightId });
|
||||
|
||||
modelBuilder.Entity<InstructionTest>()
|
||||
.HasMany(itq => itq.Questions);
|
||||
|
||||
modelBuilder.Entity<InstructionTestResult>()
|
||||
.HasOne(itr => itr.InstructionTest);
|
||||
|
||||
modelBuilder.Entity<Instruction>()
|
||||
.HasOne(i => i.Category);
|
||||
|
||||
modelBuilder.Entity<Instruction>()
|
||||
.HasMany(i => i.Paragraphs);
|
||||
}
|
||||
}
|
||||
54
Database/Extensions/ChangeTrackerExtensions.cs
Executable file
54
Database/Extensions/ChangeTrackerExtensions.cs
Executable file
@@ -0,0 +1,54 @@
|
||||
using GamificationService.Models.Database;
|
||||
using GamificationService.Services.CurrentUsers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
|
||||
namespace GamificationService.Database.Extensions;
|
||||
|
||||
public static class ChangeTrackerExtensions
|
||||
{
|
||||
public static void SetAuditProperties(this ChangeTracker changeTracker, ICurrentUserService currentUserService)
|
||||
{
|
||||
changeTracker.DetectChanges();
|
||||
IEnumerable<EntityEntry> entities =
|
||||
changeTracker
|
||||
.Entries()
|
||||
.Where(t => t.Entity is AuditableEntity &&
|
||||
(
|
||||
t.State == EntityState.Deleted
|
||||
|| t.State == EntityState.Added
|
||||
|| t.State == EntityState.Modified
|
||||
));
|
||||
|
||||
if (entities.Any())
|
||||
{
|
||||
DateTimeOffset timestamp = DateTimeOffset.UtcNow;
|
||||
|
||||
string user = currentUserService.GetCurrentUser().Login ?? "Unknown";
|
||||
|
||||
foreach (EntityEntry entry in entities)
|
||||
{
|
||||
AuditableEntity entity = (AuditableEntity)entry.Entity;
|
||||
|
||||
switch (entry.State)
|
||||
{
|
||||
case EntityState.Added:
|
||||
entity.CreatedOn = timestamp;
|
||||
entity.CreatedBy = user;
|
||||
entity.UpdatedOn = timestamp;
|
||||
entity.UpdatedBy = user;
|
||||
break;
|
||||
case EntityState.Modified:
|
||||
entity.UpdatedOn = timestamp;
|
||||
entity.UpdatedBy = user;
|
||||
break;
|
||||
case EntityState.Deleted:
|
||||
entity.UpdatedOn = timestamp;
|
||||
entity.UpdatedBy = user;
|
||||
entry.State = EntityState.Deleted;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
98
Database/Repositories/GenericRepository.cs
Executable file
98
Database/Repositories/GenericRepository.cs
Executable file
@@ -0,0 +1,98 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
|
||||
namespace GamificationService.Database.Repositories;
|
||||
|
||||
public class GenericRepository<TEntity> where TEntity : class
|
||||
{
|
||||
internal ApplicationContext context;
|
||||
internal DbSet<TEntity> dbSet;
|
||||
|
||||
public GenericRepository(ApplicationContext context)
|
||||
{
|
||||
this.context = context;
|
||||
this.dbSet = context.Set<TEntity>();
|
||||
}
|
||||
|
||||
public virtual IQueryable<TEntity> Get(
|
||||
Expression<Func<TEntity, bool>> filter = null,
|
||||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
|
||||
string includeProperties = "")
|
||||
{
|
||||
IQueryable<TEntity> query = dbSet;
|
||||
|
||||
if (filter != null)
|
||||
{
|
||||
query = query.Where(filter);
|
||||
}
|
||||
|
||||
foreach (var includeProperty in includeProperties.Split
|
||||
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
query = query.Include(includeProperty);
|
||||
}
|
||||
|
||||
if (orderBy != null)
|
||||
{
|
||||
return orderBy(query);
|
||||
}
|
||||
else
|
||||
{
|
||||
return query;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual TEntity? GetByID(object id)
|
||||
{
|
||||
return dbSet.Find(id);
|
||||
}
|
||||
|
||||
public async virtual Task<TEntity?> GetByIDAsync(object id)
|
||||
{
|
||||
return await dbSet.FindAsync(id);
|
||||
}
|
||||
public virtual void Insert(TEntity entity)
|
||||
{
|
||||
dbSet.Add(entity);
|
||||
}
|
||||
public virtual void InsertRange(IEnumerable<TEntity> entities)
|
||||
{
|
||||
dbSet.AddRange(entities);
|
||||
}
|
||||
public async virtual Task InsertAsync(TEntity entity)
|
||||
{
|
||||
await dbSet.AddAsync(entity);
|
||||
}
|
||||
public virtual void Delete(object id)
|
||||
{
|
||||
TEntity? entityToDelete = dbSet.Find(id);
|
||||
|
||||
if (entityToDelete == null)
|
||||
{
|
||||
// It's probably a good idea to throw an error here
|
||||
// but I'm leaving it as is for now
|
||||
return;
|
||||
}
|
||||
|
||||
Delete(entityToDelete);
|
||||
}
|
||||
public virtual void DeleteRange(IEnumerable<TEntity> entities)
|
||||
{
|
||||
dbSet.RemoveRange(entities);
|
||||
}
|
||||
public virtual void Delete(TEntity entityToDelete)
|
||||
{
|
||||
if (context.Entry(entityToDelete).State == EntityState.Detached)
|
||||
{
|
||||
dbSet.Attach(entityToDelete);
|
||||
}
|
||||
dbSet.Remove(entityToDelete);
|
||||
}
|
||||
|
||||
public virtual void Update(TEntity entityToUpdate)
|
||||
{
|
||||
dbSet.Attach(entityToUpdate);
|
||||
context.Entry(entityToUpdate).State = EntityState.Modified;
|
||||
}
|
||||
}
|
||||
239
Database/Repositories/UnitOfWork.cs
Executable file
239
Database/Repositories/UnitOfWork.cs
Executable file
@@ -0,0 +1,239 @@
|
||||
using GamificationService.Models.Database;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
|
||||
namespace GamificationService.Database.Repositories;
|
||||
|
||||
public class UnitOfWork : IDisposable
|
||||
{
|
||||
#region fields
|
||||
|
||||
private ApplicationContext _context;
|
||||
private GenericRepository<UserProfile> _userProfileRepository;
|
||||
private GenericRepository<ApplicationRole> _roleRepository;
|
||||
private GenericRepository<Right?> _rightRepository;
|
||||
private GenericRepository<RefreshToken> _refreshTokenRepository;
|
||||
private GenericRepository<RoleRight> _roleRightRepository;
|
||||
private GenericRepository<UserRole> _userRoleRepository;
|
||||
private GenericRepository<Instruction> _instructionRepository;
|
||||
private GenericRepository<InstructionParagraph> _instructionParagraphRepository;
|
||||
private GenericRepository<InstructionCategory> _instructionCategoryRepository;
|
||||
private GenericRepository<InstructionTest> _instructionTestRepository;
|
||||
private GenericRepository<InstructionTestQuestion> _instructionTestQuestionRepository;
|
||||
private GenericRepository<InstructionTestResult> _instructionTestResultRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
private IDbContextTransaction _transaction;
|
||||
|
||||
public UnitOfWork(ApplicationContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
|
||||
#region Properties
|
||||
|
||||
public GenericRepository<UserProfile> UserProfileRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._userProfileRepository == null)
|
||||
{
|
||||
this._userProfileRepository = new GenericRepository<UserProfile>(_context);
|
||||
}
|
||||
return _userProfileRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<ApplicationRole> RoleRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._roleRepository == null)
|
||||
{
|
||||
this._roleRepository = new GenericRepository<ApplicationRole>(_context);
|
||||
}
|
||||
return _roleRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<Right?> RightRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._rightRepository == null)
|
||||
{
|
||||
this._rightRepository = new GenericRepository<Right?>(_context);
|
||||
}
|
||||
return _rightRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<RefreshToken> RefreshTokenRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._refreshTokenRepository == null)
|
||||
{
|
||||
this._refreshTokenRepository = new GenericRepository<RefreshToken>(_context);
|
||||
}
|
||||
return _refreshTokenRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<RoleRight> RoleRightRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._roleRightRepository == null)
|
||||
{
|
||||
this._roleRightRepository = new GenericRepository<RoleRight>(_context);
|
||||
}
|
||||
return _roleRightRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<UserRole> UserRoleRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._userRoleRepository == null)
|
||||
{
|
||||
this._userRoleRepository = new GenericRepository<UserRole>(_context);
|
||||
}
|
||||
return _userRoleRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<Instruction> InstructionRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._instructionRepository == null)
|
||||
{
|
||||
this._instructionRepository = new GenericRepository<Instruction>(_context);
|
||||
}
|
||||
return _instructionRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<InstructionParagraph> InstructionParagraphRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._instructionParagraphRepository == null)
|
||||
{
|
||||
this._instructionParagraphRepository = new GenericRepository<InstructionParagraph>(_context);
|
||||
}
|
||||
return _instructionParagraphRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<InstructionCategory> InstructionCategoryRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._instructionCategoryRepository == null)
|
||||
{
|
||||
this._instructionCategoryRepository = new GenericRepository<InstructionCategory>(_context);
|
||||
}
|
||||
return _instructionCategoryRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<InstructionTest> InstructionTestRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._instructionTestRepository == null)
|
||||
{
|
||||
this._instructionTestRepository = new GenericRepository<InstructionTest>(_context);
|
||||
}
|
||||
return _instructionTestRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<InstructionTestQuestion> InstructionTestQuestionRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._instructionTestQuestionRepository == null)
|
||||
{
|
||||
this._instructionTestQuestionRepository = new GenericRepository<InstructionTestQuestion>(_context);
|
||||
}
|
||||
return _instructionTestQuestionRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<InstructionTestResult> InstructionTestResultRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._instructionTestResultRepository == null)
|
||||
{
|
||||
this._instructionTestResultRepository = new GenericRepository<InstructionTestResult>(_context);
|
||||
}
|
||||
return _instructionTestResultRepository;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
public bool Save()
|
||||
{
|
||||
return _context.SaveChanges() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> SaveAsync()
|
||||
{
|
||||
return await _context.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
|
||||
private bool disposed = false;
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!this.disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
}
|
||||
this.disposed = true;
|
||||
}
|
||||
public async Task BeginTransactionAsync()
|
||||
{
|
||||
if (_transaction is not null)
|
||||
throw new InvalidOperationException("A transaction has already been started.");
|
||||
_transaction = await _context.Database.BeginTransactionAsync();
|
||||
}
|
||||
public async Task CommitAsync()
|
||||
{
|
||||
if (_transaction is null)
|
||||
throw new InvalidOperationException("A transaction has not been started.");
|
||||
|
||||
try
|
||||
{
|
||||
await _transaction.CommitAsync();
|
||||
_transaction.Dispose();
|
||||
_transaction = null;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (_transaction is not null)
|
||||
await _transaction.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user