using System; using System.Collections.Generic; using System.Linq; using Manager.Services; using ManagerService.Data; using ManagerService.DTOs; using ManagerService.Helpers; 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 readonly MyInfoMateDbContext _myInfoMateDbContext; private InstanceDatabaseService _instanceService; private UserDatabaseService _userService; private readonly ILogger _logger; private readonly ProfileLogic _profileLogic; public InstanceController(ILogger logger, InstanceDatabaseService instanceService, UserDatabaseService userService, ProfileLogic profileLogic, MyInfoMateDbContext myInfoMateDbContext) { _logger = logger; _instanceService = instanceService; _userService = userService; _profileLogic = profileLogic; _myInfoMateDbContext = myInfoMateDbContext; } /// /// Get a list of instance /// [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet] public ObjectResult Get() { try { //List instances = _instanceService.GetAll(); List instances = _myInfoMateDbContext.Instances.ToList(); 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 = _myInfoMateDbContext.Instances.FirstOrDefault(i => i.Id == id); //OldInstance 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(); Instance instance = _myInfoMateDbContext.Instances.FirstOrDefault(i => i.Id == id);*/ if (_myInfoMateDbContext.Instances.Any(i => i.Name == newInstance.Name)) throw new InvalidOperationException("This name is already used"); //OldInstance instanceCreated = _instanceService.Create(newInstance); _myInfoMateDbContext.Instances.Add(newInstance); _myInfoMateDbContext.SaveChanges(); return new OkObjectResult(newInstance.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 = _myInfoMateDbContext.Instances.FirstOrDefault(i => i.Id == updatedInstance.Id); //OldInstance instance = _instanceService.GetById(updatedInstance.Id); if (instance == null) throw new KeyNotFoundException("instance does not exist"); instance = updatedInstance; //OldInstance instanceModified = _instanceService.Update(updatedInstance.Id, instance); _myInfoMateDbContext.SaveChanges(); return new OkObjectResult(instance.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 { OldInstance 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"); OldInstance 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 }; } } } }