Exemplo n.º 1
0
 public IHttpActionResult Get(int id)
 {
     try
     {
         var       result    = _incomeService.Get(id);
         IncomeDTO incomeDTO = IncomeDTO.From(result);
         return(Ok(incomeDTO));
     }
     catch (Exception ex)
     {
         if (ex.InnerException == null)
         {
             return(Content(HttpStatusCode.InternalServerError, ex.Message));
         }
         else
         {
             return(Content(HttpStatusCode.InternalServerError, ex.InnerException.Message));
         }
     }
 }
Exemplo n.º 2
0
        public IHttpActionResult Get([FromUri] IncomeListParameter param)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    IList <Income> incomes = _incomeService.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  = _incomeService.GetAll().Count();
                        var totalPage  = totalRows / param.PageSize;
                        var totalPages = (int)Math.Ceiling((double)totalRows / param.PageSize);
                        if (totalPages < 0)
                        {
                            totalPages = 0;
                        }

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

                        IList <IncomeDTO> colls = IncomeDTO.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 <IncomeDTO> dto = IncomeDTO.From(incomes);
                        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));
                }
            }
        }