public ActionResult OwnerPets(PetsRequest petsRequest = null) { using (Context context = Context.Create()) { var ownerId = petsRequest != null ? petsRequest.OwnerId : 1; var ownerEntity = context.Owners.FirstOrDefault(owner => owner.Id == ownerId); if (ownerEntity == null) { throw new DataException(); } var ownerName = ownerEntity.Name; var pets = from op in context.OwnerPets join p in context.Pets on op.PetId equals p.Id where op.OwnerId == ownerId select new PetViewModel { Name = p.Name, Id = p.Id }; if (petsRequest != null && !string.IsNullOrEmpty(petsRequest.sortDirection) && !string.IsNullOrEmpty(petsRequest.fieldName)) { pets = pets.OrderBy($"{petsRequest.fieldName} {petsRequest.sortDirection}"); } ViewBag.SortPets = petsRequest; return(View(new ChildPageViewModel(ownerName) { Pets = pets.ToList(), OwnerId = ownerId })); } }
public IActionResult GetPets(int id, int year) { var request = new PetsRequest() { IdVolunteer = id, Year = year }; try { return Ok(_dbService.GetPets(request)); } catch (Exception ex) { return BadRequest("Error" ); } }