public IHttpActionResult Get([FromUri] ProductApiParameterModel param) { try { List <ProductApiModel> result; IQueryable <ProductModel> query; if (param.GroupId.HasValue) { query = dbContext.GroupProductModels.Where(gp => gp.Deleted == false).Where(gp => gp.GroupModelId == (int)param.GroupId).Select(gp => gp.ProductModel).OrderBy(p => p.Id); } else if (param.MakerId.HasValue) { query = dbContext.ProductModels.Where(p => p.MakerModelId == (int)param.MakerId).OrderBy(p => p.Id); } else { query = dbContext.ProductModels.OrderBy(p => p.Id); } if (!param.Deleted) { query = query.Where(p => p.Deleted == false); } if (param.Enabled) { query = query.Where(p => p.Enabled == true); } if (param.Limit.HasValue) { if (param.Page.HasValue) { query = query.Skip((int)param.Limit * (int)param.Page).Take((int)param.Limit); } else { query = query.Take((int)param.Limit); } } result = query.ProjectTo <ProductApiModel>().ToList(); return(Ok(result)); } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult GetProductMaxPages([FromUri] ProductApiParameterModel param) { try { int maxcount = 0; int maxpages = 0; IQueryable <ProductModel> query; if (param.GroupId.HasValue) { query = dbContext.GroupProductModels.Where(gp => gp.Deleted == false).Where(gp => gp.GroupModelId == (int)param.GroupId).Select(gp => gp.ProductModel).OrderBy(p => p.Id); } else if (param.MakerId.HasValue) { query = dbContext.ProductModels.Where(p => p.MakerModelId == (int)param.MakerId).OrderBy(p => p.Id); } else { query = dbContext.ProductModels.OrderBy(p => p.Id); } if (!param.Deleted) { query = query.Where(p => p.Deleted == false); } if (param.Enabled) { query = query.Where(p => p.Enabled == true); } maxcount = query.Count(); if (param.Limit.HasValue) { maxpages = (int)(maxcount / (int)param.Limit); if ((int)(maxcount % (int)param.Limit) > 0) { maxpages += 1; } } return(Ok(new { count = maxcount, pages = maxpages })); } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult Get(int id, [FromUri] ProductApiParameterModel param) { try { if (id <= 0) { return(BadRequest(Messages.ApiIllegalParameter)); } ProductModel model; model = dbContext.ProductModels.Where(x => x.Id == id).SingleOrDefault(); if (model == null) { return(NotFound()); } ProductApiModel result = Mapper.Map <ProductApiModel>(model); return(Ok(result)); } catch (Exception ex) { return(InternalServerError(ex)); } }