From 8635e75c0676f7bfb413f4a1ab4517efcbfa2718 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Wed, 14 May 2025 15:24:43 +0200 Subject: [PATCH] update service (ressource deletion) + EF update link --- .../Controllers/ResourceController.cs | 261 ++++++++++++------ .../Data/SubSection/QuizQuestion.cs | 5 + ManagerService/Data/SubSection/SectionMap.cs | 5 + .../MyInfoMateDbContextModelSnapshot.cs | 16 +- 4 files changed, 201 insertions(+), 86 deletions(-) diff --git a/ManagerService/Controllers/ResourceController.cs b/ManagerService/Controllers/ResourceController.cs index 3da39ea..115383a 100644 --- a/ManagerService/Controllers/ResourceController.cs +++ b/ManagerService/Controllers/ResourceController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.IO; using System.Linq; using Manager.DTOs; @@ -11,6 +12,7 @@ using ManagerService.Helpers; using ManagerService.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using NSwag.Annotations; @@ -438,134 +440,237 @@ namespace ManagerService.Controllers switch (section) { case SectionMap map: - //MapDTO mapDTO = JsonConvert.DeserializeObject(section.Data); map.MapResourceId = map.MapResourceId == id ? null : map.MapResourceId; - foreach (var point in map.MapPoints) + List geoPoints = _myInfoMateDbContext.GeoPoints.Where(s => s.SectionMapId == section.Id).ToList(); + foreach (var point in geoPoints) { point.ImageResourceId = point.ImageResourceId == id ? null : point.ImageResourceId; foreach (var content in point.Contents) { - content.resource.url = content.resourceId == id ? null : content.resource.url; - content.resourceId = content.resourceId == id ? null : content.resourceId; - } - } - - foreach (var categorie in map.MapCategories) - { - // TODO - /*categorie.iconUrl = categorie.iconResourceId == id ? null : categorie.iconUrl; - categorie.iconResourceId = categorie.iconResourceId == id ? null : categorie.iconResourceId;*/ - } - //section.Data = JsonConvert.SerializeObject(mapDTO); - break; - case SectionSlider slider: - //SliderDTO sliderDTO = JsonConvert.DeserializeObject(section.Data); - /*List contentsToKeep = new List(); - foreach (var content in slider.Contents) - { - if (content.ResourceId != id) - contentsToKeep.Add(content); - }*/ - // TODO TEST - slider.SliderContents = slider.SliderContents; - //section.Data = JsonConvert.SerializeObject(sliderDTO); - break; - // TODO - /*case SectionType.Quizz: - QuizDTO quizzDTO = JsonConvert.DeserializeObject(section.Data); - foreach (var question in quizzDTO.questions) - { - if (question.label != null) - { - foreach (var questionLabel in question.label) + if (content.resourceId == id) { - - questionLabel.resourceUrl = questionLabel.resourceId == id ? null : questionLabel.resourceUrl; - questionLabel.resourceId = questionLabel.resourceId == id ? null : questionLabel.resourceId; + content.resourceId = null; + content.resource = null; + _myInfoMateDbContext.Entry(point).Property(p => p.Contents).IsModified = true; } } - - question.imageBackgroundResourceUrl = question.imageBackgroundResourceId == id ? null : question.imageBackgroundResourceUrl; - question.imageBackgroundResourceId = question.imageBackgroundResourceId == id ? null : question.imageBackgroundResourceId; + } - foreach (var response in question.responses) + if (map.MapCategories != null) + { + foreach (var categorie in map.MapCategories) + { + if (categorie.resourceDTO.id == id) + { + categorie.resourceDTO = null; + _myInfoMateDbContext.Entry(map).Property(p => p.MapCategories).IsModified = true; + } + } + } + + break; + case SectionSlider slider: + foreach (var content in slider.SliderContents) + { + if (content.resourceId == id) + { + content.resource = null; + content.resourceId = null; + _myInfoMateDbContext.Entry(slider).Property(p => p.SliderContents).IsModified = true; + } + } + break; + case SectionQuiz quiz: + //QuizDTO quizzDTO = JsonConvert.DeserializeObject(section.Data); + List quizQuestions = _myInfoMateDbContext.QuizQuestions.Where(qq => qq.SectionQuizId == section.Id).ToList(); + + foreach (var question in quizQuestions) + { + if (question.Label != null) + { + foreach (var questionLabel in question.Label) + { + if (questionLabel.resourceId == id) + { + questionLabel.resource = null; + questionLabel.resourceId = null; + _myInfoMateDbContext.Entry(question).Property(p => p.Label).IsModified = true; + } + } + } + + if (question.ResourceId == id) + { + question.ResourceId = null; + question.Resource = null; + } + + foreach (var response in question.Responses) { if (response.label != null) { foreach (var responseLabel in response.label) { - responseLabel.resourceUrl = responseLabel.resourceId == id ? null : responseLabel.resourceUrl; - responseLabel.resourceId = responseLabel.resourceId == id ? null : responseLabel.resourceId; + if (responseLabel.resourceId == id) + { + responseLabel.resource = null; + responseLabel.resourceId = null; + _myInfoMateDbContext.Entry(response).Property(p => p.label).IsModified = true; + } + //responseLabel.resourceUrl = responseLabel.resourceId == id ? null : responseLabel.resourceUrl; + //responseLabel.resourceId = responseLabel.resourceId == id ? null : responseLabel.resourceId; } } } } - if (quizzDTO.bad_level != null) + if (quiz.QuizBadLevel != null) { - if (quizzDTO.bad_level.label != null) + foreach (var quizBadLevel in quiz.QuizBadLevel) { - foreach (var badLevelLabel in quizzDTO.bad_level.label) + if (quizBadLevel.resourceId == id) { - badLevelLabel.resourceUrl = badLevelLabel.resourceId == id ? null : badLevelLabel.resourceUrl; - badLevelLabel.resourceId = badLevelLabel.resourceId == id ? null : badLevelLabel.resourceId; + quizBadLevel.resourceId = null; + quizBadLevel.resource = null; + _myInfoMateDbContext.Entry(quiz).Property(p => p.QuizBadLevel).IsModified = true; } } } - if (quizzDTO.medium_level != null) + if (quiz.QuizMediumLevel != null) { - if (quizzDTO.medium_level.label != null) + foreach (var quizMediumLevel in quiz.QuizMediumLevel) { - foreach (var medium_levelLabel in quizzDTO.medium_level.label) + if (quizMediumLevel.resourceId == id) { - medium_levelLabel.resourceUrl = medium_levelLabel.resourceId == id ? null : medium_levelLabel.resourceUrl; - medium_levelLabel.resourceId = medium_levelLabel.resourceId == id ? null : medium_levelLabel.resourceId; + quizMediumLevel.resourceId = null; + quizMediumLevel.resource = null; + _myInfoMateDbContext.Entry(quiz).Property(p => p.QuizMediumLevel).IsModified = true; } } } - if (quizzDTO.good_level != null) + if (quiz.QuizGoodLevel != null) { - if (quizzDTO.good_level.label != null) + foreach (var quizGoodLevel in quiz.QuizGoodLevel) { - foreach (var gooLevelLabel in quizzDTO.good_level.label) + if (quizGoodLevel.resourceId == id) { - gooLevelLabel.resourceUrl = gooLevelLabel.resourceId == id ? null : gooLevelLabel.resourceUrl; - gooLevelLabel.resourceId = gooLevelLabel.resourceId == id ? null : gooLevelLabel.resourceId; + quizGoodLevel.resourceId = null; + quizGoodLevel.resource = null; + _myInfoMateDbContext.Entry(quiz).Property(p => p.QuizGoodLevel).IsModified = true; } } } - if (quizzDTO.great_level != null) + if (quiz.QuizGreatLevel != null) { - if (quizzDTO.great_level.label != null) + foreach (var quizGreatLevel in quiz.QuizGreatLevel) { - foreach (var greatLevelLabel in quizzDTO.great_level.label) + if (quizGreatLevel.resourceId == id) { - greatLevelLabel.resourceUrl = greatLevelLabel.resourceId == id ? null : greatLevelLabel.resourceUrl; - greatLevelLabel.resourceId = greatLevelLabel.resourceId == id ? null : greatLevelLabel.resourceId; + quizGreatLevel.resourceId = null; + quizGreatLevel.resource = null; + _myInfoMateDbContext.Entry(quiz).Property(p => p.QuizGreatLevel).IsModified = true; } } } - section.Data = JsonConvert.SerializeObject(quizzDTO); - break;*/ + + break; case SectionArticle article: - /*ArticleDTO articleDTO = JsonConvert.DeserializeObject(section.Data); - List contentsArticleToKeep = new List();*/ - /*foreach (var content in article.contents) + if (article.ArticleContents != null) { - if (content.resourceId != id) - contentsArticleToKeep.Add(content); - }*/ - // TODO TEEEEEEEEEEESST - //articleDTO.contents = contentsArticleToKeep; - article.ArticleContents = article.ArticleContents; - //section.Data = JsonConvert.SerializeObject(articleDTO); + foreach (var content in article.ArticleContents) + { + if (content.resourceId == id) + { + content.resource = null; + content.resourceId = null; + _myInfoMateDbContext.Entry(article).Property(p => p.ArticleContents).IsModified = true; + } + } + + foreach (var audioId in article.ArticleAudioIds) + { + if (audioId.value == id) + { + audioId.value = null; + _myInfoMateDbContext.Entry(article).Property(p => p.ArticleAudioIds).IsModified = true; + } + } + } + break; + case SectionPdf pdf: + if (pdf.PDFOrderedTranslationAndResources != null) + { + foreach (var orderedTranslationAndResource in pdf.PDFOrderedTranslationAndResources) + { + if (orderedTranslationAndResource.translationAndResourceDTOs != null) + { + foreach (var translationAndResource in orderedTranslationAndResource.translationAndResourceDTOs) + { + if (translationAndResource.value == id) + { + translationAndResource.value = null; + _myInfoMateDbContext.Entry(pdf).Property(p => p.PDFOrderedTranslationAndResources).IsModified = true; + } + } + } + } + } + break; + case SectionAgenda agenda: + if (agenda.AgendaResourceIds != null) + { + foreach (var agendaResourceId in agenda.AgendaResourceIds) + { + if (agendaResourceId.value == id) + { + agendaResourceId.value = null; + _myInfoMateDbContext.Entry(agenda).Property(p => p.AgendaResourceIds).IsModified = true; + } + } + } + break; + case SectionPuzzle puzzle: + if (puzzle.PuzzleMessageDebut != null) + { + foreach (var puzzleMessageDebut in puzzle.PuzzleMessageDebut) + { + if (puzzleMessageDebut.ResourceId == id) + { + puzzleMessageDebut.ResourceId = null; + puzzleMessageDebut.Resource = null; + _myInfoMateDbContext.Entry(puzzle).Property(p => p.PuzzleMessageDebut).IsModified = true; + } + } + } + if (puzzle.PuzzleMessageFin != null) + { + foreach (var puzzleMessageFin in puzzle.PuzzleMessageFin) + { + if (puzzleMessageFin.ResourceId == id) + { + puzzleMessageFin.ResourceId = null; + puzzleMessageFin.Resource = null; + _myInfoMateDbContext.Entry(puzzle).Property(p => p.PuzzleMessageFin).IsModified = true; + } + } + } + + if (puzzle.PuzzleImageId == id) + { + puzzle.PuzzleImageId = null; + puzzle.PuzzleImage = null; + } + break; + case SectionVideo video: // TO BE TESTED .. + if (resource.Url == video.VideoSource) + { + video.VideoSource = null; + } break; } _myInfoMateDbContext.SaveChanges(); - //_sectionService.Update(section.Id, section); } - //_resourceService.Remove(id); _myInfoMateDbContext.Remove(resource); _myInfoMateDbContext.SaveChanges(); diff --git a/ManagerService/Data/SubSection/QuizQuestion.cs b/ManagerService/Data/SubSection/QuizQuestion.cs index e27b361..84daca6 100644 --- a/ManagerService/Data/SubSection/QuizQuestion.cs +++ b/ManagerService/Data/SubSection/QuizQuestion.cs @@ -30,6 +30,11 @@ namespace ManagerService.Data.SubSection [Column(TypeName = "jsonb")] public List Responses { get; set; } // TODO check + public string SectionQuizId { get; set; } + + [ForeignKey("SectionQuizId")] + public SectionQuiz SectionQuiz { get; set; } + // TODO /*public TranslationDTO ToDTO() { diff --git a/ManagerService/Data/SubSection/SectionMap.cs b/ManagerService/Data/SubSection/SectionMap.cs index 8784406..43175b1 100644 --- a/ManagerService/Data/SubSection/SectionMap.cs +++ b/ManagerService/Data/SubSection/SectionMap.cs @@ -108,5 +108,10 @@ namespace ManagerService.Data.SubSection [Required] [Column(TypeName = "jsonb")] public List Site { get; set; } + + public string SectionMapId { get; set; } + + [ForeignKey("SectionMapId")] + public SectionMap SectionMap { get; set; } } } diff --git a/ManagerService/Migrations/MyInfoMateDbContextModelSnapshot.cs b/ManagerService/Migrations/MyInfoMateDbContextModelSnapshot.cs index be1504f..0f50fdc 100644 --- a/ManagerService/Migrations/MyInfoMateDbContextModelSnapshot.cs +++ b/ManagerService/Migrations/MyInfoMateDbContextModelSnapshot.cs @@ -93,7 +93,7 @@ namespace ManagerService.Migrations b.HasKey("Id"); - b.ToTable("Configurations"); + b.ToTable("Configurations", (string)null); }); modelBuilder.Entity("ManagerService.Data.Device", b => @@ -148,7 +148,7 @@ namespace ManagerService.Migrations b.HasIndex("ConfigurationId"); - b.ToTable("Devices"); + b.ToTable("Devices", (string)null); }); modelBuilder.Entity("ManagerService.Data.Instance", b => @@ -168,7 +168,7 @@ namespace ManagerService.Migrations b.HasKey("Id"); - b.ToTable("Instances"); + b.ToTable("Instances", (string)null); }); modelBuilder.Entity("ManagerService.Data.Resource", b => @@ -195,7 +195,7 @@ namespace ManagerService.Migrations b.HasKey("Id"); - b.ToTable("Resources"); + b.ToTable("Resources", (string)null); }); modelBuilder.Entity("ManagerService.Data.Section", b => @@ -270,7 +270,7 @@ namespace ManagerService.Migrations b.HasIndex("SectionMenuId"); - b.ToTable("Sections"); + b.ToTable("Sections", (string)null); b.HasDiscriminator().HasValue("Base"); @@ -339,7 +339,7 @@ namespace ManagerService.Migrations b.HasIndex("SectionMapId"); - b.ToTable("GeoPoints"); + b.ToTable("GeoPoints", (string)null); }); modelBuilder.Entity("ManagerService.Data.SubSection.QuizQuestion", b => @@ -373,7 +373,7 @@ namespace ManagerService.Migrations b.HasIndex("SectionQuizId"); - b.ToTable("QuizQuestions"); + b.ToTable("QuizQuestions", (string)null); }); modelBuilder.Entity("ManagerService.Data.User", b => @@ -409,7 +409,7 @@ namespace ManagerService.Migrations b.HasKey("Id"); - b.ToTable("Users"); + b.ToTable("Users", (string)null); }); modelBuilder.Entity("ManagerService.Data.SubSection.SectionAgenda", b =>