0
0

Base Deleting

This commit is contained in:
Sergey Chebotar 2023-03-02 08:02:02 +03:00
parent 607d029030
commit cdd58a8f51
2 changed files with 147 additions and 126 deletions

View File

@ -1,6 +1,5 @@
using System.Data; using System.Data;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using MyDarling.Models; using MyDarling.Models;
@ -119,6 +118,20 @@ namespace MyDarling.Controllers
try try
{ {
var bundleDirPath = String.Concat(environment.WebRootPath,
"/Content/",
bundleToDelete.Id);
if (Directory.Exists(bundleDirPath))
{
Directory.Delete(bundleDirPath, true);
}
// foreach (var figure in bundleToDelete.Figures)
// {
// using FigureController controller = new(context, environment);
// await controller.Delete(figure.Id);
// }
context.UnderwearBundles.Remove(bundleToDelete); context.UnderwearBundles.Remove(bundleToDelete);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));

View File

@ -8,9 +8,11 @@ namespace MyDarling.Controllers
public class FigureController : Controller public class FigureController : Controller
{ {
private DataContext context; private DataContext context;
public FigureController(DataContext context) private IWebHostEnvironment environment;
public FigureController(DataContext context, IWebHostEnvironment environment)
{ {
this.context = context; this.context = context;
this.environment = environment;
} }
public async Task<IActionResult> Details(int id) public async Task<IActionResult> Details(int id)
@ -66,6 +68,12 @@ namespace MyDarling.Controllers
try try
{ {
FileInfo figureFile = new FileInfo(environment.WebRootPath + figureToDelete.FilePath);
if (figureFile.Exists)
{
figureFile.Delete();
}
context.Figures.Remove(figureToDelete); context.Figures.Remove(figureToDelete);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
return RedirectToAction(nameof(Index), "Bundle"); return RedirectToAction(nameof(Index), "Bundle");