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()); }
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()); }
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()); }
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)); }