/
ProductsController.cs
235 lines (210 loc) · 9.84 KB
/
ProductsController.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebBanHang.EF;
//Add The following Namespaces to the project
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob; // Multipart
namespace WebBanHang.Controllers.Backend
{
public class ProductsController : Controller
{
private QuanLyBanHangEntities db = new QuanLyBanHangEntities();
public string StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=azurestudentsdiag959;AccountKey=yYLvvu+cHBGK/sBLIvbYkVzE/VBS+z5Tw4K5komw2ybyvFYIV7ilKtYZnfa7rYQwUvTSrmFniX9QNrLyq9y3sQ==;EndpointSuffix=core.windows.net";
// GET: Products
public ActionResult Index()
{
return View("~/Views/Backend/Products/Index.cshtml", db.products.ToList());
}
// GET: Products/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
products products = db.products.Find(id);
if (products == null)
{
return HttpNotFound();
}
return View(products);
}
// GET: Products/Create
public ActionResult Create()
{
return View("~/Views/Backend/Products/Create.cshtml");
}
// POST: Products/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(
[Bind(Include = "id,product_code,product_name,description,standard_cost,list_price,target_level,reorder_level,minimum_reorder_quantity,quantity_per_unit,discontinued,category,image")] products products,
HttpPostedFileBase image)
{
if (ModelState.IsValid)
{
// Xử lý file: lưu file vào thư mục UploadedFiles/ProductImages
string _FileName = "";
// Di chuyển file vào thư mục mong muốn
if (image != null && image.ContentLength > 0)
{
_FileName = Path.GetFileName(image.FileName); // QRCode_NenTangUrl.png
string _FileNameExtension = Path.GetExtension(image.FileName); // .png
if ((_FileNameExtension == ".png"
|| _FileNameExtension == ".jpg"
|| _FileNameExtension == ".jpeg"
) == false)
{
return View(String.Format("File có đuôi {0} không được chấp nhận. Vui lòng kiểm tra lại!", _FileNameExtension));
}
// Upload file lên thư mục Web Server (VPS)
string uploadFolderPath = Server.MapPath("~/UploadedFiles/ProductImages");
if (Directory.Exists(uploadFolderPath) == false) // Nếu thư mục cần lưu trữ file upload không tồn tại (chưa có) => Tạo mới
{
Directory.CreateDirectory(uploadFolderPath);
}
// Copy file ảnh vào Thư mục UploadFiles/ProductImages trên Server
//string _path = Path.Combine(uploadFolderPath, _FileName);
//image.SaveAs(_path);
// Lưu thông tin đường dẫn vào Database
products.image = image.FileName;
// Update file lên thư mục Storage Azure
// Create Reference to Azure Storage Account
String strorageconn = this.StorageConnectionString;
CloudStorageAccount storageacc = CloudStorageAccount.Parse(strorageconn);
//Create Reference to Azure Blob
CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
//The next 2 lines create if not exists a container named "democontainer"
CloudBlobContainer container = blobClient.GetContainerReference("democontainer");
container.CreateIfNotExists();
//The next 7 lines upload the file test.txt with the name DemoBlob on the container "democontainer"
CloudBlockBlob blockBlob = container.GetBlockBlobReference(_FileName);
blockBlob.UploadFromStream(image.InputStream);
}
// Lưu dữ liệu
db.products.Add(products);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(products);
}
// GET: Products/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
products products = db.products.Find(id); // SELECT * FROM products WHERE id = 601
if (products == null)
{
return HttpNotFound();
}
ViewBag.Product = products;
return View("~/Views/Backend/Products/Edit.cshtml", products);
}
// POST: Products/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "id,product_code,product_name,description,standard_cost,list_price,target_level,reorder_level,minimum_reorder_quantity,quantity_per_unit,discontinued,category")] products products, string image_oldFile, HttpPostedFileBase image)
{
if (ModelState.IsValid)
{
string uploadFolderPath = Server.MapPath("~/UploadedFiles/ProductImages");
if (image == null) // Nếu không cập nhật file (không chọn file)
{
// Giữ nguyên giá trị của tên file trong cột `image`
products.image = image_oldFile;
}
else // Người ta có chọn file ảnh mới
{
// 1. Xóa file ảnh cũ (để tránh rác)
// ~/UploadedFiles/ProductImages/ASP.NET_CodeXuLyDangKy.png -> đường dẫn file ảnh cũ
string filePathAnhCu = Path.Combine(uploadFolderPath, (products.image == null ? "" : products.image));
if (System.IO.File.Exists(filePathAnhCu))
{
System.IO.File.Delete(filePathAnhCu);
}
// 2. Upload file ảnh mới
// Xử lý file: lưu file vào thư mục UploadedFiles/ProductImages
string _FileName = "";
// Di chuyển file vào thư mục mong muốn
if (image.ContentLength > 0)
{
_FileName = Path.GetFileName(image.FileName); // QRCode_NenTangUrl.png
string _FileNameExtension = Path.GetExtension(image.FileName); // .png
if ((_FileNameExtension == ".png"
|| _FileNameExtension == ".jpg"
|| _FileNameExtension == ".jpeg"
) == false)
{
return View(String.Format("File có đuôi {0} không được chấp nhận. Vui lòng kiểm tra lại!", _FileNameExtension));
}
if (Directory.Exists(uploadFolderPath) == false) // Nếu thư mục cần lưu trữ file upload không tồn tại (chưa có) => Tạo mới
{
Directory.CreateDirectory(uploadFolderPath);
}
string _path = Path.Combine(uploadFolderPath, _FileName);
image.SaveAs(_path);
}
// Lưu tên file vào database
products.image = _FileName;
}
/* UPDATE products
* SET product_code = 'P3333',
* product_name = 'DELL 333'
* WHERE id = 603
*/
db.Entry(products).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(products);
}
// GET: Products/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
products products = db.products.Find(id);
if (products == null)
{
return HttpNotFound();
}
ViewBag.Product = products;
return View("~/Views/Backend/Products/Delete.cshtml", products);
}
// POST: Products/Delete/5
[HttpPost, ActionName("Delete")]
//[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
products products = db.products.Find(id);
db.products.Remove(products);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}