Exemplo n.º 1
0
        public async Task TestB()
        {
            var dataProcessor = new ResetDatabaseProcessor(new HardcodedDataV1());

            dataProcessor.Reset();

            var inputModel = new EmployeeBasicDTO()
            {
                DateOfBirth = DateTime.Now.Date.AddDays(10),
                Salary      = 5000,
                FullName    = "",
                Gender      = Gender.Female
            };

            var response = await _client.PostAsync("/api1.1/employee", new StringContent(JsonConvert.SerializeObject(inputModel), Encoding.UTF8, "application/json"));

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);

            var returnModel = JsonConvert.DeserializeObject <ErrorMessageDTO>(await response.Content.ReadAsStringAsync());

            Assert.NotNull(returnModel);
            Assert.Equal("FullName property cannot be null or empty", returnModel.ErrorMessage);
            Assert.Equal(201, returnModel.InternalErrorCode);

            using (var db = new ApplicationDataContext())
            {
                Assert.Equal(3, await db.EmployeeModels.CountAsync());
            }
        }
Exemplo n.º 2
0
        public async Task TestA()
        {
            var dataProcessor = new ResetDatabaseProcessor(new HardcodedDataV1());

            dataProcessor.Reset();

            var inputModel = new EmployeeBasicDTO()
            {
                DateOfBirth = DateTime.Now.Date.AddDays(10),
                Salary      = 5000,
                FullName    = "FullName - 4",
                Gender      = Gender.Female
            };

            var response = await _client.PostAsync("/api1.1/employee", new StringContent(JsonConvert.SerializeObject(inputModel), Encoding.UTF8, "application/json"));

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var returnModel = JsonConvert.DeserializeObject <EmployeeBasicDTO>(await response.Content.ReadAsStringAsync());

            Assert.NotNull(returnModel);
            Assert.Equal("FullName - 4", returnModel.FullName);
            Assert.NotEqual(Guid.Empty, returnModel.Id);

            using (var db = new ApplicationDataContext())
            {
                Assert.Equal(4, await db.EmployeeModels.CountAsync());
                Assert.NotNull(await db.EmployeeModels.FirstOrDefaultAsync(e => e.Id == returnModel.Id));
            }
        }
        public IActionResult InsertNewEmployee([FromBody] EmployeeBasicDTO modelDTO)
        {
            var employee = _mapper.Map <EmployeeModel>(modelDTO);

            if (String.IsNullOrWhiteSpace(modelDTO.FullName))
            {
                var error = new ErrorMessageDTO("FullName property cannot be null or empty", 201);
                return(BadRequest(error));
            }

            var returnEmployee = _employeeRepo.Insert(employee);

            return(Ok(_mapper.Map <EmployeeBasicDTO>(returnEmployee)));
        }
        public IActionResult UpdateEmployee([FromBody] EmployeeBasicDTO modelDTO)
        {
            if (modelDTO == null)
            {
                return(BadRequest());
            }

            if (_employeeRepo.Get(modelDTO.Id) == null)
            {
                return(NotFound());
            }

            _employeeRepo.Update(_mapper.Map <EmployeeModel>(modelDTO));

            return(Ok());
        }