508 lines
19 KiB
C#
508 lines
19 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using QRCoder;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using AyCode.Core.Enums;
|
|
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.Entities;
|
|
using AyCode.Services.SignalRs;
|
|
using AyCode.Utils.Extensions;
|
|
using TIAM.Entities.Drivers;
|
|
using TIAM.Services;
|
|
|
|
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());
|
|
|
|
[NonAction]
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
private async Task<bool> CompanyDataChanging(Company company, TrackingState trackingState)
|
|
{
|
|
var logText = $"[{trackingState.ToString().ToUpper()}] CompanyDataChanging called; Id: {company.Id}; OwnerId: {company.OwnerId}; Name: {company.Name}";
|
|
|
|
if (company.Name.IsNullOrEmpty())
|
|
{
|
|
_logger.Error(logText);
|
|
return false;
|
|
}
|
|
|
|
_logger.Info(logText);
|
|
|
|
switch (trackingState)
|
|
{
|
|
case TrackingState.Add:
|
|
if (company.Id.IsNullOrEmpty()) company.Id = Guid.NewGuid();
|
|
|
|
//if (company.OwnerId.IsNullOrEmpty()) company.OwnerId = Guid.Parse("540271F6-C604-4C16-8160-D5A7CAFEDF00"); //TESZT - J.
|
|
|
|
company.SetProfile(new Profile(Guid.NewGuid(), company.Name));
|
|
company.Profile.SetAddress(new Address(Guid.NewGuid(), "Controller AddCompanyAsync; address text..."));
|
|
|
|
return await adminDal.CreateServiceProviderAsync(company);
|
|
|
|
case TrackingState.Update:
|
|
return await adminDal.UpdateCompanyAsync(company);
|
|
case TrackingState.Remove:
|
|
return await adminDal.RemoveCompanyAsync(company);
|
|
|
|
case TrackingState.Get:
|
|
case TrackingState.GetAll:
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(trackingState), trackingState, null);
|
|
}
|
|
}
|
|
|
|
[NonAction]
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
[SignalR(SignalRTags.AddCompany)]
|
|
public async Task<string> AddCompanyAsync(Company company)
|
|
=> await CompanyDataChanging(company, TrackingState.Add) ? company.ToJson() : string.Empty;
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.UpdateServiceProviderRouteName)]
|
|
[SignalR(SignalRTags.UpdateCompany)]
|
|
public async Task<string> UpdateServiceProvider(Company company)
|
|
=> await CompanyDataChanging(company, TrackingState.Update) ? company.ToJson() : string.Empty;
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.RemoveServiceProviderRouteName)]
|
|
[SignalR(SignalRTags.RemoveCompany)]
|
|
public async Task<string> RemoveServiceProvider(Company company)
|
|
=> await CompanyDataChanging(company, TrackingState.Remove) ? company.ToJson() : string.Empty;
|
|
|
|
//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; Id: {serializedServiceProviderModel.Id}");
|
|
|
|
return await AddCompanyAsync(serializedServiceProviderModel.CreateMainEntity());
|
|
}
|
|
|
|
//16.
|
|
[AllowAnonymous]
|
|
[HttpGet]
|
|
[Route(APIUrls.GetServiceProvidersRouteName)]
|
|
[SignalR(SignalRTags.GetCompanies)]
|
|
public async Task<string> GetServiceProviders()
|
|
{
|
|
return await adminDal.GetServiceProvidersJsonAsync();
|
|
}
|
|
|
|
//18.
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.GetServiceProviderByIdRouteName)]
|
|
[SignalR(SignalRTags.GetCompany)]
|
|
public async Task<string> GetServiceProviderById([FromBody] Guid id)
|
|
{
|
|
_logger.Info($@"GetServiceProviderById called with id: {id}");
|
|
List<Company> compList = new List<Company>();
|
|
var result = await adminDal.GetServiceProviderByIdAsync(id);
|
|
compList.Add(result);
|
|
return compList.ToJson();
|
|
}
|
|
|
|
|
|
//17.
|
|
[Authorize]
|
|
[HttpPost]
|
|
[Route(APIUrls.GetServiceProvidersByOwnerIdRouteName)]
|
|
[Tags("Finished", "ServiceProvider")]
|
|
[SignalR(SignalRTags.GetPropertiesByOwnerId)]
|
|
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;
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpPost]
|
|
[Route(APIUrls.GetCompaniesByContextIdRouteName)]
|
|
[Tags("Finished", "ServiceProvider")]
|
|
[SignalR(SignalRTags.GetCompaniesByContextId)]
|
|
public async Task<string> GetCompaniesByContextId([FromBody] Guid ownerId)
|
|
{
|
|
_logger.Info($@"GetServiceProvidersByOwnerId called with ownerId: {ownerId}");
|
|
|
|
var serviceProviders = await adminDal.GetServiceProvidersByOwnerIdAsync(ownerId);
|
|
|
|
//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 serviceProviders.ToJson();
|
|
}
|
|
|
|
//22.
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.CreateUserProductMappingRouteName)]
|
|
[Tags("Finished", "ServiceProvider")]
|
|
[EndpointSummary("Create assigned user to product")]
|
|
[SignalR(SignalRTags.CreateUserProductMapping)]
|
|
public async Task<IActionResult> 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<string> 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<string> 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.GetUserProductMappingsByProductIdRouteName)]
|
|
[SignalR(SignalRTags.GetUserProductMappingsByProductId)]
|
|
public async Task<string> GetUserProductMappingsByProductId(Guid productId)
|
|
{
|
|
_logger.Info($@"GetUserProductMappingsByUserId called with serviceProviderId: {productId}");
|
|
|
|
var userProductMappings = adminDal.GetAllUserProductMappings();
|
|
|
|
var myUserProductMappings = userProductMappings.Where(x => x.ProductId == productId).ToList();
|
|
//put serviceprovider id and name into a dictionary
|
|
|
|
return myUserProductMappings.ToJson();
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.GetUserProductMappingsByUserIdRouteName)]
|
|
[SignalR(SignalRTags.GetUserProductMappingsByUserId)]
|
|
public async Task<string> GetUserProductMappingsByUserId(Guid userId)
|
|
{
|
|
_logger.Info($@"GetUserProductMappingsByUserId called with userId: {userId}");
|
|
|
|
var userProductMappings = adminDal.GetAllUserProductMappings();
|
|
|
|
var myUserProductMappings = userProductMappings.Where(x => x.UserId == userId).OrderBy(x => x.ProductId).ToList();
|
|
//put serviceprovider id and name into a dictionary
|
|
|
|
return myUserProductMappings.ToJson();
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.GetUserProductMappingByIdRouteName)]
|
|
[SignalR(SignalRTags.GetUserProductMappingById)]
|
|
public async Task<string> GetUserProductMappingById(Guid id)
|
|
{
|
|
_logger.Info($@"GetUserProductMappingsByUserId called with userId: {id}");
|
|
|
|
var userProductMappings = adminDal.GetAllUserProductMappings();
|
|
|
|
var myUserProductMappings = userProductMappings.Where(x => x.Id == id).ToList();
|
|
//put serviceprovider id and name into a dictionary
|
|
|
|
return myUserProductMappings.ToJson();
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.GetAllUserProductMappingsRouteName)]
|
|
[SignalR(SignalRTags.GetAllUserProductMappings)]
|
|
public async Task<string> GetAllUserProductMappings()
|
|
{
|
|
_logger.Info($@"GetAllUserProductMappings called");
|
|
|
|
|
|
var serviceProviders = adminDal.GetAllUserProductMappings()!.OrderBy(x => x.ProductId);
|
|
|
|
//put serviceprovider id and name into a dictionary
|
|
|
|
return serviceProviders.ToJson();
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet]
|
|
[Route(APIUrls.GetCarsForUserProductMappingRouteName + "/{userProductMappingId}")]
|
|
[SignalR(SignalRTags.GetCarsForUserProductMapping)]
|
|
public async Task<List<Car>> GetCarsForUserProductMapping(Guid userProductMappingId)
|
|
{
|
|
_logger.Info($@"GetCarsForUserProductMapping called with userProductMappingId: {userProductMappingId}");
|
|
|
|
var cars = adminDal.GetCarByUserProductMappingId(userProductMappingId);
|
|
|
|
return cars;
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet]
|
|
[Route(APIUrls.GetAllCarsRouteName)]
|
|
[SignalR(SignalRTags.GetAllCars)]
|
|
public async Task<List<Car>> GetAllCars()
|
|
{
|
|
_logger.Info($@"GetAllCars called ");
|
|
|
|
var cars = await adminDal.GetAllCarsAsync();
|
|
|
|
return cars;
|
|
}
|
|
|
|
[NonAction]
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
private async Task<bool> CarDataChanging(Car car, TrackingState trackingState)
|
|
{
|
|
var logText = $"[{trackingState.ToString().ToUpper()}] CarDataChanging called; Id: {car.Id}; OwnerId: {car.UserProductMappingId}; LicensePlate: {car.LicencePlate}";
|
|
|
|
if (car.UserProductMappingId.IsNullOrEmpty() || car.LicencePlate.IsNullOrWhiteSpace())
|
|
{
|
|
_logger.Error(logText);
|
|
return false;
|
|
}
|
|
|
|
_logger.Info(logText);
|
|
|
|
switch (trackingState)
|
|
{
|
|
case TrackingState.Add:
|
|
if (car.Id.IsNullOrEmpty()) car.Id = Guid.NewGuid();
|
|
|
|
return await adminDal.AddCarAsync(car);
|
|
case TrackingState.Update:
|
|
return await adminDal.UpdateCarAsync(car);
|
|
case TrackingState.Remove:
|
|
return await adminDal.RemoveCarAsync(car);
|
|
|
|
case TrackingState.Get:
|
|
case TrackingState.GetAll:
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(trackingState), trackingState, null);
|
|
}
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.CreateCarRouteName)]
|
|
[Tags("Finished", "Cars")]
|
|
[EndpointSummary("Create car")]
|
|
[SignalR(SignalRTags.CreateCar)]
|
|
public async Task<Car> CreateCar(Car car)
|
|
{
|
|
var result = await CarDataChanging(car, TrackingState.Add);
|
|
if (result)
|
|
return car;
|
|
else return null;
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.UpdateCarRouteName)]
|
|
[Tags("Finished", "Cars")]
|
|
[EndpointSummary("Update car")]
|
|
[SignalR(SignalRTags.UpdateCar)]
|
|
public async Task<Car> UpdateCar(Car car)
|
|
{
|
|
var result = await CarDataChanging(car, TrackingState.Update);
|
|
if (result)
|
|
return car;
|
|
else return null;
|
|
}
|
|
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.DeleteCarRouteName)]
|
|
[Tags("Finished", "Cars")]
|
|
[EndpointSummary("Delete car")]
|
|
[SignalR(SignalRTags.DeleteCar)]
|
|
public async Task<IActionResult> DeleteCar(Car car)
|
|
=> await CarDataChanging(car, TrackingState.Remove) ? Ok(car) : BadRequest("Invalid request");
|
|
|
|
[HttpPost]
|
|
[Route(APIUrls.AddProductRouteName)]
|
|
[Tags("In-Progress", "Product")]
|
|
[SignalR(SignalRTags.AddProduct)]
|
|
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(product);
|
|
}
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|
|
|
|
[NonAction]
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
public async Task<string> GetProductsByOwnerId(Guid serviceProviderId)
|
|
{
|
|
_logger.Info($@"GetProductsByServiceProviderId called with serviceProviderId: {serviceProviderId}");
|
|
|
|
if (serviceProviderId == Guid.Empty) return await Task.FromResult(string.Empty);
|
|
|
|
var products = adminDal.GetProductsJsonByServiceProviderId(serviceProviderId);
|
|
return await Task.FromResult(products);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.GetProductsByServiceProviderIdRouteName)]
|
|
[Tags("In-Progress", "Product")]
|
|
[SignalR(SignalRTags.GetProductsByOwnerId)]
|
|
public async Task<string> GetProductsByServiceProviderId([FromBody] Guid serviceProviderId)
|
|
{
|
|
_logger.Info($@"GetProductsByServiceProviderId called with serviceProviderId: {serviceProviderId}");
|
|
|
|
if (serviceProviderId == Guid.Empty)
|
|
{
|
|
return await Task.FromResult("Invalid request");
|
|
}
|
|
|
|
var result = await GetProductsByOwnerId(serviceProviderId);
|
|
return result;
|
|
}
|
|
|
|
[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;
|
|
}
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.GetProductByIdRouteName)]
|
|
[Tags("In-Progress", "Product")]
|
|
[SignalR(SignalRTags.GetProductById)]
|
|
public async Task<Product> GetProductById(Guid productId)
|
|
{
|
|
_logger.Info("GetAllProducts called");
|
|
|
|
var products = adminDal.GetProductById(productId);
|
|
|
|
return products;
|
|
|
|
}
|
|
|
|
|
|
[NonAction]
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
[SignalR(SignalRTags.GetProductsById)]
|
|
public async Task<List<Product>> GetProductsById(Guid productId)
|
|
{
|
|
_logger.Info("GetAllProducts called");
|
|
|
|
var product = await GetProductById(productId);
|
|
var products = new List<Product>();
|
|
if (product != null) {
|
|
products.Add(product);
|
|
}
|
|
return products;
|
|
|
|
}
|
|
}
|
|
} |