Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using MySQLEntity.Models;
- using System.Data.Entity.Validation;
- namespace MySQLEntity
- {
- public partial class UserPanel : Form
- {
- public static DBApplicationContext DbConnection = new DBApplicationContext();
- private bool IsAdmin = false;
- public UserPanel()
- {
- InitializeComponent();
- }
- private void Create_Click(object sender, EventArgs e)
- {
- bool error = false;
- string username = UsernameTextBox.Text;
- if (username.Length > 20 || username.Length < 3)
- {
- error = true;
- UsernameErrorProvider.SetError(UsernameTextBox, "Username must be min 3 and max 20.");
- }
- else
- UsernameErrorProvider.Clear();
- string password = PasswordTextBox.Text;
- if (password.Length > 20 || password.Length < 3)
- {
- error = true;
- PasswordErrorProvider.SetError(PasswordTextBox, "Password must be min 3 and max 20.");
- }
- else
- PasswordErrorProvider.Clear();
- if (error)
- return;
- var res = DbConnection.User.Where(i => i.Username == username);
- int id = DbConnection.User.DefaultIfEmpty().Max(p => p == null ? 0 : p.UserId);
- if (res.Count() > 0)
- {
- MessageBox.Show("User already exists");
- }
- else
- {
- try
- {
- id++;
- DbConnection.User.Add(new Users { UserId = id, Username = username, Password = password });
- DbConnection.SaveChanges();
- MessageBox.Show("User created");
- }
- catch (DbEntityValidationException exception)
- {
- foreach (var eve in exception.EntityValidationErrors)
- {
- Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
- eve.Entry.Entity.GetType().Name, eve.Entry.State);
- foreach (var ve in eve.ValidationErrors)
- {
- Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
- ve.PropertyName, ve.ErrorMessage);
- }
- }
- Console.ReadLine();
- }
- }
- }
- private void Update_Click(object sender, EventArgs e)
- {
- string s = UserIdBox.Text;
- int.TryParse(s, out int id);
- bool error = false;
- string username = UsernameTextBox.Text;
- if (username.Length > 20 || username.Length < 3)
- {
- error = true;
- UsernameErrorProvider.SetError(UsernameTextBox, "Username must be min 3 and max 20.");
- }
- else
- UsernameErrorProvider.Clear();
- string password = PasswordTextBox.Text;
- if (password.Length > 20 || password.Length < 3)
- {
- error = true;
- PasswordErrorProvider.SetError(PasswordTextBox, "Password must be min 3 and max 20.");
- }
- else
- PasswordErrorProvider.Clear();
- if (error)
- return;
- bool Admin = SetAdminCheckBox.Checked;
- if (IsAdmin)
- {
- var res = DbConnection.User.Where(i => i.UserId != id && i.Username == username);
- if (res.Count() > 0)
- {
- MessageBox.Show("A user with this username already exists");
- return;
- }
- res = DbConnection.User.Where(i => i.UserId == id);
- if (res.Count() > 0 && id >= 1)
- {
- var member = DbConnection.User.First(i => i.UserId == id);
- member.Username = username;
- member.Password = password;
- member.IsAdmin = Admin;
- DbConnection.SaveChanges();
- MessageBox.Show("User updated");
- LoadToGrid();
- }
- else
- MessageBox.Show("user doest not exist");
- }
- else
- {
- var res = DbConnection.User.Where(i => i.Username == username && i.Password == password);
- if (res.Count() > 0)
- {
- var admin = res.Where(i => i.Username == username && i.IsAdmin == true);
- if (admin.Count() > 0)
- {
- DeleteButton.Show();
- UpdateButton.Text = "Update";
- SetAdminCheckBox.Show();
- SetAdminCheckBox.Checked = true;
- IsAdmin = true;
- LoadToGrid();
- // resize grids
- dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
- dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
- dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
- dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
- MessageBox.Show("Logged in as admin");
- }
- else
- MessageBox.Show("Logged in, but not as admin");
- LogoutButton.Show();
- }
- else
- {
- MessageBox.Show("User was not found.");
- }
- }
- }
- private void Delete_Click(object sender, EventArgs e)
- {
- string s = UserIdBox.Text;
- int.TryParse(s, out int id);
- string username = UsernameTextBox.Text;
- var res = DbConnection.User.Where(i => i.UserId == id);
- if (res.Count() > 0)
- {
- DbConnection.User.Remove(DbConnection.User.First(i => i.UserId == id));
- DbConnection.SaveChanges();
- MessageBox.Show("User Deleted");
- LoadToGrid();
- }
- else
- MessageBox.Show("User does not exist");
- }
- private void LoginForm_Load(object sender, EventArgs e)
- {
- if (!DbConnection.Database.Exists())
- {
- DbConnection.Database.CreateIfNotExists();
- DbConnection.SaveChanges();
- }
- SetAdminCheckBox.Hide();
- DeleteButton.Hide();
- LogoutButton.Hide();
- }
- private void LoadToGrid()
- {
- var load = from g in DbConnection.User select g;
- if (load != null)
- {
- dataGridView1.DataSource = load.ToList();
- }
- }
- private void UsernameTextBox_TextChanged(object sender, EventArgs e)
- {
- }
- private void Logout_Click(object sender, EventArgs e)
- {
- if (IsAdmin)
- IsAdmin = false;
- UpdateButton.Text = "Login";
- LogoutButton.Hide();
- DeleteButton.Hide();
- SetAdminCheckBox.Hide();
- dataGridView1.DataSource = null;
- }
- private void SetAdmin_CheckedChanged(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement