diff --git a/Manager.Interfaces/DTO/ResourceDTO.cs b/Manager.Interfaces/DTO/ResourceDTO.cs index 156c932..b9498c5 100644 --- a/Manager.Interfaces/DTO/ResourceDTO.cs +++ b/Manager.Interfaces/DTO/ResourceDTO.cs @@ -14,4 +14,13 @@ namespace Manager.Interfaces.DTO public DateTime dateCreation { get; set; } public string instanceId { get; set; } } + + public class ResourceOLDDTO + { + public MongoId _id { get; set; } + public ResourceType Type { get; set; } + public string Label { get; set; } + public DateCustom DateCreation { get; set; } + public string InstanceId { get; set; } + } } diff --git a/Manager.Interfaces/DTO/SectionDTO.cs b/Manager.Interfaces/DTO/SectionDTO.cs index 096c828..9f83359 100644 --- a/Manager.Interfaces/DTO/SectionDTO.cs +++ b/Manager.Interfaces/DTO/SectionDTO.cs @@ -1,7 +1,10 @@ using Manager.Interfaces.Models; +using MongoDB.Bson.IO; using System; using System.Collections.Generic; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; namespace Manager.Interfaces.DTO { @@ -27,4 +30,38 @@ namespace Manager.Interfaces.DTO public bool isBeacon { get; set; } // MyVisit - True if section use beacon, false otherwise public int? beaconId { get; set; } // MyVisit - Beacon' identifier } + + public class SectionForJsonDTO + { + public MongoId _id { get; set; } // Correspond à l'objet _id dans le JSON + public string Label { get; set; } // Correspond à "Label" dans le JSON + public List Title { get; set; } // Correspond à "Title" dans le JSON + public List Description { get; set; } // Correspond à "Description" dans le JSON + public string ImageId { get; set; } // Correspond à "ImageId" dans le JSON + public string ImageSource { get; set; } // Correspond à "ImageSource" dans le JSON + public string ConfigurationId { get; set; } // Correspond à "ConfigurationId" dans le JSON + public bool IsSubSection { get; set; } // Correspond à "IsSubSection" dans le JSON + public string ParentId { get; set; } // Correspond à "ParentId" dans le JSON + public int Type { get; set; } // Correspond à "Type" dans le JSON + public string Data { get; set; } // Correspond à "Data" dans le JSON + + public DateCustom DateCreation { get; set; } + public int? Order { get; set; } // Correspond à "Order" dans le JSON + public string InstanceId { get; set; } // Correspond à "InstanceId" dans le JSON + public bool IsBeacon { get; set; } // Correspond à "IsBeacon" dans le JSON + public string BeaconId { get; set; } // Correspond à "BeaconId" dans le JSON + public string Latitude { get; set; } // Correspond à "Latitude" dans le JSON + public string Longitude { get; set; } // Correspond à "Longitude" dans le JSON + public int? MeterZoneGPS { get; set; } // Correspond à "MeterZoneGPS" dans le JSON + } + + public class MongoId + { + public string oid { get; set; } // Correspond à "$oid" dans le JSON + } + + public class DateCustom + { + public DateTime date { get; set; } // Correspond à "$oid" dans le JSON + } } diff --git a/Manager.Interfaces/DTO/SubSection/OldArticleDTO.cs b/Manager.Interfaces/DTO/SubSection/OldArticleDTO.cs new file mode 100644 index 0000000..5b14f48 --- /dev/null +++ b/Manager.Interfaces/DTO/SubSection/OldArticleDTO.cs @@ -0,0 +1,25 @@ +using Manager.Interfaces.Models; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO +{ + public class OldArticleDTO + { + public List content { get; set; } + public bool isContentTop { get; set; } // MyVisit - True if content is displayed at top, false otherwise + public List audioIds { get; set; } + public bool isReadAudioAuto { get; set; } // MyVisit - True for audio play when open the article / false otherwise + public List images { get; set; } + } + + public class OldContentDTO + { + public List title { get; set; } + public List description { get; set; } + public string resourceId { get; set; } + public string source { get; set; } + public int order { get; set; } // Order to show + } +} diff --git a/ManagerService/Controllers/ResourceController.cs b/ManagerService/Controllers/ResourceController.cs index ce31528..912dfd3 100644 --- a/ManagerService/Controllers/ResourceController.cs +++ b/ManagerService/Controllers/ResourceController.cs @@ -18,6 +18,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Server.IIS.Core; using Microsoft.Extensions.Logging; +using MongoDB.Bson.Serialization.Serializers; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NSwag.Annotations; @@ -74,8 +75,8 @@ namespace ManagerService.Controllers resources = _resourceService.GetAll(instanceId); } - List resourceDTOs = new List(); - foreach(var resource in resources) + List resourceDTOs = new List(); + foreach (var resource in resources) { ResourceDTO resourceDTO = new ResourceDTO(); resourceDTO = resource.ToDTO(); @@ -107,7 +108,7 @@ namespace ManagerService.Controllers if (resource == null) throw new KeyNotFoundException("This resource was not found"); - + ResourceDTO resourceDTO = new ResourceDTO(); resourceDTO = resource.ToDTO(); /*if (resource.Type == ResourceType.ImageUrl) @@ -142,7 +143,7 @@ namespace ManagerService.Controllers } catch (KeyNotFoundException ex) { - return new NotFoundObjectResult(ex.Message) {}; + return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { @@ -235,23 +236,23 @@ namespace ManagerService.Controllers var resourceType = (ResourceType)Enum.Parse(typeof(ResourceType), type); List resources = new List(); - foreach (var file in Request.Form.Files) + foreach (var file in Request.Form.Files) { if (file.Length > 0) { var stringResult = ""; - double fileSizeibMbs = (double) ((double)file.Length) / (1024*1024); + double fileSizeibMbs = (double)((double)file.Length) / (1024 * 1024); if (fileSizeibMbs <= 1.5 || resourceType == ResourceType.Image) { using (var ms = new MemoryStream()) { file.CopyTo(ms); var fileBytes = ms.ToArray(); - if (resourceType == ResourceType.Image) + if (resourceType == ResourceType.Image) { bool isFort = instanceId == "633ee379d9405f32f166f047"; // If fort saint heribert, TODO add watermark in configuration and model - - if(isFort) // TODO We need to know for which purpose (mobile or tablet) + + if (isFort) // TODO We need to know for which purpose (mobile or tablet) { fileBytes = ImageHelper.ResizeAndAddWatermark(fileBytes, isFort, MaxWidth, MaxHeight); } @@ -325,11 +326,11 @@ namespace ManagerService.Controllers } catch (ArgumentNullException ex) { - return new BadRequestObjectResult(ex.Message) {}; + return new BadRequestObjectResult(ex.Message) { }; } catch (InvalidOperationException ex) { - return new ConflictObjectResult(ex.Message) {}; + return new ConflictObjectResult(ex.Message) { }; } catch (Exception ex) { @@ -372,11 +373,11 @@ namespace ManagerService.Controllers } catch (ArgumentNullException ex) { - return new BadRequestObjectResult(ex.Message) {}; + return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { - return new NotFoundObjectResult(ex.Message) {}; + return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { @@ -464,13 +465,13 @@ namespace ManagerService.Controllers { if (question.label != null) { - foreach (var questionLabel in question.label) + foreach (var questionLabel in question.label) { questionLabel.resourceUrl = questionLabel.resourceId == id ? null : questionLabel.resourceUrl; questionLabel.resourceId = questionLabel.resourceId == id ? null : questionLabel.resourceId; } } - + question.imageBackgroundResourceUrl = question.imageBackgroundResourceId == id ? null : question.imageBackgroundResourceUrl; question.imageBackgroundResourceId = question.imageBackgroundResourceId == id ? null : question.imageBackgroundResourceId; @@ -580,29 +581,61 @@ namespace ManagerService.Controllers { try { + string json = System.IO.File.ReadAllText("C:/Users/ThomasFransolet/Documents/Documents/Perso/UNOV/MongoDB 10-10-24 - MyMuseum before migration/TabletDb.Resources.json"); + var resourceAll = JsonConvert.DeserializeObject>(json); // Désérialiser en liste d'objets - 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) + using (StreamReader dataFile = new StreamReader("C:/Users/ThomasFransolet/Documents/Documents/Perso/UNOV/MongoDB 10-10-24 - MyMuseum before migration/TabletDb.ResourcesData.json")) + using (JsonTextReader dataReader = new JsonTextReader(dataFile)) { - Resource newResource = new Resource(); - newResource.Id = (string)resource["_id"]["$oid"]; - - var test = _resourceService.GetById(newResource.Id); - if (test == null) + while (dataReader.Read()) { - newResource.Type = ResourceType.Image; - newResource.DateCreation = (DateTime)resource["DateCreation"]["$date"]; - newResource.Label = (string)resource["Label"]; - newResource.InstanceId = "65ccc67265373befd15be511"; + if (dataReader.TokenType == JsonToken.StartObject) + { + JObject resourceData = JObject.Load(dataReader); - var dataTest = (string)resource["Data"]; - var downloadUrl = await _firebaseStorageService.UploadBase64Async(dataTest, newResource.Id, newResource.InstanceId); - newResource.Url = downloadUrl; + Resource newResource = new Resource(); + newResource.Id = (string)resourceData["ResourceId"]; - // TO Uncomment if needed - //Resource createdResource = _resourceService.Create(newResource); + var test = _resourceService.GetById(newResource.Id); + if (test == null) + { + + // Rechercher la ressource correspondante dans resourceAll + var resource = resourceAll.FirstOrDefault(r => r._id.oid == newResource.Id); + + if (resource != null && resource.InstanceId == "633ee379d9405f32f166f047") + { + newResource.Type = resource.Type; + newResource.DateCreation = resource.DateCreation.date; + newResource.Label = resource.Label; + newResource.InstanceId = resource.InstanceId; + + var dataTest = (string)resourceData["Data"]; + try { + // Uncomment if needed + /*var downloadUrl = await _firebaseStorageService.UploadBase64Async(dataTest, newResource.Id, newResource.InstanceId); + newResource.Url = downloadUrl; + Resource createdResource = _resourceService.Create(newResource);*/ + } + catch (Exception e) + { + Console.WriteLine("resource id " + newResource.Id); + Console.WriteLine(e); + } + } + } + else + { + // Rechercher la ressource correspondante dans resourceAll + var resource = resourceAll.FirstOrDefault(r => r._id.oid == newResource.Id); + + if (resource.Type != test.Type) + { + test.Type = resource.Type; + //_resourceService.Update(test.Id, test); + } + } + } } } @@ -623,4 +656,4 @@ namespace ManagerService.Controllers } } -} +} \ No newline at end of file diff --git a/ManagerService/Controllers/SectionController.cs b/ManagerService/Controllers/SectionController.cs index 37ef8f0..cef9d94 100644 --- a/ManagerService/Controllers/SectionController.cs +++ b/ManagerService/Controllers/SectionController.cs @@ -677,7 +677,7 @@ namespace ManagerService.Controllers { try { - List
sections = _sectionService.GetAll("65ccc67265373befd15be511"); // get by instance ID (hardcoded MDLF) + List
sections = _sectionService.GetAll("633ee379d9405f32f166f047"); // get by instance ID (hardcoded Fort) foreach (var section in sections) { section.ImageSource = CheckAndUpdateURL(section.ImageSource, _resourceService); @@ -752,6 +752,15 @@ namespace ManagerService.Controllers break; case SectionType.Menu: + break; + case SectionType.Article: + var articleData = JsonConvert.DeserializeObject(section.Data); + foreach (var content in articleData.contents) + { + content.resourceUrl = CheckAndUpdateURL(content.resourceUrl, _resourceService); + } + section.Data = JsonConvert.SerializeObject(articleData); // Include all info from specific section as JSON + break; } // Update DB @@ -776,7 +785,7 @@ namespace ManagerService.Controllers private string CheckAndUpdateURL(string sourceURL, ResourceDatabaseService resourceDatabaseService) { - if (sourceURL != null && (sourceURL.Contains("192.168.1.19") || sourceURL.Contains("localhost"))) + if (sourceURL != null && (sourceURL.Contains("192.168.1.19") || sourceURL.Contains("localhost") || sourceURL.Contains("https://api.mymuseum.be"))) { if (sourceURL.Contains("localhost")) { @@ -802,26 +811,28 @@ namespace ManagerService.Controllers { try { + var i = 1; + string json = System.IO.File.ReadAllText("C:/Users/ThomasFransolet/Documents/Documents/Perso/UNOV/MongoDB 10-10-24 - MyMuseum before migration/TabletDb.Sections.json"); - string json = System.IO.File.ReadAllText("C:/Users/ThomasFransolet/Documents/Documents/Perso/MuseeDeLaFraise/Aout 2024/TabletDb.Sections.json"); - var sections = JsonConvert.DeserializeObject(json); + var sections = JsonConvert.DeserializeObject(json); - foreach (var sectionJSON in sections) + + foreach (var sectionJSON in sections.Where(s => s.InstanceId == "633ee379d9405f32f166f047")) { // 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.Id = sectionJSON._id.oid; + section.InstanceId = "633ee379d9405f32f166f047"; // Fort hardcoded + section.Label = sectionJSON.Label; + section.ImageId = sectionJSON.ImageId; + section.ImageSource = sectionJSON.ImageSource; + section.ConfigurationId = sectionJSON.ConfigurationId; + section.DateCreation = sectionJSON.DateCreation.date; + section.IsSubSection = sectionJSON.IsSubSection; + section.ParentId = sectionJSON.ParentId; + section.Type = (SectionType)sectionJSON.Type; + section.Title = sectionJSON.Title; + section.Description = sectionJSON.Description; section.Order = _sectionService.GetAllFromConfiguration(section.ConfigurationId).Count; section.IsBeacon = false; @@ -840,7 +851,7 @@ namespace ManagerService.Controllers switch (section.Type) { case SectionType.Map: - var sectionDataMap = JsonConvert.DeserializeObject(sectionJSON.data); + /*var sectionDataMap = JsonConvert.DeserializeObject(sectionJSON.Data); var mapDTO = new MapDTO(); mapDTO.mapType = sectionDataMap.mapType; @@ -853,7 +864,7 @@ namespace ManagerService.Controllers List geoPoints = new List(); - JObject sectionMapp = JObject.Parse(sectionJSON.data); + JObject sectionMapp = JObject.Parse(sectionJSON.Data); foreach (var point in sectionMapp["points"]) { GeoPointDTO pointDTO = new GeoPointDTO(); @@ -880,12 +891,12 @@ namespace ManagerService.Controllers mapDTO.points = geoPoints; mapDTO.categories = new List(); - section.Data = JsonConvert.SerializeObject(mapDTO); // Include all info from specific section as JSON + 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); + JObject sectionSlider = JObject.Parse(sectionJSON.Data); List newContents = new List(); @@ -913,19 +924,19 @@ namespace ManagerService.Controllers section.Data = JsonConvert.SerializeObject(sliderDTO); // Include all info from specific section as JSON break; case SectionType.Video: - var sectionVideo = JsonConvert.DeserializeObject(sectionJSON.data); + var sectionVideo = JsonConvert.DeserializeObject(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(sectionJSON.data); + var sectionWeb = JsonConvert.DeserializeObject(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(sectionJSON.data); + var sectionMenu = JsonConvert.DeserializeObject(sectionJSON.Data); MenuDTO menuDTO = new MenuDTO(); menuDTO.sections = sectionMenu.sections; foreach (var sectionMenuu in menuDTO.sections) @@ -1030,7 +1041,8 @@ namespace ManagerService.Controllers section.Data = JsonConvert.SerializeObject(menuDTO); // Include all info from specific section as JSON break; case SectionType.Quizz: - var sectionQuizz = JsonConvert.DeserializeObject(sectionJSON.data); + // Fort don't use quizz for now piouf :) + /*var sectionQuizz = JsonConvert.DeserializeObject(sectionJSON.Data); QuizzDTO quizzDTO = new QuizzDTO(); quizzDTO.questions = sectionQuizz.questions; quizzDTO.bad_level = sectionQuizz.bad_level; @@ -1038,21 +1050,41 @@ namespace ManagerService.Controllers 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 + 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(); - articleDTO.content = contentArticle; - //articleDTO.audioIds = LanguageInit.Init("Audio", languages, true); + try { + var sectionArticle = JsonConvert.DeserializeObject(sectionJSON.Data); + ArticleDTO newArticle = new ArticleDTO(); + newArticle.content = sectionArticle.content; + newArticle.isContentTop = sectionArticle.isContentTop; + newArticle.audioIds = sectionArticle.audioIds; + newArticle.isReadAudioAuto = sectionArticle.isReadAudioAuto; + newArticle.contents = new List(); + foreach (var image in sectionArticle.images) // Migrate old images to contents + { + ContentDTO contentDTO = new ContentDTO(); - section.Data = JsonConvert.SerializeObject(articleDTO); // Include all info from specific section as JSON + contentDTO.title = image.title; + contentDTO.description = image.description; + contentDTO.resourceId = image.resourceId; + contentDTO.order = image.order; + contentDTO.resourceType = ResourceType.Image; + contentDTO.resourceUrl = image.source; + newArticle.contents.Add(contentDTO); + } + + section.Data = JsonConvert.SerializeObject(newArticle); // Include all info from specific section as JSON + } catch (Exception e) + { + Console.WriteLine("Issue in article with id " + sectionJSON._id + e); + + } + break; - case SectionType.PDF: + /*case SectionType.PDF: PdfDTO pdfDTO = new PdfDTO(); section.Data = JsonConvert.SerializeObject(pdfDTO); // Include all info from specific section as JSON break; @@ -1067,12 +1099,21 @@ namespace ManagerService.Controllers case SectionType.Weather: WeatherDTO weatherDTO = new WeatherDTO(); section.Data = JsonConvert.SerializeObject(weatherDTO); // Include all info from specific section as JSON - break; + break;*/ } + + if (_configurationService.IsExist(section.ConfigurationId)) + { + i += 1; + //Section sectionCreated = _sectionService.Create(section); + } + else + { + } + } - //Section sectionCreated = _sectionService.Create(section); - } - + var test = i; + return new OkObjectResult(true); } catch (ArgumentNullException ex)