Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Pomelo" value="https://www.myget.org/F/pomelo/api/v3/index.json" />
<add key="Custom Packages" value="custompackages" />
</packageSources>
</configuration>
1 change: 0 additions & 1 deletion TASVideos.Core/Services/SignInManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using TASVideos.Core.Extensions;
using TASVideos.Data;
using TASVideos.Data.Entity;

Expand Down
60 changes: 58 additions & 2 deletions TASVideos.Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHtt
_httpContext = httpContextAccessor;
}

public DbSet<AutoHistory> AutoHistory { get; set; } = null!;

public DbSet<RolePermission> RolePermission { get; set; } = null!;
public DbSet<WikiPage> WikiPages { get; set; } = null!;
public DbSet<WikiPageReferral> WikiReferrals { get; set; } = null!;
Expand Down Expand Up @@ -94,16 +96,65 @@ public override int SaveChanges(bool acceptAllChangesOnSuccess)
PerformTrackingUpdates();

ChangeTracker.AutoDetectChangesEnabled = false;

// remember added entries,
// before EF Core is assigning valid Ids (it does on save changes,
// when ids equal zero) and setting their state to
// Unchanged (it does on every save changes)
var addedEntities = ChangeTracker
.Entries()
.Where(e => e.State == EntityState.Added)
.ToArray();

this.EnsureAutoHistory(() => new CustomAutoHistory()
{
UserId = _httpContext?.HttpContext?.User.GetUserId() ?? -1
});
var result = base.SaveChanges(acceptAllChangesOnSuccess);

// after "SaveChanges" added enties now have gotten valid ids (if it was necessary)
// and the history for them can be ensured and be saved with another "SaveChanges"
this.EnsureAddedHistory(
() => new CustomAutoHistory()
{
UserId = _httpContext?.HttpContext?.User.GetUserId() ?? -1
}, addedEntities);
result += base.SaveChanges(acceptAllChangesOnSuccess);

ChangeTracker.AutoDetectChangesEnabled = true;

return result;
}

public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
PerformTrackingUpdates();
return base.SaveChangesAsync(cancellationToken);

// remember added entries,
// before EF Core is assigning valid Ids (it does on save changes,
// when ids equal zero) and setting their state to
// Unchanged (it does on every save changes)
var addedEntities = ChangeTracker
.Entries()
.Where(e => e.State == EntityState.Added)
.ToArray();

this.EnsureAutoHistory(() => new CustomAutoHistory()
{
UserId = _httpContext?.HttpContext?.User.GetUserId() ?? -1
});
var result = await base.SaveChangesAsync(cancellationToken);

// after "SaveChanges" added enties now have gotten valid ids (if it was necessary)
// and the history for them can be ensured and be saved with another "SaveChanges"
this.EnsureAddedHistory(
() => new CustomAutoHistory()
{
UserId = _httpContext?.HttpContext?.User.GetUserId() ?? -1
}, addedEntities);
result += await base.SaveChangesAsync(CancellationToken.None);

return result;
}

/// <summary>
Expand Down Expand Up @@ -421,6 +472,11 @@ protected override void OnModelCreating(ModelBuilder builder)
{
entity.HasIndex(e => e.FileExtension).IsUnique();
});

builder.EnableAutoHistory<CustomAutoHistory>(o =>
{
o.LimitChangedLength = false;
});
}

private void PerformTrackingUpdates()
Expand Down
8 changes: 8 additions & 0 deletions TASVideos.Data/CustomAutoHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data;

internal class CustomAutoHistory : AutoHistory
{
public int UserId { get; set; }
}
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Awards/Award.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
namespace TASVideos.Data.Entity.Awards;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Awards;

public enum AwardType
{
User = 1,
Movie
}

[ExcludeFromHistory]
public class Award
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Awards/PublicationAward.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Awards;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Awards;

[ExcludeFromHistory]
public class PublicationAward
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Awards/UserAward.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Awards;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Awards;

[ExcludeFromHistory]
public class UserAward
{
public int Id { get; set; }
Expand Down
8 changes: 7 additions & 1 deletion TASVideos.Data/Entity/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace TASVideos.Data.Entity;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity;

public interface ITrackable
{
Expand All @@ -11,10 +13,14 @@ public interface ITrackable

public class BaseEntity : ITrackable
{
[ExcludeFromHistory]
public DateTime CreateTimestamp { get; set; }
[ExcludeFromHistory]
public string? CreateUserName { get; set; }

[ExcludeFromHistory]
public DateTime LastUpdateTimestamp { get; set; }
[ExcludeFromHistory]
public string? LastUpdateUserName { get; set; }
}

Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/DeprecatedMovieFormat.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity;

[ExcludeFromHistory]
public class DeprecatedMovieFormat : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Flag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity;

[ExcludeFromHistory]
public class Flag : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Forum/Forum.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Forum;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Forum;

[ExcludeFromHistory]
public class Forum : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Forum/ForumCategory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Forum;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Forum;

[ExcludeFromHistory]
public class ForumCategory : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Forum/ForumPoll.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Forum;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Forum;

[ExcludeFromHistory]
public class ForumPoll : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Forum/ForumPollOption.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Forum;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Forum;

[ExcludeFromHistory]
public class ForumPollOption : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Forum/ForumPollOptionVote.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Forum;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Forum;

[ExcludeFromHistory]
public class ForumPollOptionVote
{
public int Id { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions TASVideos.Data/Entity/Forum/ForumPost.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;
using NpgsqlTypes;

namespace TASVideos.Data.Entity.Forum;

[ExcludeFromHistory]
public class ForumPost : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Forum/ForumTopic.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace TASVideos.Data.Entity.Forum;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Forum;

public enum ForumTopicType
{
Expand All @@ -7,6 +9,7 @@ public enum ForumTopicType
Announcement = 2
}

[ExcludeFromHistory]
public class ForumTopic : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Forum/ForumTopicWatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Forum;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Forum;

[ExcludeFromHistory]
public class ForumTopicWatch
{
public int UserId { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Game/GameGameGroup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Game;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Game;

[ExcludeFromHistory]
public class GameGameGroup
{
public int GameId { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Game/GameGenre.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Game;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Game;

[ExcludeFromHistory]
public class GameGenre
{
public int GameId { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Game/GameGroup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Game;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Game;

[ExcludeFromHistory]
public class GameGroup
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Game/GameSystem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace TASVideos.Data.Entity.Game;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Game;

/// <summary>
/// Represents the system that a game runs on, such as NES, SNES, Commodore 64, PSX, etc
/// </summary>
[ExcludeFromHistory]
public class GameSystem : BaseEntity
{
public int Id { get; set; } // Note that this is Non-auto-incrementing, we need Ids to be identical across any database
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Game/GameSystemFrameRate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Game;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Game;

[ExcludeFromHistory]
public class GameSystemFrameRate : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/Game/Genre.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity.Game;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity.Game;

[ExcludeFromHistory]
public class Genre
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/IpBan.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity;

[ExcludeFromHistory]
public class IpBan : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/MediaPosts.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace TASVideos.Data.Entity;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity;

/// <summary>
/// Data storage for an external media post (such as Irc, Discord).
/// </summary>
[ExcludeFromHistory]
public class MediaPost : BaseEntity
{
public int Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion TASVideos.Data/Entity/PrivateMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TASVideos.Data.Entity;
using Microsoft.EntityFrameworkCore;

namespace TASVideos.Data.Entity;

[ExcludeFromHistory]
public class PrivateMessage : BaseEntity
{
public int Id { get; set; }
Expand Down
4 changes: 3 additions & 1 deletion TASVideos.Data/Entity/Publication.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;
using TASVideos.Common;
using TASVideos.Data.Entity.Awards;
using TASVideos.Data.Entity.Game;
Expand Down Expand Up @@ -26,6 +27,7 @@ public interface IPublicationTokens
int? Limit { get; }
}

[ExcludeFromHistory]
public class Publication : BaseEntity, ITimeable
{
public int Id { get; set; }
Expand Down
Loading