-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExamQuestionCRUDForm.cs
94 lines (93 loc) · 3.91 KB
/
ExamQuestionCRUDForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using ExamProj.Class;
using ExamProj.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace ExamProj
{
public partial class ExamQuestionCRUDForm : DevExpress.XtraEditors.XtraForm
{
public Question question = new Question();
private IQuestionServices _questionServices { get; set; }
public ExamQuestionCRUDForm(Question question, IQuestionServices _questionServices)
{
InitializeComponent();
this.question = question;
this._questionServices = _questionServices;
questionTxt.Text = question.QuestionName;
difficultyCmb.Text = question.Difficulty;
if (question.Answers.Count == 0)
{
for (int i = 0; i < 5; i++)
{
Answer answer = new Answer();
answer.AnswerName = "";
question.Answers.Add(answer);
}
}
gridControl.DataSource = question.Answers;
}
private void saveBtn_Click(object sender, EventArgs e)
{
question.QuestionName = questionTxt.Text;
question.Difficulty = difficultyCmb.Text;
question.Answers = ((List<Answer>)gridControl.DataSource);
if (question.QuestionName == "")
{
MessageBox.Show("Question can't be empty!", "Empty Question", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (question.Difficulty == "")
{
MessageBox.Show("Difficulty can't be empty!", "No Difficulty Chosen", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (question.Answers.Any(x => x.AnswerName == ""))
{
MessageBox.Show($"Answer {question.Answers.IndexOf(question.Answers.FirstOrDefault(x => x.AnswerName == "")) + 1} can't be empty!", "Empty Question", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (question.Answers.Count != question.Answers.Select(x => x.AnswerName).Distinct().Count())
{
MessageBox.Show("Multiple answers can't be the same!", "Overlapping Answers", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (question.Answers.Where(x => x.IsCorrect).ToList().Count > 1)
{
MessageBox.Show("There can only be one correct answer!", "Multiple Correct Answers", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (question.Answers.Where(x => x.IsCorrect).ToList().Count == 0)
{
MessageBox.Show("There must be a correct answer!", "No Correct Answer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (question.ID != 0)
{
try
{
_questionServices.UpdateQuestion(question);
MessageBox.Show("Question was updated.", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("Unexpected error.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
try
{
_questionServices.InsertQuestion(question);
MessageBox.Show("Question was saved.", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("Unexpected error.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
gridControl.RefreshDataSource();
}
}
}