update service (ressource deletion) + EF update link

This commit is contained in:
Thomas Fransolet 2025-05-14 15:24:43 +02:00
parent f4919801b7
commit 8635e75c06
4 changed files with 201 additions and 86 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Manager.DTOs; using Manager.DTOs;
@ -11,6 +12,7 @@ using ManagerService.Helpers;
using ManagerService.Services; using ManagerService.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using NSwag.Annotations; using NSwag.Annotations;
@ -438,134 +440,237 @@ namespace ManagerService.Controllers
switch (section) switch (section)
{ {
case SectionMap map: case SectionMap map:
//MapDTO mapDTO = JsonConvert.DeserializeObject<MapDTO>(section.Data);
map.MapResourceId = map.MapResourceId == id ? null : map.MapResourceId; map.MapResourceId = map.MapResourceId == id ? null : map.MapResourceId;
foreach (var point in map.MapPoints) List<GeoPoint> geoPoints = _myInfoMateDbContext.GeoPoints.Where(s => s.SectionMapId == section.Id).ToList();
foreach (var point in geoPoints)
{ {
point.ImageResourceId = point.ImageResourceId == id ? null : point.ImageResourceId; point.ImageResourceId = point.ImageResourceId == id ? null : point.ImageResourceId;
foreach (var content in point.Contents) foreach (var content in point.Contents)
{ {
content.resource.url = content.resourceId == id ? null : content.resource.url; if (content.resourceId == id)
content.resourceId = content.resourceId == id ? null : content.resourceId; {
content.resourceId = null;
content.resource = null;
_myInfoMateDbContext.Entry(point).Property(p => p.Contents).IsModified = true;
}
} }
} }
if (map.MapCategories != null)
{
foreach (var categorie in map.MapCategories) foreach (var categorie in map.MapCategories)
{ {
// TODO if (categorie.resourceDTO.id == id)
/*categorie.iconUrl = categorie.iconResourceId == id ? null : categorie.iconUrl; {
categorie.iconResourceId = categorie.iconResourceId == id ? null : categorie.iconResourceId;*/ categorie.resourceDTO = null;
_myInfoMateDbContext.Entry(map).Property(p => p.MapCategories).IsModified = true;
} }
//section.Data = JsonConvert.SerializeObject(mapDTO); }
}
break; break;
case SectionSlider slider: case SectionSlider slider:
//SliderDTO sliderDTO = JsonConvert.DeserializeObject<SliderDTO>(section.Data); foreach (var content in slider.SliderContents)
/*List<ContentDTO> contentsToKeep = new List<ContentDTO>();
foreach (var content in slider.Contents)
{ {
if (content.ResourceId != id) if (content.resourceId == id)
contentsToKeep.Add(content); {
}*/ content.resource = null;
// TODO TEST content.resourceId = null;
slider.SliderContents = slider.SliderContents; _myInfoMateDbContext.Entry(slider).Property(p => p.SliderContents).IsModified = true;
//section.Data = JsonConvert.SerializeObject(sliderDTO); }
}
break; break;
// TODO case SectionQuiz quiz:
/*case SectionType.Quizz: //QuizDTO quizzDTO = JsonConvert.DeserializeObject<QuizDTO>(section.Data);
QuizDTO quizzDTO = JsonConvert.DeserializeObject<QuizDTO>(section.Data); List<QuizQuestion> quizQuestions = _myInfoMateDbContext.QuizQuestions.Where(qq => qq.SectionQuizId == section.Id).ToList();
foreach (var question in quizzDTO.questions)
{
if (question.label != null)
{
foreach (var questionLabel in question.label)
{
questionLabel.resourceUrl = questionLabel.resourceId == id ? null : questionLabel.resourceUrl; foreach (var question in quizQuestions)
questionLabel.resourceId = questionLabel.resourceId == id ? null : questionLabel.resourceId; {
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;
}
} }
} }
question.imageBackgroundResourceUrl = question.imageBackgroundResourceId == id ? null : question.imageBackgroundResourceUrl; if (question.ResourceId == id)
question.imageBackgroundResourceId = question.imageBackgroundResourceId == id ? null : question.imageBackgroundResourceId; {
question.ResourceId = null;
question.Resource = null;
}
foreach (var response in question.responses) foreach (var response in question.Responses)
{ {
if (response.label != null) if (response.label != null)
{ {
foreach (var responseLabel in response.label) foreach (var responseLabel in response.label)
{ {
responseLabel.resourceUrl = responseLabel.resourceId == id ? null : responseLabel.resourceUrl; if (responseLabel.resourceId == id)
responseLabel.resourceId = responseLabel.resourceId == id ? null : responseLabel.resourceId;
}
}
}
}
if (quizzDTO.bad_level != null)
{ {
if (quizzDTO.bad_level.label != null) 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 (quiz.QuizBadLevel != null)
{ {
foreach (var badLevelLabel in quizzDTO.bad_level.label) foreach (var quizBadLevel in quiz.QuizBadLevel)
{ {
badLevelLabel.resourceUrl = badLevelLabel.resourceId == id ? null : badLevelLabel.resourceUrl; if (quizBadLevel.resourceId == id)
badLevelLabel.resourceId = badLevelLabel.resourceId == id ? null : badLevelLabel.resourceId;
}
}
}
if (quizzDTO.medium_level != null)
{ {
if (quizzDTO.medium_level.label != null) quizBadLevel.resourceId = null;
quizBadLevel.resource = null;
_myInfoMateDbContext.Entry(quiz).Property(p => p.QuizBadLevel).IsModified = true;
}
}
}
if (quiz.QuizMediumLevel != null)
{ {
foreach (var medium_levelLabel in quizzDTO.medium_level.label) foreach (var quizMediumLevel in quiz.QuizMediumLevel)
{ {
medium_levelLabel.resourceUrl = medium_levelLabel.resourceId == id ? null : medium_levelLabel.resourceUrl; if (quizMediumLevel.resourceId == id)
medium_levelLabel.resourceId = medium_levelLabel.resourceId == id ? null : medium_levelLabel.resourceId;
}
}
}
if (quizzDTO.good_level != null)
{ {
if (quizzDTO.good_level.label != null) quizMediumLevel.resourceId = null;
quizMediumLevel.resource = null;
_myInfoMateDbContext.Entry(quiz).Property(p => p.QuizMediumLevel).IsModified = true;
}
}
}
if (quiz.QuizGoodLevel != null)
{ {
foreach (var gooLevelLabel in quizzDTO.good_level.label) foreach (var quizGoodLevel in quiz.QuizGoodLevel)
{ {
gooLevelLabel.resourceUrl = gooLevelLabel.resourceId == id ? null : gooLevelLabel.resourceUrl; if (quizGoodLevel.resourceId == id)
gooLevelLabel.resourceId = gooLevelLabel.resourceId == id ? null : gooLevelLabel.resourceId;
}
}
}
if (quizzDTO.great_level != null)
{ {
if (quizzDTO.great_level.label != null) quizGoodLevel.resourceId = null;
quizGoodLevel.resource = null;
_myInfoMateDbContext.Entry(quiz).Property(p => p.QuizGoodLevel).IsModified = true;
}
}
}
if (quiz.QuizGreatLevel != null)
{ {
foreach (var greatLevelLabel in quizzDTO.great_level.label) foreach (var quizGreatLevel in quiz.QuizGreatLevel)
{ {
greatLevelLabel.resourceUrl = greatLevelLabel.resourceId == id ? null : greatLevelLabel.resourceUrl; if (quizGreatLevel.resourceId == id)
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: case SectionArticle article:
/*ArticleDTO articleDTO = JsonConvert.DeserializeObject<ArticleDTO>(section.Data); if (article.ArticleContents != null)
List<ContentDTO> contentsArticleToKeep = new List<ContentDTO>();*/
/*foreach (var content in article.contents)
{ {
if (content.resourceId != id) foreach (var content in article.ArticleContents)
contentsArticleToKeep.Add(content); {
}*/ if (content.resourceId == id)
// TODO TEEEEEEEEEEESST {
//articleDTO.contents = contentsArticleToKeep; content.resource = null;
article.ArticleContents = article.ArticleContents; content.resourceId = null;
//section.Data = JsonConvert.SerializeObject(articleDTO); _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; break;
} }
_myInfoMateDbContext.SaveChanges(); _myInfoMateDbContext.SaveChanges();
//_sectionService.Update(section.Id, section);
} }
//_resourceService.Remove(id);
_myInfoMateDbContext.Remove(resource); _myInfoMateDbContext.Remove(resource);
_myInfoMateDbContext.SaveChanges(); _myInfoMateDbContext.SaveChanges();

View File

@ -30,6 +30,11 @@ namespace ManagerService.Data.SubSection
[Column(TypeName = "jsonb")] [Column(TypeName = "jsonb")]
public List<ResponseDTO> Responses { get; set; } // TODO check public List<ResponseDTO> Responses { get; set; } // TODO check
public string SectionQuizId { get; set; }
[ForeignKey("SectionQuizId")]
public SectionQuiz SectionQuiz { get; set; }
// TODO // TODO
/*public TranslationDTO ToDTO() /*public TranslationDTO ToDTO()
{ {

View File

@ -108,5 +108,10 @@ namespace ManagerService.Data.SubSection
[Required] [Required]
[Column(TypeName = "jsonb")] [Column(TypeName = "jsonb")]
public List<TranslationDTO> Site { get; set; } public List<TranslationDTO> Site { get; set; }
public string SectionMapId { get; set; }
[ForeignKey("SectionMapId")]
public SectionMap SectionMap { get; set; }
} }
} }

View File

@ -93,7 +93,7 @@ namespace ManagerService.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Configurations"); b.ToTable("Configurations", (string)null);
}); });
modelBuilder.Entity("ManagerService.Data.Device", b => modelBuilder.Entity("ManagerService.Data.Device", b =>
@ -148,7 +148,7 @@ namespace ManagerService.Migrations
b.HasIndex("ConfigurationId"); b.HasIndex("ConfigurationId");
b.ToTable("Devices"); b.ToTable("Devices", (string)null);
}); });
modelBuilder.Entity("ManagerService.Data.Instance", b => modelBuilder.Entity("ManagerService.Data.Instance", b =>
@ -168,7 +168,7 @@ namespace ManagerService.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Instances"); b.ToTable("Instances", (string)null);
}); });
modelBuilder.Entity("ManagerService.Data.Resource", b => modelBuilder.Entity("ManagerService.Data.Resource", b =>
@ -195,7 +195,7 @@ namespace ManagerService.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Resources"); b.ToTable("Resources", (string)null);
}); });
modelBuilder.Entity("ManagerService.Data.Section", b => modelBuilder.Entity("ManagerService.Data.Section", b =>
@ -270,7 +270,7 @@ namespace ManagerService.Migrations
b.HasIndex("SectionMenuId"); b.HasIndex("SectionMenuId");
b.ToTable("Sections"); b.ToTable("Sections", (string)null);
b.HasDiscriminator().HasValue("Base"); b.HasDiscriminator().HasValue("Base");
@ -339,7 +339,7 @@ namespace ManagerService.Migrations
b.HasIndex("SectionMapId"); b.HasIndex("SectionMapId");
b.ToTable("GeoPoints"); b.ToTable("GeoPoints", (string)null);
}); });
modelBuilder.Entity("ManagerService.Data.SubSection.QuizQuestion", b => modelBuilder.Entity("ManagerService.Data.SubSection.QuizQuestion", b =>
@ -373,7 +373,7 @@ namespace ManagerService.Migrations
b.HasIndex("SectionQuizId"); b.HasIndex("SectionQuizId");
b.ToTable("QuizQuestions"); b.ToTable("QuizQuestions", (string)null);
}); });
modelBuilder.Entity("ManagerService.Data.User", b => modelBuilder.Entity("ManagerService.Data.User", b =>
@ -409,7 +409,7 @@ namespace ManagerService.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Users"); b.ToTable("Users", (string)null);
}); });
modelBuilder.Entity("ManagerService.Data.SubSection.SectionAgenda", b => modelBuilder.Entity("ManagerService.Data.SubSection.SectionAgenda", b =>