Skip to content
Merged
1 change: 1 addition & 0 deletions AskFm/AskFm.API/AskFm.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="DotNetEnv" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
Expand Down
9 changes: 9 additions & 0 deletions AskFm/AskFm.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using AskFm.DAL;
using AskFm.DAL.Models;
using AskFm.DAL.Interfaces;
using AskFm.DAL.Repositories;
using DotNetEnv;
using Microsoft.EntityFrameworkCore.Proxies;
namespace AskFm.API;


public class Program
{
public static void Main(string[] args)
Expand All @@ -24,10 +27,16 @@ public static void Main(string[] args)
{
throw new Exception("Connection string is null");
}

builder.Services.AddDbContext<AppDbContext>(options =>
options
.UseLazyLoadingProxies()
.UseSqlServer(ConnectionString));

builder.Services.AddIdentity<ApplicationUser, IdentityRole<int>>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();


builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();

Expand Down
61 changes: 59 additions & 2 deletions AskFm/AskFm.DAL/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System.Data;
using AskFm.DAL.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Thread = AskFm.DAL.Models.Thread;

namespace AskFm.DAL;

public class AppDbContext : DbContext
public class AppDbContext : IdentityDbContext<ApplicationUser,IdentityRole<int>, int>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}

public DbSet<User> Users { get; set; }
public DbSet<ApplicationUser> Users { get; set; }

Check warning on line 16 in AskFm/AskFm.DAL/AppDbContext.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'AppDbContext.Users' hides inherited member 'IdentityUserContext<ApplicationUser, int, IdentityUserClaim<int>, IdentityUserLogin<int>, IdentityUserToken<int>>.Users'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
public DbSet<Thread> Threads { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Follow> Follows { get; set; }
Expand All @@ -23,5 +26,59 @@
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);

foreach (var entityType in modelBuilder.Model.GetEntityTypes()
.Where(t => typeof(ITrackable).IsAssignableFrom(t.ClrType)))
{

modelBuilder.Entity(entityType.ClrType).Property<DateTime>("DeletedAt")
.HasColumnType("DATETIME");

modelBuilder.Entity(entityType.ClrType).Property<DateTime>("UpdatedAt")
.HasColumnType("DATETIME");

modelBuilder.Entity(entityType.ClrType).Property<DateTime>("CreatedAt")
.HasColumnType("DATETIME");

modelBuilder.Entity(entityType.ClrType).Property<bool>("IsDeleted")
.HasColumnType("BIT")
.HasDefaultValue(false);
}

}

public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
_fillTrackableInfromation();
return base.SaveChangesAsync(cancellationToken);

}
public override int SaveChanges()
{
_fillTrackableInfromation();
return base.SaveChanges();
}
private void _fillTrackableInfromation()
{
foreach (var entry in ChangeTracker.Entries<ITrackable>())
{
switch (entry.State)
{
case EntityState.Added:
entry.Entity.CreatedAt = DateTime.UtcNow;
entry.Entity.DeletedAt = DateTime.UtcNow;;
entry.Entity.UpdatedAt = DateTime.UtcNow;
entry.Entity.IsDeleted = false;
break;
case EntityState.Modified:
entry.Entity.UpdatedAt = DateTime.UtcNow;
break;
case EntityState.Deleted:
entry.Entity.IsDeleted = true;
entry.Entity.DeletedAt = DateTime.UtcNow;
entry.State = EntityState.Modified;
break;
}
}
}
}
1 change: 1 addition & 0 deletions AskFm/AskFm.DAL/AskFm.DAL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion AskFm/AskFm.DAL/Interfaces/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace AskFm.DAL.Interfaces;

public interface IUnitOfWork : IDisposable
{
IRepository<User> Users { get; }
IRepository<ApplicationUser> Users { get; }
IRepository<Thread> Threads { get; }
IRepository<SavedThreads> SavedThreads { get; }
IRepository<ThreadLike> ThreadLikes { get; }
Expand Down
4 changes: 2 additions & 2 deletions AskFm/AskFm.DAL/Interfaces/IUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace AskFm.DAL.Interfaces;

public interface IUserRepository : IRepository<User>
public interface IApplicationUserRepository : IRepository<ApplicationUser>
{
Task<User?> GetByUsernameAsync(string username);
Task<ApplicationUser?> GetByUsernameAsync(string username);

}
Loading
Loading