Skip to content

Commit

Permalink
The backend part of credits (#92)
Browse files Browse the repository at this point in the history
- Credits added to story model
- Credits handled in create, edit, and display portion
- Everything works but still needs proper styling, see the aforementioned issue
  • Loading branch information
Atulin committed Sep 16, 2024
1 parent 39c9d50 commit b7e1f43
Show file tree
Hide file tree
Showing 15 changed files with 2,834 additions and 128 deletions.
276 changes: 158 additions & 118 deletions Ogma3/CompiledModels/ApplicationDbContextModelBuilder.cs

Large diffs are not rendered by default.

178 changes: 178 additions & 0 deletions Ogma3/CompiledModels/CreditEntityType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Ogma3.Data.Stories;

#pragma warning disable 219, 612, 618
#nullable disable

namespace CompiledModels
{
internal partial class CreditEntityType
{
public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType baseEntityType = null)
{
var runtimeEntityType = model.AddEntityType(
"Ogma3.Data.Stories.Credit",
typeof(Credit),
baseEntityType);

var storyId = runtimeEntityType.AddProperty(
"StoryId",
typeof(long),
afterSaveBehavior: PropertySaveBehavior.Throw,
sentinel: 0L);
storyId.TypeMapping = LongTypeMapping.Default.Clone(
comparer: new ValueComparer<long>(
(long v1, long v2) => v1 == v2,
(long v) => v.GetHashCode(),
(long v) => v),
keyComparer: new ValueComparer<long>(
(long v1, long v2) => v1 == v2,
(long v) => v.GetHashCode(),
(long v) => v),
providerValueComparer: new ValueComparer<long>(
(long v1, long v2) => v1 == v2,
(long v) => v.GetHashCode(),
(long v) => v));
storyId.AddAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.None);

var id = runtimeEntityType.AddProperty(
"Id",
typeof(int),
valueGenerated: ValueGenerated.OnAdd,
afterSaveBehavior: PropertySaveBehavior.Throw,
sentinel: 0);
id.TypeMapping = IntTypeMapping.Default.Clone(
comparer: new ValueComparer<int>(
(int v1, int v2) => v1 == v2,
(int v) => v,
(int v) => v),
keyComparer: new ValueComparer<int>(
(int v1, int v2) => v1 == v2,
(int v) => v,
(int v) => v),
providerValueComparer: new ValueComparer<int>(
(int v1, int v2) => v1 == v2,
(int v) => v,
(int v) => v),
mappingInfo: new RelationalTypeMappingInfo(
storeTypeName: "integer"));
id.AddAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);

var link = runtimeEntityType.AddProperty(
"Link",
typeof(string),
propertyInfo: typeof(Credit).GetProperty("Link", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(Credit).GetField("<Link>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly),
nullable: true);
link.TypeMapping = StringTypeMapping.Default.Clone(
comparer: new ValueComparer<string>(
(string v1, string v2) => v1 == v2,
(string v) => v.GetHashCode(),
(string v) => v),
keyComparer: new ValueComparer<string>(
(string v1, string v2) => v1 == v2,
(string v) => v.GetHashCode(),
(string v) => v),
providerValueComparer: new ValueComparer<string>(
(string v1, string v2) => v1 == v2,
(string v) => v.GetHashCode(),
(string v) => v),
mappingInfo: new RelationalTypeMappingInfo(
dbType: System.Data.DbType.String));
link.AddAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.None);

var name = runtimeEntityType.AddProperty(
"Name",
typeof(string),
propertyInfo: typeof(Credit).GetProperty("Name", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(Credit).GetField("<Name>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
name.TypeMapping = StringTypeMapping.Default.Clone(
comparer: new ValueComparer<string>(
(string v1, string v2) => v1 == v2,
(string v) => v.GetHashCode(),
(string v) => v),
keyComparer: new ValueComparer<string>(
(string v1, string v2) => v1 == v2,
(string v) => v.GetHashCode(),
(string v) => v),
providerValueComparer: new ValueComparer<string>(
(string v1, string v2) => v1 == v2,
(string v) => v.GetHashCode(),
(string v) => v),
mappingInfo: new RelationalTypeMappingInfo(
dbType: System.Data.DbType.String));
name.AddAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.None);

var role = runtimeEntityType.AddProperty(
"Role",
typeof(string),
propertyInfo: typeof(Credit).GetProperty("Role", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(Credit).GetField("<Role>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
role.TypeMapping = StringTypeMapping.Default.Clone(
comparer: new ValueComparer<string>(
(string v1, string v2) => v1 == v2,
(string v) => v.GetHashCode(),
(string v) => v),
keyComparer: new ValueComparer<string>(
(string v1, string v2) => v1 == v2,
(string v) => v.GetHashCode(),
(string v) => v),
providerValueComparer: new ValueComparer<string>(
(string v1, string v2) => v1 == v2,
(string v) => v.GetHashCode(),
(string v) => v),
mappingInfo: new RelationalTypeMappingInfo(
dbType: System.Data.DbType.String));
role.AddAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.None);

var key = runtimeEntityType.AddKey(
new[] { storyId, id });
runtimeEntityType.SetPrimaryKey(key);

return runtimeEntityType;
}

public static RuntimeForeignKey CreateForeignKey1(RuntimeEntityType declaringEntityType, RuntimeEntityType principalEntityType)
{
var runtimeForeignKey = declaringEntityType.AddForeignKey(new[] { declaringEntityType.FindProperty("StoryId") },
principalEntityType.FindKey(new[] { principalEntityType.FindProperty("Id") }),
principalEntityType,
deleteBehavior: DeleteBehavior.Cascade,
required: true,
ownership: true);

var credits = principalEntityType.AddNavigation("Credits",
runtimeForeignKey,
onDependent: false,
typeof(ICollection<Credit>),
propertyInfo: typeof(Story).GetProperty("Credits", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(Story).GetField("<Credits>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly),
eagerLoaded: true);

return runtimeForeignKey;
}

public static void CreateAnnotations(RuntimeEntityType runtimeEntityType)
{
runtimeEntityType.AddAnnotation("Relational:ContainerColumnName", "Credits");
runtimeEntityType.AddAnnotation("Relational:FunctionName", null);
runtimeEntityType.AddAnnotation("Relational:Schema", null);
runtimeEntityType.AddAnnotation("Relational:SqlQuery", null);
runtimeEntityType.AddAnnotation("Relational:TableName", "Stories");
runtimeEntityType.AddAnnotation("Relational:ViewName", null);
runtimeEntityType.AddAnnotation("Relational:ViewSchema", null);

Customize(runtimeEntityType);
}

static partial void Customize(RuntimeEntityType runtimeEntityType);
}
}
8 changes: 6 additions & 2 deletions Ogma3/Data/Stories/Story.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public sealed class Story : BaseModel, IBlockableContent, IReportableContent, ID
public string? CoverId { get; set; }
public DateTime CreationDate { get; set; }
public DateTime? PublicationDate { get; set; }

public ICollection<Credit> Credits { get; set; } = [];

public IList<Chapter> Chapters { get; set; } = null!;
public IEnumerable<Tag> Tags { get; set; } = null!;
Expand All @@ -39,11 +41,13 @@ public sealed class Story : BaseModel, IBlockableContent, IReportableContent, ID
public int ChapterCount { get; set; }

// Just for relationship purposes
public ICollection<Folder> Folders { get; set; } = null!;
public ICollection<Folder> Folders { get; set; } = [];

public ContentBlock? ContentBlock { get; set; }
public long? ContentBlockId { get; set; }

public ICollection<Report> Reports { get; set; } = null!;
public ICollection<Shelf> Shelves { get; set; } = null!;
}
}

public sealed record Credit(string Role, string Name, string? Link);
3 changes: 3 additions & 0 deletions Ogma3/Data/Stories/StoryConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public override void Configure(EntityTypeBuilder<Story> builder)
.IsRequired()
.HasDefaultValue(0);

builder
.OwnsMany(s => s.Credits, c => c.ToJson());


// NAVIGATION
builder
Expand Down
6 changes: 5 additions & 1 deletion Ogma3/Data/Stories/StoryDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public sealed class StoryDetails
public required int CommentsCount { get; init; }
public required int VotesCount { get; init; }
public required ContentBlockCard? ContentBlock { get; init; }
}

public required ICollection<CreditDto> Credits { get; init; }
}

public sealed record CreditDto(string Role, string Name, string? Link);
Loading

0 comments on commit b7e1f43

Please sign in to comment.