mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 19:00:07 +01:00
fix: db model fixes and quote mapping
This commit is contained in:
@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using QuotifyBE.Data;
|
using QuotifyBE.Data;
|
||||||
using QuotifyBE.Entities;
|
using QuotifyBE.Entities;
|
||||||
|
using QuotifyBE.Mapping;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ public class QuotesController : ControllerBase
|
|||||||
.Include(q => q.QuoteCategories!)
|
.Include(q => q.QuoteCategories!)
|
||||||
.ThenInclude(qc => qc.Category)
|
.ThenInclude(qc => qc.Category)
|
||||||
.Include(q => q.User)
|
.Include(q => q.User)
|
||||||
.Include(q => q.ImageId)
|
.Include(q => q.Image)
|
||||||
.FirstOrDefaultAsync(q => q.Id == id);
|
.FirstOrDefaultAsync(q => q.Id == id);
|
||||||
|
|
||||||
if (quote == null)
|
if (quote == null)
|
||||||
@@ -48,7 +49,7 @@ public class QuotesController : ControllerBase
|
|||||||
|
|
||||||
// TODO: Consider turning the quote into a DTO
|
// TODO: Consider turning the quote into a DTO
|
||||||
|
|
||||||
return Ok(quote);
|
return Ok(quote.ToQuoteShortDTO(_db));
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST /api/v1/quotes/new
|
// POST /api/v1/quotes/new
|
||||||
@@ -135,7 +136,7 @@ public class QuotesController : ControllerBase
|
|||||||
image = await _db.Images.FirstOrDefaultAsync(i => i.Id == quote.ImageId);
|
image = await _db.Images.FirstOrDefaultAsync(i => i.Id == quote.ImageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
var dto = new RandomQuote
|
var dto = new QuoteShortDTO
|
||||||
{
|
{
|
||||||
Text = quote.Text,
|
Text = quote.Text,
|
||||||
Author = quote.Author,
|
Author = quote.Author,
|
||||||
|
|||||||
@@ -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 Text { get; set; } = string.Empty;
|
||||||
public string Author { get; set; } = string.Empty;
|
public string Author { get; set; } = string.Empty;
|
||||||
public string? ImageUrl { get; set; }
|
public string? ImageUrl { get; set; }
|
||||||
@@ -8,12 +8,13 @@ namespace QuotifyBE.Entities
|
|||||||
required public string Text { get; set; }
|
required public string Text { get; set; }
|
||||||
required public string Author { get; set; }
|
required public string Author { get; set; }
|
||||||
//public int CategoryId { get; set; }
|
//public int CategoryId { get; set; }
|
||||||
public int ImageId { get; set; }
|
public int? ImageId { get; set; }
|
||||||
public DateTime CreatedAt { get; set; }
|
public DateTime CreatedAt { get; set; }
|
||||||
public DateTime LastUpdatedAt { get; set; }
|
public DateTime LastUpdatedAt { get; set; }
|
||||||
public int UserId { get; set; }
|
public int UserId { get; set; }
|
||||||
|
|
||||||
public User? User { 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>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace QuotifyBE.Entities
|
namespace QuotifyBE.Entities
|
||||||
{
|
{
|
||||||
public class QuoteCategory
|
public class QuoteCategory
|
||||||
{
|
{
|
||||||
|
|||||||
32
Mapping/QuoteMapping.cs
Normal file
32
Mapping/QuoteMapping.cs
Normal 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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,8 +12,8 @@ using QuotifyBE.Data;
|
|||||||
namespace QuotifyBE.Migrations
|
namespace QuotifyBE.Migrations
|
||||||
{
|
{
|
||||||
[DbContext(typeof(ApplicationDbContext))]
|
[DbContext(typeof(ApplicationDbContext))]
|
||||||
[Migration("20250714093636_initial_migration")]
|
[Migration("20250715142242_revised_model")]
|
||||||
partial class initial_migration
|
partial class revised_model
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
@@ -72,7 +72,7 @@ namespace QuotifyBE.Migrations
|
|||||||
b.Property<DateTime>("CreatedAt")
|
b.Property<DateTime>("CreatedAt")
|
||||||
.HasColumnType("timestamp with time zone");
|
.HasColumnType("timestamp with time zone");
|
||||||
|
|
||||||
b.Property<int>("ImageId")
|
b.Property<int?>("ImageId")
|
||||||
.HasColumnType("integer");
|
.HasColumnType("integer");
|
||||||
|
|
||||||
b.Property<DateTime>("LastUpdatedAt")
|
b.Property<DateTime>("LastUpdatedAt")
|
||||||
@@ -87,6 +87,8 @@ namespace QuotifyBE.Migrations
|
|||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ImageId");
|
||||||
|
|
||||||
b.HasIndex("UserId");
|
b.HasIndex("UserId");
|
||||||
|
|
||||||
b.ToTable("Quotes");
|
b.ToTable("Quotes");
|
||||||
@@ -104,7 +106,7 @@ namespace QuotifyBE.Migrations
|
|||||||
|
|
||||||
b.HasIndex("CategoryId");
|
b.HasIndex("CategoryId");
|
||||||
|
|
||||||
b.ToTable("QuoteCategory");
|
b.ToTable("QuoteCategories");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("QuotifyBE.Entities.User", b =>
|
modelBuilder.Entity("QuotifyBE.Entities.User", b =>
|
||||||
@@ -134,12 +136,18 @@ namespace QuotifyBE.Migrations
|
|||||||
|
|
||||||
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
|
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
|
||||||
{
|
{
|
||||||
|
b.HasOne("QuotifyBE.Entities.Image", "Image")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ImageId");
|
||||||
|
|
||||||
b.HasOne("QuotifyBE.Entities.User", "User")
|
b.HasOne("QuotifyBE.Entities.User", "User")
|
||||||
.WithMany()
|
.WithMany()
|
||||||
.HasForeignKey("UserId")
|
.HasForeignKey("UserId")
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
.IsRequired();
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Image");
|
||||||
|
|
||||||
b.Navigation("User");
|
b.Navigation("User");
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -152,7 +160,7 @@ namespace QuotifyBE.Migrations
|
|||||||
.IsRequired();
|
.IsRequired();
|
||||||
|
|
||||||
b.HasOne("QuotifyBE.Entities.Quote", "Quote")
|
b.HasOne("QuotifyBE.Entities.Quote", "Quote")
|
||||||
.WithMany()
|
.WithMany("QuoteCategories")
|
||||||
.HasForeignKey("QuoteId")
|
.HasForeignKey("QuoteId")
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
.IsRequired();
|
.IsRequired();
|
||||||
@@ -161,6 +169,11 @@ namespace QuotifyBE.Migrations
|
|||||||
|
|
||||||
b.Navigation("Quote");
|
b.Navigation("Quote");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("QuoteCategories");
|
||||||
|
});
|
||||||
#pragma warning restore 612, 618
|
#pragma warning restore 612, 618
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
|||||||
namespace QuotifyBE.Migrations
|
namespace QuotifyBE.Migrations
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public partial class initial_migration : Migration
|
public partial class revised_model : Migration
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
@@ -61,7 +61,7 @@ namespace QuotifyBE.Migrations
|
|||||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||||
Text = table.Column<string>(type: "text", nullable: false),
|
Text = table.Column<string>(type: "text", nullable: false),
|
||||||
Author = 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),
|
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||||
LastUpdatedAt = 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)
|
UserId = table.Column<int>(type: "integer", nullable: false)
|
||||||
@@ -69,6 +69,11 @@ namespace QuotifyBE.Migrations
|
|||||||
constraints: table =>
|
constraints: table =>
|
||||||
{
|
{
|
||||||
table.PrimaryKey("PK_Quotes", x => x.Id);
|
table.PrimaryKey("PK_Quotes", x => x.Id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_Quotes_Images_ImageId",
|
||||||
|
column: x => x.ImageId,
|
||||||
|
principalTable: "Images",
|
||||||
|
principalColumn: "Id");
|
||||||
table.ForeignKey(
|
table.ForeignKey(
|
||||||
name: "FK_Quotes_Users_UserId",
|
name: "FK_Quotes_Users_UserId",
|
||||||
column: x => x.UserId,
|
column: x => x.UserId,
|
||||||
@@ -78,7 +83,7 @@ namespace QuotifyBE.Migrations
|
|||||||
});
|
});
|
||||||
|
|
||||||
migrationBuilder.CreateTable(
|
migrationBuilder.CreateTable(
|
||||||
name: "QuoteCategory",
|
name: "QuoteCategories",
|
||||||
columns: table => new
|
columns: table => new
|
||||||
{
|
{
|
||||||
QuoteId = table.Column<int>(type: "integer", nullable: false),
|
QuoteId = table.Column<int>(type: "integer", nullable: false),
|
||||||
@@ -86,15 +91,15 @@ namespace QuotifyBE.Migrations
|
|||||||
},
|
},
|
||||||
constraints: table =>
|
constraints: table =>
|
||||||
{
|
{
|
||||||
table.PrimaryKey("PK_QuoteCategory", x => new { x.QuoteId, x.CategoryId });
|
table.PrimaryKey("PK_QuoteCategories", x => new { x.QuoteId, x.CategoryId });
|
||||||
table.ForeignKey(
|
table.ForeignKey(
|
||||||
name: "FK_QuoteCategory_Categories_CategoryId",
|
name: "FK_QuoteCategories_Categories_CategoryId",
|
||||||
column: x => x.CategoryId,
|
column: x => x.CategoryId,
|
||||||
principalTable: "Categories",
|
principalTable: "Categories",
|
||||||
principalColumn: "Id",
|
principalColumn: "Id",
|
||||||
onDelete: ReferentialAction.Cascade);
|
onDelete: ReferentialAction.Cascade);
|
||||||
table.ForeignKey(
|
table.ForeignKey(
|
||||||
name: "FK_QuoteCategory_Quotes_QuoteId",
|
name: "FK_QuoteCategories_Quotes_QuoteId",
|
||||||
column: x => x.QuoteId,
|
column: x => x.QuoteId,
|
||||||
principalTable: "Quotes",
|
principalTable: "Quotes",
|
||||||
principalColumn: "Id",
|
principalColumn: "Id",
|
||||||
@@ -102,10 +107,15 @@ namespace QuotifyBE.Migrations
|
|||||||
});
|
});
|
||||||
|
|
||||||
migrationBuilder.CreateIndex(
|
migrationBuilder.CreateIndex(
|
||||||
name: "IX_QuoteCategory_CategoryId",
|
name: "IX_QuoteCategories_CategoryId",
|
||||||
table: "QuoteCategory",
|
table: "QuoteCategories",
|
||||||
column: "CategoryId");
|
column: "CategoryId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Quotes_ImageId",
|
||||||
|
table: "Quotes",
|
||||||
|
column: "ImageId");
|
||||||
|
|
||||||
migrationBuilder.CreateIndex(
|
migrationBuilder.CreateIndex(
|
||||||
name: "IX_Quotes_UserId",
|
name: "IX_Quotes_UserId",
|
||||||
table: "Quotes",
|
table: "Quotes",
|
||||||
@@ -116,10 +126,7 @@ namespace QuotifyBE.Migrations
|
|||||||
protected override void Down(MigrationBuilder migrationBuilder)
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
migrationBuilder.DropTable(
|
migrationBuilder.DropTable(
|
||||||
name: "Images");
|
name: "QuoteCategories");
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
|
||||||
name: "QuoteCategory");
|
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
migrationBuilder.DropTable(
|
||||||
name: "Categories");
|
name: "Categories");
|
||||||
@@ -127,6 +134,9 @@ namespace QuotifyBE.Migrations
|
|||||||
migrationBuilder.DropTable(
|
migrationBuilder.DropTable(
|
||||||
name: "Quotes");
|
name: "Quotes");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Images");
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
migrationBuilder.DropTable(
|
||||||
name: "Users");
|
name: "Users");
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ namespace QuotifyBE.Migrations
|
|||||||
b.Property<DateTime>("CreatedAt")
|
b.Property<DateTime>("CreatedAt")
|
||||||
.HasColumnType("timestamp with time zone");
|
.HasColumnType("timestamp with time zone");
|
||||||
|
|
||||||
b.Property<int>("ImageId")
|
b.Property<int?>("ImageId")
|
||||||
.HasColumnType("integer");
|
.HasColumnType("integer");
|
||||||
|
|
||||||
b.Property<DateTime>("LastUpdatedAt")
|
b.Property<DateTime>("LastUpdatedAt")
|
||||||
@@ -84,6 +84,8 @@ namespace QuotifyBE.Migrations
|
|||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ImageId");
|
||||||
|
|
||||||
b.HasIndex("UserId");
|
b.HasIndex("UserId");
|
||||||
|
|
||||||
b.ToTable("Quotes");
|
b.ToTable("Quotes");
|
||||||
@@ -101,7 +103,7 @@ namespace QuotifyBE.Migrations
|
|||||||
|
|
||||||
b.HasIndex("CategoryId");
|
b.HasIndex("CategoryId");
|
||||||
|
|
||||||
b.ToTable("QuoteCategory");
|
b.ToTable("QuoteCategories");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("QuotifyBE.Entities.User", b =>
|
modelBuilder.Entity("QuotifyBE.Entities.User", b =>
|
||||||
@@ -131,12 +133,18 @@ namespace QuotifyBE.Migrations
|
|||||||
|
|
||||||
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
|
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
|
||||||
{
|
{
|
||||||
|
b.HasOne("QuotifyBE.Entities.Image", "Image")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ImageId");
|
||||||
|
|
||||||
b.HasOne("QuotifyBE.Entities.User", "User")
|
b.HasOne("QuotifyBE.Entities.User", "User")
|
||||||
.WithMany()
|
.WithMany()
|
||||||
.HasForeignKey("UserId")
|
.HasForeignKey("UserId")
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
.IsRequired();
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Image");
|
||||||
|
|
||||||
b.Navigation("User");
|
b.Navigation("User");
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -149,7 +157,7 @@ namespace QuotifyBE.Migrations
|
|||||||
.IsRequired();
|
.IsRequired();
|
||||||
|
|
||||||
b.HasOne("QuotifyBE.Entities.Quote", "Quote")
|
b.HasOne("QuotifyBE.Entities.Quote", "Quote")
|
||||||
.WithMany()
|
.WithMany("QuoteCategories")
|
||||||
.HasForeignKey("QuoteId")
|
.HasForeignKey("QuoteId")
|
||||||
.OnDelete(DeleteBehavior.Cascade)
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
.IsRequired();
|
.IsRequired();
|
||||||
@@ -158,6 +166,11 @@ namespace QuotifyBE.Migrations
|
|||||||
|
|
||||||
b.Navigation("Quote");
|
b.Navigation("Quote");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("QuotifyBE.Entities.Quote", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("QuoteCategories");
|
||||||
|
});
|
||||||
#pragma warning restore 612, 618
|
#pragma warning restore 612, 618
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,8 +28,4 @@
|
|||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Mapping\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user