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,93 @@
using AutoMapper;
using OnlyPrompt.Backend.ApiModels.Auth;
using OnlyPrompt.Backend.ApiModels.Category;
using OnlyPrompt.Backend.ApiModels.Prompt;
using OnlyPrompt.Backend.ApiModels.Subscription;
using OnlyPrompt.Backend.ApiModels.UserProfile;
using OnlyPrompt.Backend.Database.Models;
namespace OnlyPrompt.Backend.Utils
{
public static class AutoMapperSetup
{
public static void Setup(IMapperConfigurationExpression config)
{
config.CreateMap<UserModel, ApiUser>()
.MapCtorParamFrom(x => x.Id, x => x.Id)
.MapCtorParamFrom(x => x.UserName, x => x.UserName)
.MapCtorParamFrom(x => x.Roles, x => x.Roles)
.MapCtorParamFrom(x => x.Email, x => x.Email);
config.CreateMap<UserProfileModel, ApiUserProfile>()
.MapCtorParamFrom(x => x.DisplayName, x => x.DisplayName)
.MapCtorParamFrom(x => x.Slug, x => x.Slug)
.MapCtorParamFrom(x => x.Bio, x => x.Bio)
.MapCtorParamFrom(x => x.AvatarUrl, x => x.AvatarUrl)
.MapCtorParamFrom(x => x.Specialities, x => x.Specialities)
.MapCtorParamFrom(x => x.AverageRating, x => x.User.Prompts.Average(p => p.Reviews.Average(r => r.Rating)))
.MapCtorParamFrom(x => x.Subscribers, x => x.User.Subscribers.Count());
config.CreateMap<PromptModel, ApiPrompt>()
.MapCtorParamFrom(x => x.Id, x => x.Id)
.MapCtorParamFrom(x => x.Title, x => x.Title)
.MapCtorParamFrom(x => x.Description, x => x.Description)
.MapCtorParamFrom(x => x.Content, x => x.Prompt)
.MapCtorParamFrom(x => x.TimeStamp, x => x.UpdatedAt)
.MapCtorParamFrom(x => x.TierLevel, x => x.SubscriptionTier == null ? (int?)null : x.SubscriptionTier.Level)
.MapCtorParamFrom(x => x.TierName, x => x.SubscriptionTier == null ? null : x.SubscriptionTier.Name)
.MapCtorParamFrom(x => x.CreatorName, x => x.Creator.Profile.DisplayName)
.MapCtorParamFrom(x => x.CreatorId, x => x.CreatorId)
.MapCtorParamFrom(x => x.AverageRating, x => x.Reviews.Average(r => (double?)r.Rating));
config.CreateMap<PromptModel, ApiMinimalPrompt>()
.MapCtorParamFrom(x => x.Id, x => x.Id)
.MapCtorParamFrom(x => x.Title, x => x.Title)
.MapCtorParamFrom(x => x.CreatorName, x => x.Creator.Profile.DisplayName)
.MapCtorParamFrom(x => x.TimeStamp, x => x.UpdatedAt)
.MapCtorParamFrom(x => x.CreatorId, x => x.CreatorId)
.MapCtorParamFrom(x => x.TierLevel, x => x.SubscriptionTier == null ? (int?)null : x.SubscriptionTier.Level)
.MapCtorParamFrom(x => x.TierName, x => x.SubscriptionTier == null ? null : x.SubscriptionTier.Name)
.MapCtorParamFrom(x => x.AverageRating, x => x.Reviews.Average(r => (double?)r.Rating))
.MapCtorParamFrom(x => x.CanAccess, x => true);
config.CreateMap<ReviewModel, ApiReview>()
.MapCtorParamFrom(x => x.CreatorId, x => x.ReviewerId)
.MapCtorParamFrom(x => x.CreatorName, x => x.Reviewer.Profile.DisplayName)
.MapCtorParamFrom(x => x.Comment, x => x.Comment)
.MapCtorParamFrom(x => x.Rating, x => x.Rating);
config.CreateMap<CategoryModel, ApiCategory>()
.MapCtorParamFrom(x => x.Id, x => x.Id)
.MapCtorParamFrom(x => x.Name, x => x.Name)
.MapCtorParamFrom(x => x.Description, x => x.Description)
.MapCtorParamFrom(x => x.Slug, x => x.Slug);
config.CreateMap<CategoryModel, ApiMinimalCategory>()
.MapCtorParamFrom(x => x.Name, x => x.Name)
.MapCtorParamFrom(x => x.Slug, x => x.Slug);
config.CreateMap<ApiCreateCategoryRequest, CategoryModel>()
.MapMemberFrom(x => x.Description, x => x.Description)
.MapMemberFrom(x => x.Name, x => x.Name)
.MapMemberFrom(x => x.Slug, x => x.Slug);
config.CreateMap<SubscriptionTierModel, ApiSubscriptionTier>()
.MapCtorParamFrom(x => x.Id, x => x.Id)
.MapCtorParamFrom(x => x.Name, x => x.Name)
.MapCtorParamFrom(x => x.Level, x => x.Level)
.MapCtorParamFrom(x => x.MonthlyPrice, x => x.MonthlyPrice)
.MapCtorParamFrom(x => x.Description, x => x.Description);
config.CreateMap<ApiCreateSubscriptionTierRequest, SubscriptionTierModel>()
.MapMemberFrom(x => x.Name, x => x.Name)
.MapMemberFrom(x => x.Level, x => x.Level)
.MapMemberFrom(x => x.MonthlyPrice, x => x.MonthlyPrice)
.MapMemberFrom(x => x.Description, x => x.Description);
config.CreateMap<SubscriptionModel, ApiSubscription>()
.MapCtorParamFrom(x => x.SubscribedToId, x => x.SubscribedToId)
.MapCtorParamFrom(x => x.SubscribedToName, x => x.SubscribedTo.Profile.DisplayName)
.MapCtorParamFrom(x => x.CurrentTier, x => x.SubscriptionTier);
}
}
}
@@ -0,0 +1,48 @@
using AutoMapper;
using AutoMapper.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace OnlyPrompt.Backend.Utils
{
public static class AutomapperExtensions
{
public static IMappingExpression<TSource, TDestination> CreateUpdateMap<TSource, TDestination>(this IMapperConfigurationExpression cfg, MemberList memberList = MemberList.Source)
{
return cfg.CreateMap<TSource, TDestination>(memberList)
.IgnoreNullMembers();
}
public static IMappingExpression<TSource, TDestination> MapMemberFrom<TSource, TDestination, TMember, TSourceMember>(this IMappingExpression<TSource, TDestination> mapping, Expression<Func<TDestination, TMember>> destinationMember, Expression<Func<TSource, TSourceMember>> sourceMember)
{
mapping.ForMember(destinationMember, x => x.MapFrom(sourceMember));
return mapping;
}
public static IMappingExpression<TSource, TDestination> IgnoreNullMembers<TSource, TDestination>(this IMappingExpression<TSource, TDestination> mapping)
{
mapping.ForAllMembers(opts => opts.Condition((src, dest, member) => src != null));
return mapping;
}
public static IMappingExpression<TSource, TDestination> MapCtorParamFrom<TSource, TDestination, TMember, TSourceMember>(this IMappingExpression<TSource, TDestination> mapping, Expression<Func<TDestination, TMember>> destinationMember, Expression<Func<TSource, TSourceMember>> sourceMember)
{
mapping.ForCtorParam(destinationMember, x => x.MapFrom(sourceMember));
return mapping;
}
public static IMappingExpression<TSource, TDestination> ForCtorParam<TSource, TDestination, DValue>(this IMappingExpression<TSource, TDestination> mapping, Expression<Func<TDestination, DValue>> paramSelector, Action<ICtorParamConfigurationExpression<TSource>> configure)
{
var ctorParamName = ((MemberExpression)paramSelector.Body).Member.Name;
mapping.ForCtorParam(ctorParamName, configure);
return mapping;
}
}
}
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
using OnlyPrompt.Backend.Database.Core;
namespace OnlyPrompt.Backend.Utils
{
public static class EntityExtensions
{
public static Task<T?> FindBySlugAsync<T>(this IQueryable<T> queryable, string slug) where T : class, IHasSlug
{
return queryable.FirstOrDefaultAsync(e => e.Slug == slug);
}
public static Task<T?> FindByIdentifierAsync<T>(this IQueryable<T> queryable, Identifier identifier) where T : class, IHasSlug, IEntity
{
if (identifier.Id.HasValue)
return queryable.FirstOrDefaultAsync(e => e.Id == identifier.Id.Value);
return queryable.FindBySlugAsync(identifier.Slug);
}
public static IQueryable<T> OfIdentifer<T>(this IQueryable<T> queryable, Identifier identifier) where T : class, IHasSlug, IEntity
{
if (identifier.Id.HasValue)
return queryable.Where(e => e.Id == identifier.Id.Value);
return queryable.Where(e => e.Slug == identifier.Slug);
}
}
}
+80
View File
@@ -0,0 +1,80 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using OnlyPrompt.Backend.Database.Models;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Security.Claims;
namespace OnlyPrompt.Backend.Utils
{
public static class Extensions
{
public static string? GetIdentifier(this ClaimsPrincipal principal) => principal.FindFirstValue(ClaimTypes.NameIdentifier);
public static bool TryGetIdentifier(this ClaimsPrincipal principal, [NotNullWhen(true)]out string? identifier)
{
identifier = principal.FindFirstValue(ClaimTypes.NameIdentifier);
return identifier != null;
}
public static Guid? GetUserId(this ClaimsPrincipal principal)
{
if (principal.TryGetIdentifier(out var identifier) && Guid.TryParse(identifier, out var userId))
return userId;
return null;
}
public static IEnumerable<Claim> GetClaims(this UserModel user)
{
yield return new Claim(ClaimTypes.NameIdentifier, user.Id.ToString());
yield return new Claim(ClaimTypes.Name, user.UserName);
yield return new Claim(ClaimTypes.Email, user.Email);
foreach (var role in user.Roles)
yield return new Claim(ClaimTypes.Role, role);
}
public static IQueryable<T> OrderBy<T, TKey>(this IQueryable<T> source, Expression<Func<T, TKey>> selecter, bool ascending)
{
if(ascending)
return source.OrderBy(selecter);
else
return source.OrderByDescending(selecter);
}
public static CookieOptions Copy(this CookieOptions options, Action<CookieOptions>? modify = null)
{
var newOptions = new CookieOptions
{
Domain = options.Domain,
Expires = options.Expires,
HttpOnly = options.HttpOnly,
IsEssential = options.IsEssential,
MaxAge = options.MaxAge,
Path = options.Path,
SameSite = options.SameSite,
Secure = options.Secure
};
modify?.Invoke(newOptions);
return newOptions;
}
public static string Limit(this string @string, int maxLength)
{
if (@string.Length <= maxLength)
return @string;
return @string.Substring(0, maxLength);
}
public const string LowerAlphabet = "abcdefghijklmnopqrstuvwxyz";
public static string GetString(this Random @random, int lenght, string alphabet = LowerAlphabet)
{
Span<char> chars = stackalloc char[lenght];
for (int i = 0; i < lenght; i++)
chars[i] = alphabet[@random.Next(alphabet.Length)];
return new string(chars);
}
}
}
+34
View File
@@ -0,0 +1,34 @@
using Scalar.AspNetCore;
namespace OnlyPrompt.Backend.Utils
{
public struct Identifier
{
public string Slug { get; init; }
public Guid? Id { get; init; }
public Identifier(string slug)
{
this.Slug = slug;
this.Id = null;
}
public Identifier(Guid guid)
{
this.Id = guid;
this.Slug = string.Empty;
}
public static implicit operator Identifier(string slug) => new Identifier(slug);
public static implicit operator Identifier(Guid guid) => new Identifier(guid);
public static bool TryParse(string input, out Identifier identifier)
{
identifier= new Identifier(input);
if (Guid.TryParse(input, out var guid))
identifier = new Identifier(guid);
return true;
}
}
}
+42
View File
@@ -0,0 +1,42 @@
using System.Text.RegularExpressions;
namespace OnlyPrompt.Backend.Utils
{
public static class SlugHelper
{
private static readonly Regex InvalidCharacters = new(@"[^a-z0-9\-]", RegexOptions.Compiled);
private static readonly Regex MultipleDashes = new(@"-+", RegexOptions.Compiled);
public static string GenerateSlug(string input, int? maxLength = null)
{
if (string.IsNullOrWhiteSpace(input))
return string.Empty;
var slug = input.ToLowerInvariant().Replace(" ", "-").Replace("_", "-");
slug = InvalidCharacters.Replace(slug, string.Empty);
slug = MultipleDashes.Replace(slug, "-");
slug = slug.Trim('-');
if (maxLength.HasValue)
slug = slug.Limit(maxLength.Value);
return slug;
}
private const string SuffixChars = "abcdefghijklmnopqrstuvwxyz0123456789";
public static async Task<string> GenerateUniqueSlugAsync(string input, Func<string, Task<bool>> existsFunc, int? maxLenght)
{
var slug = GenerateSlug(input);
var exists = await existsFunc(slug);
if (exists)
{
var suffix = Random.Shared.GetString(8, SuffixChars);
if (maxLenght.HasValue)
slug = slug.Limit(maxLenght.Value - 9);
slug = $"{slug}-{suffix}";
}
return slug;
}
}
}