diff --git a/BL/BL.Interfaces/IXuysBL.cs b/BL/BL.Interfaces/IXuysBL.cs new file mode 100644 index 0000000..5d3e765 --- /dev/null +++ b/BL/BL.Interfaces/IXuysBL.cs @@ -0,0 +1,9 @@ +using Entities; +using Common.SearchParams; + +namespace BL.Interfaces +{ + public interface IXuysBL : ICrudBL + { + } +} diff --git a/BL/BL.Standard/DependencyInjection.cs b/BL/BL.Standard/DependencyInjection.cs index 2553fbb..6bcde7b 100644 --- a/BL/BL.Standard/DependencyInjection.cs +++ b/BL/BL.Standard/DependencyInjection.cs @@ -10,6 +10,7 @@ public static IServiceCollection AddBusinessLogicLayer(this IServiceCollection s { services.AddScoped(); services.AddScoped(); + services.AddScoped(); return services; } diff --git a/BL/BL.Standard/XuysBL.cs b/BL/BL.Standard/XuysBL.cs new file mode 100644 index 0000000..3be9ab9 --- /dev/null +++ b/BL/BL.Standard/XuysBL.cs @@ -0,0 +1,48 @@ +using BL.Interfaces; +using Common.SearchParams; +using Dal.Interfaces; +using Entities; + +namespace BL.Standard +{ + public class XuysBL : IXuysBL + { + private readonly IXuysDal _xuysDal; + + public XuysBL(IXuysDal xuysDal) + { + _xuysDal = xuysDal; + } + + public async Task AddOrUpdateAsync(Xuy entity) + { + entity.Id = await _xuysDal.AddOrUpdateAsync(entity); + return entity.Id; + } + + public Task ExistsAsync(int id) + { + return _xuysDal.ExistsAsync(id); + } + + public Task ExistsAsync(XuysSearchParams searchParams) + { + return _xuysDal.ExistsAsync(searchParams); + } + + public Task GetAsync(int id, object? convertParams = null) + { + return _xuysDal.GetAsync(id, convertParams); + } + + public Task DeleteAsync(int id) + { + return _xuysDal.DeleteAsync(id); + } + + public Task> GetAsync(XuysSearchParams searchParams, object? convertParams = null) + { + return _xuysDal.GetAsync(searchParams, convertParams); + } + } +} diff --git a/Common/SearchParams/XuysSearchParams.cs b/Common/SearchParams/XuysSearchParams.cs new file mode 100644 index 0000000..e4507a4 --- /dev/null +++ b/Common/SearchParams/XuysSearchParams.cs @@ -0,0 +1,13 @@ +namespace Common.SearchParams +{ + public class XuysSearchParams : BaseSearchParams + { + public bool? IsActive { get; set; } + public XuysSearchParams() { } + public XuysSearchParams(bool? isActive = null, string? searchQuery = null, int startIndex = 0, int? objectsCount = null) + : base(startIndex, objectsCount, searchQuery) + { + IsActive = isActive; + } + } +} diff --git a/Dal/Dal.DbModels/DefaultDbContext.cs b/Dal/Dal.DbModels/DefaultDbContext.cs index e786cca..cb158ad 100644 --- a/Dal/Dal.DbModels/DefaultDbContext.cs +++ b/Dal/Dal.DbModels/DefaultDbContext.cs @@ -10,6 +10,7 @@ public DefaultDbContext(DbContextOptions options) : base(optio public virtual DbSet Users { get; set; } public virtual DbSet Tokens { get; set; } + public virtual DbSet Xuys { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -28,6 +29,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.Property(e => e.RegistrationDate).HasColumnType("datetime"); }); + modelBuilder.Entity(entity => + { + entity.Property(e => e.Name).IsRequired(); + }); + OnModelCreatingPartial(modelBuilder); } partial void OnModelCreatingPartial(ModelBuilder modelBuilder); diff --git a/Dal/Dal.DbModels/Xuy.cs b/Dal/Dal.DbModels/Xuy.cs new file mode 100644 index 0000000..1a95bc3 --- /dev/null +++ b/Dal/Dal.DbModels/Xuy.cs @@ -0,0 +1,10 @@ +namespace Dal.DbModels +{ + public partial class Xuy + { + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + public string? Description { get; set; } + public bool IsActive { get; set; } + } +} diff --git a/Dal/Dal.Interfaces/IXuysDal.cs b/Dal/Dal.Interfaces/IXuysDal.cs new file mode 100644 index 0000000..18cfe9f --- /dev/null +++ b/Dal/Dal.Interfaces/IXuysDal.cs @@ -0,0 +1,9 @@ +using Common.SearchParams; +using Dal.DbModels; + +namespace Dal.Interfaces +{ + public interface IXuysDal : IBaseDal + { + } +} diff --git a/Dal/Dal.SQL/DependencyInjection.cs b/Dal/Dal.SQL/DependencyInjection.cs index 6e2a776..6652bab 100644 --- a/Dal/Dal.SQL/DependencyInjection.cs +++ b/Dal/Dal.SQL/DependencyInjection.cs @@ -13,6 +13,7 @@ public static IServiceCollection AddDataAccessLayer(this IServiceCollection serv services.AddDbContext(config => config.UseNpgsql(configuration["ConnectionStrings:DefaultConnectionString"])); services.AddScoped(); services.AddScoped(); + services.AddScoped(); return services; } diff --git a/Dal/Dal.SQL/XuysDal.cs b/Dal/Dal.SQL/XuysDal.cs new file mode 100644 index 0000000..5c1c3a4 --- /dev/null +++ b/Dal/Dal.SQL/XuysDal.cs @@ -0,0 +1,63 @@ +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore; +using Dal.DbModels; +using Dal.Interfaces; +using Common.SearchParams; + +namespace Dal.SQL +{ + public class XuysDal : BaseDal, IXuysDal + { + protected override bool RequiresUpdatesAfterObjectSaving => false; + + public XuysDal(DefaultDbContext context) : base(context) { } + + protected override Task UpdateBeforeSavingAsync(DefaultDbContext context, Entities.Xuy entity, Xuy dbObject, bool exists) + { + dbObject.Name = entity.Name; + dbObject.Description = entity.Description; + dbObject.IsActive = entity.IsActive; + return Task.CompletedTask; + } + + protected override IQueryable BuildDbQuery(DefaultDbContext context, IQueryable dbObjects, XuysSearchParams searchParams) + { + if (searchParams.IsActive.HasValue) + { + dbObjects = dbObjects.Where(item => item.IsActive == searchParams.IsActive.Value); + } + if (!string.IsNullOrEmpty(searchParams.SearchQuery)) + { + dbObjects = dbObjects.Where(item => item.Name.Contains(searchParams.SearchQuery)); + } + return dbObjects.OrderBy(item => item.Id); + } + + protected override async Task> BuildEntitiesListAsync(DefaultDbContext context, IQueryable dbObjects, object? convertParams, bool isFull) + { + return (await dbObjects.ToListAsync()).Select(ConvertDbObjectToEntity).ToList(); + } + + protected override Expression> GetIdByDbObjectExpression() + { + return item => item.Id; + } + + protected override Expression> GetIdByEntityExpression() + { + return item => item.Id; + } + + internal static Entities.Xuy ConvertDbObjectToEntity(Xuy dbObject) + { + if (dbObject == null) throw new ArgumentNullException(nameof(dbObject)); + + return new Entities.Xuy( + dbObject.Id, + dbObject.Name, + dbObject.Description, + dbObject.IsActive + ); + } + } +} diff --git a/DataEncryption/Controllers/XuyController.cs b/DataEncryption/Controllers/XuyController.cs new file mode 100644 index 0000000..3d95330 --- /dev/null +++ b/DataEncryption/Controllers/XuyController.cs @@ -0,0 +1,93 @@ +using BL.Interfaces; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using Common.SearchParams; +using UI.Areas.Public.Models; + +namespace DataEncryption.Controllers +{ + [ApiController] + [Route("api/[controller]")] + [ApiExplorerSettings(GroupName = "Xuys")] + public class XuyController : ControllerBase + { + private readonly IXuysBL _xuysBL; + + public XuyController(IXuysBL xuysBL) + { + _xuysBL = xuysBL; + } + + [Authorize(Roles = "Admin")] + [HttpPost("AddOrUpdateXuy")] + public async Task AddOrUpdateXuy([FromBody] XuyModel model) + { + if (model == null) + { + return BadRequest("Model is null"); + } + + var entity = XuyModel.ToEntity(model); + if (entity == null) + { + return BadRequest("Conversion to entity failed"); + } + + try + { + var id = await _xuysBL.AddOrUpdateAsync(entity); + model.Id = id; + return Ok(model); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + + [HttpGet("GetXuy")] + public async Task GetXuy(int id) + { + try + { + var entity = await _xuysBL.GetAsync(id); + var model = XuyModel.FromEntity(entity); + return Ok(model); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + + [HttpGet("SearchXuys")] + public async Task SearchXuys([FromQuery] XuysSearchParams searchParams) + { + try + { + var result = await _xuysBL.GetAsync(searchParams); + var models = XuyModel.FromEntitiesList(result.Objects); + return Ok(models); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + + [Authorize(Roles = "Admin")] + [HttpDelete("DeleteXuy")] + public async Task DeleteXuy(int id) + { + try + { + await _xuysBL.DeleteAsync(id); + return Ok(); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + } +} diff --git a/DataEncryption/Models/XuyModel.cs b/DataEncryption/Models/XuyModel.cs new file mode 100644 index 0000000..95be7b5 --- /dev/null +++ b/DataEncryption/Models/XuyModel.cs @@ -0,0 +1,42 @@ +using Entities; + +namespace UI.Areas.Public.Models +{ + public class XuyModel + { + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + public string? Description { get; set; } + public bool IsActive { get; set; } + + public static XuyModel? FromEntity(Xuy obj) + { + return obj == null ? null : new XuyModel + { + Id = obj.Id, + Name = obj.Name, + Description = obj.Description, + IsActive = obj.IsActive + }; + } + + public static Xuy? ToEntity(XuyModel obj) + { + return obj == null ? null : new Xuy( + obj.Id, + obj.Name, + obj.Description, + obj.IsActive); + } + + public static List FromEntitiesList(IEnumerable list) + { + return list?.Select(FromEntity).Where(x => x != null).Cast().ToList() ?? new List(); + } + + public static List ToEntitiesList(IEnumerable list) + { + return list?.Select(ToEntity).Where(x => x != null).Cast().ToList() ?? new List(); + } + } +} diff --git a/DataEncryption/Program.cs b/DataEncryption/Program.cs index dbaa99e..a708a1a 100644 --- a/DataEncryption/Program.cs +++ b/DataEncryption/Program.cs @@ -31,6 +31,7 @@ c.SwaggerDoc("SymmetricEncryption", new OpenApiInfo { Title = "Symmetric Encryption API", Version = "v1" }); c.SwaggerDoc("AsymmetricEncryption", new OpenApiInfo { Title = "Asymmetric Encryption API", Version = "v1" }); c.SwaggerDoc("HashEncryption", new OpenApiInfo { Title = "Hash Encryption API", Version = "v1" }); + c.SwaggerDoc("Xuys", new OpenApiInfo { Title = "Xuy API", Version = "v1" }); }); RsaEncryption.GeneratePrivateAndPublicKeys(); @@ -127,7 +128,7 @@ Scheme = "Bearer", BearerFormat = "JWT", In = ParameterLocation.Header, - Description = "Ââåäèòå òîêåí JWT äëÿ àâòîðèçàöèè" + Description = "Ââåäèòå òîêåí JWT äëÿ àâòîðèçàöèè" }); c.AddSecurityRequirement(new OpenApiSecurityRequirement @@ -160,6 +161,7 @@ c.SwaggerEndpoint("/swagger/SymmetricEncryption/swagger.json", "SymmetricEncryption API"); c.SwaggerEndpoint("/swagger/AsymmetricEncryption/swagger.json", "AsymmetricEncryption API"); c.SwaggerEndpoint("/swagger/HashEncryption/swagger.json", "HashEncryption API"); + c.SwaggerEndpoint("/swagger/Xuys/swagger.json", "Xuy API"); }); } else diff --git a/Entities/Xuy.cs b/Entities/Xuy.cs new file mode 100644 index 0000000..40cd0e0 --- /dev/null +++ b/Entities/Xuy.cs @@ -0,0 +1,18 @@ +namespace Entities +{ + public class Xuy + { + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + public string? Description { get; set; } + public bool IsActive { get; set; } + + public Xuy(int id, string name, string? description, bool isActive) + { + Id = id; + Name = name; + Description = description; + IsActive = isActive; + } + } +}