using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Manager.Framework.Business; using Manager.Interfaces.DTO; using Manager.Interfaces.Models; using Manager.Services; using ManagerService.Service.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using NSwag.Annotations; namespace ManagerService.Controllers { [Authorize] // TODO Add ROLES (Roles = "Admin") [ApiController, Route("api/[controller]")] [OpenApiTag("Instance", Description = "Instance management")] public class InstanceController : ControllerBase { private InstanceDatabaseService _instanceService; private UserDatabaseService _userService; private readonly ILogger _logger; private readonly ProfileLogic _profileLogic; public InstanceController(ILogger logger, InstanceDatabaseService instanceService, UserDatabaseService userService, ProfileLogic profileLogic) { _logger = logger; _instanceService = instanceService; _userService = userService; _profileLogic = profileLogic; } /// /// Get a list of instance /// [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet] public ObjectResult Get() { try { List instances = _instanceService.GetAll(); return new OkObjectResult(instances); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get a specific instance /// /// id instance [ProducesResponseType(typeof(InstanceDTO), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{id}")] public ObjectResult GetDetail(string id) { try { Instance instance = _instanceService.GetById(id); if (instance == null) throw new KeyNotFoundException("This instance was not found"); return new OkObjectResult(instance.ToDTO()); } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) {}; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Create an instance /// /// New instance info //[AllowAnonymous] [ProducesResponseType(typeof(InstanceDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 500)] [HttpPost] public ObjectResult CreateInstance([FromBody] Instance newInstance) { try { if (newInstance == null) throw new ArgumentNullException("instance param is null"); newInstance.DateCreation = DateTime.Now; List instances = _instanceService.GetAll(); if (instances.Select(i => i.Name).Contains(newInstance.Name)) throw new InvalidOperationException("This name is already used"); Instance instanceCreated = _instanceService.Create(newInstance); return new OkObjectResult(instanceCreated.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) {}; } catch (InvalidOperationException ex) { return new ConflictObjectResult(ex.Message) {}; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Update an instance /// /// instance to update [ProducesResponseType(typeof(InstanceDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpPut] public ObjectResult Updateinstance([FromBody] Instance updatedInstance) { try { if (updatedInstance == null) throw new ArgumentNullException("instance param is null"); Instance instance = _instanceService.GetById(updatedInstance.Id); if (instance == null) throw new KeyNotFoundException("instance does not exist"); Instance instanceModified = _instanceService.Update(updatedInstance.Id, instance); return new OkObjectResult(instanceModified.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) {}; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) {}; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get Instance by pincode /// /// Code pin [AllowAnonymous] [ProducesResponseType(typeof(InstanceDTO), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("byPin")] public ObjectResult GetInstanceByPinCode([FromQuery] int pinCode) { try { Instance instance = _instanceService.GetByPinCode(pinCode); if (instance == null) throw new KeyNotFoundException("Instance was not found"); return new OkObjectResult(instance.ToDTO()); } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Delete an instance /// /// Id of instance to delete [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{id}")] public ObjectResult DeleteInstance(string id) { try { if (id == null) throw new ArgumentNullException("instance param is null"); Instance instance = _instanceService.GetById(id); // Delete all user in instance List users = _userService.GetByInstanceId(instance.Id); foreach(var user in users) { _userService.Remove(user.Id); } if (instance == null) throw new KeyNotFoundException("instance does not exist"); _instanceService.Remove(id); return new ObjectResult("The instance has been deleted") { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } } }