public HttpResponseMessage Post([FromBody] CrewDTO crew) { if (ModelState.IsValid && crew != null && validator.Validate(crew).IsValid) { _service.Post <Crew>(_mapper.Map <CrewDTO, Crew>(crew)); _service.SaveChanges(); return(new HttpResponseMessage(HttpStatusCode.OK)); } else { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }
public async Task ValidationMappingCrew_when_validate_pilot_OK_then_map() { var validator = new CrewDTOValidator(); CrewDTO correct = new CrewDTO() { Id = 1, Stewardesses = new List <StewardessDTO> { new StewardessDTO() { Id = 1, CrewId = 1 } }, Pilot = new PilotDTO() { Id = 5, Experience = 3, Surname = "sur" } }; CrewDTO incorrect = new CrewDTO() { Id = 2 }; bool correctRes = validator.Validate(correct).IsValid; Assert.True(correctRes); var mapped = _mapper.Map <CrewDTO, Crew>(correct); if (correctRes) { await _service.Post(mapped); } bool incorrectRes = validator.Validate(incorrect).IsValid; Assert.False(incorrectRes); var mappedIncorrect = _mapper.Map <CrewDTO, Crew>(incorrect); if (incorrectRes) { await _service.Post(mapped); } }
public async Task <HttpResponseMessage> Post([FromBody] CrewDTO crew) { foreach (var st in crew.Stewardesses) { st.CrewId = crew.Id; } if (ModelState.IsValid && crew != null && validator.Validate(crew).IsValid) { await _service.Post <Crew>(_mapper.Map <CrewDTO, Crew>(crew)); await _service.SaveChanges(); return(new HttpResponseMessage(HttpStatusCode.OK)); } else { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }