1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 using System
.Collections
;
17 namespace PetStore
.Web
.Controllers
.Admin
23 using Castle
.MonoRail
.Framework
;
24 using Castle
.MonoRail
.Framework
.Helpers
;
29 /// This controller illustrates a CRUD without
30 /// Scaffolding. Note that this controller does
31 /// not use the service layer (only for the brevity's sake)
34 public class ProductManagementController
: AbstractSecureController
36 private String productImagesDir
;
39 /// Guess who is going to set this property?
40 /// Yeah, this is inversion of control, baby.
42 public string ProductImagesDir
44 get { return productImagesDir; }
45 set { productImagesDir = value; }
50 IList products
= Product
.FindAll();
52 PropertyBag
.Add("list", PaginationHelper
.CreatePagination(products
, 10));
57 // This is one approach just to reuse the same form
58 // on the edit action and to preserve the inputs
60 PropertyBag
.Add("product", new Product());
62 // To populate the select html control
63 PropertyBag
.Add("categories", Category
.FindAll());
65 // Only necessary as we might call this action directly (see below)
70 public void Create([DataBind("product")] Product product
, HttpPostedFile picture
)
72 if (picture
.ContentLength
== 0)
74 Flash
["error"] = "You must attach a picture to the product";
79 String filename
= String
.Format("{0}{1}",
80 Guid
.NewGuid().ToString("N"), Path
.GetExtension(picture
.FileName
));
82 picture
.SaveAs(Path
.Combine(GetPictureCompleteDir(), filename
));
84 product
.PictureFile
= filename
;
88 Redirect("ProductManagement", "list");
91 private String
GetPictureCompleteDir()
93 return Path
.Combine(Context
.ApplicationPhysicalPath
, ProductImagesDir
);