fix: db model fixes and quote mapping

This commit is contained in:
2025-07-15 16:38:37 +02:00
parent a355c668bd
commit e2eea51a08
9 changed files with 98 additions and 31 deletions

View File

@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using QuotifyBE.Data;
using QuotifyBE.Entities;
using QuotifyBE.Mapping;
using System.Security.Claims;
using Microsoft.EntityFrameworkCore;
@@ -40,7 +41,7 @@ public class QuotesController : ControllerBase
.Include(q => q.QuoteCategories!)
.ThenInclude(qc => qc.Category)
.Include(q => q.User)
.Include(q => q.ImageId)
.Include(q => q.Image)
.FirstOrDefaultAsync(q => q.Id == id);
if (quote == null)
@@ -48,7 +49,7 @@ public class QuotesController : ControllerBase
// TODO: Consider turning the quote into a DTO
return Ok(quote);
return Ok(quote.ToQuoteShortDTO(_db));
}
// POST /api/v1/quotes/new
@@ -135,7 +136,7 @@ public class QuotesController : ControllerBase
image = await _db.Images.FirstOrDefaultAsync(i => i.Id == quote.ImageId);
}
var dto = new RandomQuote
var dto = new QuoteShortDTO
{
Text = quote.Text,
Author = quote.Author,

View File

@@ -1,5 +1,6 @@
public record class RandomQuote
public record class QuoteShortDTO
{
public int Id { get; set; }
public string Text { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public string? ImageUrl { get; set; }

View File

@@ -8,12 +8,13 @@ namespace QuotifyBE.Entities
required public string Text { get; set; }
required public string Author { get; set; }
//public int CategoryId { get; set; }
public int ImageId { get; set; }
public int? ImageId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime LastUpdatedAt { get; set; }
public int UserId { get; set; }
public User? User { get; set; }
public ICollection<QuoteCategory>? QuoteCategories = new List<QuoteCategory>();
public Image? Image { get; set; }
public ICollection<QuoteCategory> QuoteCategories { get; set; } = new List<QuoteCategory>();
}
}

View File

@@ -1,4 +1,4 @@
namespace QuotifyBE.Entities
namespace QuotifyBE.Entities
{
public class QuoteCategory
{

32
Mapping/QuoteMapping.cs Normal file
View File

@@ -0,0 +1,32 @@
using Microsoft.Extensions.Logging;
using QuotifyBE.Data;
using QuotifyBE.DTOs;
using QuotifyBE.Entities;
namespace QuotifyBE.Mapping;
public static class QuoteMapping
{
public static QuoteShortDTO ToQuoteShortDTO(this Quote quote, ApplicationDbContext db)
{
List<string> categoryNames = [];
if (quote.QuoteCategories != null)
{
foreach (QuoteCategory quoteCategory in quote.QuoteCategories)
{
categoryNames.Add(quoteCategory.Category!.Name ?? $"Unnamed category {quoteCategory.CategoryId}");
}
}
return new QuoteShortDTO
{
Id = quote.Id,
Text = quote.Text,
Author = quote.Author,
ImageUrl = quote.Image?.Url,
Categories = categoryNames
};
}
}

View File

@@ -12,8 +12,8 @@ using QuotifyBE.Data;
namespace QuotifyBE.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20250714093636_initial_migration")]
partial class initial_migration
[Migration("20250715142242_revised_model")]
partial class revised_model
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -72,7 +72,7 @@ namespace QuotifyBE.Migrations
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<int>("ImageId")
b.Property<int?>("ImageId")
.HasColumnType("integer");
b.Property<DateTime>("LastUpdatedAt")
@@ -87,6 +87,8 @@ namespace QuotifyBE.Migrations
b.HasKey("Id");
b.HasIndex("ImageId");
b.HasIndex("UserId");
b.ToTable("Quotes");
@@ -104,7 +106,7 @@ namespace QuotifyBE.Migrations
b.HasIndex("CategoryId");
b.ToTable("QuoteCategory");
b.ToTable("QuoteCategories");
});
modelBuilder.Entity("QuotifyBE.Entities.User", b =>
@@ -134,12 +136,18 @@ namespace QuotifyBE.Migrations
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
{
b.HasOne("QuotifyBE.Entities.Image", "Image")
.WithMany()
.HasForeignKey("ImageId");
b.HasOne("QuotifyBE.Entities.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Image");
b.Navigation("User");
});
@@ -152,7 +160,7 @@ namespace QuotifyBE.Migrations
.IsRequired();
b.HasOne("QuotifyBE.Entities.Quote", "Quote")
.WithMany()
.WithMany("QuoteCategories")
.HasForeignKey("QuoteId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@@ -161,6 +169,11 @@ namespace QuotifyBE.Migrations
b.Navigation("Quote");
});
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
{
b.Navigation("QuoteCategories");
});
#pragma warning restore 612, 618
}
}

View File

@@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace QuotifyBE.Migrations
{
/// <inheritdoc />
public partial class initial_migration : Migration
public partial class revised_model : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
@@ -61,7 +61,7 @@ namespace QuotifyBE.Migrations
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Text = table.Column<string>(type: "text", nullable: false),
Author = table.Column<string>(type: "text", nullable: false),
ImageId = table.Column<int>(type: "integer", nullable: false),
ImageId = table.Column<int>(type: "integer", nullable: true),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
LastUpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
UserId = table.Column<int>(type: "integer", nullable: false)
@@ -69,6 +69,11 @@ namespace QuotifyBE.Migrations
constraints: table =>
{
table.PrimaryKey("PK_Quotes", x => x.Id);
table.ForeignKey(
name: "FK_Quotes_Images_ImageId",
column: x => x.ImageId,
principalTable: "Images",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Quotes_Users_UserId",
column: x => x.UserId,
@@ -78,7 +83,7 @@ namespace QuotifyBE.Migrations
});
migrationBuilder.CreateTable(
name: "QuoteCategory",
name: "QuoteCategories",
columns: table => new
{
QuoteId = table.Column<int>(type: "integer", nullable: false),
@@ -86,15 +91,15 @@ namespace QuotifyBE.Migrations
},
constraints: table =>
{
table.PrimaryKey("PK_QuoteCategory", x => new { x.QuoteId, x.CategoryId });
table.PrimaryKey("PK_QuoteCategories", x => new { x.QuoteId, x.CategoryId });
table.ForeignKey(
name: "FK_QuoteCategory_Categories_CategoryId",
name: "FK_QuoteCategories_Categories_CategoryId",
column: x => x.CategoryId,
principalTable: "Categories",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_QuoteCategory_Quotes_QuoteId",
name: "FK_QuoteCategories_Quotes_QuoteId",
column: x => x.QuoteId,
principalTable: "Quotes",
principalColumn: "Id",
@@ -102,10 +107,15 @@ namespace QuotifyBE.Migrations
});
migrationBuilder.CreateIndex(
name: "IX_QuoteCategory_CategoryId",
table: "QuoteCategory",
name: "IX_QuoteCategories_CategoryId",
table: "QuoteCategories",
column: "CategoryId");
migrationBuilder.CreateIndex(
name: "IX_Quotes_ImageId",
table: "Quotes",
column: "ImageId");
migrationBuilder.CreateIndex(
name: "IX_Quotes_UserId",
table: "Quotes",
@@ -116,10 +126,7 @@ namespace QuotifyBE.Migrations
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Images");
migrationBuilder.DropTable(
name: "QuoteCategory");
name: "QuoteCategories");
migrationBuilder.DropTable(
name: "Categories");
@@ -127,6 +134,9 @@ namespace QuotifyBE.Migrations
migrationBuilder.DropTable(
name: "Quotes");
migrationBuilder.DropTable(
name: "Images");
migrationBuilder.DropTable(
name: "Users");
}

View File

@@ -69,7 +69,7 @@ namespace QuotifyBE.Migrations
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<int>("ImageId")
b.Property<int?>("ImageId")
.HasColumnType("integer");
b.Property<DateTime>("LastUpdatedAt")
@@ -84,6 +84,8 @@ namespace QuotifyBE.Migrations
b.HasKey("Id");
b.HasIndex("ImageId");
b.HasIndex("UserId");
b.ToTable("Quotes");
@@ -101,7 +103,7 @@ namespace QuotifyBE.Migrations
b.HasIndex("CategoryId");
b.ToTable("QuoteCategory");
b.ToTable("QuoteCategories");
});
modelBuilder.Entity("QuotifyBE.Entities.User", b =>
@@ -131,12 +133,18 @@ namespace QuotifyBE.Migrations
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
{
b.HasOne("QuotifyBE.Entities.Image", "Image")
.WithMany()
.HasForeignKey("ImageId");
b.HasOne("QuotifyBE.Entities.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Image");
b.Navigation("User");
});
@@ -149,7 +157,7 @@ namespace QuotifyBE.Migrations
.IsRequired();
b.HasOne("QuotifyBE.Entities.Quote", "Quote")
.WithMany()
.WithMany("QuoteCategories")
.HasForeignKey("QuoteId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@@ -158,6 +166,11 @@ namespace QuotifyBE.Migrations
b.Navigation("Quote");
});
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
{
b.Navigation("QuoteCategories");
});
#pragma warning restore 612, 618
}
}

View File

@@ -28,8 +28,4 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Mapping\" />
</ItemGroup>
</Project>