Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
TeamController - Hackatopia Team Demo Controller
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