Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions BL/BL.Interfaces/IXuysBL.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Entities;
using Common.SearchParams;

namespace BL.Interfaces
{
public interface IXuysBL : ICrudBL<Xuy, XuysSearchParams, object>
{
}
}
1 change: 1 addition & 0 deletions BL/BL.Standard/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static IServiceCollection AddBusinessLogicLayer(this IServiceCollection s
{
services.AddScoped<IUsersBL, UsersBL>();
services.AddScoped<ITokensBL, TokensBL>();
services.AddScoped<IXuysBL, XuysBL>();

return services;
}
Expand Down
48 changes: 48 additions & 0 deletions BL/BL.Standard/XuysBL.cs
Original file line number Diff line number Diff line change
@@ -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<int> AddOrUpdateAsync(Xuy entity)
{
entity.Id = await _xuysDal.AddOrUpdateAsync(entity);
return entity.Id;
}

public Task<bool> ExistsAsync(int id)
{
return _xuysDal.ExistsAsync(id);
}

public Task<bool> ExistsAsync(XuysSearchParams searchParams)
{
return _xuysDal.ExistsAsync(searchParams);
}

public Task<Xuy> GetAsync(int id, object? convertParams = null)
{
return _xuysDal.GetAsync(id, convertParams);
}

public Task<bool> DeleteAsync(int id)
{
return _xuysDal.DeleteAsync(id);
}

public Task<SearchResult<Xuy>> GetAsync(XuysSearchParams searchParams, object? convertParams = null)
{
return _xuysDal.GetAsync(searchParams, convertParams);
}
}
}
13 changes: 13 additions & 0 deletions Common/SearchParams/XuysSearchParams.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
6 changes: 6 additions & 0 deletions Dal/Dal.DbModels/DefaultDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(optio

public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Token> Tokens { get; set; }
public virtual DbSet<Xuy> Xuys { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand All @@ -28,6 +29,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.Property(e => e.RegistrationDate).HasColumnType("datetime");
});

modelBuilder.Entity<Xuy>(entity =>
{
entity.Property(e => e.Name).IsRequired();
});

OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
Expand Down
10 changes: 10 additions & 0 deletions Dal/Dal.DbModels/Xuy.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
9 changes: 9 additions & 0 deletions Dal/Dal.Interfaces/IXuysDal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Common.SearchParams;
using Dal.DbModels;

namespace Dal.Interfaces
{
public interface IXuysDal : IBaseDal<DefaultDbContext, Xuy, Entities.Xuy, int, XuysSearchParams, object>
{
}
}
1 change: 1 addition & 0 deletions Dal/Dal.SQL/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static IServiceCollection AddDataAccessLayer(this IServiceCollection serv
services.AddDbContext<DefaultDbContext>(config => config.UseNpgsql(configuration["ConnectionStrings:DefaultConnectionString"]));
services.AddScoped<IUsersDal, UsersDal>();
services.AddScoped<ITokensDal, TokensDal>();
services.AddScoped<IXuysDal, XuysDal>();

return services;
}
Expand Down
63 changes: 63 additions & 0 deletions Dal/Dal.SQL/XuysDal.cs
Original file line number Diff line number Diff line change
@@ -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<DefaultDbContext, Xuy, Entities.Xuy, int, XuysSearchParams, object>, 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<Xuy> BuildDbQuery(DefaultDbContext context, IQueryable<Xuy> 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<IList<Entities.Xuy>> BuildEntitiesListAsync(DefaultDbContext context, IQueryable<Xuy> dbObjects, object? convertParams, bool isFull)
{
return (await dbObjects.ToListAsync()).Select(ConvertDbObjectToEntity).ToList();
}

protected override Expression<Func<Xuy, int>> GetIdByDbObjectExpression()
{
return item => item.Id;
}

protected override Expression<Func<Entities.Xuy, int>> 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
);
}
}
}
93 changes: 93 additions & 0 deletions DataEncryption/Controllers/XuyController.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> DeleteXuy(int id)
{
try
{
await _xuysBL.DeleteAsync(id);
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
}
42 changes: 42 additions & 0 deletions DataEncryption/Models/XuyModel.cs
Original file line number Diff line number Diff line change
@@ -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<XuyModel> FromEntitiesList(IEnumerable<Xuy> list)
{
return list?.Select(FromEntity).Where(x => x != null).Cast<XuyModel>().ToList() ?? new List<XuyModel>();
}

public static List<Xuy> ToEntitiesList(IEnumerable<XuyModel> list)
{
return list?.Select(ToEntity).Where(x => x != null).Cast<Xuy>().ToList() ?? new List<Xuy>();
}
}
}
4 changes: 3 additions & 1 deletion DataEncryption/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -127,7 +128,7 @@
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "������� ����� JWT ��� �����������"
Description = "Ââåäèòå òîêåí JWT äëÿ àâòîðèçàöèè"
});

c.AddSecurityRequirement(new OpenApiSecurityRequirement
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions Entities/Xuy.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}