Backend Services Split into separate projects for reference from other front ends
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
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.EntryKey)
|
||||
.HasColumnName("entry_key")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
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.EntryKey = track.EntryKey;
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
</PackageReference>
|
||||
<ProjectReference Include="..\DeepDrftModels\DeepDrftModels.csproj" />
|
||||
<ProjectReference Include="..\DeepDrftWeb.Client\DeepDrftWeb.Client.csproj" />
|
||||
<ProjectReference Include="..\DeepDrftWeb.Services\DeepDrftWeb.Services.csproj" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.*" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using DeepDrftWeb.Data;
|
||||
using DeepDrftWeb.Services.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using DeepDrftWeb.Data;
|
||||
using DeepDrftWeb.Services.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
using DeepDrftModels.Entities;
|
||||
using DeepDrftModels.Models;
|
||||
using DeepDrftWeb.Data;
|
||||
using DeepDrftWeb.Data.Repositories;
|
||||
using NetBlocks.Models;
|
||||
|
||||
namespace DeepDrftWeb.Services;
|
||||
|
||||
public class TrackService
|
||||
{
|
||||
private readonly string _sortLastAscending = Enumerable.Repeat(char.MaxValue, 64).Aggregate(string.Empty, (a, b) => a + b);
|
||||
private readonly string _sortLastDescending = Enumerable.Repeat(char.MinValue.ToString(), 64).Aggregate(string.Empty, (a, b) => a + b);
|
||||
private readonly TrackRepository _repository;
|
||||
|
||||
public TrackService(TrackRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public async Task<ResultContainer<TrackEntity?>> GetById(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var track = await _repository.GetById(id);
|
||||
return ResultContainer<TrackEntity?>.CreatePassResult(track);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return ResultContainer<TrackEntity?>.CreateFailResult(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ResultContainer<List<TrackEntity>>> GetAll()
|
||||
{
|
||||
try
|
||||
{
|
||||
var tracks = await _repository.GetAll();
|
||||
return ResultContainer<List<TrackEntity>>.CreatePassResult(tracks);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return ResultContainer<List<TrackEntity>>.CreateFailResult(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ResultContainer<PagedResult<TrackEntity>>> GetPaged(int pageNumber, int pageSize, string? sortColumn, bool sortDescending)
|
||||
{
|
||||
try
|
||||
{
|
||||
var parameters = new PagingParameters<TrackEntity>()
|
||||
{
|
||||
Page = pageNumber,
|
||||
PageSize = pageSize,
|
||||
IsDescending = sortDescending
|
||||
};
|
||||
|
||||
if (sortColumn != null)
|
||||
{
|
||||
switch (sortColumn)
|
||||
{
|
||||
case "TrackName":
|
||||
parameters.OrderBy = entity => entity.TrackName;
|
||||
break;
|
||||
case "Artist":
|
||||
parameters.OrderBy = entity => entity.Artist;
|
||||
break;
|
||||
case "Album":
|
||||
parameters.OrderBy = entity => entity.Album ?? _sortLastAscending;
|
||||
break;
|
||||
case "ReleaseDate":
|
||||
parameters.OrderBy = entity => entity.ReleaseDate ?? DateOnly.MaxValue;
|
||||
break;
|
||||
case "Genre":
|
||||
parameters.OrderBy = entity => entity.Genre ?? _sortLastAscending;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
var page = await _repository.GetPage(parameters);
|
||||
return ResultContainer<PagedResult<TrackEntity>>.CreatePassResult(page);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return ResultContainer<PagedResult<TrackEntity>>.CreateFailResult(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ResultContainer<TrackEntity>> Create(TrackEntity newTrack)
|
||||
{
|
||||
try
|
||||
{
|
||||
var track = await _repository.Create(newTrack);
|
||||
return ResultContainer<TrackEntity>.CreatePassResult(track);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return ResultContainer<TrackEntity>.CreateFailResult(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ResultContainer<TrackEntity>> Update(TrackEntity track)
|
||||
{
|
||||
try
|
||||
{
|
||||
var updatedTrack = await _repository.Update(track);
|
||||
return ResultContainer<TrackEntity>.CreatePassResult(updatedTrack);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return ResultContainer<TrackEntity>.CreateFailResult(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result> Delete(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _repository.Delete(id);
|
||||
return Result.CreatePassResult();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result.CreateFailResult(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using DeepDrftWeb.Data;
|
||||
using DeepDrftWeb.Data.Repositories;
|
||||
using DeepDrftWeb.Services.Data;
|
||||
using DeepDrftWeb.Services.Repositories;
|
||||
using DeepDrftWeb.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user