Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using MySQLEntity.Models;
  11. using System.Data.Entity.Validation;
  12.  
  13. namespace MySQLEntity
  14. {
  15.     public partial class UserPanel : Form
  16.     {
  17.         public static DBApplicationContext DbConnection = new DBApplicationContext();
  18.         private bool IsAdmin = false;
  19.         public UserPanel()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void Create_Click(object sender, EventArgs e)
  25.         {
  26.             bool error = false;
  27.             string username = UsernameTextBox.Text;
  28.             if (username.Length > 20 || username.Length < 3)
  29.             {
  30.                 error = true;
  31.                 UsernameErrorProvider.SetError(UsernameTextBox, "Username must be min 3 and max 20.");
  32.             }
  33.             else
  34.                 UsernameErrorProvider.Clear();
  35.  
  36.             string password = PasswordTextBox.Text;
  37.             if (password.Length > 20 || password.Length < 3)
  38.             {
  39.                 error = true;
  40.                 PasswordErrorProvider.SetError(PasswordTextBox, "Password must be min 3 and max 20.");
  41.             }
  42.             else
  43.                 PasswordErrorProvider.Clear();
  44.  
  45.             if (error)
  46.                 return;
  47.  
  48.             var res = DbConnection.User.Where(i => i.Username == username);
  49.  
  50.             int id = DbConnection.User.DefaultIfEmpty().Max(p => p == null ? 0 : p.UserId);
  51.  
  52.             if (res.Count() > 0)
  53.             {
  54.                 MessageBox.Show("User already exists");
  55.             }
  56.             else
  57.             {
  58.                 try
  59.                 {
  60.                     id++;
  61.                     DbConnection.User.Add(new Users { UserId = id, Username = username, Password = password });
  62.                     DbConnection.SaveChanges();
  63.                     MessageBox.Show("User created");
  64.                 }
  65.                 catch (DbEntityValidationException exception)
  66.                 {
  67.                     foreach (var eve in exception.EntityValidationErrors)
  68.                     {
  69.                         Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
  70.                             eve.Entry.Entity.GetType().Name, eve.Entry.State);
  71.                         foreach (var ve in eve.ValidationErrors)
  72.                         {
  73.                             Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
  74.                                 ve.PropertyName, ve.ErrorMessage);
  75.                         }
  76.                     }
  77.                     Console.ReadLine();
  78.                 }
  79.             }
  80.         }
  81.  
  82.         private void Update_Click(object sender, EventArgs e)
  83.         {
  84.             string s = UserIdBox.Text;
  85.             int.TryParse(s, out int id);
  86.             bool error = false;
  87.             string username = UsernameTextBox.Text;
  88.             if (username.Length > 20 || username.Length < 3)
  89.             {
  90.                 error = true;
  91.                 UsernameErrorProvider.SetError(UsernameTextBox, "Username must be min 3 and max 20.");
  92.             }
  93.             else
  94.                 UsernameErrorProvider.Clear();
  95.  
  96.             string password = PasswordTextBox.Text;
  97.             if (password.Length > 20 || password.Length < 3)
  98.             {
  99.                 error = true;
  100.                 PasswordErrorProvider.SetError(PasswordTextBox, "Password must be min 3 and max 20.");
  101.             }
  102.             else
  103.                 PasswordErrorProvider.Clear();
  104.  
  105.             if (error)
  106.                 return;
  107.  
  108.             bool Admin = SetAdminCheckBox.Checked;
  109.  
  110.             if (IsAdmin)
  111.             {
  112.                 var res = DbConnection.User.Where(i => i.UserId != id && i.Username == username);
  113.                 if (res.Count() > 0)
  114.                 {
  115.                     MessageBox.Show("A user with this username already exists");
  116.                     return;
  117.                 }
  118.  
  119.                 res = DbConnection.User.Where(i => i.UserId == id);
  120.  
  121.                 if (res.Count() > 0 && id >= 1)
  122.                 {
  123.                     var member = DbConnection.User.First(i => i.UserId == id);
  124.                     member.Username = username;
  125.                     member.Password = password;
  126.                     member.IsAdmin = Admin;
  127.                     DbConnection.SaveChanges();
  128.                     MessageBox.Show("User updated");
  129.                     LoadToGrid();
  130.                 }
  131.                 else
  132.                     MessageBox.Show("user doest not exist");
  133.             }
  134.             else
  135.             {
  136.                 var res = DbConnection.User.Where(i => i.Username == username && i.Password == password);
  137.  
  138.                 if (res.Count() > 0)
  139.                 {
  140.                     var admin = res.Where(i => i.Username == username && i.IsAdmin == true);
  141.                     if (admin.Count() > 0)
  142.                     {
  143.                         DeleteButton.Show();
  144.                         UpdateButton.Text = "Update";
  145.                         SetAdminCheckBox.Show();
  146.                         SetAdminCheckBox.Checked = true;
  147.                         IsAdmin = true;
  148.                         LoadToGrid();
  149.                         // resize grids
  150.                         dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
  151.                         dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  152.                         dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  153.                         dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  154.                         MessageBox.Show("Logged in as admin");
  155.                     }
  156.                     else
  157.                         MessageBox.Show("Logged in, but not as admin");
  158.  
  159.                     LogoutButton.Show();
  160.                 }
  161.                 else
  162.                 {
  163.                     MessageBox.Show("User was not found.");
  164.                 }
  165.             }
  166.         }
  167.  
  168.         private void Delete_Click(object sender, EventArgs e)
  169.         {
  170.             string s = UserIdBox.Text;
  171.             int.TryParse(s, out int id);
  172.             string username = UsernameTextBox.Text;
  173.  
  174.             var res = DbConnection.User.Where(i => i.UserId == id);
  175.  
  176.             if (res.Count() > 0)
  177.             {
  178.                 DbConnection.User.Remove(DbConnection.User.First(i => i.UserId == id));
  179.                 DbConnection.SaveChanges();
  180.                 MessageBox.Show("User Deleted");
  181.                 LoadToGrid();
  182.             }
  183.             else
  184.                 MessageBox.Show("User does not exist");
  185.         }
  186.  
  187.         private void LoginForm_Load(object sender, EventArgs e)
  188.         {
  189.             if (!DbConnection.Database.Exists())
  190.             {
  191.                 DbConnection.Database.CreateIfNotExists();
  192.                 DbConnection.SaveChanges();
  193.             }
  194.  
  195.             SetAdminCheckBox.Hide();
  196.             DeleteButton.Hide();
  197.             LogoutButton.Hide();
  198.         }
  199.  
  200.         private void LoadToGrid()
  201.         {
  202.             var load = from g in DbConnection.User select g;
  203.             if (load != null)
  204.             {
  205.                 dataGridView1.DataSource = load.ToList();
  206.             }
  207.         }
  208.  
  209.         private void UsernameTextBox_TextChanged(object sender, EventArgs e)
  210.         {
  211.  
  212.         }
  213.  
  214.         private void Logout_Click(object sender, EventArgs e)
  215.         {
  216.             if (IsAdmin)
  217.                 IsAdmin = false;
  218.  
  219.             UpdateButton.Text = "Login";
  220.             LogoutButton.Hide();
  221.             DeleteButton.Hide();
  222.             SetAdminCheckBox.Hide();
  223.             dataGridView1.DataSource = null;
  224.         }
  225.  
  226.         private void SetAdmin_CheckedChanged(object sender, EventArgs e)
  227.         {
  228.  
  229.         }
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement