Exemple #1
0
        public async Task UpdateEmployee_WhenSuppliedWithAnExistingEmployeeObject_ShouldReturnTheUpdatedObject()
        {
            Employee Source = new Employee()
            {
                id         = 1,
                name       = "Peter Parker",
                email      = "*****@*****.**",
                gender     = "Male",
                status     = "Active",
                created_at = DateTime.Now,
                updated_at = DateTime.Now
            };

            SaveEmployeeResponse response = new SaveEmployeeResponse()
            {
                data = new Employee()
                {
                    id = 1, name = "Peter Parker", email = "*****@*****.**", gender = "Male", status = "Active", created_at = DateTime.Now, updated_at = DateTime.Now
                },
                Success = true
            };

            restApiMock.Setup(x => x.UpdateEmployee(Source)).ReturnsAsync(response);

            var result = await employeeService.UpdateEmployee(Source);

            Assert.AreEqual(1, result.data.id);
            Assert.AreEqual(Source.name, result.data.name);
            Assert.AreEqual(Source.email, result.data.email);
            Assert.AreEqual(Source.gender, result.data.gender);
            Assert.AreEqual(Source.status, result.data.status);
            Assert.IsTrue(result.Success);
        }
Exemple #2
0
        public async Task <SaveEmployeeResponse> UpdateEmployee(Employee employee)
        {
            var content = new StringContent(JsonConvert.SerializeObject(employee), Encoding.UTF8, "application/json");

            using (HttpResponseMessage response = await ApiClient.PutAsync("/public-api/users/" + employee.id, content))
            {
                if (response.IsSuccessStatusCode)
                {
                    string responseContent = await response.Content.ReadAsStringAsync();

                    SaveEmployeeResponse result = JsonConvert.DeserializeObject <SaveEmployeeResponse>(responseContent);
                    result.Success = true;
                    return(result);
                }

                return(new SaveEmployeeResponse()
                {
                    Success = false, FailureReason = response.ReasonPhrase
                });
            }
        }
        public async void Save()
        {
            if (!_dataValidator.isValidEmail(SelectedEmployee.email))
            {
                MessageBox.Show("Email Validation Failed: Entered email is not in the correct format");
                return;
            }

            SaveEmployeeResponse response = new SaveEmployeeResponse();

            try
            {
                if (SelectedEmployee.isExisting)
                {
                    response = await _employeeService.UpdateEmployee(SelectedEmployee);
                }
                else
                {
                    response = await _employeeService.AddEmployee(SelectedEmployee);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
            }

            if (response.Success)
            {
                if (!SelectedEmployee.isExisting)
                {
                    Employees.Add(response.data);
                }
                New();
            }
            else
            {
                MessageBox.Show("Save Employee failed: " + response.FailureReason);
            }
        }