Пример #1
0
 public IHttpActionResult Get(int id)
 {
     try
     {
         var       result    = _budgetService.Get(id);
         BudgetDTO budgetDTO = BudgetDTO.From(result);
         return(Ok(budgetDTO));
     }
     catch (Exception ex)
     {
         if (ex.InnerException == null)
         {
             return(Content(HttpStatusCode.InternalServerError, ex.Message));
         }
         else
         {
             return(Content(HttpStatusCode.InternalServerError, ex.InnerException.Message));
         }
     }
 }
Пример #2
0
        public IHttpActionResult Get([FromUri] BudgetListParameter param)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    IList <Budget> budgets = _budgetService.GetAll().ToList();
                    if (param.IsPagination())
                    {
                        string keyword = string.Empty;
                        string field   = string.Empty;

                        param.Validate();
                        keyword = param.Search;
                        field   = param.SearchBy;

                        int skip = 0;
                        if (param.PageNo > 0)
                        {
                            skip = (param.PageNo - 1) * param.PageSize;
                        }

                        int totalRows  = _budgetService.GetAll().Count();
                        var totalPage  = totalRows / param.PageSize;
                        var totalPages = (int)Math.Ceiling((double)totalRows / param.PageSize);
                        if (totalPages < 0)
                        {
                            totalPages = 0;
                        }

                        var result = budgets.Skip(skip).Take(param.PageSize).ToList();

                        IList <BudgetDTO> colls = BudgetDTO.From(result);

                        PaginationDTO page = new PaginationDTO();
                        page.PageCount = totalPages;
                        page.PageNo    = param.PageNo;
                        page.PageSize  = param.PageSize;
                        page.results   = colls;

                        return(Ok(page));
                    }
                    else
                    {
                        IList <BudgetDTO> dto = BudgetDTO.From(budgets);
                        return(Ok(dto));
                    }
                }
                else
                {
                    string errorResult = string.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
                    return(Content(HttpStatusCode.BadRequest, errorResult));
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException == null)
                {
                    return(Content(HttpStatusCode.InternalServerError, ex.Message));
                }
                else
                {
                    return(Content(HttpStatusCode.InternalServerError, ex.InnerException.Message));
                }
            }
        }