예제 #1
0
        public async Task <IActionResult> OnPostCreateEmployee()
        {
            string       respone = "True";
            MemoryStream stream  = new MemoryStream();

            Request.Body.CopyTo(stream);
            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream))
            {
                string requestBody = reader.ReadToEnd();
                if (requestBody.Length > 0)
                {
                    var obj = JsonConvert.DeserializeObject <EmployeeVM>(requestBody);
                    if (obj != null)
                    {
                        var check = await _accountService.isExistedUsernameAsync(obj.Username);

                        if (!check)
                        {
                            respone = "False";
                        }
                        else
                        {
                            EmployeeDTO emp = new EmployeeDTO();
                            emp.LastName  = obj.LastName;
                            emp.FirstName = obj.FirstName;
                            emp.Phone     = obj.Phone;
                            emp.Salary    = obj.Salary;
                            emp.JobId     = obj.JobId;
                            if (obj.Status == "AVAILABLE")
                            {
                                emp.Status = STATUS.AVAILABLE;
                            }
                            else
                            {
                                emp.Status = STATUS.DISABLED;
                            }
                            AddressDTO address = new AddressDTO();
                            address.toValue(obj.Address);
                            emp.Address = address;
                            await _service.addEmployeeAsync(emp);

                            IEnumerable <EmployeeDTO> Lists = await _service.getAllEmployeeAsync();

                            EmployeeDTO LastEmp = Lists.Last();
                            AccountDTO  account = new AccountDTO();
                            account.PersonId = LastEmp.Id;
                            account.Username = obj.Username;
                            account.Password = obj.Password;

                            await _accountService.addAccountAsync(account);

                            // emp.Birthdate=obj.Birthdate;
                        }
                        // _service
                    }
                }
            }
            return(new JsonResult(respone));
        }