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.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<MapDTO>(section.Data);
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;
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<SliderDTO>(section.Data);
/*List<ContentDTO> contentsToKeep = new List<ContentDTO>();
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<QuizDTO>(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<QuizDTO>(section.Data);
List<QuizQuestion> 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<ArticleDTO>(section.Data);
List<ContentDTO> contentsArticleToKeep = new List<ContentDTO>();*/
/*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();

View File

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

View File

@ -108,5 +108,10 @@ namespace ManagerService.Data.SubSection
[Required]
[Column(TypeName = "jsonb")]
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.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 =>