Пример #1
0
 public virtual IHttpActionResult Add([FromBody] ViewModel entity)
 {
     if (ModelState.IsValid)
     {
         var newEntity = DTOService.ToEntity <ViewModel, DomainEntity>(entity);
         _currentRepo.Add(newEntity);
         return(Json(DTOService.ToDTO <DomainEntity, ViewModel>(_currentRepo.GetAll().Last())));
     }
     return(BadRequest());
 }
Пример #2
0
        public virtual IHttpActionResult Get(int id)
        {
            var foundedEntity = _currentRepo.Get(id);

            if (foundedEntity != null)
            {
                var foundedEntityDto = DTOService.ToDTO <DomainEntity, ViewModel>(foundedEntity);
                return(Json(foundedEntityDto, SERIALIZER_SETTINGS));
            }
            return(NotFound());
        }
Пример #3
0
 public virtual IHttpActionResult Put(int id, [FromBody] ViewModel changedEntity)
 {
     if (ModelState.IsValid)
     {
         if (changedEntity != null)
         {
             var changedDomainEntity = DTOService.ToEntity <ViewModel, DomainEntity>(changedEntity);
             _currentRepo.Update(changedDomainEntity);
             return(Json(DTOService.ToDTO <DomainEntity, ViewModel>(_currentRepo.Get(changedDomainEntity.Id)), SERIALIZER_SETTINGS));
         }
         return(NotFound());
     }
     return(BadRequest());
 }
Пример #4
0
        public virtual IHttpActionResult All(int?pageNumber = 1)
        {
            var entitiesQuery = _currentRepo.GetAll().OrderBy(x => x.Id);

            if (pageNumber.HasValue)
            {
                var totalCount = _currentRepo.GetAll().Count();
                var totalPages = Math.Ceiling((double)totalCount / ENTITIES_PER_PAGE);

                var entities = entitiesQuery
                               .Skip((pageNumber.Value - 1) * ENTITIES_PER_PAGE)
                               .Take(ENTITIES_PER_PAGE)
                               .ToList()
                               .Select(x => DTOService.ToDTO <DomainEntity, ViewModel>(x));
                return(Json(new
                {
                    totalCount = totalCount,
                    totalPages = totalPages,
                    queryResult = entities
                }, SERIALIZER_SETTINGS));
            }
            return(Json(entitiesQuery, SERIALIZER_SETTINGS));
        }