using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using QRCoder; using System.Drawing; using System.Drawing.Imaging; using TIAM.Database.DataLayers.Admins; using TIAM.Entities.ServiceProviders; using TIAM.Entities.Users; using TIAMWebApp.Shared.Application.Models; using Product = TIAM.Entities.Products.Product; namespace TIAMWebApp.Server.Controllers { [Authorize] [ApiController] [Route("api/[controller]")] public class ServiceProviderAPIController : ControllerBase { private AdminDal _adminDal; private readonly ILogger _logger; public ServiceProviderAPIController(ILogger logger, AdminDal adminDal) { _logger = logger; _adminDal = adminDal; } //15. [HttpPost] [Route("CreateServiceProvider")] [Tags("In-Progress", "ServiceProvider")] [EndpointSummary("Create assigned user")] public async Task CreateServiceProvider([FromBody] ServiceProviderModel SerializedServiceProviderModel) { Console.WriteLine("CreateUser called"); if (SerializedServiceProviderModel == null) { return BadRequest("SerializedLoginModel is required"); } else { //ServiceProviderModel? serviceProvider = JObject.Parse(SerializedServiceProviderModel.GetRawText()).ToObject(); ServiceProviderModel? serviceProvider = SerializedServiceProviderModel; if (serviceProvider != null) { //add userModel to users array //Array.Resize(ref users, users.Length + 1); //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; if (name is null || ownerId == Guid.Empty) { return BadRequest("Invalid request"); } else { Console.WriteLine($"ServiceProvider to be created: {id}, {name}, {ownerId}"); await _adminDal.CreateServiceProviderAsync(new TiamServiceProvider(id, name, ownerId)); } } return Ok("yes"); } } //16. [AllowAnonymous] [HttpGet] [Route("GetServiceProviders")] public Task> GetServiceProviders() { //var users = await _serviceProviderDal.Ctx.Users.ToListAsync();//.GetUsersAsync(); //return users; return _adminDal.GetServiceProvidersAsync(); } //18. [AllowAnonymous] [HttpPost] [Route("GetServiceProviderById")] public async Task GetServiceProviderById([FromBody] Guid id) { Console.WriteLine($"GetServiceProviderById called with id: {id}"); return await _adminDal.GetServiceProviderByIdAsync(id); } //17. [AllowAnonymous] [HttpPost] [Route("GetServiceProvidersByOwnerId")] [Tags("Finished", "ServiceProvider")] public async Task> GetServiceProvidersByOwnerId([FromBody] Guid ownerId) { Console.WriteLine($"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 TiamServiceProvider id and name into a dictionary return myServiceproviders; } //22. [AllowAnonymous] [HttpPost] [Route("CreateUserProductMapping")] [Tags("Finished", "ServiceProvider")] [EndpointSummary("Create assigned user to product")] public async Task CreateUserProductMapping(CreateUserProductMappingModel createUserProductMappingModel) { if (createUserProductMappingModel.ContextId == Guid.Empty || createUserProductMappingModel.UserId == Guid.Empty) { return BadRequest("Invalid request"); } else { Console.WriteLine($"CreateUserProductMappings called with ownerId: {createUserProductMappingModel.ContextId}, {createUserProductMappingModel.ContextId}"); UserProductMapping userProductMapping = new UserProductMapping(createUserProductMappingModel.ContextId, createUserProductMappingModel.ContextId); var result = await _adminDal.CreateUserProductMappingAsync(userProductMapping); return Ok(result); } } //23. [AllowAnonymous] [HttpPost] [Route("GetUserProductMappingsForProduct")] public async Task> GetUserProductMappingsForProduct(Guid serviceProviderId) { Console.WriteLine($"GetUserProductMappingsForServiceProvider called with serviceProviderId: {serviceProviderId}"); Dictionary 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; } [HttpPost] [Route(APIUrls.AddProductRouteName)] [Tags("In-Progress", "Product")] public async Task AddProduct([FromBody] Product product) { Console.WriteLine("AddProduct called"); if (product == null) { return BadRequest("Product is required"); } else { var result = _adminDal.AddProduct(product); return Ok(result); } } [AllowAnonymous] [HttpPost] [Route("GetQRCodeByProductId")] [Tags("In-Progress", "Product")] public async Task GetQRCodeByProductId([FromBody] Guid productId) { Console.WriteLine("GetQRCode called"); if (productId == Guid.Empty) { return BadRequest("Product is required"); } else { //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); //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")); Console.WriteLine($"qrCodeLogo: {rootpath}/myimage.png"); MemoryStream ms = new MemoryStream(); qrCodeImage.Save(ms, ImageFormat.Jpeg); byte[] byteImage = ms.ToArray(); var SigBase64 = Convert.ToBase64String(byteImage); // Get Base64 return Ok(SigBase64); } } } }