first commit
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using OpenWarehouse.auth.Common.ApiInterfeiceLib;
|
||||
using OpenWarehouse.auth.lib;
|
||||
|
||||
namespace OpenWarehouse.warehouse.api.Common.CustimIdentifyManager;
|
||||
|
||||
public class HasPrivilage : Attribute, IAsyncAuthorizationFilter
|
||||
{
|
||||
public ServicePrivilage Privilage;
|
||||
|
||||
public HasPrivilage(ServicePrivilage privilage)
|
||||
{
|
||||
Privilage = privilage;
|
||||
}
|
||||
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
|
||||
{
|
||||
context.HttpContext.Request.Headers.TryGetValue("Authorization", out var token);
|
||||
|
||||
var sToken = token.ToString();
|
||||
|
||||
if (sToken == "") context.Result = new UnauthorizedResult();
|
||||
|
||||
var config = context.HttpContext.RequestServices.GetRequiredService<IConfiguration>();
|
||||
var handler = new AuthTokenHandler(sToken);
|
||||
var httpClient = new HttpClient(handler);
|
||||
var client = new OpenWarehouseAuthClient(config["Auth:Url"], httpClient);
|
||||
|
||||
var privilage = await client.GetUserPrivilagesAsync();
|
||||
if (privilage is {Count: 0} && !privilage.Any(s => s.Equals(Privilage.GetName())))
|
||||
{
|
||||
context.Result = new ForbidResult();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.CustimIdentifyManager;
|
||||
|
||||
public enum ServicePrivilage
|
||||
{
|
||||
Admin, ServiceAdmin, ManageWarehouse, EditWarehouse, AddWarehouse, DeleteWarehouse, ManageItems, EditItems,
|
||||
AddItems, DeleteItems, ManageInvetory, EditInventory, AddItemToInventory, SeeInventory, ManageServicePrivilage, ServiceUser
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.CustimIdentifyManager;
|
||||
|
||||
public static class ServicePrivilageClass
|
||||
{
|
||||
private static readonly string PrivilagePrefix = "WarehouseApi";
|
||||
private static readonly Dictionary<ServicePrivilage, string> PrivilageNameMap = new()
|
||||
{
|
||||
{ ServicePrivilage.Admin, "Admin" },
|
||||
{ ServicePrivilage.ServiceAdmin, $"{PrivilagePrefix}-Admin" },
|
||||
{ ServicePrivilage.ManageWarehouse, $"{PrivilagePrefix}-Warehouse-Manage" },
|
||||
{ ServicePrivilage.EditWarehouse, $"{PrivilagePrefix}-Warehouse-Edit" },
|
||||
{ ServicePrivilage.AddWarehouse, $"{PrivilagePrefix}-Warehouse-Add" },
|
||||
{ ServicePrivilage.DeleteWarehouse, $"{PrivilagePrefix}-Warehouse-Delete" },
|
||||
{ ServicePrivilage.ManageItems, $"{PrivilagePrefix}-Items-Manage" },
|
||||
{ ServicePrivilage.EditItems, $"{PrivilagePrefix}-Items-Edit" },
|
||||
{ ServicePrivilage.AddItems, $"{PrivilagePrefix}-Items-Add" },
|
||||
{ ServicePrivilage.DeleteItems, $"{PrivilagePrefix}-Items-Delete" },
|
||||
{ ServicePrivilage.ManageInvetory, $"{PrivilagePrefix}-Inventory-mange"},
|
||||
{ ServicePrivilage.EditInventory, $"{PrivilagePrefix}-Inventory-Edit"},
|
||||
{ ServicePrivilage.AddItemToInventory, $"{PrivilagePrefix}-Inventory-Add-Item"},
|
||||
{ ServicePrivilage.SeeInventory, $"{PrivilagePrefix}-Inventory-See"},
|
||||
{ ServicePrivilage.ServiceUser, $"{PrivilagePrefix}-User"}
|
||||
};
|
||||
|
||||
private static readonly Dictionary<ServicePrivilage, List<ServicePrivilage>> PrivilageMap = new()
|
||||
{
|
||||
{ ServicePrivilage.Admin, [ServicePrivilage.Admin] },
|
||||
{ ServicePrivilage.ServiceAdmin, [ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin] },
|
||||
{
|
||||
ServicePrivilage.ManageWarehouse,
|
||||
[ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageWarehouse]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.EditWarehouse,
|
||||
[
|
||||
ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageWarehouse,
|
||||
ServicePrivilage.EditWarehouse
|
||||
]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.AddWarehouse,
|
||||
[
|
||||
ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageWarehouse,
|
||||
ServicePrivilage.AddWarehouse
|
||||
]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.DeleteWarehouse,
|
||||
[
|
||||
ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageWarehouse,
|
||||
ServicePrivilage.DeleteWarehouse
|
||||
]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.ManageItems,
|
||||
[ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageItems]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.EditItems,
|
||||
[
|
||||
ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageItems,
|
||||
ServicePrivilage.EditItems
|
||||
]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.AddItems,
|
||||
[
|
||||
ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageItems,
|
||||
ServicePrivilage.AddItems
|
||||
]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.DeleteItems,
|
||||
[
|
||||
ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageItems,
|
||||
ServicePrivilage.DeleteItems
|
||||
]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.ManageInvetory,
|
||||
[ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageInvetory]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.EditInventory,
|
||||
[
|
||||
ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageInvetory,
|
||||
ServicePrivilage.EditInventory
|
||||
]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.AddItemToInventory,
|
||||
[
|
||||
ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageInvetory,
|
||||
ServicePrivilage.AddItemToInventory
|
||||
]
|
||||
},
|
||||
{
|
||||
ServicePrivilage.SeeInventory,
|
||||
[
|
||||
ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageInvetory,
|
||||
ServicePrivilage.SeeInventory
|
||||
]
|
||||
},
|
||||
{ ServicePrivilage.ServiceUser, GetAllPrivilageList() }
|
||||
};
|
||||
|
||||
public static List<ServicePrivilage> GetAllPrivilageList()
|
||||
{
|
||||
var list = PrivilageNameMap.Select(pair => pair.Key).ToList();
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<ServicePrivilage> GetServicePrivilageList()
|
||||
{
|
||||
var list = PrivilageNameMap.Where(pair => pair.Value.Contains(PrivilagePrefix))
|
||||
.Select(pair => pair.Key).ToList();
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<string> GetServicePrivilageNameList()
|
||||
{
|
||||
var list = PrivilageNameMap.Where(pair => pair.Value.Contains(PrivilagePrefix))
|
||||
.Select(pair => pair.Value).ToList();
|
||||
return list;
|
||||
}
|
||||
|
||||
public static string GetName(this ServicePrivilage servicePrivilage)
|
||||
{
|
||||
var result = PrivilageNameMap.TryGetValue(servicePrivilage, out var name);
|
||||
return (result ? name : $"Uknow-{servicePrivilage}") ?? $"Role {servicePrivilage} Uknow";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
using System.Net.Http.Headers;
|
||||
using OpenWarehouse.auth.Common.ApiInterfeiceLib;
|
||||
using OpenWarehouse.auth.lib;
|
||||
using OpenWarehouse.auth.lib.Model.Role;
|
||||
using OpenWarehouse.auth.lib.Model.Service;
|
||||
using OpenWarehouse.warehouse.api.Common.CustimIdentifyManager;
|
||||
using OpenWarehouse.warehouse.api.Common.ServiceClaims;
|
||||
|
||||
namespace OpenWarehouse.warehouse.api.Common.DefaultServiceAccountAndRole;
|
||||
|
||||
public class DefaultIdentify(ILogger<DefaultIdentify> logger, IConfiguration configuration,
|
||||
IServiceClaimsManager serviceClaimsManager)
|
||||
{
|
||||
|
||||
private List<RoleModel> _listOfRole = new()
|
||||
{
|
||||
new RoleModel()
|
||||
{
|
||||
RoleName = ServicePrivilage.ServiceAdmin.GetName(),
|
||||
Privilage = new()
|
||||
{
|
||||
ServicePrivilage.ServiceAdmin.GetName(),
|
||||
ServicePrivilage.ServiceUser.GetName(),
|
||||
}
|
||||
},
|
||||
new RoleModel()
|
||||
{
|
||||
RoleName = ServicePrivilage.ServiceUser.GetName(),
|
||||
Privilage = new()
|
||||
{
|
||||
ServicePrivilage.ServiceUser.GetName(),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
public async Task<bool> TestAuth(string token)
|
||||
{
|
||||
var handler = new AuthTokenHandler(token);
|
||||
var httpClient = new HttpClient(handler);
|
||||
var client = new OpenWarehouseAuthClient(configuration["Auth:Url"], httpClient);
|
||||
|
||||
try
|
||||
{
|
||||
return await client.TestServiceTokenAsync();
|
||||
}
|
||||
catch (ApiException e)
|
||||
{
|
||||
if (e.StatusCode == 401) return false;
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<CreateServiceTokenReturnModel> SeedAsync(string adminToken)
|
||||
{
|
||||
var httpClient = new HttpClient();
|
||||
var token = adminToken.Split(" ");
|
||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token[0], token[1]);
|
||||
var client = new OpenWarehouseAuthClient(configuration["Auth:Url"], httpClient);
|
||||
|
||||
try
|
||||
{
|
||||
var service = new CreateServiceTokenModel()
|
||||
{
|
||||
ServiceName = "Warehouse",
|
||||
ServicePrivilages = ServicePrivilageClass.GetServicePrivilageNameList(),
|
||||
ServiceRoles = _listOfRole
|
||||
};
|
||||
var result = await client.GetTokenForServiceAsync(service);
|
||||
|
||||
logger.LogInformation($"Auth service initialized: \n " +
|
||||
$"Is success: {result.IsSuccess} \n " +
|
||||
$"Service username: {result.ServiceUsername} \n " +
|
||||
$"Service allerady exist: {result.UserAllereadyExist}");
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (ApiException e)
|
||||
{
|
||||
logger.LogWarning($"ApiException with code {e.StatusCode} and nessage {e.Message}");
|
||||
throw;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
logger.LogWarning(e.Message);
|
||||
throw;
|
||||
}
|
||||
|
||||
throw new Exception("This exemption shoudnt happen");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Inventory;
|
||||
|
||||
public interface IInventoryManger
|
||||
{
|
||||
public Task<List<ItemInWarehouse>> GetListOfItemInWarehouse(Data.Item item);
|
||||
public Task<ItemInWarehouse?> GetPositionById(string id);
|
||||
public Task<List<ItemInWarehouse>> GetPositionByPosition(string position);
|
||||
public Task<bool> CreateAsync(ItemInWarehouse itemInWarehouse);
|
||||
public Task<bool> AddClaims(ItemInWarehouse itemInWarehouse, List<Claim> claims);
|
||||
public Task<bool> AddClaim(ItemInWarehouse itemInWarehouse, Claim claim);
|
||||
public Task<bool> DeleteClaim(ItemInWarehouse itemInWarehouse, string claimType);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System.Data;
|
||||
using OpenWarehouse.warehouse.api.Common.Warehouse;
|
||||
|
||||
namespace OpenWarehouse.warehouse.api.Common.Inventory;
|
||||
|
||||
public class InventoryManger(ILogger<InventoryManger> logger,
|
||||
ApplicationDbContext context, IWarehouseManager warehouseManager,
|
||||
IItemManager itemManager) : IInventoryManger
|
||||
{
|
||||
public async Task<List<ItemInWarehouse>> GetListOfItemInWarehouse(Data.Item item)
|
||||
{
|
||||
return await context.ItemInWarehouses
|
||||
.Where(q => q.Item == item)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ItemInWarehouse?> GetPositionById(string id)
|
||||
{
|
||||
return await context.ItemInWarehouses
|
||||
.Where(q => q.Id == Guid.Parse(id))
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<List<ItemInWarehouse>> GetPositionByPosition(string position)
|
||||
{
|
||||
return await context.ItemInWarehouses
|
||||
.Where(q => q.Positions == position)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> CreateAsync(ItemInWarehouse itemInWarehouse)
|
||||
{
|
||||
context.ItemInWarehouses.Add(itemInWarehouse);
|
||||
|
||||
var res = await context.SaveChangesAsync();
|
||||
|
||||
return res > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> AddClaims(ItemInWarehouse itemInWarehouse, List<Claim> claims)
|
||||
{
|
||||
foreach (Claim claim in claims)
|
||||
{
|
||||
if (itemInWarehouse.ItemInWarehouseClaims.Any(q => q.ClaimType == claim.Type))
|
||||
{
|
||||
var itemInWarehouseClaims = itemInWarehouse.ItemInWarehouseClaims
|
||||
.FirstOrDefault(warehouseClaims => warehouseClaims.ClaimType == claim.Type);
|
||||
if (itemInWarehouseClaims == null) return false;
|
||||
itemInWarehouseClaims.ClaimValue = claim.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
itemInWarehouse.ItemInWarehouseClaims.Add(new ItemInWarehouseClaims(){ClaimType = claim.Type, ClaimValue = claim.Value});
|
||||
}
|
||||
}
|
||||
|
||||
var res = await context.SaveChangesAsync();
|
||||
|
||||
return res == claims.Count;
|
||||
}
|
||||
|
||||
public async Task<bool> AddClaim(ItemInWarehouse itemInWarehouse, Claim claim)
|
||||
{
|
||||
if (itemInWarehouse.ItemInWarehouseClaims.Any(q => q.ClaimType == claim.Type))
|
||||
{
|
||||
var itemInWarehouseClaims = itemInWarehouse.ItemInWarehouseClaims
|
||||
.FirstOrDefault(warehouseClaims => warehouseClaims.ClaimType == claim.Type);
|
||||
if (itemInWarehouseClaims == null) return false;
|
||||
itemInWarehouseClaims.ClaimValue = claim.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
itemInWarehouse.ItemInWarehouseClaims.Add(new ItemInWarehouseClaims(){ClaimType = claim.Type, ClaimValue = claim.Value});
|
||||
}
|
||||
|
||||
var res = await context.SaveChangesAsync();
|
||||
return res > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteClaim(ItemInWarehouse itemInWarehouse, string claimType)
|
||||
{
|
||||
var res = await context.ItemInWarehousesClaims
|
||||
.Where(q => q.ClaimType == claimType && q.Item == itemInWarehouse)
|
||||
.ExecuteDeleteAsync();
|
||||
if (res > 1) throw new DataException("Deleted more than one record!!");
|
||||
return res == 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Item;
|
||||
|
||||
public interface IItemManager
|
||||
{
|
||||
public Task<Data.Item?> FindItemByIdAsync(string id);
|
||||
public Task<Data.Item?> FindItemByNameAsync(string producerName, string itemName);
|
||||
public Task<Data.Item?> FindItemByNameAsync(Producers producers, string itemName);
|
||||
public Task<ActionResult> CreateAsync(Data.Item item);
|
||||
public Task<List<Claim>> GetListOfClaims(Data.Item item);
|
||||
public Task<ActionResult> AddClaim(Data.Item item, Claim claim);
|
||||
public Task<ActionResult> UpdateClaim(Data.Item item, Claim claim);
|
||||
public Task<ActionResult> DeleteClaim(Data.Item item, string claimType);
|
||||
public Task<ActionResult> EditClaim(Data.Item item, string claimType, string newValue);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Item;
|
||||
|
||||
public enum ItemClaimsType
|
||||
{
|
||||
Exp, DateOfProduction, Description, Price, ManufactoreContry
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Item;
|
||||
|
||||
public static class ItemClaimsTypeClass
|
||||
{
|
||||
private static readonly Dictionary<ItemClaimsType, string> ItemClaimType = new()
|
||||
{
|
||||
{ ItemClaimsType.Exp, "Exp" },
|
||||
{ ItemClaimsType.DateOfProduction, "Date_of_production" },
|
||||
{ ItemClaimsType.Price, "Price" },
|
||||
{ ItemClaimsType.Description, "Description" },
|
||||
{ ItemClaimsType.ManufactoreContry, "Manufactore_Country" },
|
||||
};
|
||||
|
||||
public static string GetName(this ItemClaimsType itemClaim)
|
||||
{
|
||||
var result = ItemClaimType.TryGetValue(itemClaim, out var name);
|
||||
|
||||
if (!result || name == null) throw new Exception("Key not found");
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
public static Claim GerClaims(this ItemClaimsType itemClaim, string value, string? issuer = null)
|
||||
{
|
||||
var result = ItemClaimType.TryGetValue(itemClaim, out var name);
|
||||
|
||||
if (!result || name == null) throw new Exception("Key not found");
|
||||
|
||||
Claim claim;
|
||||
|
||||
if (issuer != null) claim = new Claim(name, value, issuer);
|
||||
else claim = new Claim(name, value);
|
||||
|
||||
return claim;
|
||||
}
|
||||
|
||||
public static List<string> GetAllClaimName()
|
||||
{
|
||||
return ItemClaimType
|
||||
.Select(q => q.Value)
|
||||
.ToList();
|
||||
|
||||
}
|
||||
|
||||
public static bool IsDefaultClaimType(string val)
|
||||
{
|
||||
var res = ItemClaimType
|
||||
.Any(q => q.Value == val);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
using OpenWarehouse.warehouse.api.Common.Producer;
|
||||
|
||||
namespace OpenWarehouse.warehouse.api.Common.Item;
|
||||
|
||||
public class ItemManager(ApplicationDbContext context, IProducerManager producerManager) : IItemManager
|
||||
{
|
||||
public async Task<Data.Item?> FindItemByIdAsync(string id)
|
||||
{
|
||||
var result = await context.Items
|
||||
.Include(item => item.ItemCategoryTags)
|
||||
.Include(item => item.ItemClaims)
|
||||
.FirstOrDefaultAsync(item => item.Id != null && item.Id.Equals(id));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Data.Item?> FindItemByNameAsync(string producerName, string itemName)
|
||||
{
|
||||
var producer = await producerManager.FindProducerByName(producerName);
|
||||
if (producer != null)
|
||||
{
|
||||
return await FindItemByNameAsync(producer, itemName);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<Data.Item?> FindItemByNameAsync(Producers producers, string itemName)
|
||||
{
|
||||
return await context.Items
|
||||
.FirstOrDefaultAsync(item => item.Name != null &&
|
||||
item.Producer == producers && item.Name.Equals(itemName));
|
||||
}
|
||||
|
||||
public async Task<ActionResult> CreateAsync(Data.Item item)
|
||||
{
|
||||
context.Items.Add(item);
|
||||
|
||||
ActionResult? res = null;
|
||||
|
||||
var error = new Dictionary<string, string>();
|
||||
|
||||
try
|
||||
{
|
||||
res= new ActionResult((await context.SaveChangesAsync()) == 1);
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
|
||||
return res ?? new ActionResult(false, error);
|
||||
}
|
||||
|
||||
public async Task<List<Claim>> GetListOfClaims(Data.Item item)
|
||||
{
|
||||
var result = await context.ItemClaims.Where(claims => claims.Item == item)
|
||||
.Select(claims => new Claim(claims.ClaimType ?? "Uknow", claims.ClaimValue ?? "Uknow"))
|
||||
.ToListAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<ActionResult> AddClaim(Data.Item item, Claim claim)
|
||||
{
|
||||
var newClaim = new ItemClaims(item, claim);
|
||||
context.ItemClaims.Add(newClaim);
|
||||
|
||||
ActionResult? res = null;
|
||||
|
||||
var error = new Dictionary<string, string>();
|
||||
|
||||
try
|
||||
{
|
||||
res= new ActionResult((await context.SaveChangesAsync()) == 1);
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
|
||||
return res ?? new ActionResult(false, error);
|
||||
}
|
||||
|
||||
public async Task<ActionResult> UpdateClaim(Data.Item item, Claim claim)
|
||||
{
|
||||
var claimF = await context.ItemClaims
|
||||
.FirstOrDefaultAsync(q => q.ClaimType.Equals(claim.Type) && q.ClaimIssuer.Equals(claim.Issuer)
|
||||
&& q.Item == item);
|
||||
var result = 0;
|
||||
|
||||
if (claimF == null)
|
||||
{
|
||||
result = (await AddClaim(item, claim)).IsSuccess ? 0 : 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
claimF.ClaimValue = claim.Value;
|
||||
result= await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
return result > 0 ? new ActionResult(true, null) : new ActionResult(false, null);
|
||||
}
|
||||
|
||||
public async Task<ActionResult> DeleteClaim(Data.Item item, string claimType)
|
||||
{
|
||||
ActionResult? res = null;
|
||||
|
||||
var error = new Dictionary<string, string>();
|
||||
int delCount = 0;
|
||||
|
||||
try
|
||||
{
|
||||
delCount = await context.ItemClaims
|
||||
.Where(claims => claims.ClaimType != null && claims.Item == item && claims.ClaimType.Equals(claimType))
|
||||
.ExecuteDeleteAsync();
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
res = new ActionResult(delCount == 1, error);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public async Task<ActionResult> EditClaim(Data.Item item, string claimType, string newValue)
|
||||
{
|
||||
ActionResult? res = null;
|
||||
|
||||
var error = new Dictionary<string, string>();
|
||||
int editCount = 0;
|
||||
|
||||
try
|
||||
{
|
||||
var claim = await context.ItemClaims.FirstOrDefaultAsync(claims =>
|
||||
claims.ClaimType != null && claims.Item == item && claims.ClaimType.Equals(claimType));
|
||||
if (claim != null) claim.ClaimValue = newValue;
|
||||
else
|
||||
{
|
||||
error["Database"] = $"Claim {item.Name}-{claimType} not found.";
|
||||
}
|
||||
|
||||
editCount = await context.SaveChangesAsync();
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
res = new ActionResult(editCount == 1, error);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using OpenWarehouse.warehouse.api.Services;
|
||||
|
||||
namespace OpenWarehouse.warehouse.api.Common.ItemTagManager;
|
||||
|
||||
public interface IItemTagManager
|
||||
{
|
||||
public Task<bool> CreateTag(string name, Guid? parentId);
|
||||
public Task<ItemCategoryTag?> GetTagByName(string name);
|
||||
public Task<ItemCategoryTag?> GetTagById(Guid id);
|
||||
public Task<ItemCategoryTag?> GetTagById(string id);
|
||||
public Task<bool> DeleteTag(ItemCategoryTag tag);
|
||||
public Task<bool> DeleteTag(Guid id);
|
||||
public Task<int> DeleteTagsByNames(List<Model.Item.ItemCategoryTag> names);
|
||||
public Task<bool> DeleteTag(String id);
|
||||
public Task<int> CreateTagTree(List<Model.Item.ItemCategoryTag> itemCategoryTags);
|
||||
public Task<bool> DeleteAllChild(Guid id);
|
||||
public Task<bool> DeleteAllChild(String id);
|
||||
public Task<ItemCategoryTag?> GetTagTree(string root);
|
||||
public Task<List<ItemCategoryTag>> GetFullTagTree();
|
||||
}
|
||||
@@ -0,0 +1,339 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.ItemTagManager;
|
||||
|
||||
public class ItemTagManager(ApplicationDbContext applicationDbContext, ILogger<ItemManager> logger) : IItemTagManager
|
||||
{
|
||||
public async Task<bool> CreateTag(string name, Guid? parentId)
|
||||
{
|
||||
logger.LogDebug("Attempting to create a new tag with name {TagName} and parent ID {ParentId}", name, parentId);
|
||||
|
||||
ItemCategoryTag? parent = null;
|
||||
|
||||
if (parentId != null)
|
||||
{
|
||||
parent = await applicationDbContext.ItemCategoryTags
|
||||
.FirstOrDefaultAsync(q => q.Id == parentId);
|
||||
|
||||
if (parent == null)
|
||||
{
|
||||
logger.LogWarning("Parent tag with ID {ParentId} not found.", parentId);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var newTag = new ItemCategoryTag()
|
||||
{
|
||||
Name = name,
|
||||
ParentTag = parent
|
||||
};
|
||||
|
||||
applicationDbContext.ItemCategoryTags.Add(newTag);
|
||||
|
||||
bool isSaved = (await applicationDbContext.SaveChangesAsync()) > 0;
|
||||
|
||||
if (isSaved)
|
||||
{
|
||||
logger.LogInformation("Successfully created tag {Name} with ID {TagId}.", name, newTag.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogError("Failed to save the new tag {Name}.", name);
|
||||
}
|
||||
|
||||
return isSaved;
|
||||
}
|
||||
|
||||
public async Task<ItemCategoryTag?> GetTagByName(string name)
|
||||
{
|
||||
logger.LogDebug("Fetching tags with name {TagName}.", name);
|
||||
|
||||
var tags = await applicationDbContext.ItemCategoryTags
|
||||
.Where(tag => tag.Name != null && tag.Name.Equals(name)).FirstOrDefaultAsync();
|
||||
|
||||
if (tags == null)
|
||||
{
|
||||
logger.LogWarning("No tags found with name {Name}.", name);
|
||||
}
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
public async Task<ItemCategoryTag?> GetTagById(Guid id)
|
||||
{
|
||||
logger.LogDebug("Fetching tag with ID {TagId}.", id);
|
||||
|
||||
var tag = await applicationDbContext.ItemCategoryTags
|
||||
.Where(tag => tag.Id == id).FirstOrDefaultAsync();
|
||||
|
||||
if (tag == null)
|
||||
{
|
||||
logger.LogWarning("No tag found with ID {Id}.", id);
|
||||
}
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
public async Task<ItemCategoryTag?> GetTagById(string id)
|
||||
{
|
||||
logger.LogDebug("Fetching tag with string ID {TagId}.", id);
|
||||
return await GetTagById(Guid.Parse(id));
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteTag(ItemCategoryTag tag)
|
||||
{
|
||||
logger.LogDebug("Attempting to delete tag with object reference {Tag}.", tag);
|
||||
return await DeleteTag(tag.Id);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteTag(Guid id)
|
||||
{
|
||||
logger.LogDebug("Attempting to delete tag with ID {TagId}.", id);
|
||||
|
||||
bool isDeleted = await applicationDbContext.ItemCategoryTags.Where(q => q.Id == id)
|
||||
.ExecuteDeleteAsync() > 0;
|
||||
|
||||
if (isDeleted)
|
||||
{
|
||||
logger.LogInformation("Successfully deleted tag with ID {TagId}.", id);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogError("Failed to delete tag with ID {TagId}.", id);
|
||||
}
|
||||
|
||||
return isDeleted;
|
||||
}
|
||||
|
||||
public async Task<int> DeleteTagsByNames(List<Model.Item.ItemCategoryTag> names)
|
||||
{
|
||||
logger.LogDebug("Attempting to delete tags by names.");
|
||||
|
||||
if (!names.Any())
|
||||
{
|
||||
logger.LogWarning("No tags provided for deletion.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var res = 0;
|
||||
|
||||
foreach (var tag in names)
|
||||
{
|
||||
logger.LogDebug("Deleting tag with name {TagName}.", tag.Name);
|
||||
res += await applicationDbContext.ItemCategoryTags
|
||||
.Where(q => q.Name == tag.Name)
|
||||
.ExecuteDeleteAsync();
|
||||
}
|
||||
|
||||
logger.LogInformation("{Count} tags were successfully deleted.", res);
|
||||
return res;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "An error occurred while deleting tags.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteTag(string id)
|
||||
{
|
||||
logger.LogDebug("Attempting to delete tag with string ID {TagId}.", id);
|
||||
return await DeleteTag(Guid.Parse(id));
|
||||
}
|
||||
|
||||
public async Task<int> CreateTagTree(List<Model.Item.ItemCategoryTag> itemCategoryTags)
|
||||
{
|
||||
logger.LogDebug("Attempting to create a tag tree.");
|
||||
|
||||
var result = 0;
|
||||
try
|
||||
{
|
||||
if (itemCategoryTags.All(q => q.ParentName != null))
|
||||
{
|
||||
logger.LogError("Tag tree must have at least one root node.");
|
||||
throw new InvalidOperationException("Tag tree must have at least one root node.");
|
||||
}
|
||||
|
||||
var tempParent = new List<ItemCategoryTag>();
|
||||
|
||||
while (itemCategoryTags.Count > 0)
|
||||
{
|
||||
var currentLevel = itemCategoryTags
|
||||
.Where(q => q.ParentName == null || itemCategoryTags.All(qq => qq.Name != q.ParentName))
|
||||
.ToList();
|
||||
|
||||
itemCategoryTags.RemoveAll(q => currentLevel.Contains(q));
|
||||
|
||||
foreach (var tag in currentLevel)
|
||||
{
|
||||
logger.LogDebug("Processing tag {TagName} with parent {ParentName}.", tag.Name, tag.ParentName);
|
||||
|
||||
var exists = await applicationDbContext.ItemCategoryTags
|
||||
.AnyAsync(t => t.Name == tag.Name && t.ParentTag != null && (t.ParentTag.Name == tag.ParentName ||
|
||||
(t.ParentTag == null && tag.ParentName == null)));
|
||||
|
||||
if (exists)
|
||||
{
|
||||
logger.LogInformation("Tag with name {TagName} already exists.", tag.Name);
|
||||
continue;
|
||||
}
|
||||
|
||||
ItemCategoryTag? parent = null;
|
||||
|
||||
if (tag.ParentName != null)
|
||||
{
|
||||
parent = await applicationDbContext.ItemCategoryTags
|
||||
.Where(q => q.Name == tag.ParentName).FirstOrDefaultAsync();
|
||||
|
||||
if (parent == null)
|
||||
{
|
||||
if (tempParent.Any(q => q.Name == tag.ParentName))
|
||||
{
|
||||
parent = tempParent.FirstOrDefault(q => q.Name == tag.ParentName);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogWarning("Parent tag with name {ParentName} not found for child {Name}.",
|
||||
tag.ParentName, tag.Name);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var tagNew = new ItemCategoryTag()
|
||||
{
|
||||
Name = tag.Name,
|
||||
ParentTag = parent
|
||||
};
|
||||
|
||||
applicationDbContext.ItemCategoryTags.Add(tagNew);
|
||||
tempParent.Add(tagNew);
|
||||
|
||||
logger.LogDebug("Added new tag {TagName} with parent {ParentName}.", tagNew.Name,
|
||||
parent?.Name);
|
||||
}
|
||||
|
||||
result += await applicationDbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
logger.LogInformation("Successfully created tag tree with {Changes} changes.", result);
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "An error occurred while creating the tag tree.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAllChild(Guid id)
|
||||
{
|
||||
logger.LogDebug("Attempting to delete all children for tag ID {TagId}.", id);
|
||||
|
||||
var parentTag = await GetTagById(id);
|
||||
if (parentTag == null)
|
||||
{
|
||||
logger.LogWarning("Parent tag with ID {Id} not found.", id);
|
||||
return false;
|
||||
}
|
||||
|
||||
var childTags = await applicationDbContext.ItemCategoryTags
|
||||
.Where(q => q.ParentTagId == id).ToListAsync();
|
||||
|
||||
if (!childTags.Any())
|
||||
{
|
||||
logger.LogInformation("No child tags found for parent tag with ID {Id}.", id);
|
||||
return true;
|
||||
}
|
||||
|
||||
applicationDbContext.ItemCategoryTags.RemoveRange(childTags);
|
||||
bool isDeleted = (await applicationDbContext.SaveChangesAsync()) > 0;
|
||||
|
||||
if (isDeleted)
|
||||
{
|
||||
logger.LogInformation("Successfully deleted all child tags for parent tag with ID {Id}.", id);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogError("Failed to delete child tags for parent tag with ID {Id}.", id);
|
||||
}
|
||||
|
||||
return isDeleted;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAllChild(string id)
|
||||
{
|
||||
logger.LogDebug("Attempting to delete all children for tag string ID {TagId}.", id);
|
||||
return await DeleteAllChild(Guid.Parse(id));
|
||||
}
|
||||
|
||||
public async Task<ItemCategoryTag?> GetTagTree(string root)
|
||||
{
|
||||
logger.LogDebug("Fetching tag tree starting from root {RootName}.", root);
|
||||
|
||||
var rootElement = await applicationDbContext.ItemCategoryTags
|
||||
.Where(q => q.Name != null && q.Name.Equals(root))
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (rootElement == null)
|
||||
{
|
||||
logger.LogWarning("Root tag with name {RootName} not found.", root);
|
||||
return null;
|
||||
}
|
||||
|
||||
await LoadChildren(rootElement);
|
||||
|
||||
return rootElement;
|
||||
}
|
||||
|
||||
public async Task<List<ItemCategoryTag>> GetFullTagTree()
|
||||
{
|
||||
logger.LogDebug("Fetching the full tag tree.");
|
||||
|
||||
var allTags = await applicationDbContext.ItemCategoryTags
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
|
||||
var tagDictionary = allTags.ToDictionary(tag => tag.Id);
|
||||
|
||||
var rootTags = new List<ItemCategoryTag>();
|
||||
|
||||
foreach (var tag in allTags)
|
||||
{
|
||||
if (tag.ParentTagId == null)
|
||||
{
|
||||
rootTags.Add(tag);
|
||||
}
|
||||
else if (tagDictionary.TryGetValue(tag.ParentTagId.Value, out var parentTag))
|
||||
{
|
||||
if (parentTag.ListOfChildrenTag == null)
|
||||
{
|
||||
parentTag.ListOfChildrenTag = new List<ItemCategoryTag>();
|
||||
}
|
||||
|
||||
parentTag.ListOfChildrenTag.Add(tag);
|
||||
}
|
||||
}
|
||||
|
||||
logger.LogInformation("Successfully fetched the full tag tree with {Count} root nodes.", rootTags.Count);
|
||||
|
||||
return rootTags;
|
||||
}
|
||||
|
||||
private async Task LoadChildren(ItemCategoryTag parentTag)
|
||||
{
|
||||
logger.LogDebug("Loading children for tag {TagName} with ID {TagId}.", parentTag.Name, parentTag.Id);
|
||||
|
||||
parentTag.ListOfChildrenTag = await applicationDbContext.ItemCategoryTags
|
||||
.Where(child => child.ParentTagId == parentTag.Id)
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var child in parentTag.ListOfChildrenTag)
|
||||
{
|
||||
await LoadChildren(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Kafka;
|
||||
|
||||
public enum Actions
|
||||
{
|
||||
Create, Update, Delete, CheckIntegrality
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Kafka.Item;
|
||||
|
||||
public interface IItemKafkaMessage
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Kafka.Item;
|
||||
|
||||
public class ItemKafkaMessage
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Kafka;
|
||||
|
||||
public class KafkaConfig
|
||||
{
|
||||
public string? BootstrapServers { get; set; }
|
||||
public string? TopicRoot { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using ItemCategoryTag = OpenWarehouse.warehouse.api.Model.Item.ItemCategoryTag;
|
||||
|
||||
namespace OpenWarehouse.warehouse.api.Common.Kafka.TagTree;
|
||||
|
||||
public interface ITagTreeKafkaMessage
|
||||
{
|
||||
public Guid Guid { get; init; }
|
||||
public Actions Action { get; init; }
|
||||
public List<ItemCategoryTag> ItemCategoryTagsList { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using OpenWarehouse.warehouse.api.Model.Item;
|
||||
using ItemCategoryTag = OpenWarehouse.warehouse.api.Model.Item.ItemCategoryTag;
|
||||
|
||||
namespace OpenWarehouse.warehouse.api.Common.Kafka.TagTree;
|
||||
|
||||
public class TagTreeKafkaMessage : ITagTreeKafkaMessage
|
||||
{
|
||||
public Guid Guid { get; init; }
|
||||
public Actions Action { get; init; }
|
||||
public List<ItemCategoryTag> ItemCategoryTagsList { get; init; } = new();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Producer;
|
||||
|
||||
public interface IProducerManager
|
||||
{
|
||||
public Task<Producers?> FindProducerByName(string name);
|
||||
public Task<Producers?> FindProducerById(Guid? guid);
|
||||
public Task<List<Producers>> GetProducerList();
|
||||
|
||||
public Task<bool> DeleteProducerByName(string name);
|
||||
public Task<List<Claim>> GetClaims(Producers producers);
|
||||
public Task<ActionResult> CreateAsync(Producers producers);
|
||||
public Task<ActionResult> AddClaim(Producers producers, Claim claim);
|
||||
public Task<ActionResult> DeleteClaim(Producers producers, string claimType);
|
||||
public Task<ActionResult> UpdateClaim(Producers producers, Claim claim);
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Producer;
|
||||
|
||||
public class ProducerManager(ApplicationDbContext context) : IProducerManager
|
||||
{
|
||||
public async Task<Producers?> FindProducerByName(string name)
|
||||
{
|
||||
return await context.Producers
|
||||
.Include(q => q.Claims)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(producer1 => producer1.Name != null &&
|
||||
producer1.Name.Equals(name));
|
||||
}
|
||||
|
||||
public async Task<Producers?> FindProducerById(Guid? guid)
|
||||
{
|
||||
return await context.Producers
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(producer1 => producer1.Name != null &&
|
||||
producer1.Id == guid);
|
||||
}
|
||||
|
||||
public async Task<List<Producers>> GetProducerList()
|
||||
{
|
||||
return await context.Producers.Include(q => q.Claims).AsNoTracking().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteProducerByName(string name)
|
||||
{
|
||||
var res = await context.Producers
|
||||
.Where(q => q.Name != null && q.Name.Equals(name))
|
||||
.Include(q => q.Claims)
|
||||
.AsNoTracking()
|
||||
.ExecuteDeleteAsync();
|
||||
return res > 0;
|
||||
}
|
||||
|
||||
public async Task<List<Claim>> GetClaims(Producers producers)
|
||||
{
|
||||
return await context.ProducerClaims.Where(claims => claims.Producer == producers)
|
||||
.Select(claims => new Claim(claims.ClaimType ?? "Unknown", claims.ClaimValue ?? "Unknown"))
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ActionResult> CreateAsync(Producers producers)
|
||||
{
|
||||
context.Producers.Add(producers);
|
||||
ActionResult? res = null;
|
||||
|
||||
var error = new Dictionary<string, string>();
|
||||
|
||||
try
|
||||
{
|
||||
res= new ActionResult((await context.SaveChangesAsync()) == 1);
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
|
||||
return res ?? new ActionResult(false, error);
|
||||
}
|
||||
|
||||
public async Task<ActionResult> AddClaim(Producers producers, Claim claim)
|
||||
{
|
||||
context.ProducerClaims.Add(new ProducerClaims(producers, claim));
|
||||
|
||||
ActionResult? res = null;
|
||||
|
||||
var error = new Dictionary<string, string>();
|
||||
|
||||
try
|
||||
{
|
||||
res= new ActionResult((await context.SaveChangesAsync()) == 1);
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
|
||||
return res ?? new ActionResult(false, error);
|
||||
}
|
||||
|
||||
public async Task<ActionResult> DeleteClaim(Producers producers, string claimType)
|
||||
{
|
||||
ActionResult? res = null;
|
||||
|
||||
var error = new Dictionary<string, string>();
|
||||
|
||||
try
|
||||
{
|
||||
var delRes = await context.ProducerClaims
|
||||
.Where(claims => claims.ClaimType != null &&
|
||||
claims.Producer == producers && claims.ClaimType.Equals(claimType))
|
||||
.ExecuteDeleteAsync();
|
||||
res= new ActionResult((delRes) == 1);
|
||||
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Unknown"] = e.Message;
|
||||
}
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
res = new ActionResult(false, error);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public async Task<ActionResult> UpdateClaim(Producers producers, Claim claim1)
|
||||
{
|
||||
var claim = await context.ProducerClaims
|
||||
.Where(claims => claims.ClaimType != null &&
|
||||
claims.Producer == producers && claims.ClaimType.Equals(claim1.Type)
|
||||
&& claims.ClaimIssuer == claim1.Issuer).FirstOrDefaultAsync();
|
||||
|
||||
var res = 0;
|
||||
|
||||
if (claim == null)
|
||||
{
|
||||
res = (await AddClaim(producers, claim1)).IsSuccess ? 1 : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
claim.ClaimValue = claim1.Value;
|
||||
res = await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
return new ActionResult(res > 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.ServiceClaims;
|
||||
|
||||
public interface IServiceClaimsManager
|
||||
{
|
||||
public Task<Claim> GetServiceClaims(ServiceClaim claim);
|
||||
public Task<List<Claim>> GetListServiceClaims();
|
||||
public Task<bool> UpdateClaim(ServiceClaim claim, string value);
|
||||
public Task<bool> ClaimIsSet(ServiceClaim claim);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.ServiceClaims;
|
||||
|
||||
public enum ServiceClaim
|
||||
{
|
||||
ServiceAcoountUsername, ServiceAcoountToken, SericeIsnitialized,
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.ServiceClaims;
|
||||
|
||||
public static class ServiceClaimsClass
|
||||
{
|
||||
private static readonly Dictionary<ServiceClaim, string> ServiceClaimsMap = new()
|
||||
{
|
||||
{ ServiceClaim.ServiceAcoountToken, "Service_account_token" },
|
||||
{ ServiceClaim.ServiceAcoountUsername, "Service_account_username" }
|
||||
};
|
||||
|
||||
public static string GetName(this ServiceClaim serviceClaim)
|
||||
{
|
||||
var result = ServiceClaimsMap.TryGetValue(serviceClaim, out var name);
|
||||
return (result ? name : $"Uknow-{serviceClaim}") ?? $"Role {serviceClaim} Uknow";
|
||||
}
|
||||
|
||||
public static Claim GerClaims(this ServiceClaim serviceClaim, string value)
|
||||
{
|
||||
var result = ServiceClaimsMap.TryGetValue(serviceClaim, out var name);
|
||||
if (!result || value == null) throw new Exception("Claims type not implement");
|
||||
|
||||
return new Claim(name ?? throw new NullReferenceException("Claim name excemption"), value);
|
||||
}
|
||||
|
||||
public static Data.ServiceClaims CreateServiceClaims(this ServiceClaim serviceClaim, string value)
|
||||
{
|
||||
return new Data.ServiceClaims() { ClaimType = serviceClaim.GetName(), ClaimValue = value };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.ServiceClaims;
|
||||
|
||||
public class ServiceClaimsManager(ApplicationDbContext context) : IServiceClaimsManager
|
||||
{
|
||||
public async Task<Claim> GetServiceClaims(ServiceClaim claim)
|
||||
{
|
||||
var result = await context.ServiceClaims
|
||||
.Where(claims => claims.ClaimType == claim.GetName())
|
||||
.FirstOrDefaultAsync();
|
||||
if (result == null)
|
||||
{
|
||||
return new Claim(claim.GetName(), "");
|
||||
}
|
||||
|
||||
return result.Claim;
|
||||
}
|
||||
|
||||
public async Task<List<Claim>> GetListServiceClaims()
|
||||
{
|
||||
return await context.ServiceClaims.Select(q => new Claim(q.ClaimType ?? "Uknow", q.ClaimValue ?? "")).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateClaim(ServiceClaim claim, string value)
|
||||
{
|
||||
var result = await context.ServiceClaims
|
||||
.Where(claims => claims.ClaimType == claim.GetName())
|
||||
.FirstOrDefaultAsync();
|
||||
if (result == null)
|
||||
{
|
||||
Data.ServiceClaims claims = claim.CreateServiceClaims(value);
|
||||
context.ServiceClaims.Add(claims);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.ClaimValue = value;
|
||||
}
|
||||
|
||||
var res = await context.SaveChangesAsync();
|
||||
|
||||
return res > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> ClaimIsSet(ServiceClaim claim)
|
||||
{
|
||||
var result = await context.ServiceClaims
|
||||
.Where(q => q.ClaimType == claim.GetName()).AnyAsync();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.TaskManager;
|
||||
|
||||
public interface ITaskManager
|
||||
{
|
||||
public Task<string?> GetTaskStatus(Guid id);
|
||||
public Task<bool?> CreateTask(Guid id, string? actorId);
|
||||
public Task<bool> UpdateTaskStatus(Guid id, TaskStatus status);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.TaskManager;
|
||||
|
||||
public class TaskManager(ApplicationDbContext context, ILogger<TaskManager> logger) : ITaskManager
|
||||
{
|
||||
public async Task<string?> GetTaskStatus(Guid id)
|
||||
{
|
||||
logger.LogInformation("Get status of task with id:{id}", id);
|
||||
|
||||
var result = await context.TaskStatus
|
||||
.FirstOrDefaultAsync(status => status.TaskId == id);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
logger.LogWarning("Task with id:{id} not found", id);
|
||||
return null;
|
||||
}
|
||||
|
||||
return result.Status;
|
||||
}
|
||||
|
||||
public async Task<bool?> CreateTask(Guid id, string? actorId)
|
||||
{
|
||||
logger.LogInformation("Creating new task with id {id}", id);
|
||||
var newTask = new TaskStatusTable()
|
||||
{
|
||||
TaskId = id,
|
||||
Actor = actorId,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow,
|
||||
Status = TaskStatus.Accepted.ToString()
|
||||
};
|
||||
|
||||
context.TaskStatus.Add(newTask);
|
||||
|
||||
var result = await context.SaveChangesAsync();
|
||||
|
||||
return result > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateTaskStatus(Guid id, TaskStatus status)
|
||||
{
|
||||
logger.LogInformation("Updating task with id:{id} to status:{status}", id, status.ToString());
|
||||
var task = await context.TaskStatus
|
||||
.FirstOrDefaultAsync(q => q.TaskId == id);
|
||||
|
||||
if (task == null)
|
||||
{
|
||||
logger.LogWarning("Task with id:{id} not found", id);
|
||||
return false;
|
||||
}
|
||||
|
||||
task.Status = status.ToString();
|
||||
task.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
return (await context.SaveChangesAsync()) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.TaskManager;
|
||||
|
||||
public enum TaskStatus
|
||||
{
|
||||
Crated, Accepted, Processing, Error, Success, NotFullSuccess
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.TaskManager;
|
||||
|
||||
public static class TaskStatusClass
|
||||
{
|
||||
private static readonly Dictionary<TaskStatus, string?> TaskStatusDictionary = new()
|
||||
{
|
||||
{ TaskStatus.Accepted, "Accepted" },
|
||||
{ TaskStatus.Crated, "Created" },
|
||||
{ TaskStatus.Error, "Error" },
|
||||
{ TaskStatus.Processing, "Processing" },
|
||||
{ TaskStatus.Success, "Success" },
|
||||
{ TaskStatus.NotFullSuccess, "Not full sucess"}
|
||||
};
|
||||
|
||||
public static string ToString(this TaskStatus status)
|
||||
{
|
||||
if (!TaskStatusDictionary.TryGetValue(status, out string? value) && value == "")
|
||||
throw new NotImplementedException();
|
||||
return value ?? "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Warehouse;
|
||||
|
||||
public interface IWarehouseManager
|
||||
{
|
||||
public Task<Data.Warehouse?> GetWarehouse(string id);
|
||||
public Task<ActionResult> EditWarehouse(Data.Warehouse warehouse, string? newName, string? newFiestLineAdres, string? newSecondLineAdress, Image? image);
|
||||
public Task<ActionResult> CreateWarehouse(Data.Warehouse warehouse);
|
||||
public Task<ActionResult> AddWarehouseClaim(Data.Warehouse warehouse, Claim claim);
|
||||
public Task<ActionResult> AddWarehouseClaims(Data.Warehouse warehouse, List<Claim> claims);
|
||||
public Task<List<Claim>> GetWarehouseClaim(Data.Warehouse warehouse);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Warehouse;
|
||||
|
||||
public enum WarehouseClaimsType
|
||||
{
|
||||
Company, Email, Phone, OwnerId, OwnerUsername, WarehousePrivilageManage, WarehouseManageInfo, WarehouseAdmin,
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Warehouse;
|
||||
|
||||
public static class WarehouseClaimsTypeClass
|
||||
{
|
||||
private static Dictionary<WarehouseClaimsType, string> _warehouseClaimsString = new()
|
||||
{
|
||||
{ WarehouseClaimsType.Company, "Company" },
|
||||
{ WarehouseClaimsType.Email, "Email" },
|
||||
{ WarehouseClaimsType.Phone, "Phone" },
|
||||
{ WarehouseClaimsType.OwnerId, "OwnerID" },
|
||||
{ WarehouseClaimsType.OwnerUsername, "OwnerUsername" },
|
||||
{ WarehouseClaimsType.WarehouseAdmin, "WarehouseManagePrivilage" },
|
||||
{ WarehouseClaimsType.WarehouseManageInfo, "WarehouseManagePrivilageInfo" },
|
||||
{ WarehouseClaimsType.WarehousePrivilageManage, "WarehouseAdminPrivilage" }
|
||||
};
|
||||
|
||||
public static string ToString(this WarehouseClaimsType warehouseClaimsType)
|
||||
{
|
||||
var result = _warehouseClaimsString.TryGetValue(warehouseClaimsType, out var claim);
|
||||
if (!result) throw new NotImplementedException("Claim tyoe not implemented");
|
||||
return claim ?? throw new NotImplementedException("Claim tyoe not implemented");
|
||||
}
|
||||
|
||||
public static Claim ToClaim(this WarehouseClaimsType warehouseClaimsType, string value, string? issuer = null)
|
||||
{
|
||||
var result = _warehouseClaimsString.TryGetValue(warehouseClaimsType, out var claim);
|
||||
if (!result || claim == null) throw new NotImplementedException("Claim tyoe not implemented");
|
||||
|
||||
if (issuer != null)
|
||||
{
|
||||
return new Claim(claim, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Claim(claim, value, issuer);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.Warehouse;
|
||||
|
||||
public class WarehouseManager(ApplicationDbContext context) : IWarehouseManager
|
||||
{
|
||||
public async Task<Data.Warehouse?> GetWarehouse(string id)
|
||||
{
|
||||
var warehouse = await context.Warehouses
|
||||
.Include(warehouse1 => warehouse1.Image)
|
||||
.FirstOrDefaultAsync(warehouse1 => warehouse1.Id != null && warehouse1.Id.Equals(id));
|
||||
return warehouse;
|
||||
}
|
||||
|
||||
public async Task<ActionResult> EditWarehouse(Data.Warehouse warehouse, string? newName = null,
|
||||
string? newFiestLineAdres = null, string? newSecondLineAdress = null,
|
||||
Image? image = null)
|
||||
{
|
||||
ActionResult? res = null;
|
||||
|
||||
var error = new Dictionary<string, string>();
|
||||
|
||||
var warehouseEntry = await context.Warehouses
|
||||
.Include(warehouse1 => warehouse.Image)
|
||||
.FirstOrDefaultAsync(warehouse1 => warehouse1 == warehouse);
|
||||
|
||||
if (warehouseEntry == null)
|
||||
{
|
||||
error["Database"] = "Not found";
|
||||
return new ActionResult(false, error);
|
||||
}
|
||||
|
||||
if (newName != null)
|
||||
{
|
||||
warehouseEntry.Name = newName;
|
||||
}
|
||||
|
||||
if (newFiestLineAdres != null)
|
||||
{
|
||||
warehouse.FiestLineAdress = newFiestLineAdres;
|
||||
}
|
||||
|
||||
if (newSecondLineAdress != null)
|
||||
{
|
||||
warehouse.SecondLineAdress = newSecondLineAdress;
|
||||
}
|
||||
|
||||
if (image != null)
|
||||
{
|
||||
var imageToDel = warehouse.Image;
|
||||
warehouse.Image = image;
|
||||
if (imageToDel != null)
|
||||
{
|
||||
await context.Images.Where(image1 => image1 == imageToDel).ExecuteDeleteAsync();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
res= new ActionResult((await context.SaveChangesAsync()) > 0);
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
res = new ActionResult(false, error);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public async Task<ActionResult> CreateWarehouse(Data.Warehouse warehouse)
|
||||
{
|
||||
ActionResult? res = null;
|
||||
|
||||
var error = new Dictionary<string, string>();
|
||||
|
||||
context.Warehouses
|
||||
.Add(warehouse);
|
||||
try
|
||||
{
|
||||
res= new ActionResult((await context.SaveChangesAsync()) == 1);
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
res = new ActionResult(false, error);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public async Task<ActionResult> AddWarehouseClaim(Data.Warehouse warehouse, Claim claim)
|
||||
{
|
||||
ActionResult? res = null;
|
||||
var error = new Dictionary<string, string>();
|
||||
|
||||
var claimData = new WarehouseClaims(warehouse, claim);
|
||||
context.WarehouseClaims.Add(claimData);
|
||||
try
|
||||
{
|
||||
res = new ActionResult((await context.SaveChangesAsync()) == 1);
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
res = new ActionResult(false, error);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public async Task<ActionResult> AddWarehouseClaims(Data.Warehouse warehouse, List<Claim> claims)
|
||||
{
|
||||
ActionResult? res = null;
|
||||
var error = new Dictionary<string, string>();
|
||||
|
||||
foreach (var claim in claims)
|
||||
{
|
||||
var claimData = new WarehouseClaims(warehouse, claim);
|
||||
context.WarehouseClaims.Add(claimData);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
res = new ActionResult((await context.SaveChangesAsync()) == 1);
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateConcurrencyException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
error[e.Source ?? "Uknow"] = e.Message;
|
||||
}
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
res = new ActionResult(false, error);
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
public async Task<List<Claim>> GetWarehouseClaim(Data.Warehouse warehouse)
|
||||
{
|
||||
var claims = await context.WarehouseClaims
|
||||
.Where(warehouseClaims => warehouseClaims.Warehouse == warehouse)
|
||||
.Select(warehouseClaims => new Claim(warehouseClaims.ClaimType ?? "Uknow", warehouseClaims.ClaimValue ?? "Uknow"))
|
||||
.ToListAsync();
|
||||
|
||||
return claims;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user