using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using System.Text.Json; using TIAM.Database.DataLayers.ServiceProviders; using TIAM.Entities.Permissions; using TIAM.Entities.Products; using TIAM.Entities.ServiceProviders; using TIAM.Entities.Users; using TIAMWebApp.Shared.Application.Models; namespace TIAMWebApp.Server.Controllers { [Authorize] [ApiController] [Route("api/[controller]")] public class ServiceProviderAPIController : ControllerBase { private ServiceProviderDal _serviceProviderDal; private readonly ILogger _logger; public ServiceProviderAPIController(ILogger logger, ServiceProviderDal serviceProviderDal) { _logger = logger; _serviceProviderDal = serviceProviderDal; } //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 _serviceProviderDal.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 _serviceProviderDal.GetServiceProvidersAsync(); } //18. [AllowAnonymous] [HttpPost] [Route("GetServiceProviderById")] public async Task GetServiceProviderById([FromBody] Guid id) { Console.WriteLine($"GetServiceProviderById called with id: {id}"); return await _serviceProviderDal.GetServiceProviderByIdAsync(id); } //17. [AllowAnonymous] [HttpPost] [Route("GetServiceProvidersByOwnerId")] [Tags("Finished", "ServiceProvider")] public async Task> GetServiceProvidersByOwnerId(Guid ownerId) { Console.WriteLine($"GetServiceProvidersByOwnerId called with ownerId: {ownerId}"); var serviceProviders = await _serviceProviderDal.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 serviceprovider 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 _serviceProviderDal.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 _serviceProviderDal.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("CreateProduct")] [Tags("In-Progress", "Product")] public async Task CreateProduct([FromBody] TiamProduct product) { Console.WriteLine("CreateProduct called"); if (product == null) { return BadRequest("Product is required"); } else { var result = _serviceProviderDal.CreateProductAsync(product); return Ok(result); } } } }