From 2d75c081f7b68d26fd3ca116cb4f00f33f0868d0 Mon Sep 17 00:00:00 2001 From: "jozsef.b@aycode.com" <9Rj@D}fVwBaN> Date: Sat, 13 Jan 2024 16:14:31 +0100 Subject: [PATCH] Add UserProductMapping CRUD; Add Car entity; imptovements, fixes, etc... --- TIAM.Database.Test/AdminDalTest.cs | 63 +++++++++++++---- TIAM.Database/DataLayers/Admins/AdminDal.cs | 70 +++++++++++++++---- .../Admins/AdminDalDbContextExtension.cs | 13 ---- .../UserProductMappingDbSetExtensions.cs | 63 ++++++++++++++++- TIAM.Entities/Drivers/Car.cs | 13 ++-- TIAM.Entities/Drivers/CarMotorType.cs | 8 +++ .../ServiceProviders/ServiceProvider.cs | 18 +++-- TIAM.Entities/Users/UserProductMapping.cs | 15 +++- .../Pages/User/MyServiceProviders.razor | 6 +- .../ServiceProviderAPIController.cs | 49 ++++++------- 10 files changed, 231 insertions(+), 87 deletions(-) create mode 100644 TIAM.Entities/Drivers/CarMotorType.cs diff --git a/TIAM.Database.Test/AdminDalTest.cs b/TIAM.Database.Test/AdminDalTest.cs index 31910602..edcde8b5 100644 --- a/TIAM.Database.Test/AdminDalTest.cs +++ b/TIAM.Database.Test/AdminDalTest.cs @@ -32,7 +32,7 @@ namespace TIAM.Database.Test public void TearDown() { } - [TestMethod] + [DataTestMethod] [DataRow("42968456-6EF3-4D9C-8BC4-0569A129AC05")] public void GetPermissionViewBySubjectId_ReturnsPermissionContextMapping_WhenPermissionContextMappingExists(string subjectIdString) { @@ -43,7 +43,7 @@ namespace TIAM.Database.Test Assert.IsTrue(permMapping.Count > 0, "PermissionContextsView count: 0"); } - [TestMethod] + [DataTestMethod] [DataRow("814b5495-c2e9-4f1d-a73f-37cd5d353078")] public void GetPermissionViewByContextId_ReturnsPermissionContextMapping_WhenPermissionContextMappingExists(string contextIdString) { @@ -54,7 +54,7 @@ namespace TIAM.Database.Test Assert.IsTrue(permMapping.Count > 0, "PermissionContextsView count: 0"); } - [TestMethod] + [DataTestMethod] [DataRow("814b5495-c2e9-4f1d-a73f-37cd5d353078")] public async Task GetPermissionContextMappingByContextIdAsync_ReturnsPermissionContextMapping_WhenPermissionContextMappingExists(string contextIdString) { @@ -75,9 +75,9 @@ namespace TIAM.Database.Test return product; } - [TestMethod] + [DataTestMethod] [DataRow("814b5495-c2e9-4f1d-a73f-37cd5d353078")] - public void GeProductById_ReturnsProduct_WherHasUserProductMappingRelation(string productIdString) + public void GeProductById_ReturnsProduct_WhenHasUserProductMappingRelation(string productIdString) { var product = GetProductById_ReturnsProduct_WhenProductExists(productIdString); @@ -86,9 +86,9 @@ namespace TIAM.Database.Test Assert.IsNotNull(product.ServiceProvider, "ServiceProvider is null"); } - [TestMethod] + [DataTestMethod] [DataRow("540271F6-C604-4C16-8160-D5A7CAFEDF00")] - public void GetUserById_ReturnsUser_WherHasUserProductMappingRelation(string userIdString) + public void GetUserById_ReturnsUser_WhenHasUserProductMappingRelation(string userIdString) { var userId = Guid.Parse(userIdString); var user = Dal.GetUserById(userId); @@ -101,9 +101,10 @@ namespace TIAM.Database.Test Assert.IsNotNull(user.UserProductMappings[0].Product, "Product is null"); } - [TestMethod] + #region UserProductMapping + [DataTestMethod] [DataRow("a24bf07a-76a7-48a4-813f-4a77e515b2f3")] - public void GetUserProductMappingById_ReturnsUserProductMapping_WherHasUserAndProductRelation(string userProductMappingIdString) + public void GetUserProductMappingById_ReturnsUserProductMapping_WhenHasUserAndProductRelation(string userProductMappingIdString) { var userProductMappingId = Guid.Parse(userProductMappingIdString); var userProductMapping = Dal.GetUserProductMappingById(userProductMappingId, true); @@ -115,7 +116,45 @@ namespace TIAM.Database.Test Assert.IsTrue(userProductMapping.Id == userProductMappingId, "userProductMapping.Id != userProductMappingId"); } - [TestMethod] + [DataTestMethod] + [DataRow(new[] { "8EF2FC69-2338-4D9F-91B4-B1E15C241E1C", "814b5495-c2e9-4f1d-a73f-37cd5d353078" })] + public async Task SetUserProductMappingById_ReturnsUserProductMapping_WhenHasUserAndProductRelation(string[] userIdProductIdStrings) + { + var userId = Guid.Parse(userIdProductIdStrings[0]); + var productId = Guid.Parse(userIdProductIdStrings[1]); + var userProductMappingId = Guid.NewGuid(); + + await Dal.RemoveUserProductMappingAsync(userId, productId); //kitöröljük a szemetet, ha korábbról bentmaradt - J. + + var userProductMapping = await Dal.AddUserProductMappingAsync(userProductMappingId, userId, productId, 2); + Assert.IsNotNull(userProductMapping); + + userProductMapping = await Dal.GetUserProductMappingByIdAsync(userProductMappingId, true); + + Assert.IsNotNull(userProductMapping); + + //userProductMapping.Permissions = 1; + Assert.IsNotNull(await Dal.UpdateUserProductMappingAsync(userProductMappingId, 1)); + + userProductMapping = await Dal.GetUserProductMappingByIdAsync(userProductMappingId, true); + + Assert.IsNotNull(userProductMapping); + Assert.IsTrue(userProductMapping.Permissions == 1); + + Assert.IsNotNull(userProductMapping.User, "User is null"); + Assert.IsNotNull(userProductMapping.Product, "Product is null"); + + Assert.IsTrue(userProductMapping.Id == userProductMappingId, "userProductMapping.Id != userProductMappingId"); + + Assert.IsTrue(await Dal.RemoveUserProductMappingAsync(userProductMappingId)); //mielőbb kitöröljük, h ne maradjon szemét a db-ben - J. + + userProductMapping = await Dal.GetUserProductMappingByIdAsync(userProductMappingId, false); + Assert.IsNull(userProductMapping); //a korábbi törlés miatt NULL kell legyen - J. + } + + #endregion UserProductMapping + + [DataTestMethod] [DataRow("540271F6-C604-4C16-8160-D5A7CAFEDF00")] [DataRow("dcf451d2-cc4c-4ac2-8c1f-da00041be1fd")] [DataRow("ac612aa8-863b-4b4f-9d63-f5d261b5c5f9")] @@ -149,7 +188,7 @@ namespace TIAM.Database.Test Assert.IsTrue(userModel.UserToServiceProviders.Count > 0); } - [TestMethod] + [DataTestMethod] [DataRow("540271F6-C604-4C16-8160-D5A7CAFEDF00")] [DataRow("dcf451d2-cc4c-4ac2-8c1f-da00041be1fd")] [DataRow("ac612aa8-863b-4b4f-9d63-f5d261b5c5f9")] @@ -189,7 +228,7 @@ namespace TIAM.Database.Test Assert.IsTrue(users.Count>0); } - [TestMethod] + [DataTestMethod] [DataRow("273EFE3C-D19F-4C2A-BF19-7397DC835C60")] public void GetTransferDestionationById_ReturnsTransferDestination_WhenHasAddressRelation(string transferDestinationIdString) { diff --git a/TIAM.Database/DataLayers/Admins/AdminDal.cs b/TIAM.Database/DataLayers/Admins/AdminDal.cs index 3bf18b0c..06337d21 100644 --- a/TIAM.Database/DataLayers/Admins/AdminDal.cs +++ b/TIAM.Database/DataLayers/Admins/AdminDal.cs @@ -25,20 +25,21 @@ namespace TIAM.Database.DataLayers.Admins public TransferDestination? GetTransferDestinationById(Guid transferDestinationId, bool autoInclude = false) => Session(ctx=>ctx.TransferDestinations.FirstOrDefault(x=>x.Id == transferDestinationId)); public string? GetTransferDestinationJsonById(Guid transferDestinationId) => Session(ctx => ctx.TransferDestinations.FirstOrDefault(x => x.Id == transferDestinationId)?.ToJson()); - public User? GetUserById(Guid userId, bool autoInclude = false) => Session(x => x.GetUserById(userId, autoInclude)); - public User? GetUserByEmail(string email, bool autoInclude = false) => Session(x => x.GetUserByEmail(email, autoInclude)); + public User? GetUserById(Guid userId, bool autoInclude = false) => Session(ctx => ctx.GetUserById(userId, autoInclude)); + public User? GetUserByEmail(string email, bool autoInclude = false) => Session(ctx => ctx.GetUserByEmail(email, autoInclude)); - public UserModelDto? GetUserModelDtoById(Guid userId) => Session(x => x.GetUserModelDtoById(userId)); - public Task GetUserModelDtoByIdAsync(Guid userId) => SessionAsync(x => x.GetUserModelDtoById(userId)); - public UserModelDto? GetUserModelDtoByEmail(string email) => Session(x => x.GetUserModelDtoByEmail(email)); + public UserModelDto? GetUserModelDtoById(Guid userId) => Session(ctx => ctx.GetUserModelDtoById(userId)); + public Task GetUserModelDtoByIdAsync(Guid userId) => SessionAsync(ctx => ctx.GetUserModelDtoById(userId)); + public UserModelDto? GetUserModelDtoByEmail(string email) => Session(ctx => ctx.GetUserModelDtoByEmail(email)); public string? GetUserJsonById(Guid userId) => Session(ctx => ctx.GetUserById(userId)?.ToJson()); public string GetUsersJson() => Session(ctx => ctx.Users.ToJson()); - public Product? GetProductById(Guid contextId) => Session(x => x.GetProductById(contextId)); + public Product? GetProductById(Guid contextId) => Session(ctx => ctx.GetProductById(contextId)); public Task AddProduct(Product product) => TransactionAsync(ctx => ctx.AddProduct(product)); - public UserProductMapping? GetUserProductMappingById(Guid userProductMappingId, bool autoInclude = true) => Session(x => x.GetUserProductMappingById(userProductMappingId, autoInclude)); + public UserProductMapping? GetUserProductMappingById(Guid userProductMappingId, bool autoInclude = true) => Session(ctx => ctx.GetUserProductMappingById(userProductMappingId, autoInclude)); + public Task GetUserProductMappingByIdAsync(Guid userProductMappingId, bool autoInclude = true) => SessionAsync(ctx => ctx.GetUserProductMappingById(userProductMappingId, autoInclude)); public List GetPermissionContextsView(Guid subjectId, Guid contextId) => Session(x => x.GetPermissionContextsView(subjectId, contextId).ToList()); @@ -52,6 +53,49 @@ namespace TIAM.Database.DataLayers.Admins public Task> GetPermissionContextsViewByContextIdAsync(Guid contextId) => SessionAsync(x => x.GetPermissionContextsViewByContextId(contextId).ToList()); + #region UserProductMapping + public Task AddUserProductMappingAsync(UserProductMapping userProductMapping) + => TransactionAsync(ctx => ctx.AddUserProductMapping(userProductMapping) && ctx.SaveChanges() == 1); + + public async Task AddUserProductMappingAsync(Guid userProductMappingId, Guid userId, Guid productId, int permissions = 1, string? jsonDetails = null) + { + UserProductMapping? userProductMapping = null; + + var isSucces = await TransactionAsync(ctx => + { + userProductMapping = ctx.AddUserProductMapping(userProductMappingId, userId, productId, permissions, jsonDetails); + + return userProductMapping != null && ctx.SaveChanges() == 1; + }); + + return isSucces ? userProductMapping : null; + } + + public Task UpdateUserProductMappingAsync(UserProductMapping userProductMapping) + => TransactionAsync(ctx => ctx.UpdateUserProductMapping(userProductMapping) && ctx.SaveChanges() > 0); + + public async Task UpdateUserProductMappingAsync(Guid userProductMappingId, int permissions = 1, string? jsonDetails = null) + { + UserProductMapping? userProductMapping = null; + + var isSucces = await TransactionAsync(ctx => + { + userProductMapping = ctx.UpdateUserProductMapping(userProductMappingId, permissions, jsonDetails); + + return userProductMapping != null && ctx.SaveChanges() == 1; + }); + + return isSucces ? userProductMapping : null; + } + + public Task RemoveUserProductMappingAsync(Guid userProductMappingId) + => TransactionAsync(ctx => ctx.RemoveUserProductMapping(userProductMappingId) && ctx.SaveChanges() == 1); + + public Task RemoveUserProductMappingAsync(Guid userId, Guid productId) + => TransactionAsync(ctx => ctx.RemoveUserProductMapping(userId, productId) && ctx.SaveChanges() == 1); + + #endregion UserProductMapping + //15. (IServiceProviderDataService) Create service provider public Task CreateServiceProviderAsync(TiamServiceProvider serviceProvider) { @@ -78,12 +122,12 @@ namespace TIAM.Database.DataLayers.Admins return Context.ServiceProviders.SingleOrDefaultAsync(x => x.Id == id); } - public Task CreateUserProductMappingAsync(UserProductMapping userProductMapping) - { - Context.UserProductMappings.Add(userProductMapping); - Console.WriteLine($"Saving userProductMapping to db {userProductMapping.Id}, {userProductMapping.ProductId}, {userProductMapping.UserId}"); - return Context.SaveChangesAsync().ContinueWith(x => userProductMapping); - } + //public Task CreateUserProductMappingAsync(UserProductMapping userProductMapping) + //{ + // Context.UserProductMappings.Add(userProductMapping); + // Console.WriteLine($"Saving userProductMapping to db {userProductMapping.Id}, {userProductMapping.ProductId}, {userProductMapping.UserId}"); + // return Context.SaveChangesAsync().ContinueWith(x => userProductMapping); + //} #region ServiceProviders diff --git a/TIAM.Database/DataLayers/Admins/AdminDalDbContextExtension.cs b/TIAM.Database/DataLayers/Admins/AdminDalDbContextExtension.cs index 057a4c8a..4721399a 100644 --- a/TIAM.Database/DataLayers/Admins/AdminDalDbContextExtension.cs +++ b/TIAM.Database/DataLayers/Admins/AdminDalDbContextExtension.cs @@ -77,19 +77,6 @@ namespace TIAM.Database.DataLayers.Admins return true; } - public static UserProductMapping UpdateUserProductMapping(this IAdminDbContext context, UserProductMapping userProductMapping) - { - if (userProductMapping == null) return null; - var existingUserProductMapping = context.UserProductMappings.FirstOrDefault(u => u.Id == userProductMapping.Id); - if (existingUserProductMapping == null) return null; - existingUserProductMapping.Id = userProductMapping.Id; - existingUserProductMapping.UserId = userProductMapping.UserId; - existingUserProductMapping.ProductId = userProductMapping.ProductId; - - return existingUserProductMapping; - - } - public static Product UpdateProduct(this IAdminDbContext ctx, Product product) { if (product == null) return null; diff --git a/TIAM.Database/DbSets/Users/UserProductMappingDbSetExtensions.cs b/TIAM.Database/DbSets/Users/UserProductMappingDbSetExtensions.cs index f92c6087..da60162c 100644 --- a/TIAM.Database/DbSets/Users/UserProductMappingDbSetExtensions.cs +++ b/TIAM.Database/DbSets/Users/UserProductMappingDbSetExtensions.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using AyCode.Utils.Extensions; +using Microsoft.EntityFrameworkCore; using TIAM.Database.DbContexts.ServiceProviders; using TIAM.Entities.Users; @@ -11,7 +12,7 @@ public static class UserProductMappingDbSetExtensions ? ctx.UserProductMappings.Include(x => x.User).Include(x => x.Product) : ctx.UserProductMappings; - + public static UserProductMapping? GetUserProductMappingById(this IUserProductMappingDbSet ctx, Guid userProductMappingId, bool autoInclude = true) => ctx.UserProductMappingsWithRelations(autoInclude).FirstOrDefault(x => x.Id == userProductMappingId); @@ -23,4 +24,62 @@ public static class UserProductMappingDbSetExtensions public static IQueryable GetUserProductMappingsByProductId(this IUserProductMappingDbSet ctx, Guid productId, bool autoInclude = true) => ctx.UserProductMappingsWithRelations(autoInclude).Where(x => x.ProductId == productId); + + + public static bool AddUserProductMapping(this IUserProductMappingDbSet ctx, UserProductMapping userProductMapping) + { + if (userProductMapping.UserId.IsNullOrEmpty() || userProductMapping.ProductId.IsNullOrEmpty() || userProductMapping.Permissions < 0) + return false; + + if (userProductMapping.Id.IsNullOrEmpty()) + userProductMapping.Id = Guid.NewGuid(); + + return ctx.UserProductMappings.Add(userProductMapping).State == EntityState.Added; + } + + public static UserProductMapping? AddUserProductMapping(this IUserProductMappingDbSet ctx, Guid userProductMappingId, Guid userId, Guid productId, int permissions = 1, string? jsonDetails = null) + { + var userProductMapping = new UserProductMapping(userProductMappingId, userId, productId, permissions, jsonDetails); + + return ctx.AddUserProductMapping(userProductMapping) ? userProductMapping : null; + } + + public static bool UpdateUserProductMapping(this IUserProductMappingDbSet ctx, UserProductMapping userProductMapping) + { + if (userProductMapping.Id.IsNullOrEmpty() || userProductMapping.UserId.IsNullOrEmpty() || userProductMapping.ProductId.IsNullOrEmpty() || userProductMapping.Permissions < 0) + return false; + + return ctx.UserProductMappings.Update(userProductMapping).State == EntityState.Modified; + } + + public static UserProductMapping? UpdateUserProductMapping(this IUserProductMappingDbSet ctx, Guid userProductMappingId, int permissions = 1, string? jsonDetails = null) + { + if (userProductMappingId.IsNullOrEmpty() || permissions < 0) + return null; + + var userProductMapping = ctx.GetUserProductMappingById(userProductMappingId, false); + if (userProductMapping == null) return null; + + userProductMapping.Permissions = permissions; + userProductMapping.JsonDetails = jsonDetails; + + return ctx.UpdateUserProductMapping(userProductMapping) ? userProductMapping : null; + } + + public static bool RemoveUserProductMapping(this IUserProductMappingDbSet ctx, Guid userProductMappingId) + { + var userProductMapping = ctx.GetUserProductMappingById(userProductMappingId, false); + if (userProductMapping == null) return true; + + return ctx.UserProductMappings.Remove(userProductMapping).State == EntityState.Deleted; + } + + public static bool RemoveUserProductMapping(this IUserProductMappingDbSet ctx, Guid userId, Guid productId) + { + var userProductMapping = ctx.GetUserProductMapping(userId, productId, false); + if (userProductMapping == null) return true; + + return ctx.UserProductMappings.Remove(userProductMapping).State == EntityState.Deleted; + } + } \ No newline at end of file diff --git a/TIAM.Entities/Drivers/Car.cs b/TIAM.Entities/Drivers/Car.cs index 4c5f3e5a..e17a1b25 100644 --- a/TIAM.Entities/Drivers/Car.cs +++ b/TIAM.Entities/Drivers/Car.cs @@ -9,27 +9,24 @@ using AyCode.Interfaces.TimeStampInfo; namespace TIAM.Entities.Drivers { - public enum CarMotorType : byte - { - Gas = 5, - Diesel = 10, - Electric = 15, - } - public class Car : IEntityGuid, ITimeStampModified { public Guid Id { get; set; } + public int CountryCode { get; set; } + public string LicencePlate { get; set; } public string Color { get; set; } public string Manufacture{ get; set; } - public string Model { get; set; } + public string CarModel { get; set; } public int YearOfMake { get; set; } public int SeetNumber { get; set; } public CarMotorType CarMotorType { get; set; } + + public DateTime Modified { get; set; } public DateTime Created { get; set; } } diff --git a/TIAM.Entities/Drivers/CarMotorType.cs b/TIAM.Entities/Drivers/CarMotorType.cs new file mode 100644 index 00000000..c03429f3 --- /dev/null +++ b/TIAM.Entities/Drivers/CarMotorType.cs @@ -0,0 +1,8 @@ +namespace TIAM.Entities.Drivers; + +public enum CarMotorType : byte +{ + Gas = 5, + Diesel = 10, + Electric = 15, +} \ No newline at end of file diff --git a/TIAM.Entities/ServiceProviders/ServiceProvider.cs b/TIAM.Entities/ServiceProviders/ServiceProvider.cs index 546222f4..359cef8e 100644 --- a/TIAM.Entities/ServiceProviders/ServiceProvider.cs +++ b/TIAM.Entities/ServiceProviders/ServiceProvider.cs @@ -13,11 +13,19 @@ public class TiamServiceProvider : AcServiceProvider Products { get; } = new(); - public TiamServiceProvider(){} - public TiamServiceProvider(string name, Guid ownerId) : this(Guid.NewGuid(), name, ownerId) { } - public TiamServiceProvider(Guid id, string name, Guid ownerId) : base(id, name, ownerId) - { - + public TiamServiceProvider() + { } + public TiamServiceProvider(string name, Guid ownerId) : this(Guid.NewGuid(), name, ownerId) + { + } + + public TiamServiceProvider(Guid id, string name, Guid ownerId) : this(id, name, ownerId, Guid.NewGuid()) + { + } + + public TiamServiceProvider(Guid id, string name, Guid ownerId, Guid affiliateId) : base(id, name, ownerId, affiliateId) + { + } } \ No newline at end of file diff --git a/TIAM.Entities/Users/UserProductMapping.cs b/TIAM.Entities/Users/UserProductMapping.cs index 35350235..67b5092d 100644 --- a/TIAM.Entities/Users/UserProductMapping.cs +++ b/TIAM.Entities/Users/UserProductMapping.cs @@ -17,7 +17,10 @@ public class UserProductMapping : IEntityGuid, ITimeStampInfo public virtual User User { get; set; } public virtual Product Product { get; set; } - + + public int Permissions { get; set; } = 1; + public string? JsonDetails { get; set; } + public DateTime Created { get; set; } public DateTime Modified { get; set; } @@ -27,10 +30,18 @@ public class UserProductMapping : IEntityGuid, ITimeStampInfo public UserProductMapping(Guid userId, Guid productId) : this(Guid.NewGuid(), userId, productId) { } - public UserProductMapping(Guid id, Guid userId, Guid productId) + public UserProductMapping(Guid id, Guid userId, Guid productId) : this(id, userId, productId, 1) + { } + + public UserProductMapping(Guid id, Guid userId, Guid productId, int permissions) : this(id, userId, productId, permissions, null) + { } + public UserProductMapping(Guid id, Guid userId, Guid productId, int permissions, string? jsonDetails) : this() { Id = id; UserId = userId; ProductId = productId; + + Permissions = permissions; + JsonDetails = jsonDetails; } } \ No newline at end of file diff --git a/TIAMSharedUI/Pages/User/MyServiceProviders.razor b/TIAMSharedUI/Pages/User/MyServiceProviders.razor index 52b1e5d8..bc9c54b7 100644 --- a/TIAMSharedUI/Pages/User/MyServiceProviders.razor +++ b/TIAMSharedUI/Pages/User/MyServiceProviders.razor @@ -182,9 +182,9 @@ base.OnInitialized(); OrderData = new TiamServiceProvider[] { - new TiamServiceProvider(Guid.NewGuid(), "BAT", Guid.NewGuid()), - new TiamServiceProvider(Guid.NewGuid(), "TIAM", Guid.NewGuid()), - new TiamServiceProvider(Guid.NewGuid(), "TestHotel", Guid.NewGuid()) + new TiamServiceProvider(Guid.NewGuid(), "BAT", Guid.NewGuid(), Guid.NewGuid()), + new TiamServiceProvider(Guid.NewGuid(), "TIAM", Guid.NewGuid(), Guid.NewGuid()), + new TiamServiceProvider(Guid.NewGuid(), "TestHotel", Guid.NewGuid(), Guid.NewGuid()) }; diff --git a/TIAMWebApp/Server/Controllers/ServiceProviderAPIController.cs b/TIAMWebApp/Server/Controllers/ServiceProviderAPIController.cs index 1fce899a..342584b4 100644 --- a/TIAMWebApp/Server/Controllers/ServiceProviderAPIController.cs +++ b/TIAMWebApp/Server/Controllers/ServiceProviderAPIController.cs @@ -31,7 +31,6 @@ namespace TIAMWebApp.Server.Controllers public ServiceProviderAPIController(ILogger logger, AdminDal adminDal) { _logger = logger; - _adminDal = adminDal; } @@ -40,17 +39,17 @@ namespace TIAMWebApp.Server.Controllers [Route("CreateServiceProvider")] [Tags("In-Progress", "ServiceProvider")] [EndpointSummary("Create assigned user")] - public async Task CreateServiceProvider([FromBody] ServiceProviderModel SerializedServiceProviderModel) + public async Task CreateServiceProvider([FromBody] ServiceProviderModel serializedServiceProviderModel) { Console.WriteLine("CreateUser called"); - if (SerializedServiceProviderModel == null) + if (serializedServiceProviderModel == null) { return BadRequest("SerializedLoginModel is required"); } else { - //ServiceProviderModel? serviceProvider = JObject.Parse(SerializedServiceProviderModel.GetRawText()).ToObject(); - ServiceProviderModel? serviceProvider = SerializedServiceProviderModel; + //ServiceProviderModel? serviceProvider = JObject.Parse(serializedServiceProviderModel.GetRawText()).ToObject(); + var serviceProvider = serializedServiceProviderModel; if (serviceProvider != null) @@ -60,8 +59,8 @@ namespace TIAMWebApp.Server.Controllers //users[users.Length - 1] = new UserModel(user.Email, user.PhoneNumber, user.Password); var id = Guid.NewGuid(); - string? name = SerializedServiceProviderModel?.Name; - Guid ownerId = SerializedServiceProviderModel?.OwnerId ?? Guid.Empty; + var name = serializedServiceProviderModel.Name; + var ownerId = serializedServiceProviderModel.OwnerId; @@ -74,17 +73,14 @@ namespace TIAMWebApp.Server.Controllers Console.WriteLine($"ServiceProvider to be created: {id}, {name}, {ownerId}"); - await _adminDal.CreateServiceProviderAsync(new TiamServiceProvider(id, name, ownerId)); + await _adminDal.CreateServiceProviderAsync(new TiamServiceProvider(id, name, ownerId, Guid.NewGuid())); } } + return Ok("yes"); - - } - } - //16. [AllowAnonymous] [HttpGet] @@ -96,7 +92,6 @@ namespace TIAMWebApp.Server.Controllers return _adminDal.GetServiceProvidersAsync(); } - //18. [AllowAnonymous] [HttpPost] @@ -143,13 +138,11 @@ namespace TIAMWebApp.Server.Controllers { Console.WriteLine($"CreateUserProductMappings called with ownerId: {createUserProductMappingModel.ContextId}, {createUserProductMappingModel.ContextId}"); - UserProductMapping userProductMapping = new UserProductMapping(createUserProductMappingModel.ContextId, createUserProductMappingModel.ContextId); + var userProductMapping = new UserProductMapping(createUserProductMappingModel.ContextId, createUserProductMappingModel.ContextId); - var result = await _adminDal.CreateUserProductMappingAsync(userProductMapping); + var result = await _adminDal.AddUserProductMappingAsync(userProductMapping); - return Ok(result); - } } @@ -161,7 +154,7 @@ namespace TIAMWebApp.Server.Controllers { Console.WriteLine($"GetUserProductMappingsForServiceProvider called with serviceProviderId: {serviceProviderId}"); - Dictionary userProductMappingDictionary = new Dictionary(); + var userProductMappingDictionary = new Dictionary(); var serviceProviders = await _adminDal.GetServiceProvidersAsync(); @@ -203,23 +196,21 @@ namespace TIAMWebApp.Server.Controllers { //var result = _serviceProviderDal.GetQRCodeAsync(productId); - QRCodeGenerator qrGenerator = new QRCodeGenerator(); - QRCodeData qrCodeData = qrGenerator.CreateQrCode($"https://touriam.com/{productId}", QRCodeGenerator.ECCLevel.Q); - QRCode qrCode = new QRCode(qrCodeData); + var qrGenerator = new QRCodeGenerator(); + var qrCodeData = qrGenerator.CreateQrCode($"https://touriam.com/{productId}", QRCodeGenerator.ECCLevel.Q); + var qrCode = new QRCode(qrCodeData); //Bitmap qrCodeImage = qrCode.GetGraphic(20); - string rootpath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "assets"); - Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.DarkMagenta, Color.White, (Bitmap)Bitmap.FromFile(rootpath + "/myimage.png")); + var rootpath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "assets"); + var qrCodeImage = qrCode.GetGraphic(20, Color.DarkMagenta, Color.White, (Bitmap)Bitmap.FromFile(rootpath + "/myimage.png")); Console.WriteLine($"qrCodeLogo: {rootpath}/myimage.png"); - MemoryStream ms = new MemoryStream(); + var ms = new MemoryStream(); qrCodeImage.Save(ms, ImageFormat.Jpeg); - byte[] byteImage = ms.ToArray(); + var byteImage = ms.ToArray(); - var SigBase64 = Convert.ToBase64String(byteImage); // Get Base64 + var sigBase64 = Convert.ToBase64String(byteImage); // Get Base64 - return Ok(SigBase64); + return Ok(sigBase64); } } - - } } \ No newline at end of file