Inital DeepDrftWeb Entity Framework setup

- DeepDrft DbContext
 - Inital Migration for Track Entity model
 - Track Repository
   - Full CRUD
This commit is contained in:
2025-08-30 21:48:21 -04:00
parent aac0004e03
commit 0e37cbf712
7 changed files with 356 additions and 0 deletions
@@ -0,0 +1,49 @@
using DeepDrftModels.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace DeepDrftWeb.Data.Configurations;
public class TrackConfiguration : IEntityTypeConfiguration<TrackEntity>
{
public void Configure(EntityTypeBuilder<TrackEntity> builder)
{
builder.ToTable("track");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasColumnName("id")
.IsRequired();
builder.Property(x => x.MediaPath)
.HasColumnName("media_path")
.IsRequired()
.HasMaxLength(500);
builder.Property(x => x.TrackName)
.HasColumnName("track_name")
.IsRequired()
.HasMaxLength(200);
builder.Property(x => x.Artist)
.HasColumnName("artist")
.IsRequired()
.HasMaxLength(200);
builder.Property(x => x.Album)
.HasColumnName("album")
.HasMaxLength(200);
builder.Property(x => x.Genre)
.HasColumnName("genre")
.HasMaxLength(100);
builder.Property(x => x.ReleaseDate)
.HasColumnName("release_date");
builder.Property(x => x.ImagePath)
.HasColumnName("image_path")
.HasMaxLength(500);
}
}
@@ -0,0 +1,21 @@
using DeepDrftModels.Entities;
using DeepDrftWeb.Data.Configurations;
using Microsoft.EntityFrameworkCore;
namespace DeepDrftWeb.Data;
public class DeepDrftContext : DbContext
{
public DeepDrftContext(DbContextOptions<DeepDrftContext> options) : base(options)
{
}
public DbSet<TrackEntity> Tracks { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new TrackConfiguration());
}
}
@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace DeepDrftWeb.Data;
public static class DeepDrftContextFactory
{
public class Factory : IDesignTimeDbContextFactory<DeepDrftContext>
{
public DeepDrftContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
.Build();
var optionsBuilder = new DbContextOptionsBuilder<DeepDrftContext>();
optionsBuilder.UseSqlite(configuration.GetConnectionString("DefaultConnection"));
return new DeepDrftContext(optionsBuilder.Options);
}
}
}
@@ -0,0 +1,74 @@
// <auto-generated />
using System;
using DeepDrftWeb.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DeepDrftWeb.Data.Migrations
{
[DbContext(typeof(DeepDrftContext))]
[Migration("20250831000622_Initial")]
partial class Initial
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.8");
modelBuilder.Entity("DeepDrftModels.Entities.TrackEntity", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<string>("Album")
.HasMaxLength(200)
.HasColumnType("TEXT")
.HasColumnName("album");
b.Property<string>("Artist")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("TEXT")
.HasColumnName("artist");
b.Property<string>("Genre")
.HasMaxLength(100)
.HasColumnType("TEXT")
.HasColumnName("genre");
b.Property<string>("ImagePath")
.HasMaxLength(500)
.HasColumnType("TEXT")
.HasColumnName("image_path");
b.Property<string>("MediaPath")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("TEXT")
.HasColumnName("media_path");
b.Property<DateOnly?>("ReleaseDate")
.HasColumnType("TEXT")
.HasColumnName("release_date");
b.Property<string>("TrackName")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("TEXT")
.HasColumnName("track_name");
b.HasKey("Id");
b.ToTable("track", (string)null);
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,41 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DeepDrftWeb.Data.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "track",
columns: table => new
{
id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
media_path = table.Column<string>(type: "TEXT", maxLength: 500, nullable: false),
track_name = table.Column<string>(type: "TEXT", maxLength: 200, nullable: false),
artist = table.Column<string>(type: "TEXT", maxLength: 200, nullable: false),
album = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
genre = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true),
release_date = table.Column<DateOnly>(type: "TEXT", nullable: true),
image_path = table.Column<string>(type: "TEXT", maxLength: 500, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_track", x => x.id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "track");
}
}
}
@@ -0,0 +1,71 @@
// <auto-generated />
using System;
using DeepDrftWeb.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DeepDrftWeb.Data.Migrations
{
[DbContext(typeof(DeepDrftContext))]
partial class DeepDrftContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.8");
modelBuilder.Entity("DeepDrftModels.Entities.TrackEntity", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<string>("Album")
.HasMaxLength(200)
.HasColumnType("TEXT")
.HasColumnName("album");
b.Property<string>("Artist")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("TEXT")
.HasColumnName("artist");
b.Property<string>("Genre")
.HasMaxLength(100)
.HasColumnType("TEXT")
.HasColumnName("genre");
b.Property<string>("ImagePath")
.HasMaxLength(500)
.HasColumnType("TEXT")
.HasColumnName("image_path");
b.Property<string>("MediaPath")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("TEXT")
.HasColumnName("media_path");
b.Property<DateOnly?>("ReleaseDate")
.HasColumnType("TEXT")
.HasColumnName("release_date");
b.Property<string>("TrackName")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("TEXT")
.HasColumnName("track_name");
b.HasKey("Id");
b.ToTable("track", (string)null);
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,76 @@
using DeepDrftModels.Entities;
using DeepDrftModels.Models;
using Microsoft.EntityFrameworkCore;
namespace DeepDrftWeb.Data.Repositories;
public class TrackRepository
{
private readonly DeepDrftContext _db;
public TrackRepository(DeepDrftContext db)
{
_db = db;
}
public async Task<TrackEntity?> GetById(long id)
{
return await _db.Tracks.FindAsync(id);
}
public async Task<List<TrackEntity>> GetAll()
{
return await _db.Tracks.ToListAsync();
}
public async Task<PagedResult<TrackEntity>> GetPage(PagingParameters<TrackEntity> pageParameters)
{
var count = await _db.Tracks.CountAsync();
var page = await _db.Tracks
.OrderBy(pageParameters.OrderBy ?? (t => t.Id))
.Skip((pageParameters.Page - 1) * pageParameters.PageSize)
.Take(pageParameters.PageSize)
.ToListAsync();
return new PagedResult<TrackEntity>(page, count, pageParameters.Page, pageParameters.PageSize);
}
public async Task<TrackEntity> Create(TrackEntity newTrack)
{
var track = _db.Tracks.Add(newTrack);
await _db.SaveChangesAsync();
return track.Entity;
}
public async Task<TrackEntity> Update(TrackEntity track)
{
var trackEntity = await GetById(track.Id);
if (trackEntity == null)
{
return await Create(track);
}
trackEntity.Album = track.Album;
trackEntity.Artist = track.Artist;
trackEntity.Genre = track.Genre;
trackEntity.ImagePath = track.ImagePath;
trackEntity.MediaPath = track.MediaPath;
trackEntity.ReleaseDate = track.ReleaseDate;
trackEntity.TrackName = track.TrackName;
await _db.SaveChangesAsync();
return trackEntity;
}
public async Task Delete(long id)
{
var track = await GetById(id);
if (track != null)
{
_db.Tracks.Remove(track);
await _db.SaveChangesAsync();
}
}
}