blob: 76edb55f46efafb1bd9240fe882abe3d148ec4a7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
using System.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyDarling.Models;
namespace MyDarling.Controllers
{
[Authorize]
public class FigureController : Controller
{
private DataContext context;
private IWebHostEnvironment environment;
public FigureController(DataContext context, IWebHostEnvironment environment)
{
this.context = context;
this.environment = environment;
}
public async Task<IActionResult> Details(string id)
{
var figure = await context.Figures
.Where(f => f.Id.Equals(id))
.FirstOrDefaultAsync();
if (figure == null)
{
return NotFound();
}
var product = await context.Products
.Where(b => b.Figures.Contains(figure))
.FirstOrDefaultAsync();
if (product == null)
{
return NotFound();
}
return View(figure);
}
[HttpPost]
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
return NotFound();
}
var figure = await context.Figures
.Where(f => f.Id.Equals(id))
.FirstOrDefaultAsync();
if (figure == null)
{
return NotFound();
}
var product = await context.Products
.Where(b => b.Figures.Contains(figure))
.FirstOrDefaultAsync();
if (await TryUpdateModelAsync<Figure>(
figure,
"",
f => f.Description))
{
try
{
await context.SaveChangesAsync();
return RedirectToAction("Details", "Products", new { Id = product?.Id });
}
catch (SystemException)
{
ModelState.AddModelError("", "Unable to save changes");
}
}
return View(figure);
}
[HttpPost]
public async Task<ActionResult> Delete(string id)
{
var figure = await context.Figures.FindAsync(id);
if (figure == null)
{
return NotFound();
}
var product = await context.Products
.Where(b => b.Figures.Contains(figure))
.FirstAsync();
try
{
string filePath = $"/Content/{product.Id}/{figure.Id}.jpg";
FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath);
if (figureFile.Exists)
{
figureFile.Delete();
}
string thumbFilePath = $"/Content/{product.Id}/{figure.Id}_thumb.jpg";
FileInfo thumbFile = new FileInfo(environment.WebRootPath + thumbFilePath);
if (thumbFile.Exists)
{
thumbFile.Delete();
}
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 });
}
}
}
}
|