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.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")] 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")] 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("CreateAssignedUser")] public async Task CreateAssignedUser(Guid productId, Guid userId) { if(productId == Guid.Empty || userId == Guid.Empty) { return BadRequest("Invalid request"); } else { Console.WriteLine($"CreateAssignedUsers called with ownerId: {productId}, {userId}"); AssignedUser assignedUser = new AssignedUser(productId, userId, 1); var result = await _serviceProviderDal.CreateAssignedUserAsync(assignedUser); return Ok(result); } } //23. [AllowAnonymous] [HttpPost] [Route("GetAssignedUsersForProduct")] public async Task> GetAssignedUsersForProduct(Guid serviceProviderId) { Console.WriteLine($"GetAssignedUsersForServiceProvider called with serviceProviderId: {serviceProviderId}"); Dictionary assignedUserDictionary = 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; } } }