Permalink
Browse files

Fixed: Ajax calls to get Auction details

  • Loading branch information...
mrchief committed Apr 11, 2013
1 parent bdf5ddb commit 0c8e441a751ae40a7caa1aa1b4b9503ede977e51
@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using AutoMapper;
+using Ebuy.Website.Models;
+
+namespace Ebuy.Website.App_Start
+{
+ public class AutoMapperConfig
+ {
+ public static void RegisterMappings()
+ {
+ Mapper.CreateMap<Auction, AuctionViewModel>();
+
+ Mapper.AssertConfigurationIsValid();
+ }
+ }
+}
@@ -1,91 +1,94 @@
using System;
+using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
+using AutoMapper;
using Ebuy.DataAccess;
+using Ebuy.Website.Models;
namespace Ebuy.Website.Controllers
{
- public class AuctionsController : Controller
- {
- [MultipleResponseFormats]
- public ActionResult Index(int page = 0, int size = 25)
- {
- var db = new EbuyDataContext();
- var auctions = db.Auctions.OrderByDescending(x => x.EndTime).Skip(page * 25).Take(size);
- return View("Auctions", auctions.ToArray());
- }
-
- [MultipleResponseFormats]
- public ActionResult Auction(long id)
- {
- var db = new EbuyDataContext();
- var auction = db.Auctions.Find(id);
-
- // The following moved to MultipleResponseFormatsAttribute:
- /*
- // Respond to AJAX requests
- if (Request.IsAjaxRequest())
- return PartialView("Auction", auction);
-
- // Respond to JSON requests
- if (Request.IsJsonRequest())
- return Json(auction);
- */
-
- // Default to a "normal" view with layout
- return View("Auction", auction);
- }
-
- public ActionResult JsonAuction(long id)
- {
- var db = new EbuyDataContext();
- var auction = db.Auctions.Find(id);
- return Json(auction, JsonRequestBehavior.AllowGet);
- }
-
- public ActionResult PartialAuction(long id)
- {
- var db = new EbuyDataContext();
- var auction = db.Auctions.Find(id);
- return PartialView("Auction", auction);
- }
-
- //
- // GET: /Auctions/Create
-
- [HttpGet]
- public ActionResult Create()
- {
- return View();
- }
-
- //
- // POST: /Auctions/Create
-
- [HttpPost]
- public ActionResult Create(Auction auction)
- {
- if (ModelState.IsValid)
- {
- var db = new EbuyDataContext();
+ public class AuctionsController : Controller
+ {
+ [MultipleResponseFormats]
+ public ActionResult Index(int page = 0, int size = 25)
+ {
+ var db = new EbuyDataContext();
+ var auctions = db.Auctions.OrderByDescending(x => x.EndTime).Skip(page * 25).Take(size);
+ return View("Auctions", Mapper.Map<IEnumerable<AuctionViewModel>>(auctions).ToArray());
+ }
+
+ [MultipleResponseFormats]
+ public ActionResult Auction(string id)
+ {
+ var db = new EbuyDataContext();
+ var auction = db.Auctions.FirstOrDefault(x => x.Key == id);
+
+ // The following moved to MultipleResponseFormatsAttribute:
+ /*
+ // Respond to AJAX requests
+ if (Request.IsAjaxRequest())
+ return PartialView("Auction", auction);
+
+ // Respond to JSON requests
+ if (Request.IsJsonRequest())
+ return Json(auction);
+ */
+
+ // Default to a "normal" view with layout
+ return View("Auction", auction);
+ }
+
+ public ActionResult JsonAuction(string id)
+ {
+ var db = new EbuyDataContext();
+ var auction = db.Auctions.FirstOrDefault(x => x.Key == id);
+ return Json(Mapper.Map<AuctionViewModel>(auction), JsonRequestBehavior.AllowGet);
+ }
+
+ public ActionResult PartialAuction(string id)
+ {
+ var db = new EbuyDataContext();
+ var auction = db.Auctions.FirstOrDefault(x => x.Key == id);
+ return PartialView("Auction", auction);
+ }
+
+ //
+ // GET: /Auctions/Create
+
+ [HttpGet]
+ public ActionResult Create()
+ {
+ return View();
+ }
+
+ //
+ // POST: /Auctions/Create
+
+ [HttpPost]
+ public ActionResult Create(Auction auction)
+ {
+ if (ModelState.IsValid)
+ {
+ var db = new EbuyDataContext();
auction.CurrentPrice = auction.StartPrice;
- db.Auctions.Add(auction);
- db.SaveChanges();
+ db.Auctions.Add(auction);
+ db.SaveChanges();
- return RedirectToAction("Details", new { id = auction.Id });
- }
+ return RedirectToAction("Details", new { id = auction.Key });
+ }
- return View(auction);
- }
+ return View(auction);
+ }
//
// GET: /Test/Details/{guid}
- public ActionResult Details(Guid id)
+ public ActionResult Details(string id)
{
var db = new EbuyDataContext();
- var auction = db.Auctions.Find(id);
+ var auction = db.Auctions.FirstOrDefault(x => x.Key == id);
if (auction == null)
{
return HttpNotFound();
@@ -94,56 +97,56 @@ public ActionResult Details(Guid id)
}
- //
- // GET: /Auctions/Edit/5
-
- public ActionResult Edit(int id)
- {
- return View();
- }
-
- //
- // POST: /Auctions/Edit/5
-
- [HttpPost]
- public ActionResult Edit(int id, FormCollection collection)
- {
- try
- {
- // TODO: Add update logic here
-
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
- }
- }
-
- //
- // GET: /Auctions/Delete/5
-
- public ActionResult Delete(int id)
- {
- return View();
- }
-
- //
- // POST: /Auctions/Delete/5
-
- [HttpPost]
- public ActionResult Delete(int id, FormCollection collection)
- {
- try
- {
- // TODO: Add delete logic here
-
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
- }
- }
- }
+ //
+ // GET: /Auctions/Edit/5
+
+ public ActionResult Edit(int id)
+ {
+ return View();
+ }
+
+ //
+ // POST: /Auctions/Edit/5
+
+ [HttpPost]
+ public ActionResult Edit(int id, FormCollection collection)
+ {
+ try
+ {
+ // TODO: Add update logic here
+
+ return RedirectToAction("Index");
+ }
+ catch
+ {
+ return View();
+ }
+ }
+
+ //
+ // GET: /Auctions/Delete/5
+
+ public ActionResult Delete(int id)
+ {
+ return View();
+ }
+
+ //
+ // POST: /Auctions/Delete/5
+
+ [HttpPost]
+ public ActionResult Delete(int id, FormCollection collection)
+ {
+ try
+ {
+ // TODO: Add delete logic here
+
+ return RedirectToAction("Index");
+ }
+ catch
+ {
+ return View();
+ }
+ }
+ }
}
@@ -177,6 +177,7 @@
<Compile Include="Api\AuctionsDataController.cs" />
<Compile Include="Api\CustomExceptionFilter.cs" />
<Compile Include="App_Start\AuthConfig.cs" />
+ <Compile Include="App_Start\AutoMapperConfig.cs" />
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\NinjectWebCommon.cs" />
@@ -186,6 +187,8 @@
<Compile Include="Controllers\AuctionsController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\SearchController.cs" />
+ <Compile Include="Models\AuctionViewModel.cs" />
+ <Compile Include="Models\BidViewModel.cs" />
<Compile Include="Models\SearchViewModel.cs" />
<Compile Include="Filters\InitializeSimpleMembershipAttribute.cs" />
<Compile Include="Global.asax.cs">
@@ -6,6 +6,7 @@
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
+using Ebuy.Website.App_Start;
namespace Ebuy.Website
{
@@ -23,6 +24,8 @@ protected void Application_Start()
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
+
+ AutoMapperConfig.RegisterMappings();
}
}
}
Oops, something went wrong.

0 comments on commit 0c8e441

Please sign in to comment.