Summary

Class:ProjectX.Ef.MainDbContext
Assembly:ProjectX.Ef
File(s):C:\private\projects\projectx\backend\src\ProjectX.Ef\MainDbContext.cs
Covered lines:44
Uncovered lines:5
Coverable lines:49
Total lines:110
Line coverage:89.7%
Branch coverage:37.5%

History

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)10100100
OnModelCreating(...)10100100
SaveChanges()10100100
AddId()5287.566.67
AddAudit()8877.7842.86

File(s)

C:\private\projects\projectx\backend\src\ProjectX.Ef\MainDbContext.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Microsoft.AspNetCore.Http;
 6using Microsoft.EntityFrameworkCore;
 7using Microsoft.EntityFrameworkCore.ChangeTracking;
 8using ProjectX.Ef.Maps;
 9using ProjectX.Entities;
 10using ServiceBase;
 11
 12namespace ProjectX.Ef
 13{
 14    // G2K
 15    // https://blogs.msdn.microsoft.com/dotnet/2016/09/29/implementing-seeding-custom-conventions-and-interceptors-in-ef
 16
 17    public class MainDbContext : DbContext
 18    {
 19        private readonly IDateTimeAccessor _dateTimeAccessor;
 20        private readonly AppContext _appContext;
 21
 22        public MainDbContext(
 23            DbContextOptions<MainDbContext> options,
 24            IDateTimeAccessor dateTimeAccessor,
 25            AppContext appContext)
 4626            : base(options)
 27        {
 4628            this._dateTimeAccessor = dateTimeAccessor;
 4629            this._appContext = appContext;
 4630        }
 31
 18132        public DbSet<User> Users { get; set; }
 4633        public DbSet<UserImage> UserImages { get; set; }
 9634        public DbSet<Event> Events { get; set; }
 15335        public DbSet<EventCategory> EventCategories { get; set; }
 4636        public DbSet<Participation> Participations { get; set; }
 37
 38        protected override void OnModelCreating(ModelBuilder modelBuilder)
 39        {
 340            modelBuilder.MapUserEntity();
 341            modelBuilder.MapUserImageEntity();
 342            modelBuilder.MapEventEntity();
 343            modelBuilder.MapEventCategoryEntity();
 344            modelBuilder.MapParticipationEntity();
 45
 346            base.OnModelCreating(modelBuilder);
 347        }
 48
 49        public override int SaveChanges()
 50        {
 6351            this.AddId();
 6352            this.AddAudit();
 6353            return base.SaveChanges();
 54        }
 55
 56        private void AddId()
 57        {
 6358            var entities = ChangeTracker.Entries()
 71459                .Where(x => x.Entity is IEntityWithId && (
 71460                    x.State == EntityState.Added)
 6361                );
 62
 63063            foreach (var entity in entities)
 64            {
 25265                var idEntity = (IEntityWithId)entity.Entity;
 66
 25267                 if (idEntity.Id.Equals(Guid.Empty))
 68                {
 069                    idEntity.Id = Guid.NewGuid();
 70                }
 71            }
 6372        }
 73
 74        // TODO: handle deleted events
 75        private void AddAudit()
 76        {
 6377            var entities = ChangeTracker.Entries()
 71478                .Where(x => x.Entity is IEntityWithAudit && (
 71479                    x.State == EntityState.Added ||
 71480                    x.State == EntityState.Modified ||
 71481                    x.State == EntityState.Deleted)
 6382                );
 83
 84            // Get curret user here
 6385             var user = _appContext?.User;
 6386            var now = _dateTimeAccessor.UtcNow;
 87
 79888            foreach (var entity in entities)
 89            {
 33690                var auditEntity = (IEntityWithAudit)entity.Entity;
 91
 33692                 if (entity.State == EntityState.Added)
 93                {
 33694                    auditEntity.CreatedAt = now;
 33695                    auditEntity.CreatedBy = user;
 33696                }
 097                 else if (entity.State == EntityState.Deleted)
 98                {
 099                    auditEntity.DeletedAt = now;
 0100                    auditEntity.DeletedBy = user;
 0101                    entity.State = EntityState.Modified;
 102                }
 103
 336104                auditEntity.UpdatedAt = now;
 336105                auditEntity.UpdatedBy = user;
 106            }
 63107        }
 108    }
 109}
 110