ressource to reSource + fixs + resourceController (upload and show)
This commit is contained in:
parent
f9b3344a77
commit
fed416d078
@ -5,10 +5,10 @@ using System.Text;
|
|||||||
|
|
||||||
namespace Manager.Interfaces.DTO
|
namespace Manager.Interfaces.DTO
|
||||||
{
|
{
|
||||||
public class RessourceDTO
|
public class ResourceDTO
|
||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public RessourceType Type { get; set; }
|
public ResourceType Type { get; set; }
|
||||||
public string Label { get; set; }
|
public string Label { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -5,10 +5,10 @@ using System.Text;
|
|||||||
|
|
||||||
namespace Manager.Interfaces.DTO
|
namespace Manager.Interfaces.DTO
|
||||||
{
|
{
|
||||||
public class RessourceDetailDTO
|
public class ResourceDetailDTO
|
||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public RessourceType Type { get; set; }
|
public ResourceType Type { get; set; }
|
||||||
public string Label { get; set; }
|
public string Label { get; set; }
|
||||||
public DateTime DateCreation { get; set; }
|
public DateTime DateCreation { get; set; }
|
||||||
public string Data { get; set; }
|
public string Data { get; set; }
|
||||||
@ -11,7 +11,7 @@ namespace Manager.Interfaces.DTO
|
|||||||
public string Label { get; set; } // use in manager
|
public string Label { get; set; } // use in manager
|
||||||
public List<TranslationDTO> Title { get; set; }
|
public List<TranslationDTO> Title { get; set; }
|
||||||
public List<TranslationDTO> Description { get; set; }
|
public List<TranslationDTO> Description { get; set; }
|
||||||
public string ImageId { get; set; } // == RessourceId
|
public string ImageId { get; set; } // == ResourceId
|
||||||
public string ConfigurationId { get; set; }
|
public string ConfigurationId { get; set; }
|
||||||
public bool IsSubSection { get; set; } // true if part of menu type
|
public bool IsSubSection { get; set; } // true if part of menu type
|
||||||
public string ParentId { get; set; } // only if it's an subsection
|
public string ParentId { get; set; } // only if it's an subsection
|
||||||
|
|||||||
@ -10,7 +10,7 @@ namespace Manager.Interfaces.DTO
|
|||||||
public int Zoom { get; set; } // Default = 18
|
public int Zoom { get; set; } // Default = 18
|
||||||
public MapType MapType { get; set; } // Default = Hybrid
|
public MapType MapType { get; set; } // Default = Hybrid
|
||||||
public List<GeoPointDTO> Points { get; set; }
|
public List<GeoPointDTO> Points { get; set; }
|
||||||
public string Icon { get; set; } // url to ressource id (local) or on internet
|
public string Icon { get; set; } // url to resource id (local) or on internet
|
||||||
}
|
}
|
||||||
|
|
||||||
public class GeoPointDTO
|
public class GeoPointDTO
|
||||||
@ -18,8 +18,8 @@ namespace Manager.Interfaces.DTO
|
|||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public List<TranslationDTO> Title { get; set; }
|
public List<TranslationDTO> Title { get; set; }
|
||||||
public List<TranslationDTO> Description { get; set; }
|
public List<TranslationDTO> Description { get; set; }
|
||||||
public string Image { get; set; } // url to ressource id (local) or on internet
|
public string Image { get; set; } // url to resource id (local) or on internet
|
||||||
public string ImageType { get; set; } // url or ressource
|
public string ImageType { get; set; } // url or resource
|
||||||
public List<TranslationDTO> Text { get; set; }
|
public List<TranslationDTO> Text { get; set; }
|
||||||
public string Latitude { get; set; }
|
public string Latitude { get; set; }
|
||||||
public string Longitude { get; set; }
|
public string Longitude { get; set; }
|
||||||
|
|||||||
@ -12,6 +12,6 @@ namespace Manager.Interfaces.DTO
|
|||||||
public class ImageDTO {
|
public class ImageDTO {
|
||||||
public List<TranslationDTO> Title { get; set; }
|
public List<TranslationDTO> Title { get; set; }
|
||||||
public List<TranslationDTO> Description { get; set; }
|
public List<TranslationDTO> Description { get; set; }
|
||||||
public string Source { get; set; } // url to ressource id (local) or on internet
|
public string Source { get; set; } // url to resource id (local) or on internet
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,6 @@ namespace Manager.Interfaces.DTO
|
|||||||
public class VideoDTO
|
public class VideoDTO
|
||||||
{
|
{
|
||||||
//public string Title { get; set; } // Dictionary<string, object> with all languages
|
//public string Title { get; set; } // Dictionary<string, object> with all languages
|
||||||
public string Source { get; set; } // url to ressource id (local) or on internet
|
public string Source { get; set; } // url to resource id (local) or on internet
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,6 @@ namespace Manager.Interfaces.DTO
|
|||||||
public class WebDTO
|
public class WebDTO
|
||||||
{
|
{
|
||||||
//public string Title { get; set; } // Dictionary<string, object> with all languages
|
//public string Title { get; set; } // Dictionary<string, object> with all languages
|
||||||
public string Source { get; set; } // url to ressource id (local) or on internet
|
public string Source { get; set; } // url to resource id (local) or on internet
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,9 +8,9 @@ using System.Text;
|
|||||||
namespace Manager.Interfaces.Models
|
namespace Manager.Interfaces.Models
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ressource Information
|
/// Resource Information
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Ressource
|
public class Resource
|
||||||
{
|
{
|
||||||
[BsonId]
|
[BsonId]
|
||||||
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
|
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
|
||||||
@ -18,7 +18,7 @@ namespace Manager.Interfaces.Models
|
|||||||
|
|
||||||
[BsonElement("Type")]
|
[BsonElement("Type")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public RessourceType Type { get; set; }
|
public ResourceType Type { get; set; }
|
||||||
|
|
||||||
[BsonElement("Label")]
|
[BsonElement("Label")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
@ -31,9 +31,9 @@ namespace Manager.Interfaces.Models
|
|||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string Data { get; set; }
|
public string Data { get; set; }
|
||||||
|
|
||||||
public RessourceDTO ToDTO()
|
public ResourceDTO ToDTO()
|
||||||
{
|
{
|
||||||
return new RessourceDTO()
|
return new ResourceDTO()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
Label = Label,
|
Label = Label,
|
||||||
@ -41,9 +41,9 @@ namespace Manager.Interfaces.Models
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public RessourceDetailDTO ToDetailDTO()
|
public ResourceDetailDTO ToDetailDTO()
|
||||||
{
|
{
|
||||||
return new RessourceDetailDTO()
|
return new ResourceDetailDTO()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
Label = Label,
|
Label = Label,
|
||||||
@ -55,7 +55,7 @@ namespace Manager.Interfaces.Models
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum RessourceType
|
public enum ResourceType
|
||||||
{
|
{
|
||||||
Image,
|
Image,
|
||||||
Video,
|
Video,
|
||||||
@ -24,7 +24,7 @@ namespace Manager.Interfaces.Models
|
|||||||
public List<GeoPoint> Points { get; set; }
|
public List<GeoPoint> Points { get; set; }
|
||||||
|
|
||||||
[BsonElement("Icon")]
|
[BsonElement("Icon")]
|
||||||
public string Icon { get; set; } // url to ressource id (local) or on internet
|
public string Icon { get; set; } // url to resource id (local) or on internet
|
||||||
|
|
||||||
public MapDTO ToDetailDTO()
|
public MapDTO ToDetailDTO()
|
||||||
{
|
{
|
||||||
@ -47,7 +47,7 @@ namespace Manager.Interfaces.Models
|
|||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public List<TranslationDTO> Title { get; set; }
|
public List<TranslationDTO> Title { get; set; }
|
||||||
public List<TranslationDTO> Description { get; set; }
|
public List<TranslationDTO> Description { get; set; }
|
||||||
public string Image { get; set; } // url to ressource id (local) or on internet
|
public string Image { get; set; } // url to resource id (local) or on internet
|
||||||
public List<TranslationDTO> Text { get; set; }
|
public List<TranslationDTO> Text { get; set; }
|
||||||
public string Latitude { get; set; }
|
public string Latitude { get; set; }
|
||||||
public string Longitude { get; set; }
|
public string Longitude { get; set; }
|
||||||
|
|||||||
@ -33,7 +33,7 @@ namespace Manager.Interfaces.Models
|
|||||||
{
|
{
|
||||||
public List<TranslationDTO> Title { get; set; }
|
public List<TranslationDTO> Title { get; set; }
|
||||||
public List<TranslationDTO> Description { get; set; }
|
public List<TranslationDTO> Description { get; set; }
|
||||||
public string Source { get; set; } // url to ressource id (local) or on internet
|
public string Source { get; set; } // url to resource id (local) or on internet
|
||||||
|
|
||||||
public ImageDTO ToDTO()
|
public ImageDTO ToDTO()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -84,7 +84,7 @@ namespace ManagerService.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new device
|
/// Create a new device
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="newRessource">New device info</param>
|
/// <param name="newDevice">New device info</param>
|
||||||
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
|
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 409)]
|
[ProducesResponseType(typeof(string), 409)]
|
||||||
|
|||||||
318
ManagerService/Controllers/ResourceController.cs
Normal file
318
ManagerService/Controllers/ResourceController.cs
Normal file
@ -0,0 +1,318 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
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 Newtonsoft.Json;
|
||||||
|
using NSwag.Annotations;
|
||||||
|
|
||||||
|
namespace ManagerService.Controllers
|
||||||
|
{
|
||||||
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
||||||
|
[ApiController, Route("api/[controller]")]
|
||||||
|
[OpenApiTag("Resource", Description = "Resource management")]
|
||||||
|
public class ResourceController : ControllerBase
|
||||||
|
{
|
||||||
|
private ResourceDatabaseService _resourceService;
|
||||||
|
private readonly ILogger<ResourceController> _logger;
|
||||||
|
|
||||||
|
public ResourceController(ILogger<ResourceController> logger, ResourceDatabaseService resourceService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_resourceService = resourceService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a list of all resources (summary)
|
||||||
|
/// </summary>
|
||||||
|
[ProducesResponseType(typeof(List<ResourceDTO>), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpGet]
|
||||||
|
public ObjectResult Get()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
List<Resource> resources = _resourceService.GetAll();
|
||||||
|
|
||||||
|
return new OkObjectResult(resources.Select(r => r.ToDTO()));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a specific resource
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">id resource</param>
|
||||||
|
[AllowAnonymous]
|
||||||
|
[ProducesResponseType(typeof(ResourceDetailDTO), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpGet("{id}/detail")]
|
||||||
|
public ObjectResult GetDetail(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Resource resource = _resourceService.GetById(id);
|
||||||
|
|
||||||
|
if (resource == null)
|
||||||
|
throw new KeyNotFoundException("This resource was not found");
|
||||||
|
|
||||||
|
return new OkObjectResult(resource.ToDetailDTO());
|
||||||
|
}
|
||||||
|
catch (KeyNotFoundException ex)
|
||||||
|
{
|
||||||
|
return new NotFoundObjectResult(ex.Message) {};
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Show a specific resource (as a picture or video stream)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">id resource</param>
|
||||||
|
[AllowAnonymous]
|
||||||
|
[ProducesResponseType(typeof(FileResult), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public ActionResult Show(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Resource resource = _resourceService.GetById(id);
|
||||||
|
|
||||||
|
if (resource == null)
|
||||||
|
throw new KeyNotFoundException("This resource was not found");
|
||||||
|
|
||||||
|
var file = Convert.FromBase64String(resource.Data);
|
||||||
|
|
||||||
|
if (resource.Type == ResourceType.Image)
|
||||||
|
{
|
||||||
|
return new FileContentResult(file, "image/png");
|
||||||
|
}
|
||||||
|
if (resource.Type == ResourceType.Video)
|
||||||
|
{
|
||||||
|
return new FileContentResult(file, "application/octet-stream");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FileContentResult(file, "image/png");
|
||||||
|
}
|
||||||
|
catch (KeyNotFoundException ex)
|
||||||
|
{
|
||||||
|
return new NotFoundObjectResult(ex.Message) { };
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upload a specific resource (picture or video)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">id resource</param>
|
||||||
|
[AllowAnonymous] // TODO: TO DELETEEE
|
||||||
|
[ProducesResponseType(typeof(string), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpPost("upload"), DisableRequestSizeLimit]
|
||||||
|
public IActionResult Upload([FromForm] string label, [FromForm] string type) // Create but with local //[FromBody] ResourceDetailDTO uploadResource
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
var test = label;
|
||||||
|
var test0 = type;
|
||||||
|
/*if (uploadResource == null)
|
||||||
|
throw new ArgumentNullException("Resource param is null");*/
|
||||||
|
ResourceDetailDTO uploadResource = new ResourceDetailDTO();
|
||||||
|
uploadResource.Type = (ResourceType) Enum.Parse(typeof(ResourceType), type);
|
||||||
|
uploadResource.Label = label;
|
||||||
|
|
||||||
|
var file = Request.Form.Files[0];
|
||||||
|
|
||||||
|
if (file.Length > 0)
|
||||||
|
{
|
||||||
|
var stringResult = "";
|
||||||
|
if (file.Length > 0)
|
||||||
|
{
|
||||||
|
using (var ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
file.CopyTo(ms);
|
||||||
|
var fileBytes = ms.ToArray();
|
||||||
|
stringResult = Convert.ToBase64String(fileBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Todo add some verification ?
|
||||||
|
Resource resource = new Resource();
|
||||||
|
resource.Label = uploadResource.Label;
|
||||||
|
resource.Type = uploadResource.Type;
|
||||||
|
resource.DateCreation = DateTime.Now;
|
||||||
|
resource.Data = stringResult;
|
||||||
|
Resource resourceCreated = _resourceService.Create(resource);
|
||||||
|
return Ok(resourceCreated.ToDTO());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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>
|
||||||
|
/// Create a new resource
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="newResource">New resource info</param>
|
||||||
|
[ProducesResponseType(typeof(ResourceDetailDTO), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
|
[ProducesResponseType(typeof(string), 409)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpPost]
|
||||||
|
public ObjectResult Create([FromBody] ResourceDetailDTO newResource)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (newResource == null)
|
||||||
|
throw new ArgumentNullException("Resource param is null");
|
||||||
|
|
||||||
|
// Todo add some verification ?
|
||||||
|
Resource resource = new Resource();
|
||||||
|
resource.Label = newResource.Label;
|
||||||
|
resource.Type = newResource.Type;
|
||||||
|
resource.DateCreation = DateTime.Now;
|
||||||
|
resource.Data = newResource.Data;
|
||||||
|
|
||||||
|
Resource resourceCreated = _resourceService.Create(resource);
|
||||||
|
|
||||||
|
return new OkObjectResult(resourceCreated.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 resource
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="updatedResource">Resource to update</param>
|
||||||
|
[ProducesResponseType(typeof(ResourceDetailDTO), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpPut]
|
||||||
|
public ObjectResult Update([FromBody] ResourceDetailDTO updatedResource)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (updatedResource == null)
|
||||||
|
throw new ArgumentNullException("Resource param is null");
|
||||||
|
|
||||||
|
Resource resource = _resourceService.GetById(updatedResource.Id);
|
||||||
|
|
||||||
|
if (resource == null)
|
||||||
|
throw new KeyNotFoundException("Resource does not exist");
|
||||||
|
|
||||||
|
// Todo add some verification ?
|
||||||
|
resource.Label = updatedResource.Label;
|
||||||
|
resource.Type = updatedResource.Type;
|
||||||
|
resource.Data = updatedResource.Data;
|
||||||
|
|
||||||
|
Resource resourceModified = _resourceService.Update(updatedResource.Id, resource);
|
||||||
|
|
||||||
|
return new OkObjectResult(resourceModified.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 resource
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">Id of resource 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("Resource param is null");
|
||||||
|
|
||||||
|
if (!_resourceService.IsExist(id))
|
||||||
|
throw new KeyNotFoundException("Resource does not exist");
|
||||||
|
|
||||||
|
_resourceService.Remove(id);
|
||||||
|
|
||||||
|
return new ObjectResult("The resource 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 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,248 +0,0 @@
|
|||||||
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 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -4,7 +4,7 @@
|
|||||||
"anonymousAuthentication": true,
|
"anonymousAuthentication": true,
|
||||||
"iisExpress": {
|
"iisExpress": {
|
||||||
"applicationUrl": "http://localhost:50185",
|
"applicationUrl": "http://localhost:50185",
|
||||||
"sslPort": 44339
|
"sslPort": 44316
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
|||||||
59
ManagerService/Services/ResourceDatabaseService.cs
Normal file
59
ManagerService/Services/ResourceDatabaseService.cs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Manager.Interfaces.Models;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace Manager.Services
|
||||||
|
{
|
||||||
|
public class ResourceDatabaseService
|
||||||
|
{
|
||||||
|
private readonly IMongoCollection<Resource> _Resources;
|
||||||
|
|
||||||
|
public ResourceDatabaseService(IConfiguration config)
|
||||||
|
{
|
||||||
|
var client = new MongoClient(config.GetConnectionString("TabletDb"));
|
||||||
|
var database = client.GetDatabase("TabletDb");
|
||||||
|
_Resources = database.GetCollection<Resource>("Resources");
|
||||||
|
}
|
||||||
|
public List<Resource> GetAll()
|
||||||
|
{
|
||||||
|
return _Resources.Find(r => true).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Resource GetByType(ResourceType type)
|
||||||
|
{
|
||||||
|
return _Resources.Find<Resource>(r => r.Type == type).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Resource GetById(string id)
|
||||||
|
{
|
||||||
|
return _Resources.Find<Resource>(r => r.Id == id).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsExist(string id)
|
||||||
|
{
|
||||||
|
return _Resources.Find<Resource>(r => r.Id == id).FirstOrDefault() != null ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Resource Create(Resource resource)
|
||||||
|
{
|
||||||
|
_Resources.InsertOne(resource);
|
||||||
|
return resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Resource Update(string id, Resource resourceIn)
|
||||||
|
{
|
||||||
|
_Resources.ReplaceOne(r => r.Id == id, resourceIn);
|
||||||
|
return resourceIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(string id)
|
||||||
|
{
|
||||||
|
_Resources.DeleteOne(r => r.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,59 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Manager.Interfaces.Models;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using MongoDB.Driver;
|
|
||||||
|
|
||||||
namespace Manager.Services
|
|
||||||
{
|
|
||||||
public class RessourceDatabaseService
|
|
||||||
{
|
|
||||||
private readonly IMongoCollection<Ressource> _Ressources;
|
|
||||||
|
|
||||||
public RessourceDatabaseService(IConfiguration config)
|
|
||||||
{
|
|
||||||
var client = new MongoClient(config.GetConnectionString("TabletDb"));
|
|
||||||
var database = client.GetDatabase("TabletDb");
|
|
||||||
_Ressources = database.GetCollection<Ressource>("Ressources");
|
|
||||||
}
|
|
||||||
public List<Ressource> GetAll()
|
|
||||||
{
|
|
||||||
return _Ressources.Find(r => true).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Ressource GetByType(RessourceType type)
|
|
||||||
{
|
|
||||||
return _Ressources.Find<Ressource>(r => r.Type == type).FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Ressource GetById(string id)
|
|
||||||
{
|
|
||||||
return _Ressources.Find<Ressource>(r => r.Id == id).FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsExist(string id)
|
|
||||||
{
|
|
||||||
return _Ressources.Find<Ressource>(r => r.Id == id).FirstOrDefault() != null ? true : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Ressource Create(Ressource ressource)
|
|
||||||
{
|
|
||||||
_Ressources.InsertOne(ressource);
|
|
||||||
return ressource;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Ressource Update(string id, Ressource ressourceIn)
|
|
||||||
{
|
|
||||||
_Ressources.ReplaceOne(r => r.Id == id, ressourceIn);
|
|
||||||
return ressourceIn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Remove(string id)
|
|
||||||
{
|
|
||||||
_Ressources.DeleteOne(r => r.Id == id);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -9,10 +9,12 @@ using Microsoft.AspNetCore.Builder;
|
|||||||
using Microsoft.AspNetCore.Diagnostics;
|
using Microsoft.AspNetCore.Diagnostics;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
using Microsoft.AspNetCore.HttpsPolicy;
|
using Microsoft.AspNetCore.HttpsPolicy;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
@ -21,6 +23,7 @@ using NSwag.Generation.AspNetCore;
|
|||||||
using NSwag.Generation.Processors.Security;
|
using NSwag.Generation.Processors.Security;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
@ -59,6 +62,12 @@ namespace ManagerService
|
|||||||
.AllowAnyHeader();
|
.AllowAnyHeader();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
services.Configure<FormOptions>(o => {
|
||||||
|
o.ValueLengthLimit = int.MaxValue;
|
||||||
|
o.MultipartBodyLengthLimit = int.MaxValue;
|
||||||
|
o.MemoryBufferThreshold = int.MaxValue;
|
||||||
|
});
|
||||||
|
|
||||||
// Authentication
|
// Authentication
|
||||||
|
|
||||||
var tokensConfiguration = Configuration.GetSection("Tokens");
|
var tokensConfiguration = Configuration.GetSection("Tokens");
|
||||||
@ -103,7 +112,7 @@ namespace ManagerService
|
|||||||
services.AddScoped<UserDatabaseService>();
|
services.AddScoped<UserDatabaseService>();
|
||||||
services.AddScoped<SectionDatabaseService>();
|
services.AddScoped<SectionDatabaseService>();
|
||||||
services.AddScoped<ConfigurationDatabaseService>();
|
services.AddScoped<ConfigurationDatabaseService>();
|
||||||
services.AddScoped<RessourceDatabaseService>();
|
services.AddScoped<ResourceDatabaseService>();
|
||||||
services.AddScoped<DeviceDatabaseService>();
|
services.AddScoped<DeviceDatabaseService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user