Fix bugs + delete all resource occurence (when delete resource)

This commit is contained in:
Thomas Fransolet 2021-07-26 17:18:06 +02:00
parent 62aa7453c0
commit cbd4773697
2 changed files with 45 additions and 16 deletions

View File

@ -26,12 +26,14 @@ namespace ManagerService.Controllers
public class ResourceController : ControllerBase public class ResourceController : ControllerBase
{ {
private ResourceDatabaseService _resourceService; private ResourceDatabaseService _resourceService;
private SectionDatabaseService _sectionService;
private readonly ILogger<ResourceController> _logger; private readonly ILogger<ResourceController> _logger;
public ResourceController(ILogger<ResourceController> logger, ResourceDatabaseService resourceService) public ResourceController(ILogger<ResourceController> logger, ResourceDatabaseService resourceService, SectionDatabaseService sectionService)
{ {
_logger = logger; _logger = logger;
_resourceService = resourceService; _resourceService = resourceService;
_sectionService = sectionService;
} }
/// <summary> /// <summary>
@ -295,6 +297,47 @@ namespace ManagerService.Controllers
if (!_resourceService.IsExist(id)) if (!_resourceService.IsExist(id))
throw new KeyNotFoundException("Resource does not exist"); throw new KeyNotFoundException("Resource does not exist");
// Delete all resource occurence
foreach (var section in _sectionService.GetAll())
{
if (section.ImageId == id)
{
section.ImageId = null;
section.ImageSource = null;
}
switch (section.Type)
{
case SectionType.Map:
MapDTO mapDTO = JsonConvert.DeserializeObject<MapDTO>(section.Data);
mapDTO.iconResourceId = mapDTO.iconResourceId == id ? null : mapDTO.iconResourceId;
foreach (var point in mapDTO.points)
{
foreach (var image in point.images)
{
image.imageSource = image.imageResourceId == id ? null : image.imageSource;
image.imageResourceId = image.imageResourceId == id ? null : image.imageResourceId;
}
}
section.Data = JsonConvert.SerializeObject(mapDTO);
break;
case SectionType.Slider:
SliderDTO sliderDTO = JsonConvert.DeserializeObject<SliderDTO>(section.Data);
List<ImageDTO> imagesToKeep = new List<ImageDTO>();
foreach (var image in sliderDTO.images)
{
if (image.resourceId != id)
imagesToKeep.Add(image);
}
sliderDTO.images = imagesToKeep;
section.Data = JsonConvert.SerializeObject(sliderDTO);
break;
}
_sectionService.Update(section.Id, section);
}
_resourceService.Remove(id); _resourceService.Remove(id);
return new ObjectResult("The resource has been deleted") { StatusCode = 202 }; return new ObjectResult("The resource has been deleted") { StatusCode = 202 };

View File

@ -279,27 +279,13 @@ namespace ManagerService.Controllers
mapDTO.mapType = MapTypeApp.hybrid; mapDTO.mapType = MapTypeApp.hybrid;
mapDTO.zoom = 18; mapDTO.zoom = 18;
mapDTO.points = new List<GeoPointDTO>() { mapDTO.points = new List<GeoPointDTO>();
new GeoPointDTO() {
id = 0,
title = section.Title,
description = section.Description,
latitude = "50.416639",
longitude= "4.879169",
images = new List<ImageGeoPoint>()
}
};
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; break;
case SectionType.Slider: case SectionType.Slider:
sliderDTO = new SliderDTO(); sliderDTO = new SliderDTO();
ImageDTO imageDTO = new ImageDTO();
imageDTO.title = section.Title;
imageDTO.description = section.Description;
imageDTO.source = null;
sliderDTO.images = new List<ImageDTO>(); sliderDTO.images = new List<ImageDTO>();
sliderDTO.images.Add(imageDTO);
section.Data = JsonConvert.SerializeObject(sliderDTO); // Include all info from specific section as JSON section.Data = JsonConvert.SerializeObject(sliderDTO); // Include all info from specific section as JSON
break; break;