using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using PagedList; using Newtonsoft.Json; using System.Collections; [ezAuthorize] public class countryController : BaseApiController { // GET api/ public IEnumerable Get() { var list = _db.countries.ToList(); if (list == null) throw new HttpResponseException(HttpStatusCode.NotFound); return list; } // GET api//5 public Model.country Get(string id) { var item = _db.countries.Where(q => q.ID == id).FirstOrDefault(); if (item == null) throw new HttpResponseException(HttpStatusCode.NotFound); return item; } // POST api/ public void Post([FromBody] string value) { } // PUT api//5 public void Put(string id, [FromBody] string value) { } [HttpPost] [Route("api/country/GetList")] public IHttpActionResult GetList([FromBody] Model.ViewModel.country q, int page, int pageSize = 10, string sortBy = "", bool sortDesc = false) { var qry = _db.countries.AsEnumerable(); if (!string.IsNullOrEmpty(q.keyword)) qry = qry.Where(o => o.ID.Contains(q.keyword.Trim()) || o.name_en.Contains(q.keyword.Trim()) || o.name_zh.Contains(q.keyword.Trim())); qry = qry.OrderBy(o => o.range).ThenBy(o => o.name_en); var ret = new { list = qry.ToPagedList(page, pageSize).Select(x => new { id = x.ID, name_en = x.name_en, name_zh = x.name_zh, }), count = qry.Count() }; if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound); return Ok(ret); } }