TourIAm/TIAMWebApp/Server/Controllers/ServiceProviderAPIControlle...

307 lines
11 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using QRCoder;
using System.Drawing;
using System.Drawing.Imaging;
using AyCode.Core.Server.Loggers;
using TIAM.Database.DataLayers.Admins;
//using TIAM.Database.DataLayers.ServiceProviders;
using TIAM.Entities.ServiceProviders;
using TIAM.Entities.Users;
using TIAMWebApp.Shared.Application.Models;
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.Profiles;
using TIAM.Entities.Addresses;
namespace TIAMWebApp.Server.Controllers
{
[Authorize]
[ApiController]
[Route("api/v1/[controller]")]
public class ServiceProviderAPIController : ControllerBase
{
private readonly AdminDal _adminDal;
private readonly ILogger<ServiceProviderAPIController> _logger;
public ServiceProviderAPIController(ILogger<ServiceProviderAPIController> logger, AdminDal adminDal)
{
_logger = logger;
_adminDal = adminDal;
}
//15.
[AllowAnonymous]
[HttpPost]
[Route(APIUrls.CreateServiceProviderRouteName)]
[Tags("In-Progress", "ServiceProvider")]
[EndpointSummary("Create service provider")]
public async Task<IActionResult> CreateServiceProvider([FromBody] ServiceProviderModel serializedServiceProviderModel)
{
GlobalLogger.Info(@"CreateUser called");
//if (serializedServiceProviderModel.GetArrayLength() == 0)
if (serializedServiceProviderModel == null)
{
return BadRequest("SerializedLoginModel is required");
}
else
{
//Company? serviceProvider = JObject.Parse(serializedServiceProviderModel.GetRawText()).ToObject<Company>();
var serviceProvider = serializedServiceProviderModel;
if (serviceProvider != null)
{
var id = Guid.NewGuid();
var name = serviceProvider.Name;
var commissionRate = serviceProvider.CommissionPercent;
Guid ownerId;
if(serviceProvider.OwnerId == Guid.Empty)
{
//no owner set yet
ownerId = serviceProvider.OwnerId;
}
else
{
ownerId = serviceProvider.OwnerId;
}
if (name is null)
{
return BadRequest("Invalid request");
}
else
{
GlobalLogger.Info($@"ServiceProvider to be created: {id}, {name}, {ownerId}");
Company toCreate = new Company(id, name, ownerId, Guid.NewGuid());
toCreate.Profile = new Profile();
toCreate.Profile.Id = Guid.NewGuid();
toCreate.Profile.Address = new Address();
toCreate.Profile.Address.Id = Guid.NewGuid();
toCreate.CommissionPercent = commissionRate;
var result = await _adminDal.CreateServiceProviderAsync(toCreate);
if (!result)
{
serviceProvider = null;
return BadRequest(result);
}
else
{
return Ok(toCreate);
}
}
}
else {
return BadRequest();
}
}
}
//16.
[AllowAnonymous]
[HttpGet]
[Route(APIUrls.GetServiceProvidersRouteName)]
public async Task<string> GetServiceProviders()
{
return await _adminDal.GetServiceProvidersJsonAsync();
}
//18.
[AllowAnonymous]
[HttpPost]
[Route(APIUrls.GetServiceProviderByIdRouteName)]
public async Task<Company?> GetServiceProviderById([FromBody] Guid id)
{
GlobalLogger.Info($@"GetServiceProviderById called with id: {id}");
return await _adminDal.GetServiceProviderByIdAsync(id);
}
[AllowAnonymous]
[HttpPost]
[Route(APIUrls.UpdateServiceProviderRouteName)]
public async Task<Company> UpdateServiceProvider(Company companyToModify)
{
GlobalLogger.Info($"UpdateServiceProvider called! + {companyToModify.Id}");
var result = await _adminDal.UpdateServiceProviderAsync(companyToModify);
if(result)
{
return companyToModify;
}
else
{
return null;
}
}
//17.
[Authorize]
[HttpPost]
[Route(APIUrls.GetServiceProvidersByOwnerIdRouteName)]
[Tags("Finished", "ServiceProvider")]
public async Task<Dictionary<Guid, string>> GetServiceProvidersByOwnerId([FromBody] Guid ownerId)
{
GlobalLogger.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")]
public async Task<IActionResult> CreateUserProductMapping(CreateUserProductMappingModel createUserProductMappingModel)
{
if(createUserProductMappingModel.ContextId == Guid.Empty || createUserProductMappingModel.UserId == Guid.Empty)
{
return BadRequest("Invalid request");
}
else
{
GlobalLogger.Info($@"CreateUserProductMappings called with ownerId: {createUserProductMappingModel.ContextId}, {createUserProductMappingModel.ContextId}");
var userProductMapping = new UserProductMapping(createUserProductMappingModel.ContextId, createUserProductMappingModel.ContextId);
var result = await _adminDal.AddUserProductMappingAsync(userProductMapping);
return Ok(result);
}
}
//23.
[AllowAnonymous]
[HttpPost]
[Route(APIUrls.GetUserProductMappingsForProductRouteName)]
public async Task<Dictionary<Guid, string>> GetUserProductMappingsForProduct(Guid serviceProviderId)
{
GlobalLogger.Info($@"GetUserProductMappingsForServiceProvider called with serviceProviderId: {serviceProviderId}");
var userProductMappingDictionary = new Dictionary<Guid, string>();
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<IActionResult> AddProduct([FromBody] Product product)
{
GlobalLogger.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<IActionResult> GetQRCodeByProductId([FromBody] Guid productId)
{
GlobalLogger.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"));
GlobalLogger.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)
{
GlobalLogger.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<string> GetAllProducts()
{
GlobalLogger.Info("GetAllProducts called");
var products = _adminDal.GetProductsJson();
if (products != null)
{
return products;
}
else
{
return null;
}
}
}
}