public IHttpActionResult RemoveProfession(int id, ICollection <string> professionNames) { if (professionNames.Any(p => !EmployeeControllerHelper.NameValidator(p))) { throw new IncorrectDataException(nameof(professionNames)); } var existModel = db.FindModelById(id); if (existModel == null) { return(NotFound()); } var removeProfessionList = professionNames .Select(p => new ProfessionModel { ProfessionName = p, EmployeeModelId = id }) .ToList(); var intersectProfessions = existModel.Professions.Intersect(removeProfessionList, new ProfessionNameEqualityComparer <ProfessionModel>()); db.Professions.RemoveRange(intersectProfessions); db.SaveChanges(); return(Ok(existModel)); }
public override bool IsValid(object value) { var str = value as string; if (str == null) { return(true); } return(EmployeeControllerHelper.IsGenderFromSexParty(str)); }
public override bool IsValid(object value) { var str = value as string; if (str == null) { return(false); } return(EmployeeControllerHelper.NameValidator(str)); }
public IHttpActionResult RemoveProfession(int id, string professionName) { if (!EmployeeControllerHelper.NameValidator(professionName)) { throw new IncorrectDataException(nameof(professionName)); } return(RemoveProfession(id, new List <string> { professionName })); }
public IHttpActionResult Update(int id, string newFirstName = null, string newLastName = null, string newAge = null, string newgender = null) { var newAgeInt = 0; if (newFirstName != null && !EmployeeControllerHelper.IsStringOnlyLetters(newFirstName)) { throw new IncorrectDataException(nameof(newFirstName)); } if (newLastName != null && !EmployeeControllerHelper.IsStringOnlyLetters(newLastName)) { throw new IncorrectDataException(nameof(newLastName)); } if (newgender != null && !EmployeeControllerHelper.IsGenderFromSexParty(newgender)) { throw new IncorrectDataException(nameof(newgender)); } if (newAge != null && !EmployeeControllerHelper.IsAgeInRange(newAge, out newAgeInt)) { throw new IncorrectDataException(nameof(newAge)); } var employeeModel = db.Employees.Find(id); if (employeeModel == null) { return(NotFound()); } if (newFirstName != null) { employeeModel.FirstName = newFirstName; } if (newLastName != null) { employeeModel.LastName = newLastName; } if (newAge != null) { employeeModel.Age = newAgeInt; } if (newgender != null) { employeeModel.Gender = newgender; } db.MarkAsModified(employeeModel); db.SaveChanges(); return(Ok(employeeModel)); }
public IHttpActionResult AddProfession(int id, string professionName) { if (!EmployeeControllerHelper.NameValidator(professionName)) { throw new IncorrectDataException(nameof(professionName)); } var professions = new NewProfessionsDto { Id = id, ProfessionNames = new string[] { professionName } }; return(AddProfession(professions)); }
public IHttpActionResult Add(string firstName, string lastName, string age, string gender = null) { var ageInt = 0; if (!EmployeeControllerHelper.NameValidator(firstName)) { throw new IncorrectDataException(nameof(firstName)); } if (!EmployeeControllerHelper.NameValidator(lastName)) { throw new IncorrectDataException(nameof(lastName)); } if (!EmployeeControllerHelper.AgeValidator(age, ref ageInt)) { throw new IncorrectDataException(nameof(age)); } if (gender != null && !EmployeeControllerHelper.IsGenderFromSexParty(gender)) { throw new IncorrectDataException(nameof(gender)); } var findResult = FindEmployee(firstName, lastName, age) as OkNegotiatedContentResult <List <EmployeeModel> >; if (findResult?.Content?.Count > 0) { throw new InvalidOperationException("Employee with such data already exists in the database"); } var employeeModel = db.Employees.Add(new EmployeeModel { Age = ageInt, FirstName = firstName, LastName = lastName, Gender = gender }); db.SaveChanges(); return(Ok(employeeModel)); }
public IHttpActionResult FindEmployee(string firstName = null, string lastName = null, string age = null, string gender = null, string profession = null) { var ageInt = 0; if (firstName != null && !EmployeeControllerHelper.IsStringOnlyLetters(firstName)) { throw new IncorrectDataException(nameof(firstName)); } if (lastName != null && !EmployeeControllerHelper.IsStringOnlyLetters(lastName)) { throw new IncorrectDataException(nameof(lastName)); } if (age != null && !EmployeeControllerHelper.IsAgeInRange(age, out ageInt)) { throw new IncorrectDataException(nameof(age)); } if (gender != null && !EmployeeControllerHelper.IsGenderFromSexParty(gender)) { throw new IncorrectDataException(nameof(lastName)); } var searchModel = new EmployeeModel { Age = ageInt, FirstName = firstName, LastName = lastName, Gender = gender, Professions = new List <ProfessionModel> { new ProfessionModel { ProfessionName = profession } } }; return(FindEmployee(searchModel)); }
//for tests only public EmployeeController(IEmployeeDbContext context, EmployeeControllerHelper helper) { db = context ?? new EmployeeDbContext(); _helper = helper; }