public async Task <IActionResult> PutGender(byte id, Gender gender) { if (id != gender.GenderId) { return(BadRequest()); } _context.Entry(gender).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GenderExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutDepartment(byte id, Department department) { if (id != department.DepartmentId) { return(BadRequest()); } _context.Entry(department).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DepartmentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutBusinessTravel(byte id, BusinessTravel businessTravel) { if (id != businessTravel.BusinessTravelId) { return(BadRequest()); } _context.Entry(businessTravel).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BusinessTravelExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutJobRole(byte id, JobRole jobRole) { if (id != jobRole.JobRoleId) { return(BadRequest()); } _context.Entry(jobRole).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!JobRoleExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
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 async Task <IActionResult> PutMaritalStatus(byte id, MaritalStatus maritalStatus) { if (id != maritalStatus.MaritalStatusId) { return(BadRequest()); } _context.Entry(maritalStatus).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MaritalStatusExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutEducationField(byte id, EducationField educationField) { if (id != educationField.EducationFieldId) { return(BadRequest()); } _context.Entry(educationField).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EducationFieldExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutEmployee(int id, EmployeeUpdate 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(); var employeeobj = await _context.Employee .Where(x => x.EmployeeNumber == id) .FirstOrDefaultAsync(); EmployeeSurvey employeessurveyobj = new EmployeeSurvey(); if (userName == null) { return(BadRequest(new { message = "Woops, that user doesn't exist" })); } bool isAdmin = RoleClaim.Value == "admin"; bool isSelf = userName.EmployeeNumber == id; if (isAdmin || isSelf) { if (employee.Age > 0) { employeeobj.Age = employee.Age; } if (employee.Attrition != null) { employeeobj.Attrition = employee.Attrition ?? default(bool); } 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 ?? default(bool); } 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; } int surveycount = 0; 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 = id; _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 == id) .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 == id) .FirstOrDefaultAsync(); employeeobj.EmployeeSurvey = latestemployeesurvey.SurveyId; _context.Entry(employeeobj).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(NotFound()); } else { throw; } } } else { return(Forbid()); } return(Ok(new { message = "Modified Employee: " + id + " with provided values.\n" + employee })); }
public async Task <IActionResult> PutUser(int id, UpdateUser user) { // Cast to ClaimsIdentity. var identity = HttpContext.User.Identity as ClaimsIdentity; // Gets list of claims. IEnumerable <Claim> claim = identity.Claims; // Gets ID from claims. var usernameClaim = claim .Where(x => x.Type == ClaimTypes.Name) .FirstOrDefault(); // Finds user. using ID converted from string to int var userName = await _context.User .Where(x => x.Id == Int32.Parse(usernameClaim.Value)) .AsNoTracking() .FirstOrDefaultAsync(); //Username Token == null, then Badrequest if (userName == null) { return(BadRequest(new { message = "Woops" })); } bool isadmin = (userName.Role != "admin"); if ((id < 0)) { return(BadRequest()); } // if Username.Id (The id of the token != id of the user being edited, unauthorized; //and user is not an admin return unauthorized. if (!(userName.Id == id) & isadmin) { return(Unauthorized(new { message = "Trying to update other users if you're not an admin is not allowed. Message Administrator" })); } //throw unauthorized, if the user is not an admin and is trying to set a role ! if (!(user.Role == null) & isadmin) { return(Unauthorized(new { message = "User can't adjust role, please try a different update command without including role." })); } var userobj = await _context.User .FindAsync(id); //only admin can adjust role if (isadmin & user.Role != null) { userobj.Role = user.Role; } //if password change is being requested, we need to update the password, hash & salt. if (user.Password != null) { byte[] passwordHash, passwordSalt; CreatePasswordHash(user.Password, out passwordHash, out passwordSalt); userobj.PasswordHash = passwordHash; userobj.PasswordSalt = passwordSalt; } if (user.FirstName != null) { userobj.FirstName = user.FirstName; } if (user.LastName != null) { userobj.LastName = user.LastName; } if (user.Username != null) { userobj.UserName = user.Username; } if (user.EmployeeNumber > 0) { userobj.EmployeeNumber = user.EmployeeNumber; } _context.Entry(userobj).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } return(Ok(new{ message = "Updated User: "******" " + userobj.UserName })); }