Created
March 23, 2011 23:51
-
-
Save jgable/884284 to your computer and use it in GitHub Desktop.
TeamController - Hackatopia Team Demo Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Web.Mvc; | |
using JSONPhoneApp1.Web.Models; | |
public class TeamController : Controller | |
{ | |
// Our Team data; fake data is loaded in the constructor. | |
private static TeamRepository _teams = new TeamRepository(); | |
/// <summary> | |
/// A simple echo "Hello World" service for testing. | |
/// </summary> | |
[OutputCache(NoStore=true, Duration=1)] | |
public ActionResult Index() | |
{ | |
return new JsonResult | |
{ | |
JsonRequestBehavior = JsonRequestBehavior.AllowGet, | |
Data = new | |
{ | |
Success = true, | |
Time = DateTimeOffset.UtcNow.ToString(), | |
Message = "Hello Hackatopia!" | |
} | |
}; | |
} | |
/// <summary> | |
/// List all teams | |
/// </summary> | |
public ActionResult List() | |
{ | |
return new JsonResult | |
{ | |
JsonRequestBehavior = JsonRequestBehavior.AllowGet, | |
Data = new | |
{ | |
Success = true, | |
Teams = _teams.Where(x => true).ToList(), | |
} | |
}; | |
} | |
/// <summary> | |
/// Get Team by Id | |
/// </summary> | |
public ActionResult Id(int id) | |
{ | |
var found = _teams.First(x => x.Id == id); | |
return new JsonResult | |
{ | |
JsonRequestBehavior = JsonRequestBehavior.AllowGet, | |
Data = new | |
{ | |
Success = found != null, | |
Team = found, | |
} | |
}; | |
} | |
/// <summary> | |
/// Search by a term; matches Team Name, Description or Membernames | |
/// </summary> | |
public ActionResult Search(string id) | |
{ | |
var loweredTerm = id.ToLower(); | |
var found = _teams.Where(x => x.Name.ToLower().Contains(loweredTerm) | |
|| x.Description.ToLower().Contains(loweredTerm) | |
|| x.MemberNames.Any(name => name.ToLower().Contains(loweredTerm))) | |
.ToList(); | |
return new JsonResult | |
{ | |
JsonRequestBehavior = JsonRequestBehavior.AllowGet, | |
Data = new | |
{ | |
Success = found.Count > 0, | |
Teams = found | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment