public ActionResult EncontreiAnimal(CreateAnimalViewModel viewModel) { MapperConfiguration config = new MapperConfiguration(cfg => cfg.CreateMap <CreateAnimalViewModel, Animal>()); var mapper = config.CreateMapper(); Animal domain = mapper.Map <Animal>(viewModel); domain.UsuarioID = int.Parse(Cookie.Get()); MemoryStream target = new MemoryStream(); viewModel.ImagemAnimal.InputStream.CopyTo(target); domain.Foto = target.ToArray(); domain.Status = Domain.Enums.Status.Perdido; try { domain.Status = Domain.Enums.Status.Perdido; bll.Inserir(domain); return(RedirectToAction("Lista", "Home")); } catch (PetShopException ex) { StringBuilder errors = new StringBuilder(); foreach (ErrorField item in ex.Errors) { errors.Append("<div style='display: block'>" + item.Error + "</div>"); } ViewBag.Erros = errors.ToString(); } return(View()); }
public async Task Animal_CreateAnimal_WrongUserName_ReturnNotFound() { var userPostModel = new CreateAnimalViewModel { Name = "Craig", UserId = "mn12311111", AnimalType = API.Enums.AnimalType.Dog }; var responseApi = await _fixture.Client.PostAsync("api/Animal/", new JsonContent(userPostModel)); Assert.Equal(System.Net.HttpStatusCode.NotFound, responseApi.StatusCode); Assert.Equal("\"No user for userId provided\"", await responseApi.Content.ReadAsStringAsync()); }
public ActionResult Post([FromBody] CreateAnimalViewModel viewModel) { var user = Models.User.GetUserByUserName(viewModel.UserId); if (user == null) { return(NotFound("No user for userId provided")); } var animal = Animal.CreateAnimal(viewModel.AnimalType, viewModel.Name, user); if (animal == null) { return(BadRequest("Can't create animal")); } var animalViewModel = _mapper.Map <AnimalViewModel>(animal); return(CreatedAtAction("Get", new { id = animalViewModel.Id }, animalViewModel)); }
public async Task Animal_CreateAnimal_ReturnCreatedResponseCode() { var userPostModel = new CreateAnimalViewModel { Name = "Craig", UserId = "mn123", AnimalType = API.Enums.AnimalType.Dog }; var responseApi = await _fixture.Client.PostAsync("api/Animal/", new JsonContent(userPostModel)); responseApi.EnsureSuccessStatusCode(); Assert.Equal(System.Net.HttpStatusCode.Created, responseApi.StatusCode); var responseStrong = await responseApi.Content.ReadAsStringAsync(); var userViewModel = JsonConvert.DeserializeObject <AnimalViewModel>(responseStrong); Assert.Equal(userViewModel.Name, userPostModel.Name); }
public CreateAnimal(CreateAnimalViewModel model) { InitializeComponent(); vm = model; DataContext = vm; }