private void UpdateEmployee(Database.Employee employee, Model.Requests.EmployeeUpdateRequest request)
 {
     if (request.Active != null)
     {
         employee.Active = request.Active;
     }
     if (request.ContractSigned != null)
     {
         employee.ContractSigned = (DateTime)request.ContractSigned;
     }
     if (request.FirstName != null)
     {
         employee.FirstName = request.FirstName;
     }
     if (request.LastName != null)
     {
         employee.LastName = request.LastName;
     }
     if (request.Position != null)
     {
         employee.Position = request.Position;
     }
     if (request.Salary != null)
     {
         employee.Salary = (decimal)request.Salary;
     }
     _context.SaveChanges();
 }
Example #2
0
        public void FillForm()
        {
            try
            {
                string EmployeeIDObj = Request.QueryString[Constants.queryEmployeeID];

                if (!string.IsNullOrEmpty(EmployeeIDObj))
                {
                    int EmployeeID = Convert.ToInt32(EmployeeIDObj);
                    if (EmployeeID > 0)
                    {
                        var EmployeeCollection = context.Employees.Where(x => x.IsActive == true && x.EmployeeID == EmployeeID);
                        if (EmployeeCollection.Count() > 0)
                        {
                            Database.Employee employee = EmployeeCollection.FirstOrDefault();
                            txtEmail.Text           = employee.Email;
                            txtEmpJoiningDate.Text  = employee.JoiningDate == null ? "" : employee.JoiningDate.Value.ToString(Constants.DateFormatDatePicker);
                            txtEmployeeAddress.Text = employee.Address;
                            txtEmployeeDetail.Text  = employee.Detail;
                            txtEmployeeMobile.Text  = employee.Mobile;
                            txtEmployeeName.Text    = employee.EmployeeName;
                            hdnEmployeeID.Value     = Convert.ToString(EmployeeID);
                        }
                        else
                        {
                            lblMessage.Text      = "Employee Not Found";
                            lblMessage.ForeColor = Color.Red;
                        }
                    }
                    else
                    {
                        lblMessage.Text      = "Employee ID Not Valid";
                        lblMessage.ForeColor = Color.Red;
                    }
                }
                else
                {
                    lblMessage.Text      = "Employee ID Not in right format";
                    lblMessage.ForeColor = Color.Red;
                }
            }
            catch
            {
                throw;
            }
        }
        public async Task <ActionResult> UpdateEmployee(int Id, [FromBody] Database.Employee employee)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var res = await employeeProvider.UpdateEmployeeAsync(Id, employee);

                    if (res.IsSuccess)
                    {
                        return(Ok(res.EmployeeId));
                    }
                    return(NotFound(res.ErrorMessage));
                }
                catch (Exception ex)
                {
                    return(BadRequest());
                }
            }
            return(BadRequest());
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                var EmployeeCollection = context.Employees.Where(x => x.IsActive == true && x.EmployeeName == txtEmployeeName.Text && x.Mobile == txtEmployeeMobile.Text);
                if (EmployeeCollection.Count() == 0)
                {
                    Database.Employee employee = new Database.Employee();
                    employee.Address      = txtEmployeeAddress.Text;
                    employee.CreatedBy    = currentUserName;
                    employee.CreatedDate  = DateTime.Now.Date;
                    employee.Detail       = txtEmployeeDetail.Text;
                    employee.Email        = txtEmail.Text;
                    employee.EmployeeName = txtEmployeeName.Text;
                    employee.IsActive     = true;
                    employee.JoiningDate  = Convert.ToDateTime(txtEmpJoiningDate.Text);
                    employee.Mobile       = txtEmployeeMobile.Text;
                    context.Employees.Add(employee);
                    context.SaveChanges();

                    lblMessage.Text      = "Employee Added Successfully";
                    lblMessage.ForeColor = Color.Green;
                    ClearForm();
                }
                else
                {
                    lblMessage.Text      = "Employee Already exist with same name and Mobile";
                    lblMessage.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                Helper.LogError(ex);
                lblMessage.Text      = "Something went Wrong kindly check log";
                lblMessage.ForeColor = Color.Red;
            }
        }
        public async Task <(bool IsSuccess, int EmployeeId, string ErrorMessage)> AddEmployeeAsync(Database.Employee employee)
        {
            try
            {
                logger?.LogInformation("Adding Employee Data");
                if (employee != null)
                {
                    //Adding Employee and Employee Address data
                    await dBContext.Employees.AddRangeAsync(employee);

                    foreach (EmployeeAddress employeeAddress in employee.EmployeeAddresses)
                    {
                        await dBContext.EmployeeAddresses.AddAsync(employeeAddress);
                    }

                    await dBContext.SaveChangesAsync();

                    return(true, employee.Id, null);
                }
                return(false, 0, "No Records Found");
            }
            catch (Exception ex)
            {
                logger?.LogError(ex.ToString());
                return(false, 0, ex.Message);
            }
        }
        public async Task <(bool IsSuccess, int EmployeeId, string ErrorMessage)> UpdateEmployeeAsync(int Id, Database.Employee employee)
        {
            try
            {
                logger?.LogInformation($"Updating Employee {Id} record");
                if (employee != null)
                {
                    //Updating EmployeeAddress
                    foreach (EmployeeAddress employeeAddress in employee.EmployeeAddresses)
                    {
                        dBContext.EmployeeAddresses.UpdateRange(employeeAddress);
                    }

                    dBContext.Entry(employee).State = EntityState.Modified;
                    dBContext.Update(employee);
                    await dBContext.SaveChangesAsync();

                    return(true, employee.Id, null);
                }
                return(false, 0, "Not Found");
            }
            catch (Exception ex)
            {
                logger?.LogError(ex.ToString());
                return(false, 0, ex.Message);
            }
        }
Example #7
0
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string EmpIDObj = hdnEmployeeID.Value;
                if (!string.IsNullOrEmpty(EmpIDObj))
                {
                    int EmpID = Convert.ToInt32(EmpIDObj);

                    if (EmpID > 0)
                    {
                        var EmployeeObj = context.Employees.Where(x => x.IsActive == true && x.EmployeeID == EmpID);
                        if (EmployeeObj.Count() > 0)
                        {
                            var EmployeeCollection = context.Employees.Where(x => x.IsActive == true && x.EmployeeName == txtEmployeeName.Text && x.Mobile == txtEmployeeMobile.Text && x.EmployeeID != EmpID);
                            if (EmployeeCollection.Count() == 0)
                            {
                                Database.Employee employee = EmployeeObj.FirstOrDefault();
                                employee.Address      = txtEmployeeAddress.Text;
                                employee.CreatedBy    = currentUserName;
                                employee.CreatedDate  = DateTime.Now.Date;
                                employee.Detail       = txtEmployeeDetail.Text;
                                employee.Email        = txtEmail.Text;
                                employee.EmployeeName = txtEmployeeName.Text;
                                employee.IsActive     = true;
                                employee.JoiningDate  = Convert.ToDateTime(txtEmpJoiningDate.Text);
                                employee.Mobile       = txtEmployeeMobile.Text;
                                context.SaveChanges();

                                lblMessage.Text      = "Employee Updated Successfully";
                                lblMessage.ForeColor = Color.Green;
                            }
                            else
                            {
                                lblMessage.Text      = "Employee Already Exist Choose Diffrent Name Or Mobile";
                                lblMessage.ForeColor = Color.Red;
                            }
                        }
                        else
                        {
                            lblMessage.Text      = "Employee Not Found";
                            lblMessage.ForeColor = Color.Red;
                        }
                    }
                    else
                    {
                        lblMessage.Text      = "Employee ID Not Valid";
                        lblMessage.ForeColor = Color.Red;
                    }
                }
                else
                {
                    lblMessage.Text      = "Employee ID Not in correct  format";
                    lblMessage.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                Helper.LogError(ex);
                lblMessage.Text      = "Something went Wrong kindly check log";
                lblMessage.ForeColor = Color.Red;
            }
        }