public Entry Delete(Models.Tuple <long, long> id)
        {
            log.InfoFormat("Entering Delete with value {0}", id);
            IDbConnection con = DBUtils.getConnection();

            using (var comm = con.CreateCommand())
            {
                comm.CommandText = "delete from Entries where kid=@kid and cid=@cid";
                IDbDataParameter paramKId = comm.CreateParameter();
                paramKId.ParameterName = "@kid";
                paramKId.Value         = id.Left;
                comm.Parameters.Add(paramKId);

                IDbDataParameter paramCId = comm.CreateParameter();
                paramCId.ParameterName = "@cid";
                paramCId.Value         = id.Right;
                comm.Parameters.Add(paramCId);

                Entry entry  = FindOne(id);
                var   result = comm.ExecuteNonQuery();
                if (result == 0)
                {
                    log.InfoFormat("Exiting Delete with value {0}", null);
                    return(null);
                }
                else
                {
                    log.InfoFormat("Exiting Delete with value {0}", entry);
                    return(entry);
                }
            }
        }
        public IEnumerable <Entry> FindAll()
        {
            log.Info("Entering FindAll");
            IDbConnection con     = DBUtils.getConnection();
            IList <Entry> entries = new List <Entry>();

            using (var comm = con.CreateCommand())
            {
                comm.CommandText = "select kid , cid , date from Entries";

                using (var dataR = comm.ExecuteReader())
                {
                    while (dataR.Read())
                    {
                        long     kid  = dataR.GetInt64(0);
                        long     cid  = dataR.GetInt64(1);
                        DateTime date = dataR.GetDateTime(2);
                        Models.Tuple <long, long> id = new Models.Tuple <long, long>(kid, cid);
                        Child     child     = childRepo.FindOne(kid);
                        Challenge challenge = challengeRepo.FindOne(cid);
                        Entry     entry     = new Entry(date, child, challenge);
                        entry.Id = id;
                        entries.Add(entry);
                    }
                }
            }
            log.InfoFormat("Exiting FindAll with Children {0}", entries);
            return(entries);
        }
 public bool Exists(Models.Tuple <long, long> id)
 {
     log.InfoFormat("Entering Exists with value {0}", id);
     if (FindOne(id) == null)
     {
         log.InfoFormat("Exiting Exists with value {0}", false);
         return(false);
     }
     log.InfoFormat("Exiting Exists with value {0}", true);
     return(true);
 }
        public Entry FindOne(Models.Tuple <long, long> id)
        {
            log.InfoFormat("Entering FindOne with value {0}", id);
            IDbConnection con = DBUtils.getConnection();

            using (var comm = con.CreateCommand())
            {
                comm.CommandText = "select kid , cid , date from Entries where kid = @kid and cid = @cid";

                IDbDataParameter paramKId = comm.CreateParameter();
                paramKId.ParameterName = "@kid";
                paramKId.Value         = id.Left;
                comm.Parameters.Add(paramKId);

                IDbDataParameter paramCId = comm.CreateParameter();
                paramCId.ParameterName = "@cid";
                paramCId.Value         = id.Right;
                comm.Parameters.Add(paramCId);


                using (var dataR = comm.ExecuteReader())
                {
                    if (dataR.Read())
                    {
                        long     kidE = dataR.GetInt64(0);
                        long     cidE = dataR.GetInt64(1);
                        DateTime date = dataR.GetDateTime(2);
                        Models.Tuple <long, long> idE = new Models.Tuple <long, long>(kidE, cidE);
                        Child     child     = childRepo.FindOne(kidE);
                        Challenge challenge = challengeRepo.FindOne(cidE);
                        Entry     entry     = new Entry(date, child, challenge);
                        entry.Id = idE;
                        log.InfoFormat("Exiting FindOne with value {0}", entry);
                        return(entry);
                    }
                }
            }
            log.InfoFormat("Exiting FindOne with value {0}", null);
            return(null);
        }
        public Child RegisterChild(String name, int age, String challenge1, String challenge2)
        {
            if (challenge1.CompareTo(challenge2) == 0)
            {
                throw new ValidationException("Participantul nu poate fi inscris de 2 ori la aceeasi proba ! ");
            }

            Child child = new Child(name, age);

            Child foundChild;

            if (childRepo.FindByProperties(name, age) != null)
            {
                foundChild = childRepo.FindByProperties(name, age);
                if (entriesRepo.FindChallengeNumber(foundChild.Id) == 1 && challenge2 != "")
                {
                    throw new ValidationException("Participantul mai poate fi inscris la o singura proba ! ");
                }
                else if (entriesRepo.FindChallengeNumber(foundChild.Id) == 2)
                {
                    throw new ValidationException("Participantul nu mai poate fi inscris la nici o proba ! ");
                }

                Models.Tuple <int, int> ageInterval = CreateAgeInterval(age);
                Challenge challengeFound1           = challengeRepo.FindByProperties(ageInterval.Left, ageInterval.Right, challenge1);

                if (challengeFound1 != null &&
                    entriesRepo.Exists(new Models.Tuple <long, long>(foundChild.Id, challengeFound1.Id)))
                {
                    throw new ValidationException("Participantul nu mai poate fi inscris la aceeasi proba ! ");
                }

                Entry entry = new Entry(DateTime.Now, foundChild, challengeFound1);
                if (challengeFound1 != null)
                {
                    entry.Id = new Models.Tuple <long, long>(foundChild.Id, challengeFound1.Id);
                    if (entriesRepo.Save(entry) != null)
                    {
                        return(foundChild);
                    }
                }
                notifyRegisterChild(foundChild);
                return(null);
            }

            Child added = childRepo.Save(child);

            foundChild = childRepo.FindByProperties(name, age);

            if (challenge1 != "")
            {
                Models.Tuple <int, int> ageInterval = CreateAgeInterval(age);
                Challenge foundChallenge1           = challengeRepo.FindByProperties(ageInterval.Left, ageInterval.Right, challenge1);
                Entry     entry = new Entry(DateTime.Now, foundChild, foundChallenge1);
                if (foundChallenge1 != null)
                {
                    entry.Id = new Models.Tuple <long, long>(foundChild.Id, foundChallenge1.Id);
                    if (entriesRepo.Save(entry) != null)
                    {
                        return(foundChild);
                    }
                }
            }

            if (challenge2 != "")
            {
                Models.Tuple <int, int> ageInterval = CreateAgeInterval(age);
                Challenge foundChallenge2           = challengeRepo.FindByProperties(ageInterval.Left, ageInterval.Right, challenge2);
                Entry     entry = new Entry(DateTime.Now, foundChild, foundChallenge2);
                if (foundChallenge2 != null)
                {
                    entry.Id = new Models.Tuple <long, long>(foundChild.Id, foundChallenge2.Id);
                    if (entriesRepo.Save(entry) != null)
                    {
                        return(foundChild);
                    }
                }
            }

            if (added == null)
            {
                notifyRegisterChild(foundChild);
            }

            return(added);
        }