273 lines
9.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Manager.Services;
using ManagerService.Data;
using ManagerService.DTOs;
using ManagerService.Helpers;
using ManagerService.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 readonly MyInfoMateDbContext _myInfoMateDbContext;
private InstanceDatabaseService _instanceService;
private UserDatabaseService _userService;
private readonly ILogger<InstanceController> _logger;
private readonly ProfileLogic _profileLogic;
IHexIdGeneratorService idService = new HexIdGeneratorService();
public InstanceController(ILogger<InstanceController> logger, InstanceDatabaseService instanceService, UserDatabaseService userService, ProfileLogic profileLogic, MyInfoMateDbContext myInfoMateDbContext)
{
_logger = logger;
_instanceService = instanceService;
_userService = userService;
_profileLogic = profileLogic;
_myInfoMateDbContext = myInfoMateDbContext;
}
/// <summary>
/// Get a list of instance
/// </summary>
[ProducesResponseType(typeof(List<Instance>), 200)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet]
public ObjectResult Get()
{
try
{
//List<OldInstance> instances = _instanceService.GetAll();
List<Instance> instances = _myInfoMateDbContext.Instances.ToList();
return new OkObjectResult(instances);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Get a specific instance
/// </summary>
/// <param name="id">id instance</param>
[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 };
}
}
/// <summary>
/// Create an instance
/// </summary>
/// <param name="newInstance">New instance info</param>
//[AllowAnonymous]
[ProducesResponseType(typeof(InstanceDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)]
[HttpPost]
public ObjectResult CreateInstance([FromBody] InstanceDTO newInstance)
{
try
{
if (newInstance == null)
throw new ArgumentNullException("instance param is null");
Instance instance = new Instance().FromDTO(newInstance);
instance.DateCreation = DateTime.Now.ToUniversalTime();
instance.Id = idService.GenerateHexId();
/*List<OldInstance> instances = _instanceService.GetAll();
Instance instance = _myInfoMateDbContext.Instances.FirstOrDefault(i => i.Id == id);*/
if (_myInfoMateDbContext.Instances.Any(i => i.Name == instance.Name))
throw new InvalidOperationException("This name is already used");
//OldInstance instanceCreated = _instanceService.Create(newInstance);
_myInfoMateDbContext.Instances.Add(instance);
_myInfoMateDbContext.SaveChanges();
return new OkObjectResult(instance.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 };
}
}
/// <summary>
/// Update an instance
/// </summary>
/// <param name="updatedinstance">instance to update</param>
[ProducesResponseType(typeof(InstanceDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpPut]
public ObjectResult Updateinstance([FromBody] InstanceDTO 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.DateCreation = updatedInstance.dateCreation != null ? updatedInstance.dateCreation.Value : instance.DateCreation;
instance.Name= updatedInstance.name != null ? updatedInstance.name : instance.Name;
instance.PinCode = updatedInstance.pinCode != null ? updatedInstance.pinCode : instance.PinCode;
//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 };
}
}
/// <summary>
/// Get Instance by pincode
/// </summary>
/// <param name="pinCode">Code pin</param>
[AllowAnonymous]
[ProducesResponseType(typeof(InstanceDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("byPin")]
public ObjectResult GetInstanceByPinCode([FromQuery] string pinCode)
{
try
{
//OldInstance instance = _instanceService.GetByPinCode(pinCode);
Instance instance = _myInfoMateDbContext.Instances.FirstOrDefault(i => i.PinCode == 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 };
}
}
/// <summary>
/// Delete an instance
/// </summary>
/// <param name="id">Id of instance to delete</param>
[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);
Instance instance = _myInfoMateDbContext.Instances.FirstOrDefault(i => i.Id == id);
if (instance == null)
throw new KeyNotFoundException("instance does not exist");
// Delete all user in instance
//List<OldUser> users = _userService.GetByInstanceId(instance.Id);
List<User> users = _myInfoMateDbContext.Users.Where(u => u.InstanceId == instance.Id).ToList();
foreach (var user in users)
{
//_userService.Remove(user.Id);
_myInfoMateDbContext.Users.Remove(user);
}
//_instanceService.Remove(id);
_myInfoMateDbContext.Instances.Remove(instance);
_myInfoMateDbContext.SaveChanges();
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 };
}
}
}
}