public IActionResult GetEmployeeSurveyConsent([FromBody] EmployeeSurvey model) { if (model == null) { return(BadRequest("Error, Contact your Manager/ Administrator!")); } try { Employee employee = _context.Employees.FirstOrDefault(id => id.Identifier.ToString() == model.EmployeeIdentifier); Project project = _context.Projects.FirstOrDefault(id => id.ProjectIdentifier.ToString() == model.ProjectIdentifier); ProjectParticipant projectParticipant = _context.ProjectParticipants.FirstOrDefault(id => (id.ParticipantId == employee.EmployeeId) && (id.ProjectId == project.ProjectId)); projectParticipant.IsAcknowledged = true; _context.ProjectParticipants.Update(projectParticipant); _context.SaveChanges(); return(Ok()); } catch (Exception Ex) { return(BadRequest("Something bad happened! " + Ex.Message)); } }
public async Task <ActionResult <EmployeeSurvey> > PostEmployeeSurvey(EmployeeSurvey employeeSurvey) { _context.EmployeeSurvey.Add(employeeSurvey); await _context.SaveChangesAsync(); return(CreatedAtAction("GetEmployeeSurvey", new { id = employeeSurvey.SurveyId }, employeeSurvey)); }
public async Task <IActionResult> PutEmployeeSurvey(int id, EmployeeSurvey employeeSurvey) { if (id != employeeSurvey.SurveyId) { return(BadRequest()); } _context.Entry(employeeSurvey).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeSurveyExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public IActionResult GetEmployeeSurvey([FromBody] EmployeeSurvey model) { if (model == null) { return(BadRequest("Error, Make sure form is complete!")); } try { Employee employee = _context.Employees.FirstOrDefault(id => id.Identifier.ToString() == model.EmployeeIdentifier); Project project = _context.Projects.FirstOrDefault(id => id.ProjectIdentifier.ToString() == model.ProjectIdentifier); ProjectParticipant projectParticipant = _context.ProjectParticipants.FirstOrDefault(id => (id.ParticipantId == employee.EmployeeId) && (id.ProjectId == project.ProjectId)); if ((employee != null) && (project != null)) { var employeeSurvey = new { Name = employee.Name, Surname = employee.Surname, Email = employee.Email, Organization = _context.AssetNodes.FirstOrDefault(id => id.AssetNodeId == employee.AssetNodeId).Name, Root = _context.AssetNodes.FirstOrDefault(id => id.AssetNodeId == _context.AssetNodes.FirstOrDefault(id => id.AssetNodeId == employee.AssetNodeId).RootAssetNodeId).Name, ProjectName = project.Name, ProjectStartDate = project.StartDate.ToString("dd MMM yyyy"), ProjectEndDate = project.EndDate.ToString("dd MMM yyyy"), ProjectType = _context.Surveys.FirstOrDefault(id => id.SurveyId == project.SurveyId).Name, IsParticipated = projectParticipant.IsParticipated, IsAcknowleged = projectParticipant.IsAcknowledged }; return(Ok(employeeSurvey)); } return(Ok()); } catch (Exception Ex) { return(BadRequest("Something bad happened. " + Ex.Message)); } }
public async Task <ActionResult <Employee> > PostEmployee(EmployeeCreate employee) { var identity = HttpContext.User.Identity as ClaimsIdentity; IEnumerable <Claim> claim = identity.Claims; var RoleClaim = claim .Where(x => x.Type == ClaimTypes.Role) .FirstOrDefault(); var usernameClaim = claim .Where(x => x.Type == ClaimTypes.Name) .FirstOrDefault(); var userName = await _context.User .Where(x => x.Id == Int32.Parse(usernameClaim.Value)) .AsNoTracking() .FirstOrDefaultAsync(); Employee employeeobj = new Employee(); EmployeeSurvey employeessurveyobj = new EmployeeSurvey(); if (userName == null) { return(BadRequest(new { message = "Woops" })); } bool isAdmin = RoleClaim.Value == "admin"; if (isAdmin) { if (employee.Age > 0) { employeeobj.Age = employee.Age; } if (employee.Attrition != null) { employeeobj.Attrition = employee.Attrition; } if (employee.BusinessTravel != null) { var BusinessTravel = await _context.BusinessTravel .AsNoTracking() .FirstOrDefaultAsync(x => x.BusinessTravel1 == employee.BusinessTravel); if (BusinessTravel != null) { employeeobj.BusinessTravel = BusinessTravel.BusinessTravelId; } else { return(BadRequest(new { message = "Business Travel Value needs to match the table's value or you need to add a new value" })); } } if (employee.DailyRate > 0) { employeeobj.DailyRate = employee.DailyRate; } if (employee.Department != null) { var Department = await _context.Department .AsNoTracking() .FirstOrDefaultAsync(x => x.Department1 == employee.Department); if (Department != null) { employeeobj.Department = Department.DepartmentId; } else { return(BadRequest(new { message = "Department Value needs to match the table's value or you need to add a new department" })); } } if (employee.DistanceFromHome > 0) { employeeobj.DistanceFromHome = employee.DistanceFromHome; } if (employee.Education > 0) { employeeobj.Education = employee.Education; } if (employee.Education > 0) { employeeobj.Education = employee.Education; } if (employee.EducationField != null) { var EducationField = await _context.EducationField .AsNoTracking() .FirstOrDefaultAsync(x => x.EducationField1 == employee.EducationField); if (EducationField != null) { employeeobj.EducationField = EducationField.EducationFieldId; } else { return(BadRequest(new { message = "Education Field Value needs to match the table's value or you need to add a new Education Field" })); } } if (employee.Gender != null) { var Gender = await _context.Gender .AsNoTracking() .FirstOrDefaultAsync(x => x.Gender1 == employee.Gender); if (Gender != null) { employeeobj.EducationField = Gender.GenderId; } else { return(BadRequest(new { message = "That Gender Value needs to match the table's value or you need to add a new Gender" })); } } if (employee.HourlyRate > 0 && !isAdmin) { return(Forbid()); } else if (employee.HourlyRate > 0 && isAdmin) { employeeobj.HourlyRate = employee.HourlyRate; } if (employee.JobInvolvement > 0) { employeeobj.JobInvolvement = employee.JobInvolvement; } if (employee.JobLevel > 0) { employeeobj.JobLevel = employee.JobLevel; } if (employee.MaritalStatus != null) { var MaritalStatus = await _context.MaritalStatus .AsNoTracking() .FirstOrDefaultAsync(x => x.MaritalStatus1 == employee.MaritalStatus); if (MaritalStatus != null) { employeeobj.MaritalStatus = MaritalStatus.MaritalStatusId; } else { return(BadRequest(new { message = "That Marital Status needs to match the table's value or you need to add a new Marital Status" })); } } if (employee.MonthlyIncome > 0 && !isAdmin) { return(Forbid()); } else if (employee.MonthlyIncome > 0 && isAdmin) { employeeobj.MonthlyIncome = employee.MonthlyIncome; } if (employee.MonthlyRate > 0 && !isAdmin) { return(Forbid()); } else if (employee.MonthlyRate > 0 && isAdmin) { employeeobj.MonthlyRate = employee.MonthlyRate; } if (employee.NumCompaniesWorked > 0) { employeeobj.NumCompaniesWorked = employee.NumCompaniesWorked; } if (employee.OverTime != null) { employeeobj.OverTime = employee.OverTime; } if (employee.PercentSalaryHike > 0 && !isAdmin) { return(Forbid()); } else if (employee.PercentSalaryHike > 0 && isAdmin) { employeeobj.PercentSalaryHike = employee.PercentSalaryHike; } if (employee.PerformanceRating > 0 && !isAdmin) { return(Forbid()); } else if (employee.PerformanceRating > 0 && isAdmin) { employeeobj.PerformanceRating = employee.PerformanceRating; } if (employee.StandardHours > 0) { employeeobj.StandardHours = employee.StandardHours; } if (employee.StockOptionLevel > 0) { employeeobj.StockOptionLevel = employee.StockOptionLevel; } if (employee.TotalWorkingYears > 0) { employeeobj.TotalWorkingYears = employee.TotalWorkingYears; } if (employee.TrainingTimesLastYear > 0) { employeeobj.TrainingTimesLastYear = employee.TrainingTimesLastYear; } if (employee.YearsAtCompany > 0) { employeeobj.YearsAtCompany = employee.YearsAtCompany; } if (employee.YearsInCurrentRole > 0) { employeeobj.YearsInCurrentRole = employee.YearsInCurrentRole; } if (employee.YearsSinceLastPromotion > 0) { employeeobj.YearsSinceLastPromotion = employee.YearsSinceLastPromotion; } if (employee.YearsWithCurrManager > 0) { employeeobj.YearsWithCurrManager = employee.YearsWithCurrManager; } employeeobj.EmployeeSurvey = 101; _context.Employee.Add(employeeobj); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (EmployeeExists(employee.EmployeeNumber)) { return(Conflict()); } else { throw; } } int surveycount = 0; var getemployeenumber = await _context.Employee .AsNoTracking() .Where(x => x.EmployeeNumber == employeeobj.EmployeeNumber) .Select(a => new { EmployeeNumber = a.EmployeeNumber }) .FirstOrDefaultAsync(); if (employee.EnvironmentSatisfaction > 0) { employeessurveyobj.EnvironmentSatisfaction = employee.EnvironmentSatisfaction; surveycount++; } if (employee.RelationshipSatisfaction > 0) { employeessurveyobj.RelationshipSatisfaction = employee.RelationshipSatisfaction; surveycount++; } if (employee.JobSatisfaction > 0) { employeessurveyobj.JobSatisfaction = employee.JobSatisfaction; surveycount++; } if (employee.WorkLifeBalance > 0) { employeessurveyobj.WorkLifeBalance = employee.WorkLifeBalance; surveycount++; } if (surveycount == 4) //if all values added then add new survey { employeessurveyobj.EmployeeId = getemployeenumber.EmployeeNumber; _context.EmployeeSurvey.Add(employeessurveyobj); } else if (surveycount > 0 && surveycount != 4) //if some values added, I assume you're fixing a survey mistake. { var employeessurveyobjoriginal = await _context.EmployeeSurvey .OrderByDescending(x => x.EmployeeId == getemployeenumber.EmployeeNumber) .FirstOrDefaultAsync(); employeessurveyobjoriginal.EnvironmentSatisfaction = employeessurveyobj.EnvironmentSatisfaction > 0 ? default(byte) : employeessurveyobj.EnvironmentSatisfaction; employeessurveyobjoriginal.EnvironmentSatisfaction = employeessurveyobj.RelationshipSatisfaction > 0 ? default(byte) : employeessurveyobj.RelationshipSatisfaction; employeessurveyobjoriginal.EnvironmentSatisfaction = employeessurveyobj.JobSatisfaction > 0 ? default(byte) : employeessurveyobj.JobSatisfaction; employeessurveyobjoriginal.EnvironmentSatisfaction = employeessurveyobj.WorkLifeBalance > 0 ? default(byte) : employeessurveyobj.WorkLifeBalance; _context.Entry(employeessurveyobjoriginal).State = EntityState.Modified; } var latestemployeesurvey = await _context.EmployeeSurvey .AsNoTracking() .OrderByDescending(x => x.EmployeeId == getemployeenumber.EmployeeNumber) .FirstOrDefaultAsync(); employeeobj.EmployeeSurvey = latestemployeesurvey.SurveyId; _context.Entry(employeeobj).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { throw; } var createdEmployeeObj = await _context.Employee .AsNoTracking() .Where(x => x.EmployeeNumber == employeeobj.EmployeeNumber) .Select(a => new { EmployeeNumber = a.EmployeeNumber }) .FirstOrDefaultAsync(); return(Ok(new { message = "Created Employee: " + createdEmployeeObj.EmployeeNumber + " with provided values.\n" + employee })); } else { return(BadRequest(new { message = "You are not an admin only admin's can create employees" })); } }