forked from rapPayne/WebGoat.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Product.aspx.cs
82 lines (76 loc) · 2.85 KB
/
Product.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.IO;
using Core;
using Infrastructure;
namespace WebSite
{
public partial class Product : System.Web.UI.Page
{
ProductRepository _repository;
int _productId;
protected void Page_Load(object sender, EventArgs e)
{
try
{
_productId = Convert.ToInt32(RouteData.Values["ProductId"]);
_productId = (_productId == 0) ? Convert.ToInt32(Request.QueryString["Id"]) : _productId;
_repository = new ProductRepository();
var prod = _repository.GetProductById(_productId);
pnlAddToCart.Visible = true;
var imageUrl = "/images/productImages/" + prod.ProductId.ToString() + ".jpg";
if (!File.Exists(Server.MapPath(imageUrl)))
{
imageUrl = "/images/productImages/NoImage.jpg";
}
lblDescription.Text = prod.ToDetailHtml(imageUrl);
}
catch (InvalidOperationException)
{
lblMessage.Text = "Product not found.";
lblMessage.CssClass = "Error";
}
catch (Exception ex)
{
lblMessage.Text = string.Format("An error has occurred: {0}",ex.Message);
lblMessage.CssClass = "Error";
}
}
protected void btnAddToCart_Click(object sender, EventArgs e)
{
Cart cart;
if (Session["Cart"] is Cart)
cart = Session["Cart"] as Cart;
else
cart = new Cart();
short quantity = 1;
try
{
quantity = Convert.ToInt16(txtQuantity.Text);
}
catch (Exception ex)
{
lblMessage.Text = string.Format("An error has occurred: {0}", ex.ToString());
}
//TODO: Put this in try/catch as well
//TODO: Feels like this is too much business logic. Should be moved to OrderDetail constructor?
var productRepository = new ProductRepository();
var product = productRepository.GetProductById(_productId);
var orderDetail = new OrderDetail()
{
Discount = 0.0F,
ProductId = _productId,
Quantity = quantity,
Product = product,
UnitPrice = product.UnitPrice
};
cart.OrderDetails.Add(orderDetail);
Session["Cart"] = cart;
Response.Redirect("~/ViewCart.aspx");
}
public override void Dispose()
{
_repository.Dispose();
base.Dispose();
}
}
}