-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddItem.aspx.cs
82 lines (68 loc) · 2.34 KB
/
AddItem.aspx.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
using System;
using System.Data;
using System.IO;
using System.Web;
public partial class AddItem : System.Web.UI.Page
{
private bool isAuthorized = false;
protected void Page_Load(object sender, EventArgs e)
{
AccountController AccountController = new AccountController();
if (Session["CurrentAccount"] != null)
{
Account currentAccount = (Account)Session["CurrentAccount"];
if (AccountController.isAccountAdmin(currentAccount.Id.ToString()))
{
isAuthorized = true;
}
else
{
isAuthorized = false;
Response.Redirect("Index.aspx");
}
}
else
{
isAuthorized = false;
Response.Redirect("Index.aspx");
}
}
protected void AddButton_Click(object sender, EventArgs e)
{
ItemController itemController = new ItemController();
Item newItem = new Item();
newItem.Name = ItemNameTextBox.Text;
newItem.PricePerItem = Convert.ToDouble(PriceTextBox.Text);
newItem.Description = DescriptionTextBox.Text;
string uploadFolderPath = Server.MapPath(@"/product_images/" + newItem.Id + "-" + newItem.Name + "/");
if (isAuthorized)
{
if (!Directory.Exists(uploadFolderPath))
{
Directory.CreateDirectory(uploadFolderPath);
}
string filePath;
if (ProductImageFileUpload.HasFile)
{
string uploadFilePath = uploadFolderPath + ProductImageFileUpload.FileName;
ProductImageFileUpload.SaveAs(uploadFilePath);
filePath = @"~/product_images/" + newItem.Id + "-" + newItem.Name + "/" + ProductImageFileUpload.FileName;
}
else
{
filePath = @"~/product_images/no_image.png";
}
newItem.ProductImagePath = filePath;
itemController.addItem(newItem);
ErrorLabel.Text = "Item added!";
ItemNameTextBox.Text = "";
PriceTextBox.Text = "";
DescriptionTextBox.Text = "";
ProductImageFileUpload.Dispose();
}
else
{
ErrorLabel.Text = "You are not authorized to enter new items in the database!";
}
}
}