Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed: Ajax calls to get Auction details

  • Loading branch information...
commit 0c8e441a751ae40a7caa1aa1b4b9503ede977e51 1 parent bdf5ddb
@mrchief mrchief authored
View
19 Ebuy.Website/App_Start/AutoMapperConfig.cs
@@ -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();
+ }
+ }
+}
View
249 Ebuy.Website/Controllers/AuctionsController.cs
@@ -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();
+ }
+ }
+ }
}
View
3  Ebuy.Website/Ebuy.Website.csproj
@@ -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">
View
3  Ebuy.Website/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();
}
}
}
View
107 Ebuy.Website/Models/AuctionViewModel.cs
@@ -0,0 +1,107 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Ebuy.Util;
+
+namespace Ebuy.Website.Models
+{
+ public class AuctionViewModel
+ {
+
+ public string Description { get; set; }
+
+ public DateTime? EndTime { get; set; }
+
+ /// <summary>
+ /// Closing Date in mm/dd/yyyy format
+ /// </summary>
+ /// <example>18th Oct, 2012 at 5:00 PM</example>
+ public string EndTimeDisplay
+ {
+ get
+ {
+ if (EndTime == null)
+ return string.Empty;
+
+
+ return EndTime.Value.ToString("MMM d, yyyy 'at' h:mm tt 'GMT'");
+ }
+ }
+
+ public bool HasWinningBid
+ {
+ get { return WinningBid != null; }
+ }
+
+ public WebsiteImage Image
+ {
+ get
+ {
+ var images = Images ?? Enumerable.Empty<WebsiteImage>();
+ return images.FirstOrDefault();
+ }
+ }
+
+ public IEnumerable<WebsiteImage> Images { get; set; }
+
+ public string Key { get; set; }
+
+
+ /// <summary>
+ /// Display Representation in days, hours, minutes
+ /// </summary>
+ /// <example>05d:12h:30m</example>
+ public string RemainingTimeDisplay
+ {
+ get
+ {
+ if (EndTime == null)
+ return "N/A";
+
+
+ var time = EndTime.Value.Subtract(Clock.Now);
+
+
+ if (time.Days > 0)
+ return string.Format("{0} days, {1} hours",
+ time.Days, time.Hours);
+
+
+ if (time.Hours > 0)
+ return string.Format("{0} hours, {1} minutes",
+ time.Hours, time.Minutes);
+
+
+ if (time.Minutes > 0)
+ return string.Format("{0} minutes, {1} seconds",
+ time.Minutes, time.Seconds);
+
+
+ if (time.Seconds > 0)
+ return string.Format("{0} seconds", time.Seconds);
+
+
+ return "Closed";
+ }
+ }
+
+ public string CurrentPrice { get; set; }
+
+ public string Title { get; set; }
+
+ public Bid WinningBid { get; set; }
+
+ public string WinningBidUsername
+ {
+ get
+ {
+ if (HasWinningBid && WinningBid.User != null)
+ return WinningBid.User.DisplayName;
+
+
+ return string.Empty;
+ }
+ }
+ }
+
+}
View
36 Ebuy.Website/Models/BidViewModel.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+
+namespace Ebuy.Website.Models
+{
+ public class BidsViewModel
+ {
+ public AuctionViewModel Auction { get; set; }
+
+ public IEnumerable<BidViewModel> Bids { get; set; }
+
+
+ public string Title
+ {
+ get { return Auction.Title; }
+ }
+ }
+
+
+ public class BidViewModel
+ {
+ public string Amount { get; set; }
+
+ public DateTime Timestamp { get; set; }
+
+ public string TimestampDisplay
+ {
+ get { return Timestamp.ToString("G"); }
+ }
+
+ public string UserDisplayName { get; set; }
+ }
+
+}
View
15 Ebuy.Website/Models/SearchViewModel.cs
@@ -17,19 +17,4 @@ public class SearchViewModel
public IEnumerable<AuctionViewModel> SearchResult { get; set; }
}
-
- public class AuctionViewModel
- {
- public string Image { get; set; }
-
- public long Id { get; set; }
-
- public string Title { get; set; }
-
- public string Description { get; set; }
-
- public string CurrentPrice { get; set; }
-
- public string RemainingTimeDisplay { get; set; }
- }
}
View
2  Ebuy.Website/Views/Auctions/Auction.cshtml
@@ -1,4 +1,4 @@
-@model Ebuy.Auction
+@model AuctionViewModel
<div class="title">@Model.Title</div>
<div class="overview">
View
4 Ebuy.Website/Views/Auctions/Auctions.cshtml
@@ -1,10 +1,10 @@
-@model IEnumerable<Ebuy.Auction>
+@model IEnumerable<AuctionViewModel>
<h2>Auctions</h2>
<ul class="auctions">
@foreach (var auction in Model)
{
- <li class="auction" data-key="@auction.Id">
+ <li class="auction" data-key="@auction.Key">
<a href="#">
<span>@auction.Title</span>
</a>
View
2  Ebuy.Website/Views/Search/Search.cshtml
@@ -65,7 +65,7 @@
<table>
@foreach (var item in @Model.SearchResult)
{
- var auctionUrl = Url.Action("Auction", "Auctions", new { id = item.Id });
+ var auctionUrl = Url.Action("Auction", "Auctions", new { id = item.Key });
<tr>
<td class="searchDescription">
<div class="fieldContainer">
Please sign in to comment.
Something went wrong with that request. Please try again.