예제 #1
0
        public async Task <ActionResult <Voter> > CreateVoter([FromBody] Voter model)
        {
            if (ModelState.IsValid)
            {
                //First we get today's date.
                var today = DateTime.Today;

                //Now calculate age.
                var age = today.Year - model.DateOfBirth.Year;

                if (age > 18)
                {
                    try
                    {
                        var voterId = await _voterRepository.CreateVoter(model);

                        if (voterId > 0)
                        {
                            return(Ok("Voter Added Successfully"));
                        }

                        return(NotFound());
                    }
                    catch (Exception)
                    {
                        return(BadRequest());
                    }
                }
                else
                {
                    return(NotFound("Voter's Age must be > 18."));
                }
            }
            return(NotFound());
        }