249 lines
8.4 KiB
C#
249 lines
8.4 KiB
C#
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<RessourceController> _logger;
|
|
|
|
public RessourceController(ILogger<RessourceController> logger, RessourceDatabaseService ressourceService)
|
|
{
|
|
_logger = logger;
|
|
_ressourceService = ressourceService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a list of all ressources (summary)
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(List<RessourceDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet]
|
|
public ObjectResult Get()
|
|
{
|
|
try
|
|
{
|
|
List<Ressource> ressources = _ressourceService.GetAll();
|
|
|
|
return new OkObjectResult(ressources.Select(r => r.ToDTO()));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get a specific ressource
|
|
/// </summary>
|
|
/// <param name="id">id ressource</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Show a specific ressource (as a picture or video stream)
|
|
/// </summary>
|
|
/// <param name="id">id ressource</param>
|
|
[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 !
|
|
|
|
/// <summary>
|
|
/// Create a new ressource
|
|
/// </summary>
|
|
/// <param name="newRessource">New ressource info</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Update a ressource
|
|
/// </summary>
|
|
/// <param name="updatedRessource">Ressource to update</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Delete a ressource
|
|
/// </summary>
|
|
/// <param name="id">Id of ressource to delete</param>
|
|
[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 };
|
|
}
|
|
}
|
|
}
|
|
}
|