using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using QRCoder; using System.Drawing; using System.Drawing.Imaging; using AyCode.Core.Extensions; using TIAM.Database.DataLayers.Admins; using TIAM.Entities.ServiceProviders; using TIAM.Entities.Users; using TIAMWebApp.Shared.Application.Models; using Product = TIAM.Entities.Products.Product; using TIAM.Entities.Addresses; using TIAM.Entities.Profiles; using AyCode.Core.Loggers; using AyCode.Services.SignalRs; using TIAM.Services; namespace TIAMWebApp.Server.Controllers { [Authorize] [ApiController] [Route("api/v1/[controller]")] public class ServiceProviderAPIController(AdminDal adminDal, IEnumerable logWriters) : ControllerBase { private readonly TIAM.Core.Loggers.Logger _logger = new(logWriters.ToArray()); //15. [AllowAnonymous] [HttpPost] [Route(APIUrls.CreateServiceProviderRouteName)] [Tags("In-Progress", "ServiceProvider")] [EndpointSummary("Create service provider")] public async Task CreateServiceProvider([FromBody] ServiceProviderModel serializedServiceProviderModel) { _logger.Info(@"CreateServiceProvider called"); //if (serializedServiceProviderModel.GetArrayLength() == 0) if (serializedServiceProviderModel == null) { return string.Empty;//BadRequest("SerializedLoginModel is required").ToJson(); } else { //Company? serviceProvider = JObject.Parse(serializedServiceProviderModel.GetRawText()).ToObject(); var serviceProvider = serializedServiceProviderModel; if (serviceProvider != null) { var id = Guid.NewGuid(); var name = serviceProvider.Name; var commissionRate = serviceProvider.CommissionPercent; //no owner set yet var ownerId = serviceProvider.OwnerId == Guid.Empty ? null : serviceProvider.OwnerId; //ownerId = Guid.Parse("540271F6-C604-4C16-8160-D5A7CAFEDF00"); //TESZT - J. if (name is null) { return string.Empty;//BadRequest("Invalid request"); } else { var company = new Company(id, name, ownerId, Guid.NewGuid()) { CommissionPercent = commissionRate }; return await AddCompanyAsync(company); } } else { return string.Empty;//BadRequest(); } } } [SignalR(SignalRTags.AddCompany)] public async Task AddCompanyAsync(Company company) { if (company.Id.IsNullOrEmpty()) company.Id = Guid.NewGuid(); _logger.Info($@"ServiceProvider to be creating; id: {company.Id}, name: {company.Name}, ownerId: {company.OwnerId}"); company.SetProfile(new Profile(Guid.NewGuid(), company.Name)); company.Profile.SetAddress(new Address(Guid.NewGuid(), "Controller AddCompanyAsync; address text...")); var result = await adminDal.CreateServiceProviderAsync(company); if (!result) { return string.Empty;//BadRequest(result); } else { return company.ToJson();//Ok(toCreate); } } //16. [AllowAnonymous] [HttpGet] [Route(APIUrls.GetServiceProvidersRouteName)] [SignalR(SignalRTags.GetCompanies)] public async Task GetServiceProviders() { return await adminDal.GetServiceProvidersJsonAsync(); } //[AllowAnonymous] //[HttpGet] //[Route(APIUrls.GetServiceProvidersRouteName)] //public Task GetServiceProviders() => Task.FromResult(GetServiceProvidersMethod()); //[SignalR(SignalRTags.GetCompaniesAsync)] //public string GetServiceProvidersMethod() //{ // return adminDal.GetServiceProvidersJson(); //} //18. [AllowAnonymous] [HttpPost] [Route(APIUrls.GetServiceProviderByIdRouteName)] public async Task GetServiceProviderById([FromBody] Guid id) { _logger.Info($@"GetServiceProviderById called with id: {id}"); return await adminDal.GetServiceProviderByIdAsync(id); } [AllowAnonymous] [HttpPost] [Route(APIUrls.UpdateServiceProviderRouteName)] [SignalR(SignalRTags.UpdateCompany)] public async Task UpdateServiceProvider(Company companyToModify) { _logger.Info($"UpdateServiceProvider called! + {companyToModify.Id}"); var result = await adminDal.UpdateCompanyAsync(companyToModify); return result ? companyToModify.ToJson() : string.Empty; } //17. [Authorize] [HttpPost] [Route(APIUrls.GetServiceProvidersByOwnerIdRouteName)] [Tags("Finished", "ServiceProvider")] [SignalR(SignalRTags.GetPropertiesByOwnerId)] public async Task> GetServiceProvidersByOwnerId([FromBody] Guid ownerId) { _logger.Info($@"GetServiceProvidersByOwnerId called with ownerId: {ownerId}"); var serviceProviders = await adminDal.GetServiceProvidersAsync(); //return serviceProviders.Where(x => x.OwnerId == ownerId).ToList(); var myServiceproviders = serviceProviders.Where(x => x.OwnerId == ownerId).ToDictionary(x => x.Id, x => x.Name); //put Company id and name into a dictionary return myServiceproviders; } //22. [AllowAnonymous] [HttpPost] [Route(APIUrls.CreateUserProductMappingRouteName)] [Tags("Finished", "ServiceProvider")] [EndpointSummary("Create assigned user to product")] [SignalR(SignalRTags.CreateUserProductMapping)] public async Task CreateUserProductMapping(UserProductMapping userProductMapping) { if(userProductMapping.ProductId == Guid.Empty || userProductMapping.UserId == Guid.Empty) { return BadRequest("Invalid request"); } else { _logger.Info($@"CreateUserProductMappings called with ownerId: {userProductMapping.ProductId}, {userProductMapping.UserId}"); var result = await adminDal.AddUserProductMappingAsync(userProductMapping); return Ok(result); } } [AllowAnonymous] [HttpPost] [Route(APIUrls.UpdateUserProductMappingRouteName)] [SignalR(SignalRTags.UpdateUserProductMapping)] public async Task UpdateUserProductMapping(UserProductMapping userProductMapping) { _logger.Info($"UpdateUserProductMapping called! + {userProductMapping.Id}"); var result = await adminDal.UpdateUserProductMappingAsync(userProductMapping); return result ? userProductMapping.ToJson() : string.Empty; } [AllowAnonymous] [HttpPost] [Route(APIUrls.DeleteUserProductMappingRouteName)] [SignalR(SignalRTags.DeleteUserProductMapping)] public async Task DeleteUserProductMapping(UserProductMapping userProductMapping) { _logger.Info($"UpdateUserProductMapping called! + {userProductMapping.Id}"); var result = await adminDal.RemoveUserProductMappingAsync(userProductMapping.Id); return result ? userProductMapping.ToJson() : string.Empty; } //23. [AllowAnonymous] [HttpPost] [Route(APIUrls.GetUserProductMappingsForProductRouteName)] public async Task> GetUserProductMappingsForProduct(Guid serviceProviderId) { _logger.Info($@"GetUserProductMappingsForServiceProvider called with serviceProviderId: {serviceProviderId}"); var userProductMappingDictionary = new Dictionary(); var serviceProviders = await adminDal.GetServiceProvidersAsync(); var myServiceproviders = serviceProviders.Where(x => x.Id == serviceProviderId).ToDictionary(x => x.Id, x => x.Name); //put serviceprovider id and name into a dictionary return myServiceproviders; } [AllowAnonymous] [HttpGet] [Route(APIUrls.GetCarsForUserProductMappingRouteName + "/{userProductMappingId}")] [SignalR(SignalRTags.GetCarsForUserProductMapping)] public async Task> GetCarsForUserProductMapping(string userProductMappingId) { _logger.Info($@"GetCarsForUserProductMapping called with userProductMappingId: {userProductMappingId}"); var cars = adminDal.GetCarByUserProductMappingId(Guid.Parse(userProductMappingId)); return cars; } [AllowAnonymous] [HttpPost] [Route(APIUrls.CreateCarRouteName)] [Tags("Finished", "Cars")] [EndpointSummary("Create car")] [SignalR(SignalRTags.CreateCar)] public async Task CreateCar(Car car) { if (car.UserProductMappingId == Guid.Empty || car.LicencePlate == null) { return BadRequest("Invalid request"); } else { _logger.Info($@"CreateCar called with ownerId: {car.UserProductMappingId}, {car.LicencePlate}"); var result = await adminDal.AddCarAsync(car); return Ok(car); } } [AllowAnonymous] [HttpPost] [Route(APIUrls.UpdateCarRouteName)] [Tags("Finished", "Cars")] [EndpointSummary("Update car")] [SignalR(SignalRTags.UpdateCar)] public async Task UpdateCar(Car car) { if (car.UserProductMappingId == Guid.Empty || car.LicencePlate == null) { return BadRequest("Invalid request"); } else { _logger.Info($@"CreateCar called with ownerId: {car.UserProductMappingId}, {car.LicencePlate}"); var result = await adminDal.UpdateCarAsync(car); return Ok(car); } } [AllowAnonymous] [HttpPost] [Route(APIUrls.DeleteCarRouteName)] [Tags("Finished", "Cars")] [EndpointSummary("Delete car")] [SignalR(SignalRTags.DeleteCar)] public async Task DeleteCar(Car car) { if (car.UserProductMappingId == Guid.Empty || car.LicencePlate == null) { return BadRequest("Invalid request"); } else { _logger.Info($@"CreateCar called with ownerId: {car.UserProductMappingId}, {car.LicencePlate}"); var result = await adminDal.RemoveCarAsync(car); return Ok(car); } } [HttpPost] [Route(APIUrls.AddProductRouteName)] [Tags("In-Progress", "Product")] public async Task AddProduct([FromBody] Product product) { _logger.Info(@"AddProduct called"); if (product == null) { return BadRequest("Product is required"); } else { var result = adminDal.AddProductAsync(product); return Ok(result); } } [AllowAnonymous] [HttpPost] [Route(APIUrls.GetQrCodeByProductIdRouteName)] [Tags("In-Progress", "Product")] public async Task GetQRCodeByProductId([FromBody] Guid productId) { _logger.Info(@"GetQRCode called"); if (productId == Guid.Empty) { return BadRequest("Product is required"); } else { //var result = _serviceProviderDal.GetQRCodeAsync(productId); 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); 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")); _logger.Info($@"qrCodeLogo: {rootpath}/myimage.png"); var ms = new MemoryStream(); qrCodeImage.Save(ms, ImageFormat.Jpeg); var byteImage = ms.ToArray(); var sigBase64 = Convert.ToBase64String(byteImage); // Get Base64 return Ok(sigBase64); } } [AllowAnonymous] [HttpPost] [Route(APIUrls.GetProductsByServiceProviderIdRouteName)] [Tags("In-Progress", "Product")] public IActionResult GetProductsByServiceProviderId([FromBody] Guid serviceProviderId) { _logger.Info($@"GetProductsByServiceProviderId called with serviceProviderId: {serviceProviderId}"); if (serviceProviderId == Guid.Empty) { return BadRequest("Invalid request"); } else { var products = adminDal.GetProductsJsonByServiceProviderId(serviceProviderId); if (products != null) { return Ok(products); } else { //some Iactionresult that explains that there were errors return StatusCode(500); } } } [AllowAnonymous] [HttpGet] [Route(APIUrls.GetAllProductsRouteName)] [Tags("In-Progress", "Product")] public async Task GetAllProducts() { _logger.Info("GetAllProducts called"); var products = adminDal.GetProductsJson(); if (products != null) { return products; } else { return null; } } } }