Created
March 6, 2015 16:13
-
-
Save theuntitled/a4f4b0db50ac5c49dd1b to your computer and use it in GitHub Desktop.
Bootgrid WebAPI Request / Response Handling
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.Collections.Generic; | |
using Newtonsoft.Json; | |
using System.Linq; | |
using System.Web.Http; | |
using System.Web.Http.Description; | |
namespace Projektname.BootgridStuff { | |
/// <summary> | |
/// Represents a xhr request from a bootgrid component | |
/// </summary> | |
public class BootgridRequest { | |
/// <summary> | |
/// The current page number | |
/// </summary> | |
[JsonProperty( PropertyName = "current" )] | |
public int Current { get; set; } | |
/// <summary> | |
/// The number of items to display | |
/// </summary> | |
[JsonProperty( PropertyName = "rowCount" )] | |
public int RowCount { get; set; } | |
/// <summary> | |
/// A list of properties and directions to sort by | |
/// </summary> | |
[JsonProperty( PropertyName = "sort" )] | |
public Dictionary<string , string> Sort { get; set; } | |
/// <summary> | |
/// The search phrase for filtering the results | |
/// </summary> | |
[JsonProperty( PropertyName = "searchPhrase" )] | |
public string SearchPhrase { get; set; } | |
} | |
/// <summary> | |
/// Represents a response for the bootgrid component | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
public class BootgridResponse<T> { | |
/// <summary> | |
/// The current page number | |
/// </summary> | |
[JsonProperty( PropertyName = "current" )] | |
public int Current { get; set; } | |
/// <summary> | |
/// The number of items to display | |
/// </summary> | |
[JsonProperty( PropertyName = "rowCount" )] | |
public int RowCount { get; set; } | |
/// <summary> | |
/// The result list | |
/// </summary> | |
[JsonProperty( PropertyName = "rows" )] | |
public List<T> Rows { get; set; } | |
/// <summary> | |
/// The total number of items for the request | |
/// </summary> | |
[JsonProperty( PropertyName = "total" )] | |
public int Total { get; set; } | |
} | |
public class WhateverModel { | |
public string Id { get; set; } | |
public string Something { get; set; } | |
} | |
public class SomeApiController : ApiController { | |
private readonly List<WhateverModel> _whatevers; | |
public SomeApiController() { | |
_whatevers = new List<WhateverModel>(); | |
} | |
[ResponseType( typeof (BootgridResponse<WhateverModel>) )] | |
public IHttpActionResult PostLookup( BootgridRequest whateverRequest ) { | |
var allValues = _whatevers.Where( item => item.Something == whateverRequest.SearchPhrase ).ToList(); | |
return Ok(new BootgridResponse<WhateverModel> { | |
Total = allValues.Count() , | |
Current = request.Current , | |
RowCount = request.RowCount , | |
Rows = allValues.Skip( (request.Current - 1) * request.RowCount ) | |
.Take( request.RowCount ) | |
.ToList() , | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment