private void ValidateTake(ref SkipTakeModel model, int howManyTake) { if (model.Take == 0) { model.Take = howManyTake; } }
/// <summary> /// 根据总数,每页数,和当前第几页,算出应该skip多少条,超出最大页数之后,返回最后一页面数据 /// </summary> /// <param name="allcount">信息总数</param> /// <param name="pagesize">每页条数</param> /// <param name="pageindex">当前页</param> /// <returns></returns> public static SkipTakeModel GetSkipShowFinalPage(int allcount, int pagesize, int pageindex) { SkipTakeModel model = new SkipTakeModel(); if (pageindex > 0) { int maxpage = allcount / pagesize; if (allcount % pagesize > 0) { maxpage++; } pageindex = Math.Min(maxpage, pageindex); var max = Math.Max(0, pagesize * (pageindex - 1)); model.Skip = max; model.Take = pagesize; model.MaxPage = maxpage; } else { model.Skip = 0; model.Take = pagesize; model.MaxPage = 1; } return(model); }
public async Task <IActionResult> GetLastCoinsValues([FromBody] SkipTakeModel skipTake) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } ValidateTake(ref skipTake, 10); try { var getLatestCoinsValue = await this.coinService.TakeAndSkipLatestCoinsValue(skipTake.Skip, skipTake.Take); return(Ok(getLatestCoinsValue)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }