public async Task <ActionResult> NovoOuEditar([Bind("Nome,Email,Id,CreateAt,UpdateAt")] UserDTOCreate usuario) { try { if (ModelState.IsValid) { if (usuario.Id == 0) { await _serService.Post(usuario); } else { var usr = _mapper.Map <UserDTO>(usuario); var u = _mapper.Map <UserDTOUpdate>(usr); await _serService.Put(u); } return(RedirectToAction(nameof(Index))); } return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public async Task <ActionResult> Create([FromBody] UserDTOCreate user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var result = await _service.Post(user); if (result != null) { return(Created(new Uri(Url.Link("GetWithId", new { result.Id })), result)); } else { return(BadRequest()); } } catch (Exception ex) { return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public async Task E_Possivel_Invocar_a_Controller_Create() { var serviceMock = new Mock <IUserService>(); var nome = Faker.Name.FullName(); var email = Faker.Internet.Email(); serviceMock.Setup(c => c.Post(It.IsAny <UserDTOCreate>())).ReturnsAsync( new UserDTOCreateResult { Id = Guid.NewGuid(), Name = nome, Email = email, CreateAt = DateTime.UtcNow } ); _controller = new UsersController(serviceMock.Object); _controller.ModelState.AddModelError("Name", "É um campo Obrigatório"); Mock <IUrlHelper> url = new Mock <IUrlHelper>(); url.Setup(x => x.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:5000"); _controller.Url = url.Object; var userDTOCreate = new UserDTOCreate { Name = nome, Email = email }; var result = await _controller.Post(userDTOCreate); Assert.True(result is BadRequestObjectResult); }
public async Task <ActionResult> Post([FromBody] UserDTOCreate user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); //400 bad request - solicitaçao invalidos } try { var result = await _service.Post(user); if (result != null) { //return Created(new Uri(Url.Link("/api/user/", new { id = result.Id })), result); return(CreatedAtAction(nameof(Get), new { id = result.Id }, result)); } else { return(BadRequest()); } } catch (ArgumentException e) { return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message)); } }
public async Task <UserDTOCreateResult> Post(UserDTOCreate user) { var model = _mapper.Map <UserModel>(user); var entity = _mapper.Map <UserEntity>(model); var result = await _repository.InsertAsync(entity); return(_mapper.Map <UserDTOCreateResult>(result)); }
public UserTest() { IdUsuario = Guid.NewGuid(); NomeUsuario = Faker.Name.FullName(); EmailUsuario = Faker.Internet.Email(); NomeUsuarioAlterado = Faker.Name.FullName(); EmailUsuarioAlterado = Faker.Internet.Email(); for (int i = 0; i < 10; i++) { var dto = new UserDTO() { Id = Guid.NewGuid(), Name = Faker.Name.FullName(), Email = Faker.Internet.Email() }; listaUserDto.Add(dto); } userDto = new UserDTO { Id = IdUsuario, Name = NomeUsuario, Email = EmailUsuario }; userDTOCreate = new UserDTOCreate { Name = NomeUsuario, Email = EmailUsuario }; userCreateResult = new UserCreateResultDTO { Id = IdUsuario, Name = NomeUsuario, Email = EmailUsuario, CreateAt = DateTime.UtcNow }; userDTOUpdate = new UserDTOUpdate { id = IdUsuario, Name = NomeUsuarioAlterado, Email = EmailUsuarioAlterado }; userUpdateResult = new UserUpdateResultDTO { Id = IdUsuario, Name = NomeUsuarioAlterado, Email = EmailUsuarioAlterado, UpdateAt = DateTime.UtcNow }; }
public async Task <ActionResult> Post([FromBody] UserDTOCreate user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); //400 bad request - solicitacao inválida } try { var result = await _service.Post(user); if (result != null) { return(Created(new Uri(Url.Link("GetWithId", new { id = result.Id })), result)); } return(BadRequest()); } catch (ArgumentException e) { return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message)); } }
public async Task E_Possivel_Invocar_a_Controller_Create() { var serviceMock = new Mock <IUserService>(); var nome = Faker.Name.FullName(); var email = Faker.Internet.Email(); serviceMock.Setup(m => m.Post(It.IsAny <UserDTOCreate>())).ReturnsAsync( new UserCreateResultDTO { Id = Guid.NewGuid(), Name = nome, Email = email, CreateAt = DateTime.UtcNow } ); _controller = new UsersController(serviceMock.Object); Mock <IUrlHelper> url = new Mock <IUrlHelper>(); url.Setup(x => x.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:41165"); _controller.Url = url.Object; var userDtoCreate = new UserDTOCreate { Name = nome, Email = email, }; var result = await _controller.Post(userDtoCreate); Assert.True(result is CreatedResult); var resultValue = ((CreatedResult)result).Value as UserCreateResultDTO; Assert.NotNull(resultValue); Assert.Equal(userDtoCreate.Name, resultValue.Name); Assert.Equal(userDtoCreate.Email, resultValue.Email); }
public async Task E_Possivel_Realizar_Crud_Usuario() { await AdicionarToken(); _name = Faker.Name.First(); _email = Faker.Internet.Email(); var userDto = new UserDTOCreate() { Name = _name, Email = _email }; //Post var response = await PostJsonAsync(userDto, $"{hostApi}Users", client); var postResult = await response.Content.ReadAsStringAsync(); var registroPost = JsonConvert.DeserializeObject <UserCreateResultDTO>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(_name, registroPost.Name); Assert.Equal(_email, registroPost.Email); Assert.True(registroPost.Id != default(Guid)); //Get All response = await client.GetAsync($"{hostApi}Users"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var jsonResult = await response.Content.ReadAsStringAsync(); var listaFromJson = JsonConvert.DeserializeObject <IEnumerable <UserDTO> >(jsonResult); Assert.NotNull(listaFromJson); Assert.True(listaFromJson.Count() > 0); Assert.True(listaFromJson.Where(r => r.Id == registroPost.Id).Count() == 1); var updateUserDto = new UserDTOUpdate() { id = registroPost.Id, Name = Faker.Name.FullName(), Email = Faker.Internet.Email() }; //PUT var stringContent = new StringContent(JsonConvert.SerializeObject(updateUserDto), Encoding.UTF8, "application/json"); response = await client.PutAsync($"{hostApi}Users", stringContent); jsonResult = await response.Content.ReadAsStringAsync(); var registroAtualizado = JsonConvert.DeserializeObject <UserUpdateResultDTO>(jsonResult); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotEqual(registroPost.Name, registroAtualizado.Name); Assert.NotEqual(registroPost.Email, registroAtualizado.Email); //GET Id response = await client.GetAsync($"{hostApi}Users/{registroAtualizado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); jsonResult = await response.Content.ReadAsStringAsync(); var registroSelecionado = JsonConvert.DeserializeObject <UserDTO>(jsonResult); Assert.NotNull(registroSelecionado); Assert.Equal(registroSelecionado.Name, registroAtualizado.Name); Assert.Equal(registroSelecionado.Email, registroAtualizado.Email); //DELETE response = await client.DeleteAsync($"{hostApi}Users/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); //GET ID depois do DELETE response = await client.GetAsync($"{hostApi}Users/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }