Skip to content

Instantly share code, notes, and snippets.

@jmatt
Created April 4, 2012 20:37
Show Gist options
  • Save jmatt/2305387 to your computer and use it in GitHub Desktop.
Save jmatt/2305387 to your computer and use it in GitHub Desktop.
BooksController.cs for RAtLL
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RAtLL_Preview.Models;
namespace RAtLL_Preview.Controllers
{
public class BooksController : Controller
{
private BookDBContext db;
public BooksController() : base()
{
Database.SetInitializer<BookDBContext>(null);
db = new BookDBContext();
}
//
// GET: /Books/All
[OutputCache(Duration = 60, VaryByParam = "none")]
public ActionResult All()
{
var books = db.Books.Include(c => c.Category);
return Json(books, JsonRequestBehavior.AllowGet);
}
//
// GET: /Books/Map
[OutputCache(Duration = 60, VaryByParam = "none")]
public ActionResult Map()
{
var locations = db.Books
.Include(c => c.Category)
.Where(b => b.Map == true);
return Json(locations, JsonRequestBehavior.AllowGet);
}
//
// GET: /Books/Search?term=foo
[OutputCache(Duration = 60, VaryByParam = "q")]
public ActionResult Search(string q)
{
int qint = -1;
IQueryable<Book> locations;
if (int.TryParse(q, out qint))
{
locations = db.Books
.Include(c => c.Category)
.Where(b => b.Title.Contains(q) ||
b.Authors.Contains(q) ||
b.Category.Name.Contains(q) ||
b.Description.Contains(q) ||
b.ISBN.Contains(q) ||
b.PublishDate == qint ||
b.Place.Contains(q));
}
else
{
locations = db.Books
.Include(c => c.Category)
.Where(b => b.Title.Contains(q) ||
b.Authors.Contains(q) ||
b.Category.Name.Contains(q) ||
b.Description.Contains(q) ||
b.ISBN.Contains(q) ||
b.Place.Contains(q));
}
return Json(locations, JsonRequestBehavior.AllowGet);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment