using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Diplom_O.DataBase { public class User { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string Login { get; set; } public string Pass { get; set; } public bool Hide { get; set; } public int Default { get; set; } public List Accessy { get; set; } } public static partial class FuncDB { public static void UserCheckValid(User obj) { try { if (obj == null) throw new Exception("Ошибка инициализации."); if (string.IsNullOrEmpty(obj.Login)) throw new Exception("Логин не указан."); if (string.IsNullOrEmpty(obj.Pass)) throw new Exception("Пароль не указан."); if (obj.Default < 0 || obj.Default > 7) throw new Exception("Форма не существует."); } catch { throw; } } public static User UserGetById(int id) { try { using (var db = new MainDB()) { var res = from a in db.Usery where a.Id == id select a; try { return res.Single(); } catch { return null; } } } catch { throw; } } public static User UserGetByLogin(string login) { try { using (var db = new MainDB()) { var res = from a in db.Usery where a.Login == login select a; try { return res.Single(); } catch { return null; } } } catch { throw; } } public static void UserAdd(User obj) { try { UserCheckValid(obj); if (UserGetByLogin(obj.Login) != null) throw new Exception("Пользователь существует."); using (var db = new MainDB()) { db.Usery.Add(obj); db.SaveChanges(); } var user = UserGetByLogin(obj.Login); for (var i = 0; i < FormArray.formAccess.Length; i++) { var access = new Access() { UserId = user.Id, FormId = user.Default, FormatAccess = i }; AccessAdd(access); } } catch { throw; } } public static void UserChange(User obj) { try { UserCheckValid(obj); var localObj = UserGetById(obj.Id); if (localObj == null) throw new Exception("Пользователь не указан."); var localObjByLogin = UserGetByLogin(obj.Login); if (localObjByLogin != null && localObjByLogin.Id != localObj.Id) throw new Exception("Логин существует."); if (AccessGetByUserIdFormIdAccessId(localObj.Id, obj.Default, 0) == null) throw new Exception("Сначала нужен доступ к форме."); using (var db = new MainDB()) { db.Usery.Update(obj); db.SaveChanges(); } } catch { throw; } } public static void UserDelete(User obj) { try { if (obj == null) return; var localObj = UserGetById(obj.Id); if (localObj == null) throw new Exception("Пользователь не существует."); var acessy = AccessList(localObj.Id); foreach (var a in acessy) AccessDelete(a); using (var db = new MainDB()) { db.Usery.Remove(localObj); db.SaveChanges(); } } catch { throw; } } public static User[] UserList(bool showHide = false) { try { using (var db = new MainDB()) { var user = ( from a in db.Usery where (showHide || !a.Hide) select a ).ToArray(); return user; } } catch { throw; } } public static string[][] UserListTable(bool showHide = false) { try { using (var db = new MainDB()) { var list = UserList(showHide); var subres = new List(); foreach (var r in list) { subres.Add( new string[]{ r.Login }); } return subres.ToArray(); } } catch { throw; } } } }