Delete thumb file
This commit is contained in:
parent
afb8a23588
commit
0f5cbb4d87
@ -6,104 +6,111 @@ using MyDarling.Models;
|
|||||||
|
|
||||||
namespace MyDarling.Controllers
|
namespace MyDarling.Controllers
|
||||||
{
|
{
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class FigureController : Controller
|
public class FigureController : Controller
|
||||||
{
|
{
|
||||||
private DataContext context;
|
private DataContext context;
|
||||||
private IWebHostEnvironment environment;
|
private IWebHostEnvironment environment;
|
||||||
public FigureController(DataContext context, IWebHostEnvironment environment)
|
public FigureController(DataContext context, IWebHostEnvironment environment)
|
||||||
{
|
{
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> Details(string id)
|
public async Task<IActionResult> Details(string id)
|
||||||
{
|
{
|
||||||
var figure = await context.Figures
|
var figure = await context.Figures
|
||||||
.Where(f => f.Id.Equals(id))
|
.Where(f => f.Id.Equals(id))
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
if (figure == null)
|
if (figure == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
var product = await context.Products
|
var product = await context.Products
|
||||||
.Where(b => b.Figures.Contains(figure))
|
.Where(b => b.Figures.Contains(figure))
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
if (product == null)
|
if (product == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
return View(figure);
|
return View(figure);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> Edit(string id)
|
public async Task<IActionResult> Edit(string id)
|
||||||
{
|
{
|
||||||
if (id == null)
|
if (id == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
var figure = await context.Figures
|
var figure = await context.Figures
|
||||||
.Where(f => f.Id.Equals(id))
|
.Where(f => f.Id.Equals(id))
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
if (figure == null)
|
if (figure == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
var product = await context.Products
|
var product = await context.Products
|
||||||
.Where(b => b.Figures.Contains(figure))
|
.Where(b => b.Figures.Contains(figure))
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
if (await TryUpdateModelAsync<Figure>(
|
if (await TryUpdateModelAsync<Figure>(
|
||||||
figure,
|
figure,
|
||||||
"",
|
"",
|
||||||
f => f.Description))
|
f => f.Description))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
return RedirectToAction("Details", "Products", new { Id = product?.Id });
|
return RedirectToAction("Details", "Products", new { Id = product?.Id });
|
||||||
}
|
}
|
||||||
catch (SystemException)
|
catch (SystemException)
|
||||||
{
|
{
|
||||||
ModelState.AddModelError("", "Unable to save changes");
|
ModelState.AddModelError("", "Unable to save changes");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return View(figure);
|
return View(figure);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult> Delete(string id)
|
public async Task<ActionResult> Delete(string id)
|
||||||
{
|
{
|
||||||
var figure = await context.Figures.FindAsync(id);
|
var figure = await context.Figures.FindAsync(id);
|
||||||
if (figure == null)
|
if (figure == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
var product = await context.Products
|
var product = await context.Products
|
||||||
.Where(b => b.Figures.Contains(figure))
|
.Where(b => b.Figures.Contains(figure))
|
||||||
.FirstAsync();
|
.FirstAsync();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string filePath = $"/Content/{product.Id}/{figure.Id}.jpg";
|
string filePath = $"/Content/{product.Id}/{figure.Id}.jpg";
|
||||||
FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath);
|
FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath);
|
||||||
if (figureFile.Exists)
|
if (figureFile.Exists)
|
||||||
{
|
{
|
||||||
figureFile.Delete();
|
figureFile.Delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
context.Figures.Remove(figure);
|
string thumbFilePath = $"/Content/{product.Id}/{figure.Id}_thumb.jpg";
|
||||||
await context.SaveChangesAsync();
|
FileInfo thumbFile = new FileInfo(environment.WebRootPath + thumbFilePath);
|
||||||
return RedirectToAction("Details", "Products", new { Id = product?.Id });
|
if (thumbFile.Exists)
|
||||||
}
|
{
|
||||||
catch (DbUpdateException)
|
thumbFile.Delete();
|
||||||
{
|
}
|
||||||
return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true });
|
|
||||||
}
|
context.Figures.Remove(figure);
|
||||||
}
|
await context.SaveChangesAsync();
|
||||||
}
|
return RedirectToAction("Details", "Products", new { Id = product?.Id });
|
||||||
|
}
|
||||||
|
catch (DbUpdateException)
|
||||||
|
{
|
||||||
|
return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user