-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExamUserCRUDForm.cs
76 lines (75 loc) · 2.83 KB
/
ExamUserCRUDForm.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
using ExamProj.Class;
using ExamProj.Context;
using ExamProj.Repositories;
using ExamProj.Services;
using ExamProj.Services.Interfaces;
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace ExamProj
{
public partial class ExamUserCRUDForm : DevExpress.XtraEditors.XtraForm
{
public User user = new User();
private IUserServices _userServices { get; set; }
public ExamUserCRUDForm(User user)
{
InitializeComponent();
this.user = user;
_userServices = new UserServices(new Repository<User>(new ExamDbContext()));
usernameTxt.Text = user.Username;
passwordTxt.Text = user.Password;
teacherChk.Checked = user.IsTeacher;
}
private void saveBtn_Click(object sender, EventArgs e)
{
user.Username = usernameTxt.Text;
user.Password = passwordTxt.Text;
user.IsTeacher = teacherChk.Checked;
if (user.Username == "")
{
MessageBox.Show("Username can't be empty!", "No Username", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (user.Password == "")
{
MessageBox.Show("Password can't be empty!", "No Password", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (!Regex.Match(user.Username, "^[A-Za-z0-9_-]*$").Success)
{
MessageBox.Show("Please enter a valid username.", "Invalid Username", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (!Regex.Match(user.Password, "^[A-Za-z0-9_-]*$").Success)
{
MessageBox.Show("Please enter a valid password.", "Invalid Password", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (user.ID != 0)
{
try
{
_userServices.UpdateUser(user);
MessageBox.Show("User was updated.", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("Unexpected error.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
try
{
_userServices.InsertUser(user);
MessageBox.Show("User was saved.", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("Unexpected error.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
}