288 lines
10 KiB
C#
288 lines
10 KiB
C#
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;
|
|
|
|
namespace TIAMWebApp.Server.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/v1/[controller]")]
|
|
public class ServiceProviderAPIController(AdminDal adminDal, IEnumerable<IAcLogWriterBase> logWriters) : ControllerBase
|
|
{
|
|
private readonly TIAM.Core.Loggers.Logger<ServiceProviderAPIController> _logger = new(logWriters.ToArray());
|
|
|
|
//15.
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.CreateServiceProviderRouteName)]
|
|
[Tags("In-Progress", "ServiceProvider")]
|
|
[EndpointSummary("Create service provider")]
|
|
public async Task<string> 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<Company>();
|
|
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
|
|
{
|
|
|
|
_logger.Info($@"ServiceProvider to be creating; id: {id}, name: {name}, ownerId: {ownerId}");
|
|
|
|
var toCreate = new Company(id, name, ownerId, Guid.NewGuid())
|
|
{
|
|
CommissionPercent = commissionRate
|
|
};
|
|
|
|
toCreate.SetProfile(new Profile(Guid.NewGuid(), toCreate.Name));
|
|
toCreate.Profile.SetAddress(new Address(Guid.NewGuid(), "Controller CreateServiceProvider; address text..."));
|
|
|
|
var result = await adminDal.CreateServiceProviderAsync(toCreate);
|
|
if (!result)
|
|
{
|
|
serviceProvider = null;
|
|
return string.Empty;//BadRequest(result);
|
|
}
|
|
else
|
|
{
|
|
|
|
return toCreate.ToJson();//Ok(toCreate);
|
|
}
|
|
}
|
|
}
|
|
|
|
else {
|
|
|
|
return string.Empty;//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)
|
|
{
|
|
_logger.Info($@"GetServiceProviderById called with id: {id}");
|
|
|
|
return await adminDal.GetServiceProviderByIdAsync(id);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.UpdateServiceProviderRouteName)]
|
|
public async Task<string> 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")]
|
|
public async Task<Dictionary<Guid, string>> 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")]
|
|
public async Task<IActionResult> CreateUserProductMapping(CreateUserProductMappingModel createUserProductMappingModel)
|
|
{
|
|
if(createUserProductMappingModel.ContextId == Guid.Empty || createUserProductMappingModel.UserId == Guid.Empty)
|
|
{
|
|
return BadRequest("Invalid request");
|
|
}
|
|
else
|
|
{
|
|
_logger.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)
|
|
{
|
|
_logger.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)
|
|
{
|
|
_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<IActionResult> 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<string> GetAllProducts()
|
|
{
|
|
_logger.Info("GetAllProducts called");
|
|
|
|
var products = adminDal.GetProductsJson();
|
|
if (products != null)
|
|
{
|
|
return products;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|
|
} |