//[AutoValidateAntiforgeryToken] public async Task <IActionResult> Edit(PersonWithCityVM person) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var newPerson = await _service.Edit(person.Person, person.CityId); if (newPerson == null) { return(BadRequest("Something went wrong while updating the person. Please try again")); } PersonWithCityVM personVM = new PersonWithCityVM { CityName = newPerson.City?.Name ?? "Homeless", CityId = newPerson.City?.Id ?? null }; newPerson.City = null; personVM.Person = newPerson; return(Ok(personVM)); }
public async Task <PersonWithCityVM> FindPerson(Guid id) { if (id == null || string.IsNullOrWhiteSpace(id.ToString())) { return(null); } var person = await _db.People .Include(x => x.City) .SingleOrDefaultAsync(x => x.Id == id); PersonWithCityVM personVM = new PersonWithCityVM { CityName = person.City?.Name, CityId = person.City?.Id }; person.City = null; personVM.Person = person; if (person == null) { return(null); } return(personVM); }
public async Task <List <PersonWithCityVM> > AllPeople() { var people = await _db.People .Include(x => x.City) .ToListAsync(); if (people == null || people.Count == 0) { return(null); } List <PersonWithCityVM> peopleVM = new List <PersonWithCityVM>(); foreach (var item in people) { PersonWithCityVM personVM = new PersonWithCityVM { CityName = item.City?.Name ?? "Homeless", CityId = item.City?.Id ?? null }; item.City = null; personVM.Person = item; peopleVM.Add(personVM); } return(peopleVM); }
public async Task Find_FindOneInvalidPersonWithCity_ReturnsNullValueAsync() { var person = new PersonWithCityVM { Person = OneInvalidPersonWithCity() }; _service.Setup(x => x.Create(person.Person, person.CityId)); _service.Setup(x => x.FindPerson(person.Person.Id)).Returns(Task.FromResult <PersonWithCityVM>(null)); var result = await _service.Object.FindPerson(person.Person.Id); Assert.Null(result); }
public async Task Find_FindOnePersonWithCity_ReturnsCorrectPersonAsync() { var personOne = OneValidPersonWithoutCity(); var person = new PersonWithCityVM { Person = OneValidPersonWithCity() }; _service.Setup(x => x.Create(personOne, null)); _service.Setup(x => x.Create(person.Person, person.CityId)); _service.Setup(x => x.FindPerson(person.Person.Id)).ReturnsAsync(person); var result = await _service.Object.FindPerson(person.Person.Id); Assert.Equal(person, result); }
public async Task Update_UpdateToInvalidWithCity_ReturnsNullValueAndDoesNotChangeOriginalPersonAsync() { var person = OneValidPersonWithCity(); var personVM = new PersonWithCityVM { Person = person }; var editPerson = person; editPerson.FirstName = ""; _service.Setup(x => x.Create(person, person.City.Id)); _service.Setup(x => x.Edit(editPerson, editPerson.City.Id)).Returns(Task.FromResult <Person>(null)); _service.Setup(x => x.FindPerson(person.Id)).ReturnsAsync(personVM); var nullResult = await _service.Object.Edit(editPerson, editPerson.City.Id); var result = await _service.Object.FindPerson(person.Id); Assert.Null(nullResult); Assert.Equal(personVM, result); }