Added methods for mdlf migration in MyInfomate + firebase service (linked)
This commit is contained in:
parent
9ff5a7fec8
commit
aedd5f33fa
@ -13,11 +13,13 @@ using Manager.Interfaces.Models;
|
||||
using Manager.Services;
|
||||
using ManagerService.Helpers;
|
||||
using ManagerService.Service.Services;
|
||||
using ManagerService.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Server.IIS.Core;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NSwag.Annotations;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
@ -31,17 +33,21 @@ namespace ManagerService.Controllers
|
||||
private ResourceDatabaseService _resourceService;
|
||||
private SectionDatabaseService _sectionService;
|
||||
private ConfigurationDatabaseService _configurationService;
|
||||
private FirebaseService _firebaseService;
|
||||
private FirebaseStorageService _firebaseStorageService;
|
||||
private readonly ILogger<ResourceController> _logger;
|
||||
|
||||
private static int MaxWidth = 1024;
|
||||
private static int MaxHeight = 1024;
|
||||
|
||||
public ResourceController(ILogger<ResourceController> logger, ResourceDatabaseService resourceService, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService)
|
||||
public ResourceController(ILogger<ResourceController> logger, ResourceDatabaseService resourceService, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService, FirebaseService firebaseService, FirebaseStorageService firebaseStorageService)
|
||||
{
|
||||
_logger = logger;
|
||||
_resourceService = resourceService;
|
||||
_sectionService = sectionService;
|
||||
_configurationService = configurationService;
|
||||
_firebaseService = firebaseService;
|
||||
_firebaseStorageService = firebaseStorageService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -560,5 +566,61 @@ namespace ManagerService.Controllers
|
||||
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create new ressources from json
|
||||
/// </summary>
|
||||
/// <param name="newSection">New section info</param>
|
||||
[ProducesResponseType(typeof(SectionDTO), 200)]
|
||||
[ProducesResponseType(typeof(string), 400)]
|
||||
[ProducesResponseType(typeof(string), 409)]
|
||||
[ProducesResponseType(typeof(string), 500)]
|
||||
[HttpPost("json")]
|
||||
public async Task<ObjectResult> CreateFromJSON()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
string json = System.IO.File.ReadAllText("C:/Users/ThomasFransolet/Documents/Documents/Perso/MuseeDeLaFraise/Aout 2024/TabletDb.Resources.json");
|
||||
JArray resourceAll = JArray.Parse(json);
|
||||
|
||||
foreach (var resource in resourceAll)
|
||||
{
|
||||
Resource newResource = new Resource();
|
||||
newResource.Id = (string)resource["_id"]["$oid"];
|
||||
|
||||
var test = _resourceService.GetById(newResource.Id);
|
||||
if (test == null)
|
||||
{
|
||||
newResource.Type = ResourceType.Image;
|
||||
newResource.DateCreation = (DateTime)resource["DateCreation"]["$date"];
|
||||
newResource.Label = (string)resource["Label"];
|
||||
newResource.InstanceId = "65ccc67265373befd15be511";
|
||||
|
||||
var dataTest = (string)resource["Data"];
|
||||
var downloadUrl = await _firebaseStorageService.UploadBase64Async(dataTest, newResource.Id, newResource.InstanceId);
|
||||
newResource.Url = downloadUrl;
|
||||
|
||||
// TO Uncomment if needed
|
||||
//Resource createdResource = _resourceService.Create(newResource);
|
||||
}
|
||||
}
|
||||
|
||||
return new OkObjectResult(true);
|
||||
}
|
||||
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 };
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,16 +2,20 @@
|
||||
using Manager.Interfaces.DTO;
|
||||
using Manager.Interfaces.Models;
|
||||
using Manager.Services;
|
||||
using ManagerService.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Mqtt.Client.AspNetCore.Services;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NSwag.Annotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace ManagerService.Controllers
|
||||
{
|
||||
@ -20,15 +24,17 @@ namespace ManagerService.Controllers
|
||||
[OpenApiTag("Section", Description = "Section management")]
|
||||
public class SectionController : ControllerBase
|
||||
{
|
||||
private ResourceDatabaseService _resourceService;
|
||||
private SectionDatabaseService _sectionService;
|
||||
private ConfigurationDatabaseService _configurationService;
|
||||
private readonly ILogger<SectionController> _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public SectionController(IConfiguration configuration, ILogger<SectionController> logger, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService)
|
||||
public SectionController(IConfiguration configuration, ILogger<SectionController> logger, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService, ResourceDatabaseService resourceService)
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_resourceService = resourceService;
|
||||
_sectionService = sectionService;
|
||||
_configurationService = configurationService;
|
||||
}
|
||||
@ -659,6 +665,430 @@ namespace ManagerService.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update section image url order
|
||||
/// </summary>
|
||||
[ProducesResponseType(typeof(string), 200)]
|
||||
[ProducesResponseType(typeof(string), 400)]
|
||||
[ProducesResponseType(typeof(string), 404)]
|
||||
[ProducesResponseType(typeof(string), 500)]
|
||||
[HttpPut("imageURL")]
|
||||
public ObjectResult UpdateImageURL()
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Section> sections = _sectionService.GetAll("65ccc67265373befd15be511"); // get by instance ID (hardcoded MDLF)
|
||||
foreach (var section in sections)
|
||||
{
|
||||
section.ImageSource = CheckAndUpdateURL(section.ImageSource, _resourceService);
|
||||
switch (section.Type)
|
||||
{
|
||||
case SectionType.Map:
|
||||
var mapSection = JsonConvert.DeserializeObject<MapDTO>(section.Data);
|
||||
mapSection.iconSource = CheckAndUpdateURL(mapSection.iconSource, _resourceService);
|
||||
foreach (var point in mapSection.points)
|
||||
{
|
||||
point.imageUrl = CheckAndUpdateURL(point.imageUrl, _resourceService);
|
||||
|
||||
foreach (var content in point.contents)
|
||||
{
|
||||
content.resourceUrl = CheckAndUpdateURL(content.resourceUrl, _resourceService);
|
||||
}
|
||||
}
|
||||
section.Data = JsonConvert.SerializeObject(mapSection); // Include all info from specific section as JSON
|
||||
|
||||
break;
|
||||
case SectionType.Slider:
|
||||
var sliderData = JsonConvert.DeserializeObject<SliderDTO>(section.Data);
|
||||
foreach (var content in sliderData.contents)
|
||||
{
|
||||
content.resourceUrl = CheckAndUpdateURL(content.resourceUrl, _resourceService);
|
||||
}
|
||||
section.Data = JsonConvert.SerializeObject(sliderData); // Include all info from specific section as JSON
|
||||
|
||||
break;
|
||||
case SectionType.Video:
|
||||
break;
|
||||
case SectionType.Web:
|
||||
break;
|
||||
case SectionType.Quizz:
|
||||
var quizData = JsonConvert.DeserializeObject<QuizzDTO>(section.Data);
|
||||
foreach (var badLevelLabel in quizData.bad_level.label)
|
||||
{
|
||||
badLevelLabel.resourceUrl = CheckAndUpdateURL(badLevelLabel.resourceUrl, _resourceService);
|
||||
}
|
||||
|
||||
foreach (var mediumLevelLabel in quizData.medium_level.label)
|
||||
{
|
||||
mediumLevelLabel.resourceUrl = CheckAndUpdateURL(mediumLevelLabel.resourceUrl, _resourceService);
|
||||
}
|
||||
|
||||
foreach (var goodLevelLabel in quizData.good_level.label)
|
||||
{
|
||||
goodLevelLabel.resourceUrl = CheckAndUpdateURL(goodLevelLabel.resourceUrl, _resourceService);
|
||||
}
|
||||
|
||||
foreach (var greatLevelLabel in quizData.great_level.label)
|
||||
{
|
||||
greatLevelLabel.resourceUrl = CheckAndUpdateURL(greatLevelLabel.resourceUrl, _resourceService);
|
||||
}
|
||||
|
||||
foreach (var question in quizData.questions)
|
||||
{
|
||||
question.imageBackgroundResourceUrl = CheckAndUpdateURL(question.imageBackgroundResourceUrl, _resourceService);
|
||||
foreach (var labelQuestion in question.label)
|
||||
{
|
||||
labelQuestion.resourceUrl = CheckAndUpdateURL(labelQuestion.resourceUrl, _resourceService);
|
||||
}
|
||||
foreach (var labelResponse in question.responses)
|
||||
{
|
||||
foreach (var LabelResponseLabel in labelResponse.label)
|
||||
{
|
||||
LabelResponseLabel.resourceUrl = CheckAndUpdateURL(LabelResponseLabel.resourceUrl, _resourceService);
|
||||
}
|
||||
}
|
||||
}
|
||||
section.Data = JsonConvert.SerializeObject(quizData); // Include all info from specific section as JSON
|
||||
|
||||
break;
|
||||
case SectionType.Menu:
|
||||
break;
|
||||
}
|
||||
// Update DB
|
||||
//_sectionService.Update(section.Id, section);
|
||||
}
|
||||
|
||||
return new ObjectResult("OK, done") { StatusCode = 200 };
|
||||
}
|
||||
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 };
|
||||
}
|
||||
}
|
||||
|
||||
private string CheckAndUpdateURL(string sourceURL, ResourceDatabaseService resourceDatabaseService)
|
||||
{
|
||||
if (sourceURL != null && (sourceURL.Contains("192.168.1.19") || sourceURL.Contains("localhost")))
|
||||
{
|
||||
if (sourceURL.Contains("localhost"))
|
||||
{
|
||||
}
|
||||
string[] segments = sourceURL.Split('/');
|
||||
string sourceIDFromURL = segments[segments.Length - 1];
|
||||
Resource resource = resourceDatabaseService.GetById(sourceIDFromURL);
|
||||
|
||||
return resource != null ? resource.Url : sourceURL;
|
||||
} else return sourceURL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new sections from json
|
||||
/// </summary>
|
||||
/// <param name="newSection">New section info</param>
|
||||
[ProducesResponseType(typeof(SectionDTO), 200)]
|
||||
[ProducesResponseType(typeof(string), 400)]
|
||||
[ProducesResponseType(typeof(string), 409)]
|
||||
[ProducesResponseType(typeof(string), 500)]
|
||||
[HttpPost("json")]
|
||||
public ObjectResult CreateFromJSON()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
string json = System.IO.File.ReadAllText("C:/Users/ThomasFransolet/Documents/Documents/Perso/MuseeDeLaFraise/Aout 2024/TabletDb.Sections.json");
|
||||
var sections = JsonConvert.DeserializeObject<SectionDTO[]>(json);
|
||||
|
||||
foreach (var sectionJSON in sections)
|
||||
{
|
||||
// Todo add some verification ?
|
||||
Section section = new Section();
|
||||
section.Id = sectionJSON.id;
|
||||
section.InstanceId = "65ccc67265373befd15be511"; // MDLF hardcoded
|
||||
section.Label = sectionJSON.label;
|
||||
section.ImageId = sectionJSON.imageId;
|
||||
section.ImageSource = sectionJSON.imageSource;
|
||||
section.ConfigurationId = sectionJSON.configurationId;
|
||||
section.DateCreation = sectionJSON.dateCreation;
|
||||
section.IsSubSection = sectionJSON.isSubSection;
|
||||
section.ParentId = sectionJSON.parentId;
|
||||
section.Type = sectionJSON.type;
|
||||
section.Title = sectionJSON.title;
|
||||
section.Description = sectionJSON.description;
|
||||
section.Order = _sectionService.GetAllFromConfiguration(section.ConfigurationId).Count;
|
||||
section.IsBeacon = false;
|
||||
|
||||
// Preparation
|
||||
//List<string> languages = _configuration.GetSection("SupportedLanguages").Get<List<string>>();
|
||||
|
||||
var contentArticle = new List<TranslationDTO>();
|
||||
|
||||
/*section.Title = LanguageInit.Init("Title", languages);
|
||||
section.Description = LanguageInit.Init("Description", languages);
|
||||
contentArticle = LanguageInit.Init("Content", languages);
|
||||
|
||||
section.Title = section.Title.OrderBy(t => t.language).ToList();
|
||||
section.Description = section.Description.OrderBy(d => d.language).ToList();*/
|
||||
|
||||
switch (section.Type)
|
||||
{
|
||||
case SectionType.Map:
|
||||
var sectionDataMap = JsonConvert.DeserializeObject<MapDTO>(sectionJSON.data);
|
||||
var mapDTO = new MapDTO();
|
||||
|
||||
mapDTO.mapType = sectionDataMap.mapType;
|
||||
mapDTO.mapProvider = MapProvider.Google;
|
||||
mapDTO.zoom = sectionDataMap.zoom;
|
||||
mapDTO.iconSource = sectionDataMap.iconSource;
|
||||
mapDTO.iconResourceId = sectionDataMap.iconResourceId;
|
||||
|
||||
//mapDTO.points = sectionDataMap.points ?? new List<GeoPointDTO>();
|
||||
|
||||
List<GeoPointDTO> geoPoints = new List<GeoPointDTO>();
|
||||
|
||||
JObject sectionMapp = JObject.Parse(sectionJSON.data);
|
||||
foreach (var point in sectionMapp["points"])
|
||||
{
|
||||
GeoPointDTO pointDTO = new GeoPointDTO();
|
||||
pointDTO.id = (int)point["id"];
|
||||
pointDTO.latitude = (string)point["latitude"];
|
||||
pointDTO.longitude = (string)point["longitude"];
|
||||
pointDTO.title = JsonConvert.DeserializeObject<List<TranslationDTO>>(point["title"].ToString());
|
||||
pointDTO.description = JsonConvert.DeserializeObject<List<TranslationDTO>>(point["description"].ToString());
|
||||
List<ContentGeoPoint> contents = new List<ContentGeoPoint>();
|
||||
|
||||
foreach (var image in point["images"])
|
||||
{
|
||||
ContentGeoPoint contentDTO = new ContentGeoPoint();
|
||||
contentDTO.resourceType = ResourceType.Image; // ce n'est que des images..
|
||||
contentDTO.resourceId = (string)image["imageResourceId"];
|
||||
contentDTO.resourceUrl = (string)image["imageSource"];
|
||||
|
||||
contents.Add(contentDTO);
|
||||
}
|
||||
pointDTO.contents = contents;
|
||||
|
||||
geoPoints.Add(pointDTO);
|
||||
}
|
||||
|
||||
mapDTO.points = geoPoints;
|
||||
mapDTO.categories = new List<CategorieDTO>();
|
||||
section.Data = JsonConvert.SerializeObject(mapDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Slider:
|
||||
//var sectionSlider = JsonConvert.DeserializeObject(sectionJSON.data);
|
||||
var sliderDTO = new SliderDTO();
|
||||
JObject sectionSlider = JObject.Parse(sectionJSON.data);
|
||||
|
||||
List<ContentDTO> newContents = new List<ContentDTO>();
|
||||
|
||||
if (sectionSlider["images"].Count() > 0)
|
||||
{
|
||||
}
|
||||
|
||||
foreach (var image in sectionSlider["images"])
|
||||
{
|
||||
ContentDTO newContent = new ContentDTO();
|
||||
int order = (int)image["order"];
|
||||
string resourceId = (string)image["resourceId"];
|
||||
string source = (string)image["source"];
|
||||
|
||||
newContent.order = order;
|
||||
newContent.resourceId = resourceId;
|
||||
newContent.resourceUrl = source;
|
||||
newContent.resourceType = ResourceType.Image; // ONLY IMAGE WAS POSSIBLE
|
||||
|
||||
newContent.title = JsonConvert.DeserializeObject<List<TranslationDTO>>(image["title"].ToString());
|
||||
newContent.description = JsonConvert.DeserializeObject<List<TranslationDTO>>(image["description"].ToString());
|
||||
newContents.Add(newContent);
|
||||
}
|
||||
sliderDTO.contents = newContents;
|
||||
section.Data = JsonConvert.SerializeObject(sliderDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Video:
|
||||
var sectionVideo = JsonConvert.DeserializeObject<VideoDTO>(sectionJSON.data);
|
||||
VideoDTO videoDTO = new VideoDTO();
|
||||
videoDTO.source = sectionVideo.source;
|
||||
section.Data = JsonConvert.SerializeObject(videoDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Web:
|
||||
var sectionWeb = JsonConvert.DeserializeObject<WebDTO>(sectionJSON.data);
|
||||
WebDTO webDTO = new WebDTO();
|
||||
webDTO.source = sectionWeb.source;
|
||||
section.Data = JsonConvert.SerializeObject(webDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Menu:
|
||||
var sectionMenu = JsonConvert.DeserializeObject<MenuDTO>(sectionJSON.data);
|
||||
MenuDTO menuDTO = new MenuDTO();
|
||||
menuDTO.sections = sectionMenu.sections;
|
||||
foreach (var sectionMenuu in menuDTO.sections)
|
||||
{
|
||||
sectionMenuu.instanceId = section.InstanceId;
|
||||
switch (sectionMenuu.type)
|
||||
{
|
||||
case SectionType.Map:
|
||||
var sectionMAPSub = JsonConvert.DeserializeObject<MapDTO>(sectionMenuu.data);
|
||||
var mapDTOSUB = new MapDTO();
|
||||
|
||||
mapDTOSUB.mapType = sectionMAPSub.mapType;
|
||||
mapDTOSUB.mapProvider = MapProvider.Google;
|
||||
mapDTOSUB.zoom = sectionMAPSub.zoom;
|
||||
mapDTOSUB.iconSource = sectionMAPSub.iconSource;
|
||||
mapDTOSUB.iconResourceId = sectionMAPSub.iconResourceId;
|
||||
|
||||
List<GeoPointDTO> geoPointsSUB = new List<GeoPointDTO>();
|
||||
|
||||
JObject sectionMappSUB = JObject.Parse(sectionMenuu.data);
|
||||
foreach (var pointSUB in sectionMappSUB["points"])
|
||||
{
|
||||
GeoPointDTO pointDTOSUB = new GeoPointDTO();
|
||||
pointDTOSUB.id = (int)pointSUB["id"];
|
||||
pointDTOSUB.latitude = (string)pointSUB["latitude"];
|
||||
pointDTOSUB.longitude = (string)pointSUB["longitude"];
|
||||
pointDTOSUB.title = JsonConvert.DeserializeObject<List<TranslationDTO>>(pointSUB["title"].ToString());
|
||||
pointDTOSUB.description = JsonConvert.DeserializeObject<List<TranslationDTO>>(pointSUB["description"].ToString());
|
||||
List<ContentGeoPoint> contentsSUB = new List<ContentGeoPoint>();
|
||||
|
||||
foreach (var imageSUB in pointSUB["images"])
|
||||
{
|
||||
ContentGeoPoint contentDTOSUB = new ContentGeoPoint();
|
||||
contentDTOSUB.resourceType = ResourceType.Image; // ce n'est que des images..
|
||||
contentDTOSUB.resourceId = (string)imageSUB["imageResourceId"];
|
||||
contentDTOSUB.resourceUrl = (string)imageSUB["imageSource"];
|
||||
|
||||
contentsSUB.Add(contentDTOSUB);
|
||||
}
|
||||
pointDTOSUB.contents = contentsSUB;
|
||||
|
||||
geoPointsSUB.Add(pointDTOSUB);
|
||||
}
|
||||
|
||||
mapDTOSUB.points = geoPointsSUB;
|
||||
mapDTOSUB.categories = new List<CategorieDTO>();
|
||||
section.Data = JsonConvert.SerializeObject(mapDTOSUB); // Include all info from specific section as JSON
|
||||
|
||||
break;
|
||||
case SectionType.Slider:
|
||||
|
||||
var sliderDTOSUB = new SliderDTO();
|
||||
JObject sliderSUB = JObject.Parse(sectionMenuu.data);
|
||||
|
||||
List<ContentDTO> newContentsSUB = new List<ContentDTO>();
|
||||
|
||||
foreach (var image in sliderSUB["images"])
|
||||
{
|
||||
ContentDTO newContentSUB = new ContentDTO();
|
||||
int order = (int)image["order"];
|
||||
string resourceId = (string)image["resourceId"];
|
||||
string source = (string)image["source"]; // TODO REPLACE SOURCE.. UPLOAD TO FIREBASE WHEN UPLOAD RESSOURCE FROM JSON..
|
||||
|
||||
newContentSUB.order = order;
|
||||
newContentSUB.resourceId = resourceId;
|
||||
newContentSUB.resourceUrl = source;
|
||||
newContentSUB.resourceType = ResourceType.Image; // ONLY IMAGE WAS POSSIBLE
|
||||
|
||||
newContentSUB.title = JsonConvert.DeserializeObject<List<TranslationDTO>>(image["title"].ToString());
|
||||
newContentSUB.description = JsonConvert.DeserializeObject<List<TranslationDTO>>(image["description"].ToString());
|
||||
newContentsSUB.Add(newContentSUB);
|
||||
}
|
||||
|
||||
sliderDTOSUB.contents = newContentsSUB;
|
||||
sectionMenuu.data = JsonConvert.SerializeObject(sliderDTOSUB);
|
||||
break;
|
||||
case SectionType.Video:
|
||||
var sectionVideoSUB = JsonConvert.DeserializeObject<VideoDTO>(sectionMenuu.data);
|
||||
VideoDTO videoDTOSUB = new VideoDTO();
|
||||
videoDTOSUB.source = sectionVideoSUB.source;
|
||||
section.Data = JsonConvert.SerializeObject(videoDTOSUB); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Web:
|
||||
var sectionWebSUB = JsonConvert.DeserializeObject<WebDTO>(sectionMenuu.data);
|
||||
WebDTO webDTOSUB = new WebDTO();
|
||||
webDTOSUB.source = sectionWebSUB.source;
|
||||
section.Data = JsonConvert.SerializeObject(webDTOSUB); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Quizz:
|
||||
var sectionSUBQuizz = JsonConvert.DeserializeObject<QuizzDTO>(sectionMenuu.data);
|
||||
var quizzSUBDTO = new QuizzDTO();
|
||||
quizzSUBDTO.questions = sectionSUBQuizz.questions;
|
||||
quizzSUBDTO.bad_level = sectionSUBQuizz.bad_level;
|
||||
quizzSUBDTO.medium_level = sectionSUBQuizz.medium_level;
|
||||
quizzSUBDTO.good_level = sectionSUBQuizz.medium_level;
|
||||
quizzSUBDTO.great_level = sectionSUBQuizz.great_level;
|
||||
|
||||
sectionMenuu.data = JsonConvert.SerializeObject(quizzSUBDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
}
|
||||
}
|
||||
section.Data = JsonConvert.SerializeObject(menuDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Quizz:
|
||||
var sectionQuizz = JsonConvert.DeserializeObject<QuizzDTO>(sectionJSON.data);
|
||||
QuizzDTO quizzDTO = new QuizzDTO();
|
||||
quizzDTO.questions = sectionQuizz.questions;
|
||||
quizzDTO.bad_level = sectionQuizz.bad_level;
|
||||
quizzDTO.medium_level = sectionQuizz.medium_level;
|
||||
quizzDTO.good_level = sectionQuizz.medium_level;
|
||||
quizzDTO.great_level = sectionQuizz.great_level;
|
||||
|
||||
section.Data = JsonConvert.SerializeObject(quizzDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
|
||||
|
||||
|
||||
// NEW CONTENTS AFTER MDLF
|
||||
case SectionType.Article:
|
||||
ArticleDTO articleDTO = new ArticleDTO();
|
||||
articleDTO.contents = new List<ContentDTO>();
|
||||
articleDTO.content = contentArticle;
|
||||
//articleDTO.audioIds = LanguageInit.Init("Audio", languages, true);
|
||||
|
||||
section.Data = JsonConvert.SerializeObject(articleDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.PDF:
|
||||
PdfDTO pdfDTO = new PdfDTO();
|
||||
section.Data = JsonConvert.SerializeObject(pdfDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Puzzle:
|
||||
PuzzleDTO puzzleDTO = new PuzzleDTO();
|
||||
section.Data = JsonConvert.SerializeObject(puzzleDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Agenda:
|
||||
AgendaDTO agendaDTO = new AgendaDTO();
|
||||
section.Data = JsonConvert.SerializeObject(agendaDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
case SectionType.Weather:
|
||||
WeatherDTO weatherDTO = new WeatherDTO();
|
||||
section.Data = JsonConvert.SerializeObject(weatherDTO); // Include all info from specific section as JSON
|
||||
break;
|
||||
}
|
||||
|
||||
//Section sectionCreated = _sectionService.Create(section);
|
||||
}
|
||||
|
||||
return new OkObjectResult(true);
|
||||
}
|
||||
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>
|
||||
/// Useless, just to generate dto code
|
||||
/// </summary>
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FirebaseAdmin" Version="3.0.0" />
|
||||
<PackageReference Include="FirebaseStorage.net" Version="1.0.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.30" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.2.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
|
||||
|
||||
18
ManagerService/Services/FirebaseService.cs
Normal file
18
ManagerService/Services/FirebaseService.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using FirebaseAdmin;
|
||||
using Google.Apis.Auth.OAuth2;
|
||||
|
||||
namespace ManagerService.Services
|
||||
{
|
||||
public class FirebaseService
|
||||
{
|
||||
public FirebaseService()
|
||||
{
|
||||
// Remplace le chemin par le chemin vers ton fichier JSON
|
||||
var pathToKey = "C:/Users/ThomasFransolet/Documents/Documents/Perso/MuseeDeLaFraise/mymuseum-3b97f-firebase-adminsdk-sdbbn-7ec3e24a91.json";
|
||||
FirebaseApp.Create(new AppOptions()
|
||||
{
|
||||
Credential = GoogleCredential.FromFile(pathToKey),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
64
ManagerService/Services/FirebaseStorageService.cs
Normal file
64
ManagerService/Services/FirebaseStorageService.cs
Normal file
@ -0,0 +1,64 @@
|
||||
using Firebase.Storage;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ManagerService.Services
|
||||
{
|
||||
|
||||
|
||||
public class FirebaseStorageService
|
||||
{
|
||||
private readonly FirebaseStorage _firebaseStorage;
|
||||
|
||||
public FirebaseStorageService()
|
||||
{
|
||||
// Remplace "your-project-id.appspot.com" par l'identifiant de ton bucket Firebase Storage
|
||||
_firebaseStorage = new FirebaseStorage("mymuseum-3b97f.appspot.com");
|
||||
}
|
||||
|
||||
public async Task<string> UploadFileAsync(Stream fileStream, string fileName, string instanceId)
|
||||
{
|
||||
// Téléversement du fichier dans le dossier "uploads"
|
||||
var task = _firebaseStorage
|
||||
.Child("pictures")
|
||||
.Child(instanceId)
|
||||
.Child(fileName)
|
||||
.PutAsync(fileStream);
|
||||
|
||||
// Optionnel: Suivre la progression du téléversement
|
||||
task.Progress.ProgressChanged += (s, e) => Console.WriteLine($"Progress: {e.Percentage} %");
|
||||
|
||||
// Attendre la fin du téléversement
|
||||
var downloadUrl = await task;
|
||||
|
||||
return downloadUrl;
|
||||
}
|
||||
|
||||
public async Task<string> UploadBase64Async(string base64String, string fileName, string instanceId)
|
||||
{
|
||||
// Convertir la chaîne base64 en un tableau de bytes
|
||||
byte[] bytes = Convert.FromBase64String(base64String);
|
||||
|
||||
// Créer un flux de mémoire à partir des bytes
|
||||
using (var stream = new MemoryStream(bytes))
|
||||
{
|
||||
// Téléversement du fichier dans le dossier "uploads"
|
||||
var task = _firebaseStorage
|
||||
.Child("pictures")
|
||||
.Child(instanceId)
|
||||
.Child(fileName)
|
||||
.PutAsync(stream);
|
||||
|
||||
// Optionnel: Suivre la progression du téléversement
|
||||
task.Progress.ProgressChanged += (s, e) => Console.WriteLine($"Progress: {e.Percentage} %");
|
||||
|
||||
// Attendre la fin du téléversement
|
||||
var downloadUrl = await task;
|
||||
|
||||
return downloadUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
using Firebase.Storage;
|
||||
using Manager.Framework.Business;
|
||||
using Manager.Framework.Models;
|
||||
using Manager.Helpers;
|
||||
@ -6,6 +7,7 @@ using Manager.Services;
|
||||
using ManagerService.Extensions;
|
||||
using ManagerService.Service;
|
||||
using ManagerService.Service.Services;
|
||||
using ManagerService.Services;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Diagnostics;
|
||||
@ -145,6 +147,8 @@ namespace ManagerService
|
||||
services.AddScoped<LanguageInit>();
|
||||
services.AddScoped<DeviceDatabaseService>();
|
||||
services.AddScoped<InstanceDatabaseService>();
|
||||
services.AddScoped<FirebaseService>();
|
||||
services.AddScoped<FirebaseStorageService>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user