Initial commit

This commit is contained in:
GeNii96 committed 2026-05-27 12:47:16 +02:00
commit 63ee71a19b
123 files changed
+11268

No files matched your search

@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace OnlyPrompt.Backend.Database.Core
{
public class EntityBase : IEntity, ITrackableEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}
}
@@ -0,0 +1,7 @@
namespace OnlyPrompt.Backend.Database.Core
{
public interface IEntity
{
public Guid Id { get; }
}
}
@@ -0,0 +1,7 @@
namespace OnlyPrompt.Backend.Database.Core
{
public interface IHasSlug
{
public string Slug { get; set; }
}
}
@@ -0,0 +1,8 @@
namespace OnlyPrompt.Backend.Database.Core
{
public interface ITrackableEntity
{
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
}
@@ -0,0 +1,14 @@
using System.Collections.Frozen;
namespace OnlyPrompt.Backend.Database
{
public static class ModelConstants
{
public const int MaxSlugLength = 100;
public const string UserRole = "user";
public const string AdminRole = "admin";
public const string SysAdminRole = "sys-admin";
public static readonly FrozenSet<string> AllRoles = FrozenSet.Create(StringComparer.OrdinalIgnoreCase, UserRole, AdminRole, SysAdminRole);
}
}
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using OnlyPrompt.Backend.Database.Core;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace OnlyPrompt.Backend.Database.Models
{
[Index(nameof(Slug), IsUnique = true)]
public class CategoryModel : EntityBase, IHasSlug
{
[MaxLength(ModelConstants.MaxSlugLength)]
[Column(TypeName = "citext")]
public required string Slug { get; set; }
public required string Name { get; set; }
public string? Description { get; set; }
public virtual IList<PromptModel> Prompts { get; set; } = new List<PromptModel>();
}
}
@@ -0,0 +1,45 @@
using Microsoft.EntityFrameworkCore;
using OnlyPrompt.Backend.Database.Core;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace OnlyPrompt.Backend.Database.Models
{
[Index(nameof(Slug), IsUnique = true)]
public class PromptModel : EntityBase, IHasSlug
{
[Required]
[ForeignKey(nameof(Creator))]
public Guid CreatorId { get; set; }
[DeleteBehavior(DeleteBehavior.Cascade)]
public virtual UserModel Creator { get; set; }
[Required]
[ForeignKey(nameof(Category))]
public Guid CategoryId { get; set; }
[DeleteBehavior(DeleteBehavior.Cascade)]
public virtual CategoryModel Category { get; set; }
[MaxLength(200)]
public required string Title { get; set; }
[MaxLength(4000)]
public required string Prompt { get; set; }
[MaxLength(1000)]
public required string Description { get; set; }
[MaxLength(ModelConstants.MaxSlugLength)]
[Column(TypeName = "citext")]
public required string Slug { get; set; }
[ForeignKey(nameof(SubscriptionTier))]
public Guid? SubscriptionTierId { get; set; }
[DeleteBehavior(DeleteBehavior.SetNull)]
public virtual SubscriptionTierModel? SubscriptionTier { get; set; }
public virtual IList<ReviewModel> Reviews { get; set; } = new List<ReviewModel>();
}
}
@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;
using OnlyPrompt.Backend.Database.Core;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace OnlyPrompt.Backend.Database.Models
{
[PrimaryKey(nameof(ReviewerId), nameof(PromptId))]
public class ReviewModel : ITrackableEntity
{
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
[Required]
[ForeignKey(nameof(Reviewer))]
public Guid ReviewerId { get; set; }
[DeleteBehavior(DeleteBehavior.Cascade)]
public virtual UserModel Reviewer { get; set; }
[Required]
[ForeignKey(nameof(Prompt))]
public Guid PromptId { get; set; }
[DeleteBehavior(DeleteBehavior.Cascade)]
public virtual PromptModel Prompt { get; set; }
[Range(1, 5)]
public int Rating { get; set; }
[MaxLength(200)]
public string? Comment { get; set; } = null;
}
}
@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace OnlyPrompt.Backend.Database.Models
{
[Table("UserSubscriptions")]
[PrimaryKey(nameof(SubscriberId), nameof(SubscribedToId))]
public class SubscriptionModel
{
[Required]
[ForeignKey(nameof(SubscribedTo))]
public Guid SubscribedToId { get; set; }
[DeleteBehavior(DeleteBehavior.Cascade)]
public virtual UserModel SubscribedTo { get; set; }
[Required]
[ForeignKey(nameof(Subscriber))]
public Guid SubscriberId { get; set; }
[DeleteBehavior(DeleteBehavior.Cascade)]
public virtual UserModel Subscriber { get; set; }
[ForeignKey(nameof(SubscriptionTier))]
public virtual Guid? SubscriptionTierId { get; set; }
[DeleteBehavior(DeleteBehavior.SetNull)]
public virtual SubscriptionTierModel? SubscriptionTier { get; set; }
}
}
@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore;
using OnlyPrompt.Backend.Database.Core;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace OnlyPrompt.Backend.Database.Models
{
[Index(nameof(Level), nameof(UserId), IsUnique = true)]
public class SubscriptionTierModel : EntityBase
{
[Required]
public required string Name { get; set; }
[MaxLength(1000)]
public string? Description { get; set; }
[Required]
[ForeignKey(nameof(User))]
public Guid UserId { get; set; }
[DeleteBehavior(DeleteBehavior.Cascade)]
public virtual UserModel User { get; set; }
public decimal MonthlyPrice { get; set; }
public int Level { get; set; }
public virtual IList<PromptModel> Prompts { get; set; } = new List<PromptModel>();
public virtual IList<SubscriptionModel> Subscriptions { get; set; } = new List<SubscriptionModel>();
}
}
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using OnlyPrompt.Backend.Database.Core;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace OnlyPrompt.Backend.Database.Models
{
[Index(nameof(Email), IsUnique = true)]
[Index(nameof(UserName), IsUnique = true)]
public class UserModel : EntityBase
{
[MaxLength(100)]
[Column(TypeName = "citext")]
public required string UserName { get; set; }
[Column(TypeName = "citext")]
public required string Email { get; set; }
public required string PasswordHash { get; set; }
public required List<string> Roles { get; set; } = new List<string>();
[Required]
public required virtual UserProfileModel Profile { get; set; }
public virtual IList<PromptModel> Prompts { get; set; } = new List<PromptModel>();
public virtual IList<SubscriptionModel> Subscriptions { get; set; } = new List<SubscriptionModel>();
public virtual IList<SubscriptionModel> Subscribers { get; set; } = new List<SubscriptionModel>();
public virtual IList<SubscriptionTierModel> SubscriptionTiers { get; set; } = new List<SubscriptionTierModel>();
public bool IsLockoutEnabled { get; set; } = false;
}
}
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
using OnlyPrompt.Backend.Database.Core;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace OnlyPrompt.Backend.Database.Models
{
[Index(nameof(Slug), IsUnique = true)]
public class UserProfileModel : EntityBase, IHasSlug
{
[MaxLength(100)]
public required string DisplayName { get; set; }
[MaxLength(ModelConstants.MaxSlugLength)]
[Column(TypeName = "citext")]
public required string Slug { get; set; }
[MaxLength(2000)]
public string? Bio { get; set; }
public required string AvatarUrl { get; set; }
[MaxLength(200)]
public string? Specialities { get; set; }
public virtual UserModel User { get; set; }
public bool IsPublic { get; set; } = false;
}
}
@@ -0,0 +1,91 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using OnlyPrompt.Backend.Database.Core;
using OnlyPrompt.Backend.Database.Models;
namespace OnlyPrompt.Backend.Database
{
public class OnlyPromptContext : DbContext
{
public DbSet<UserModel> Users { get; set; }
public DbSet<UserProfileModel> UserProfiles { get; set; }
public DbSet<CategoryModel> Categories { get; set; }
public DbSet<PromptModel> Prompts { get; set; }
public DbSet<SubscriptionTierModel> SubscriptionTiers { get; set; }
public DbSet<SubscriptionModel> Subscriptions { get; set; }
public DbSet<ReviewModel> Reviews { get; set; }
public OnlyPromptContext(DbContextOptions<OnlyPromptContext> options) : base(options)
{
}
private void HandleEntityTimestamps()
{
var entries = ChangeTracker.Entries<ITrackableEntity>();
var now = DateTime.UtcNow;
foreach (var entry in entries)
{
if (entry.State == EntityState.Added)
{
entry.Entity.CreatedAt = now;
entry.Entity.UpdatedAt = now;
}
else if (entry.State == EntityState.Modified)
{
entry.Entity.UpdatedAt = now;
}
}
}
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
HandleEntityTimestamps();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
HandleEntityTimestamps();
return base.SaveChanges(acceptAllChangesOnSuccess);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
if(optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql();
optionsBuilder.UseLazyLoadingProxies();
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<UserModel>(entity =>
{
entity.HasOne(e => e.Profile)
.WithOne(p => p.User)
.HasForeignKey<UserProfileModel>(p => p.Id);
});
modelBuilder.Entity<SubscriptionModel>(entity =>
{
entity.HasOne(e => e.Subscriber)
.WithMany(s => s.Subscriptions)
.HasForeignKey(e => e.SubscriberId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(e => e.SubscribedTo)
.WithMany(c => c.Subscribers)
.HasForeignKey(e => e.SubscribedToId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(e => e.SubscriptionTier)
.WithMany(t => t.Subscriptions)
.HasForeignKey(e => e.SubscriptionTierId)
.OnDelete(DeleteBehavior.SetNull);
});
}
}
}