public override bool IsValid(object value)
        {
            var str = value as string;

            if (str == null)
            {
                return(true);
            }

            return(EmployeeControllerHelper.IsGenderFromSexParty(str));
        }
Exemple #2
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));
        }
Exemple #3
0
        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));
        }
Exemple #4
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));
        }