public void UpdateEmployeeToProject(EmployeeToProjectEntity employeeToProjectEntity)
        {
            EmployeeToProject employeeToProject = new EmployeeToProject();

            employeeToProject.Id        = employeeToProjectEntity.Id;
            employeeToProject.EmpId     = employeeToProjectEntity.EmpId;
            employeeToProject.ProjectId = employeeToProjectEntity.ProjectId;

            _repositoryEmployeeToProject.Update(employeeToProject);
            _repositoryEmployeeToProject.SaveChanges();
        }
        public void InsertEmployeeToProject(EmployeeToProjectEntity employeeToProjectEntity)
        {
            EmployeeToProject employeeToProject = new EmployeeToProject();

            employeeToProject.Id        = employeeToProjectEntity.Id;
            employeeToProject.EmpId     = employeeToProjectEntity.EmpId;
            employeeToProject.ProjectId = employeeToProjectEntity.ProjectId;

            if (!_repositoryEmployeeToProject.Table.Any(x => x.ProjectId == employeeToProject.ProjectId &&
                                                        x.EmpId == employeeToProject.EmpId))
            {
                _repositoryEmployeeToProject.Insert(employeeToProject);
                _repositoryEmployeeToProject.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        public async Task <JsonResult> AddEmployee(EmployeeDetailsViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (model.EmployeeId != null)
                    {
                        var employee = await _context.Employees.Where(p => p.Id == model.EmployeeId).FirstOrDefaultAsync();

                        if (employee != null)
                        {
                            employee.FirstName   = model.FirstName;
                            employee.LastName    = model.LastName;
                            employee.JoiningDate = DateTime.ParseExact(model.JoiningDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

                            await _context.SaveChangesAsync(HttpContext.User.Identity.Name);

                            return(Json(new { Success = true }));
                        }
                        else
                        {
                            return(Json(new { Error = true }));
                        }
                    }
                    else
                    {
                        var project = await _context.Projects.Where(p => p.Id == model.ProjectId).FirstOrDefaultAsync();

                        if (project != null)
                        {
                            var employee = new Employee
                            {
                                FirstName   = model.FirstName,
                                LastName    = model.LastName,
                                JoiningDate = DateTime.ParseExact(model.JoiningDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)
                            };

                            _context.Employees.Add(employee);
                            await _context.SaveChangesAsync(HttpContext.User.Identity.Name);

                            var empProjectMapping = new EmployeeToProject
                            {
                                ProjectId  = project.Id,
                                EmployeeId = employee.Id
                            };

                            _context.EmployeeToProjects.Add(empProjectMapping);
                            await _context.SaveChangesAsync(HttpContext.User.Identity.Name);

                            return(Json(new { Success = true }));
                        }
                        return(Json(new { Error = true }));
                    }
                }
                return(Json(new { Error = true }));
            }
            catch (Exception ex)
            {
                return(Json(new { Error = true }));
            }
        }