add logger to ServiceProviderApiController

This commit is contained in:
jozsef.b@aycode.com 2024-05-28 16:13:35 +02:00
parent 194fb862ea
commit 269e8bd679
1 changed files with 38 additions and 59 deletions

View File

@ -3,36 +3,23 @@ using Microsoft.AspNetCore.Mvc;
using QRCoder; using QRCoder;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using AyCode.Core.Server.Loggers;
using TIAM.Database.DataLayers.Admins; using TIAM.Database.DataLayers.Admins;
//using TIAM.Database.DataLayers.ServiceProviders;
using TIAM.Entities.ServiceProviders; using TIAM.Entities.ServiceProviders;
using TIAM.Entities.Users; using TIAM.Entities.Users;
using TIAMWebApp.Shared.Application.Models; using TIAMWebApp.Shared.Application.Models;
using Product = TIAM.Entities.Products.Product; using Product = TIAM.Entities.Products.Product;
using TIAM.Entities.Transfers;
using System.Text.Json.Nodes;
using System.Text.Json;
using Newtonsoft.Json.Linq;
using TIAM.Entities.Addresses; using TIAM.Entities.Addresses;
using TIAM.Entities.Profiles; using TIAM.Entities.Profiles;
using AyCode.Core.Loggers;
namespace TIAMWebApp.Server.Controllers namespace TIAMWebApp.Server.Controllers
{ {
[Authorize] [Authorize]
[ApiController] [ApiController]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
public class ServiceProviderAPIController : ControllerBase public class ServiceProviderAPIController(AdminDal adminDal, IEnumerable<IAcLogWriterBase> logWriters) : ControllerBase
{ {
private readonly AdminDal _adminDal; private readonly TIAM.Core.Loggers.Logger<ServiceProviderAPIController> _logger = new(logWriters.ToArray());
private readonly ILogger<ServiceProviderAPIController> _logger;
public ServiceProviderAPIController(ILogger<ServiceProviderAPIController> logger, AdminDal adminDal)
{
_logger = logger;
_adminDal = adminDal;
}
//15. //15.
[AllowAnonymous] [AllowAnonymous]
@ -42,7 +29,8 @@ namespace TIAMWebApp.Server.Controllers
[EndpointSummary("Create service provider")] [EndpointSummary("Create service provider")]
public async Task<IActionResult> CreateServiceProvider([FromBody] ServiceProviderModel serializedServiceProviderModel) public async Task<IActionResult> CreateServiceProvider([FromBody] ServiceProviderModel serializedServiceProviderModel)
{ {
GlobalLogger.Info(@"CreateUser called"); _logger.Info(@"CreateServiceProvider called");
//if (serializedServiceProviderModel.GetArrayLength() == 0) //if (serializedServiceProviderModel.GetArrayLength() == 0)
if (serializedServiceProviderModel == null) if (serializedServiceProviderModel == null)
{ {
@ -55,21 +43,13 @@ namespace TIAMWebApp.Server.Controllers
var serviceProvider = serializedServiceProviderModel; var serviceProvider = serializedServiceProviderModel;
if (serviceProvider != null) if (serviceProvider != null)
{ {
var id = Guid.NewGuid(); var id = Guid.NewGuid();
var name = serviceProvider.Name; var name = serviceProvider.Name;
var commissionRate = serviceProvider.CommissionPercent; var commissionRate = serviceProvider.CommissionPercent;
Guid ownerId; Guid ownerId;
if(serviceProvider.OwnerId == Guid.Empty)
{ //no owner set yet
//no owner set yet ownerId = serviceProvider.OwnerId == Guid.Empty ? serviceProvider.OwnerId : serviceProvider.OwnerId;
ownerId = serviceProvider.OwnerId;
}
else
{
ownerId = serviceProvider.OwnerId;
}
if (name is null) if (name is null)
{ {
@ -78,14 +58,17 @@ namespace TIAMWebApp.Server.Controllers
else else
{ {
GlobalLogger.Info($@"ServiceProvider to be created: {id}, {name}, {ownerId}"); _logger.Info($@"ServiceProvider to be creating; id: {id}, name: {name}, ownerId: {ownerId}");
Company toCreate = new Company(id, name, ownerId, Guid.NewGuid());
toCreate.CommissionPercent = commissionRate; var toCreate = new Company(id, name, ownerId, Guid.NewGuid())
{
CommissionPercent = commissionRate
};
toCreate.SetProfile(new Profile(Guid.NewGuid(), toCreate.Name)); toCreate.SetProfile(new Profile(Guid.NewGuid(), toCreate.Name));
toCreate.Profile.SetAddress(new Address(Guid.NewGuid(), "Controller CreateServiceProvider; address text...")); toCreate.Profile.SetAddress(new Address(Guid.NewGuid(), "Controller CreateServiceProvider; address text..."));
var result = await _adminDal.CreateServiceProviderAsync(toCreate); var result = await adminDal.CreateServiceProviderAsync(toCreate);
if (!result) if (!result)
{ {
serviceProvider = null; serviceProvider = null;
@ -113,7 +96,7 @@ namespace TIAMWebApp.Server.Controllers
[Route(APIUrls.GetServiceProvidersRouteName)] [Route(APIUrls.GetServiceProvidersRouteName)]
public async Task<string> GetServiceProviders() public async Task<string> GetServiceProviders()
{ {
return await _adminDal.GetServiceProvidersJsonAsync(); return await adminDal.GetServiceProvidersJsonAsync();
} }
//18. //18.
@ -122,9 +105,9 @@ namespace TIAMWebApp.Server.Controllers
[Route(APIUrls.GetServiceProviderByIdRouteName)] [Route(APIUrls.GetServiceProviderByIdRouteName)]
public async Task<Company?> GetServiceProviderById([FromBody] Guid id) public async Task<Company?> GetServiceProviderById([FromBody] Guid id)
{ {
GlobalLogger.Info($@"GetServiceProviderById called with id: {id}"); _logger.Info($@"GetServiceProviderById called with id: {id}");
return await _adminDal.GetServiceProviderByIdAsync(id); return await adminDal.GetServiceProviderByIdAsync(id);
} }
[AllowAnonymous] [AllowAnonymous]
@ -132,17 +115,11 @@ namespace TIAMWebApp.Server.Controllers
[Route(APIUrls.UpdateServiceProviderRouteName)] [Route(APIUrls.UpdateServiceProviderRouteName)]
public async Task<Company> UpdateServiceProvider(Company companyToModify) public async Task<Company> UpdateServiceProvider(Company companyToModify)
{ {
GlobalLogger.Info($"UpdateServiceProvider called! + {companyToModify.Id}"); _logger.Info($"UpdateServiceProvider called! + {companyToModify.Id}");
var result = await _adminDal.UpdateServiceProviderAsync(companyToModify);
if(result)
{
return companyToModify;
} var result = await adminDal.UpdateServiceProviderAsync(companyToModify);
else
{ return result ? companyToModify : null;
return null;
}
} }
//17. //17.
@ -152,9 +129,9 @@ namespace TIAMWebApp.Server.Controllers
[Tags("Finished", "ServiceProvider")] [Tags("Finished", "ServiceProvider")]
public async Task<Dictionary<Guid, string>> GetServiceProvidersByOwnerId([FromBody] Guid ownerId) public async Task<Dictionary<Guid, string>> GetServiceProvidersByOwnerId([FromBody] Guid ownerId)
{ {
GlobalLogger.Info($@"GetServiceProvidersByOwnerId called with ownerId: {ownerId}"); _logger.Info($@"GetServiceProvidersByOwnerId called with ownerId: {ownerId}");
var serviceProviders = await _adminDal.GetServiceProvidersAsync(); var serviceProviders = await adminDal.GetServiceProvidersAsync();
//return serviceProviders.Where(x => x.OwnerId == ownerId).ToList(); //return serviceProviders.Where(x => x.OwnerId == ownerId).ToList();
var myServiceproviders = serviceProviders.Where(x => x.OwnerId == ownerId).ToDictionary(x => x.Id, x => x.Name); var myServiceproviders = serviceProviders.Where(x => x.OwnerId == ownerId).ToDictionary(x => x.Id, x => x.Name);
@ -177,11 +154,11 @@ namespace TIAMWebApp.Server.Controllers
} }
else else
{ {
GlobalLogger.Info($@"CreateUserProductMappings called with ownerId: {createUserProductMappingModel.ContextId}, {createUserProductMappingModel.ContextId}"); _logger.Info($@"CreateUserProductMappings called with ownerId: {createUserProductMappingModel.ContextId}, {createUserProductMappingModel.ContextId}");
var userProductMapping = new UserProductMapping(createUserProductMappingModel.ContextId, createUserProductMappingModel.ContextId); var userProductMapping = new UserProductMapping(createUserProductMappingModel.ContextId, createUserProductMappingModel.ContextId);
var result = await _adminDal.AddUserProductMappingAsync(userProductMapping); var result = await adminDal.AddUserProductMappingAsync(userProductMapping);
return Ok(result); return Ok(result);
} }
@ -193,11 +170,11 @@ namespace TIAMWebApp.Server.Controllers
[Route(APIUrls.GetUserProductMappingsForProductRouteName)] [Route(APIUrls.GetUserProductMappingsForProductRouteName)]
public async Task<Dictionary<Guid, string>> GetUserProductMappingsForProduct(Guid serviceProviderId) public async Task<Dictionary<Guid, string>> GetUserProductMappingsForProduct(Guid serviceProviderId)
{ {
GlobalLogger.Info($@"GetUserProductMappingsForServiceProvider called with serviceProviderId: {serviceProviderId}"); _logger.Info($@"GetUserProductMappingsForServiceProvider called with serviceProviderId: {serviceProviderId}");
var userProductMappingDictionary = new Dictionary<Guid, string>(); var userProductMappingDictionary = new Dictionary<Guid, string>();
var serviceProviders = await _adminDal.GetServiceProvidersAsync(); var serviceProviders = await adminDal.GetServiceProvidersAsync();
var myServiceproviders = serviceProviders.Where(x => x.Id == serviceProviderId).ToDictionary(x => x.Id, x => x.Name); var myServiceproviders = serviceProviders.Where(x => x.Id == serviceProviderId).ToDictionary(x => x.Id, x => x.Name);
//put serviceprovider id and name into a dictionary //put serviceprovider id and name into a dictionary
@ -210,14 +187,15 @@ namespace TIAMWebApp.Server.Controllers
[Tags("In-Progress", "Product")] [Tags("In-Progress", "Product")]
public async Task<IActionResult> AddProduct([FromBody] Product product) public async Task<IActionResult> AddProduct([FromBody] Product product)
{ {
GlobalLogger.Info(@"AddProduct called"); _logger.Info(@"AddProduct called");
if (product == null) if (product == null)
{ {
return BadRequest("Product is required"); return BadRequest("Product is required");
} }
else else
{ {
var result = _adminDal.AddProductAsync(product); var result = adminDal.AddProductAsync(product);
return Ok(result); return Ok(result);
} }
} }
@ -228,7 +206,8 @@ namespace TIAMWebApp.Server.Controllers
[Tags("In-Progress", "Product")] [Tags("In-Progress", "Product")]
public async Task<IActionResult> GetQRCodeByProductId([FromBody] Guid productId) public async Task<IActionResult> GetQRCodeByProductId([FromBody] Guid productId)
{ {
GlobalLogger.Info(@"GetQRCode called"); _logger.Info(@"GetQRCode called");
if (productId == Guid.Empty) if (productId == Guid.Empty)
{ {
return BadRequest("Product is required"); return BadRequest("Product is required");
@ -243,7 +222,7 @@ namespace TIAMWebApp.Server.Controllers
//Bitmap qrCodeImage = qrCode.GetGraphic(20); //Bitmap qrCodeImage = qrCode.GetGraphic(20);
var rootpath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "assets"); 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")); var qrCodeImage = qrCode.GetGraphic(20, Color.DarkMagenta, Color.White, (Bitmap)Bitmap.FromFile(rootpath + "/myimage.png"));
GlobalLogger.Info($@"qrCodeLogo: {rootpath}/myimage.png"); _logger.Info($@"qrCodeLogo: {rootpath}/myimage.png");
var ms = new MemoryStream(); var ms = new MemoryStream();
qrCodeImage.Save(ms, ImageFormat.Jpeg); qrCodeImage.Save(ms, ImageFormat.Jpeg);
var byteImage = ms.ToArray(); var byteImage = ms.ToArray();
@ -260,7 +239,7 @@ namespace TIAMWebApp.Server.Controllers
[Tags("In-Progress", "Product")] [Tags("In-Progress", "Product")]
public IActionResult GetProductsByServiceProviderId([FromBody] Guid serviceProviderId) public IActionResult GetProductsByServiceProviderId([FromBody] Guid serviceProviderId)
{ {
GlobalLogger.Info($@"GetProductsByServiceProviderId called with serviceProviderId: {serviceProviderId}"); _logger.Info($@"GetProductsByServiceProviderId called with serviceProviderId: {serviceProviderId}");
if (serviceProviderId == Guid.Empty) if (serviceProviderId == Guid.Empty)
{ {
@ -268,7 +247,7 @@ namespace TIAMWebApp.Server.Controllers
} }
else else
{ {
var products = _adminDal.GetProductsJsonByServiceProviderId(serviceProviderId); var products = adminDal.GetProductsJsonByServiceProviderId(serviceProviderId);
if (products != null) if (products != null)
{ {
return Ok(products); return Ok(products);
@ -290,9 +269,9 @@ namespace TIAMWebApp.Server.Controllers
[Tags("In-Progress", "Product")] [Tags("In-Progress", "Product")]
public async Task<string> GetAllProducts() public async Task<string> GetAllProducts()
{ {
GlobalLogger.Info("GetAllProducts called"); _logger.Info("GetAllProducts called");
var products = _adminDal.GetProductsJson(); var products = adminDal.GetProductsJson();
if (products != null) if (products != null)
{ {
return products; return products;