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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
using System.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyDarling.Models;
using MyDarling.Services;
namespace MyDarling.Controllers
{
[Authorize]
public class ProductsController : Controller
{
private DataContext context;
private IWebHostEnvironment environment;
private IImageResizer resizer;
public ProductsController(DataContext context, IImageResizer resizer, IWebHostEnvironment environment)
{
this.environment = environment;
this.context = context;
this.resizer = resizer;
}
public IActionResult Index()
{
return View(context.Products.Include(b => b.Figures));
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create([Bind] Product bundle)
{
try
{
if (ModelState.IsValid)
{
await context.Products.AddAsync(bundle);
context.SaveChanges();
return RedirectToAction(nameof(Index));
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to save changes");
}
return View(bundle);
}
public async Task<IActionResult> Details(string id)
{
return View(await context.Products.Include(b => b.Figures).Where(b => b.Id.Equals(id)).FirstOrDefaultAsync());
}
[HttpPost]
public async Task<IActionResult> Edit(string id)
{
if (string.IsNullOrEmpty(id))
{
return NotFound();
}
var product = await context.Products.FindAsync(id);
if (product == null)
{
return NotFound();
}
var file = Request.Form.Files.FirstOrDefault();
string fullPath = string.Empty;
if (await TryUpdateModelAsync<Product>(
product,
"",
b => b.Name, b => b.Description, b => b.Figures, b => b.Price))
{
if (file != null)
{
var newFigure = new Figure(string.Empty, product.Id);
product.Figures.Add(newFigure);
string filePath = $"/Content/{product.Id}/{newFigure.Id}.jpg";
string directoryPath = environment.WebRootPath + "/Content/" + product.Id + "/";
fullPath = environment.WebRootPath + filePath;
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
resizer.WriteResized(file, fullPath);
resizer.CreateThumbnail(fullPath);
}
try
{
await context.SaveChangesAsync();
return RedirectToAction("Details", "Products", new { Id = product?.Id});
}
catch (System.Exception)
{
ModelState.AddModelError("", "Unable to save changes");
}
}
return View(product);
}
[HttpPost]
public async Task<IActionResult> Delete(string id)
{
var productToDelete = await context.Products.FindAsync(id);
if (productToDelete == null)
{
return NotFound();
}
try
{
var bundleDirPath = String.Concat(environment.WebRootPath,
"/Content/",
productToDelete.Id);
if (Directory.Exists(bundleDirPath))
{
Directory.Delete(bundleDirPath, true);
}
context.Products.Remove(productToDelete);
await context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
catch (DbUpdateException)
{
return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true });
}
}
}
}
|