Created color service using ServiceBase

This commit is contained in:
quentin 2024-04-22 16:54:28 -05:00
parent 0b2c77d4cc
commit f7b337e25a
13 changed files with 254 additions and 34 deletions

View File

@ -9,16 +9,12 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0-preview.3.24172.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0-preview.3.24172.4"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>
<ItemGroup>
<Folder Include="Authentication\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DAL\DAL.csproj" />
<ProjectReference Include="..\DAL\DAL.csproj"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,9 @@
using API.DTO.Base;
using DAL.Models;
namespace API.Authentication.Interfaces
{
public interface IColorAuthentication : IGenericAuthentication<ColorDTO, Color>
{
}
}

View File

@ -0,0 +1,13 @@
using DAL.Models;
namespace API.Authentication.Interfaces
{
public interface IGenericAuthentication<T, TModel>
{
bool canGetAll(User user);
bool canGet(TModel model, User user);
bool canAdd(T item, User user);
bool canUpdate(TModel model, User user);
bool canDelete(TModel model, User user);
}
}

26
API/DTO/Base/ColorDTO.cs Normal file
View File

@ -0,0 +1,26 @@
using DAL.Models;
namespace API.DTO.Base
{
public class ColorDTO : IAdaptable<Color>
{
public ulong id { get; set; }
public byte red { get; set; }
public byte blue { get; set; }
public byte green { get; set; }
public string name { get; set; } = null!;
public DateTime updated { get; set; }
public ulong updater { get; set; }
public Color adaptToModel()
{
return new Color
{
id = id,
red = red,
blue = blue,
green = green,
name = name
};
}
}
}

7
API/DTO/IAdaptable.cs Normal file
View File

@ -0,0 +1,7 @@
namespace API.DTO
{
public interface IAdaptable<TModel>
{
TModel adaptToModel();
}
}

View File

@ -1,3 +1,4 @@
using API.Services;
using DAL.Contexts;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
@ -21,10 +22,9 @@ namespace API
options.IncludeXmlComments(xmlPath, true);
});
builder.Services.AddDbContext<SASGContext>(options =>
{
options.UseMySQL(builder.Configuration["connectionString"] ?? throw new InvalidOperationException("Connection String is null"));
});
builder.Services.AddDbContext<SASGContext>(options => { options.UseMySQL(builder.Configuration["connectionString"] ?? throw new InvalidOperationException("Connection String is null")); });
builder.Services.AddTransient<ColorService>();
WebApplication app = builder.Build();
@ -44,4 +44,4 @@ namespace API
app.Run();
}
}
}
}

View File

@ -0,0 +1,15 @@
using API.Authentication.Interfaces;
using API.DTO.Base;
using DAL.Contexts;
using DAL.Models;
using DAL.Models.Audits;
namespace API.Services
{
public class ColorService : ServiceBase<ColorService, ColorDTO, Color, AuditColor, IColorAuthentication>
{
public ColorService(ILogger<ColorService> logger, SASGContext context, IColorAuthentication auth) : base(logger, context, auth)
{
}
}
}

View File

@ -0,0 +1,16 @@
using DAL.Models;
using System.Linq.Expressions;
namespace API.Services.Interfaces
{
public interface IGenericService<in T, TModel, out TAudit>
{
TModel? get(ulong id, User user);
IEnumerable<TModel>? get(User user, Expression<Func<TModel, bool>>? whereClause = null);
TModel? getNoAuthentication(ulong id);
IEnumerable<TModel> getNoAuthentication(Expression<Func<TModel, bool>>? whereClause = null);
TModel? add(T item, User user);
TModel? update(TModel model, User user);
TAudit? delete(TModel model, User user);
}
}

115
API/Services/ServiceBase.cs Normal file
View File

@ -0,0 +1,115 @@
using API.Authentication.Interfaces;
using API.DTO;
using API.Services.Interfaces;
using DAL.Contexts;
using DAL.Models;
using System.Linq.Expressions;
namespace API.Services
{
public class ServiceBase<TLoggerCategory, TDTO, TModel, TAudit, TAuthentication> : IGenericService<TDTO, TModel, TAudit>
where TAuthentication : IGenericAuthentication<TDTO, TModel>
where TModel : Model<TAudit>
where TAudit : class
where TDTO : IAdaptable<TModel>
{
private readonly TAuthentication _auth;
private readonly SASGContext _context;
private readonly ILogger<TLoggerCategory> _logger;
public ServiceBase(ILogger<TLoggerCategory> logger, SASGContext context, TAuthentication auth)
{
_logger = logger;
_context = context;
_auth = auth;
}
public TModel? get(ulong id, User user)
{
TModel? result = _context.Set<TModel>().Find(id);
if (result == null)
return null;
return _auth.canGet(result, user) ? result : null;
}
public IEnumerable<TModel>? get(User user, Expression<Func<TModel, bool>>? whereClause = null)
{
if (!_auth.canGetAll(user))
return null;
return whereClause != null ? _context.Set<TModel>().Where(whereClause) : _context.Set<TModel>();
}
public TModel? getNoAuthentication(ulong id)
{
return _context.Set<TModel>().Find(id);
}
public IEnumerable<TModel> getNoAuthentication(Expression<Func<TModel, bool>>? whereClause = null)
{
return whereClause != null ? _context.Set<TModel>().Where(whereClause) : _context.Set<TModel>();
}
public TModel? add(TDTO item, User user)
{
if (!_auth.canAdd(item, user))
return null;
TModel model = item.adaptToModel();
model.updater = user.id;
model.updated = DateTime.Now;
_context.Add(model);
_context.SaveChanges();
return model;
}
public TModel? update(TModel model, User user)
{
if (!_auth.canUpdate(model, user))
return null;
TModel? origModel = _context.Set<TModel>().Find(model.id);
if (origModel == null)
return null;
copyToAudit(origModel);
origModel.updated = DateTime.Now;
origModel.updater = user.id;
_context.SaveChanges();
return origModel;
}
public TAudit? delete(TModel model, User user)
{
if (!_auth.canDelete(model, user))
return null;
TModel? origModel = _context.Set<TModel>().Find(model.id);
if (origModel == null)
return null;
copyToAudit(origModel);
origModel.updated = DateTime.Now;
origModel.updater = user.id;
copyToAudit(origModel);
_context.Remove(origModel);
_context.SaveChanges();
return origModel.adaptToAudit();
}
private void copyToAudit(TModel model)
{
_context.Set<TAudit>().Add(model.adaptToAudit());
}
}
}

View File

@ -7,11 +7,8 @@ namespace DAL.Models.Audits
[Index("id", Name = "audit_colors_colors_id_fk")]
[Table("audit_colors")]
[Keyless]
public class AuditColor
public class AuditColor : AuditModel<Color>
{
[Column("id")]
public ulong id { get; set; }
[Column("red")]
public byte red { get; set; }
@ -24,12 +21,5 @@ namespace DAL.Models.Audits
[Column("name")]
[MaxLength(64)]
public string name { get; set; } = null!;
[Column("updated")]
[DataType("datetime")]
public DateTime updated { get; set; }
[Column("updater")]
public ulong updater { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DAL.Models.Audits
{
public abstract class AuditModel<TModel>
{
[Column("id")]
public ulong id { get; set; }
[Column("updated")]
[DataType("datetime")]
public DateTime updated { get; set; }
[Column("updater")]
public ulong updater { get; set; }
}
}

View File

@ -7,12 +7,8 @@ namespace DAL.Models
{
[Table("colors")]
[Index("updater", Name = "colors_users_id_fk")]
public class Color
public class Color : Model<AuditColor>
{
[Key]
[Column("id")]
public ulong id { get; set; }
[Column("red")]
public byte red { get; set; }
@ -26,15 +22,13 @@ namespace DAL.Models
[MaxLength(64)]
public string name { get; set; } = null!;
[Column("updated")]
[DataType("datetime")]
public DateTime updated { get; set; }
[Column("updater")]
public ulong updater { get; set; }
public User updaterRelation { get; set; } = null!;
public ICollection<AuditColor> audits { get; set; } = new List<AuditColor>();
public override AuditColor adaptToAudit()
{
throw new NotImplementedException();
}
}
}

21
DAL/Models/Model.cs Normal file
View File

@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DAL.Models
{
public abstract class Model<TAudit>
{
[Key]
[Column("id")]
public ulong id { get; set; }
[Column("updated")]
[DataType("datetime")]
public DateTime updated { get; set; }
[Column("updater")]
public ulong updater { get; set; }
public abstract TAudit adaptToAudit();
}
}