Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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));
        }