Example #1
0
        public EditCriteriaValues(MainJudgeDashboardForm mainJudgeDashboardForm, EditContestForm editContestForm, Contest contest)
        {
            this.mainJudgeDashboardForm = mainJudgeDashboardForm;
            this.editContestForm        = editContestForm;
            this.contest = contest;

            InitializeComponent();

            int count = 0;

            // We do this so the header in every row is sized to fit it's text
            criteriaValuesDataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;

            // Fill the DataGridView with all criteria
            foreach (Criteria criteria in editContestForm.criterias)
            {
                criteriaValuesDataGridView.Columns.Add("criteria" + count, criteria.Name);

                criteriaValuesDataGridView.Columns[count].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                criteriaValuesDataGridView.Rows.Add();

                criteriaValuesDataGridView.Rows[count].HeaderCell.Value = criteria.Name;

                // Make it so identity cells cannot be edited
                criteriaValuesDataGridView.Rows[count].Cells[count].ReadOnly        = true;
                criteriaValuesDataGridView.Rows[count].Cells[count].Value           = 1;
                criteriaValuesDataGridView.Rows[count].Cells[count].Style.BackColor = Color.Gray;

                count++;
            }
        }
        private void criteriaValuesButton_Click(object sender, EventArgs e)
        {
            string contestName;

            // Check if there's any project
            if (projects.Count > 0)
            {
                // Check if there is a description
                if (description != "")
                {
                    contestName = contestNameTextBox.Text;
                    // Check if the contest has a name
                    if (contestName != "")
                    {
                        // Check if contest's start date is after today
                        if (contestStartDateTimePicker.Value.Date >= DateTime.Today)
                        {
                            // Check if contest's limit date is after today and after the start date
                            if (contestLimitDateTimePicker.Value.Date > DateTime.Today && contestLimitDateTimePicker.Value.Date > contestStartDateTimePicker.Value.Date)
                            {
                                // Check if there's any judge member
                                if (judgeMembers.Count > 0)
                                {
                                    // Check if there's any criteria
                                    if (criterias.Count > 0)
                                    {
                                        ContestPreview contestPreview = new ContestPreview(0, contestName, description, contestStartDateTimePicker.Value, contestLimitDateTimePicker.Value);
                                        Contest        contest        = new Contest(contestPreview, projects, judgeMembers, criterias, new double[, ] {
                                        });

                                        // Open criteria values form
                                        EditCriteriaValues editCriteriaValues = new EditCriteriaValues(mainJudgeDashboardForm, this, contest);
                                        editCriteriaValues.MdiParent = this.MdiParent;
                                        editCriteriaValues.Dock      = DockStyle.Fill;
                                        editCriteriaValues.Show();
                                    }
                                    else
                                    {
                                        MessageBox.Show(null, "There must be criterias assigned to the contest.", "Error");
                                    }
                                }
                                else
                                {
                                    MessageBox.Show(null, "There must be judges assigned to the contest.", "Error");
                                }
                            }
                            else
                            {
                                MessageBox.Show(null, "Limit date must be after today's date and after the start date.", "Error");
                            }
                        }
                        else
                        {
                            MessageBox.Show(null, "Start date must be after today's date and before the limit date.", "Error");
                        }
                    }
                    else
                    {
                        MessageBox.Show(null, "Contest name cannot be empty.", "Error");
                    }
                }
                else
                {
                    MessageBox.Show(null, "There must be a description.", "Error");
                }
            }
            else
            {
                MessageBox.Show(null, "There must be projects to add.", "Error");
            }
        }
Example #3
0
        /// <summary>
        /// Inserts a Contest into the database.
        /// </summary>
        /// <param name="contestToInsert">The Constest to insert into the database.</param>
        /// <returns>True if inserted successfully, false otherwise.</returns>
        public bool InsertContestToDB(Contest contestToInsert)
        {
            try
            {
                string query = "INSERT INTO contest_table ([contest_name], [descript], [start_date], [limit_date], [criteria_values]) " +
                               "VALUES (@contest_name, @descript, @start_date, @limit_date, @criteria_values);" +
                               "SELECT CAST(scope_identity() AS int)";

                SqlCommand cmd = DBSqlHelper._instance.conn.CreateCommand();
                cmd.CommandText = query;

                SqlParameter sqlContestName = new SqlParameter("@contest_name", SqlDbType.NVarChar);
                sqlContestName.Value = contestToInsert.contest.name;
                cmd.Parameters.Add(sqlContestName);

                SqlParameter sqlDescript = new SqlParameter("@descript", SqlDbType.NVarChar);
                sqlDescript.Value = contestToInsert.contest.description;
                cmd.Parameters.Add(sqlDescript);

                SqlParameter sqlStartdate = new SqlParameter("@start_date", SqlDbType.DateTime);
                sqlStartdate.Value = contestToInsert.contest.startDate;
                cmd.Parameters.Add(sqlStartdate);

                SqlParameter sqlLimitdate = new SqlParameter("@limit_date", SqlDbType.DateTime);
                sqlLimitdate.Value = contestToInsert.contest.limitDate;
                cmd.Parameters.Add(sqlLimitdate);

                SqlParameter sqlCriteriaValues = new SqlParameter("@criteria_values", SqlDbType.NVarChar);
                sqlCriteriaValues.Value = contestToInsert.GetCriteriaValuesJSON();
                cmd.Parameters.Add(sqlCriteriaValues);

                // Execute query
                int insertedId = (int)cmd.ExecuteScalar();

                SqlParameter sqlContestId;

                SqlParameter sqlProjectCategory, sqlProjectName, sqlPromoterName;

                foreach (Project project in editContestForm.projects)
                {
                    // Insert projects into database
                    query = "INSERT INTO project_table ([id_contest], [id_category], [descript], [project_name], [promoter_name]) " +
                            "VALUES (@id_contest, @id_category, @descript, @project_name, @promoter_name)";

                    cmd             = DBSqlHelper._instance.conn.CreateCommand();
                    cmd.CommandText = query;

                    sqlContestId       = new SqlParameter("@id_contest", SqlDbType.Int);
                    sqlContestId.Value = insertedId;
                    cmd.Parameters.Add(sqlContestId);

                    sqlProjectCategory       = new SqlParameter("@id_category", SqlDbType.Int);
                    sqlProjectCategory.Value = project.category;
                    cmd.Parameters.Add(sqlProjectCategory);

                    sqlDescript       = new SqlParameter("@descript", SqlDbType.NVarChar);
                    sqlDescript.Value = project.description;
                    cmd.Parameters.Add(sqlDescript);

                    sqlProjectName       = new SqlParameter("@project_name", SqlDbType.NVarChar);
                    sqlProjectName.Value = project.name;
                    cmd.Parameters.Add(sqlProjectName);

                    sqlPromoterName       = new SqlParameter("@promoter_name", SqlDbType.NVarChar);
                    sqlPromoterName.Value = project.promoterName;
                    cmd.Parameters.Add(sqlPromoterName);

                    // Execute query
                    cmd.ExecuteNonQuery();
                }

                // Insert main judge into database
                query = "INSERT INTO contest_juri_table (id_contest, id_user, has_voted, president) VALUES (@id_contest, @id_user, 0, 1)";

                cmd             = DBSqlHelper._instance.conn.CreateCommand();
                cmd.CommandText = query;

                sqlContestId       = new SqlParameter("@id_contest", SqlDbType.Int);
                sqlContestId.Value = insertedId;
                cmd.Parameters.Add(sqlContestId);

                SqlParameter sqlUserId = new SqlParameter("@id_user", SqlDbType.Int);
                sqlUserId.Value = Data._instance.loggedInUser.id;
                cmd.Parameters.Add(sqlUserId);

                cmd.ExecuteNonQuery();

                cmd             = DBSqlHelper._instance.conn.CreateCommand();
                cmd.CommandText = query;

                // Now let's add the remaining judges
                foreach (JudgeMember judgeMember in editContestForm.judgeMembers)
                {
                    // Insert judge into database
                    query = "INSERT INTO contest_juri_table (id_contest, id_user, has_voted, president) VALUES (@id_contest, @id_user, 0, 0)";

                    cmd             = DBSqlHelper._instance.conn.CreateCommand();
                    cmd.CommandText = query;

                    sqlContestId       = new SqlParameter("@id_contest", SqlDbType.Int);
                    sqlContestId.Value = insertedId;
                    cmd.Parameters.Add(sqlContestId);

                    sqlUserId       = new SqlParameter("@id_user", SqlDbType.Int);
                    sqlUserId.Value = judgeMember.Id;
                    cmd.Parameters.Add(sqlUserId);

                    // Execute query
                    cmd.ExecuteNonQuery();
                }

                SqlParameter sqlCriteriaName, sqlCriteriaDescription, sqlCriteriaId;

                int insertedCriteriaId;

                foreach (Criteria criteria in editContestForm.criterias)
                {
                    // Insert criterias into database
                    query = "INSERT INTO criteria_data_table ([criteria_name], [descript]) " +
                            "VALUES (@criteria_name, @descript); " +
                            "SELECT CAST(scope_identity() AS int)";

                    cmd             = DBSqlHelper._instance.conn.CreateCommand();
                    cmd.CommandText = query;

                    sqlCriteriaName       = new SqlParameter("@criteria_name", SqlDbType.NVarChar);
                    sqlCriteriaName.Value = criteria.Name;
                    cmd.Parameters.Add(sqlCriteriaName);

                    sqlCriteriaDescription       = new SqlParameter("@descript", SqlDbType.NVarChar);
                    sqlCriteriaDescription.Value = criteria.Description;
                    cmd.Parameters.Add(sqlCriteriaDescription);

                    // Execute query
                    insertedCriteriaId = (int)cmd.ExecuteScalar();

                    // Insert criteria into relationship table
                    query = "INSERT INTO contest_criteria_table ([id_criteria], [id_contest]) " +
                            "VALUES (@id_criteria, @id_contest)";

                    cmd             = DBSqlHelper._instance.conn.CreateCommand();
                    cmd.CommandText = query;

                    sqlCriteriaId       = new SqlParameter("@id_criteria", SqlDbType.Int);
                    sqlCriteriaId.Value = insertedCriteriaId;
                    cmd.Parameters.Add(sqlCriteriaId);

                    sqlContestId       = new SqlParameter("@id_contest", SqlDbType.Int);
                    sqlContestId.Value = insertedId;
                    cmd.Parameters.Add(sqlContestId);

                    // Execute query
                    cmd.ExecuteNonQuery();
                }

                return(true);
            }
            catch (Exception e)
            {
                MessageBox.Show(null, "Error: " + e, "Error");

                return(false);
            }
        }