using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Threading.Tasks; 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("Ressource", Description = "Ressource management")] public class RessourceController : ControllerBase { private RessourceDatabaseService _ressourceService; private readonly ILogger _logger; public RessourceController(ILogger logger, RessourceDatabaseService ressourceService) { _logger = logger; _ressourceService = ressourceService; } /// /// Get a list of all ressources (summary) /// [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet] public ObjectResult Get() { try { List ressources = _ressourceService.GetAll(); return new OkObjectResult(ressources.Select(r => r.ToDTO())); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get a specific ressource /// /// id ressource [AllowAnonymous] [ProducesResponseType(typeof(RessourceDetailDTO), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{id}/detail")] public ObjectResult GetDetail(string id) { try { Ressource ressource = _ressourceService.GetById(id); if (ressource == null) throw new KeyNotFoundException("This ressource was not found"); return new OkObjectResult(ressource.ToDetailDTO()); } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) {}; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Show a specific ressource (as a picture or video stream) /// /// id ressource [AllowAnonymous] [ProducesResponseType(typeof(FileStreamResult), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{id}")] public ActionResult Show(string id) { try { Ressource ressource = _ressourceService.GetById(id); if (ressource == null) throw new KeyNotFoundException("This ressource was not found"); switch (ressource.Type) { case RessourceType.Image: return new FileStreamResult(System.IO.File.OpenRead(ressource.Data), "image/jpeg"); case RessourceType.Video: return PhysicalFile(@"d:\test\somemovie.mp4", "application/octet-stream"); // TODO ! default: return new OkObjectResult(ressource.ToDetailDTO()); } } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } // TODO upload ! /// /// Create a new ressource /// /// New ressource info [ProducesResponseType(typeof(RessourceDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 500)] [HttpPost] public ObjectResult Create([FromBody] RessourceDetailDTO newRessource) { try { if (newRessource == null) throw new ArgumentNullException("Ressource param is null"); // Todo add some verification ? Ressource ressource = new Ressource(); ressource.Label = newRessource.Label; ressource.Type = newRessource.Type; ressource.DateCreation = DateTime.Now; ressource.Data = newRessource.Data; Ressource ressourceCreated = _ressourceService.Create(ressource); return new OkObjectResult(ressourceCreated.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 a ressource /// /// Ressource to update [ProducesResponseType(typeof(RessourceDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpPut] public ObjectResult Update([FromBody] RessourceDetailDTO updatedRessource) { try { if (updatedRessource == null) throw new ArgumentNullException("Ressource param is null"); Ressource ressource = _ressourceService.GetById(updatedRessource.Id); if (ressource == null) throw new KeyNotFoundException("Ressource does not exist"); // Todo add some verification ? ressource.Label = updatedRessource.Label; ressource.Type = updatedRessource.Type; ressource.Data = updatedRessource.Data; Ressource ressourceModified = _ressourceService.Update(updatedRessource.Id, ressource); return new OkObjectResult(ressourceModified.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 }; } } /// /// Delete a ressource /// /// Id of ressource to delete [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{id}")] public ObjectResult Delete(string id) { try { if (id == null) throw new ArgumentNullException("Ressource param is null"); if (!_ressourceService.IsExist(id)) throw new KeyNotFoundException("Ressource does not exist"); _ressourceService.Remove(id); return new ObjectResult("The ressource 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 }; } } } }