Beispiel #1
0
        public bool ChangeStatus(Menu menu, bool status)
        {
            try
            {
                using (LCDBEntities model = new LCDBEntities())
                {
                    var _menu = model.Menus.FirstOrDefault(o => o.ID == menu.ID);
                    if (_menu == null)
                    {
                        throw new MenuNotFoundException("Menu not found with this id");
                    }
                    _menu.IsActive = status;

                    model.SaveChanges();
                }
                return(true);
            }
            catch (Exception ex)
            {
                if (ex is MenuNotFoundException)
                {
                    throw ex;
                }
                return(false);
            }
        }
Beispiel #2
0
        public List <MenuClass> GetMenus(int roleId, int languageId)
        {
            try
            {
                using (var model = new LCDBEntities())
                {
                    var r = (from mr in model.MenusRoles.AsQueryable()
                             join m in model.Menus.AsQueryable() on mr.MenuID equals m.ID
                             join l in model.TranslateMenus.AsQueryable() on m.ID equals l.MenuID
                             where mr.RoleID == roleId && l.LanguageID == languageId
                             select new MenuClass
                    {
                        ID = m.ID,
                        Name = m.Name,
                        Description = m.Description,
                        Tittle = l.Title,
                        EventName = m.EventName,
                        IsActive = m.IsActive,
                        OrderNumber = m.OrderNumber,
                        ParentID = m.ParentID
                    }).ToList();

                    return(r);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(new List <MenuClass>());
            }
        }
Beispiel #3
0
        public bool EditCatalog(int id, string name, int?parentId)
        {
            try
            {
                using (var model = new LCDBEntities())
                {
                    var catalog = model.Catalogs.FirstOrDefault(c => c.ID == id);
                    if (catalog == null)
                    {
                        throw new CatalogNotFoundException("Catalog with this id not found in database");
                    }

                    catalog.Name     = name;
                    catalog.ParentID = parentId;
                    model.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                if (ex is CatalogNotFoundException)
                {
                    throw ex;
                }

                return(false);
            }
        }
Beispiel #4
0
        public bool EditMenu(Menu menu)
        {
            try
            {
                using (LCDBEntities model = new LCDBEntities())
                {
                    var _menu = model.Menus.FirstOrDefault(m => m.ID == menu.ID);
                    if (_menu == null)
                    {
                        throw new MenuNotFoundException("Menu not found with this id");
                    }

                    _menu.IsActive    = menu.IsActive;
                    _menu.Name        = menu.Name;
                    _menu.Description = menu.Description;
                    _menu.ParentID    = menu.ParentID;
                    _menu.EventName   = menu.EventName;
                    _menu.OrderNumber = menu.OrderNumber;

                    model.SaveChanges();
                }
                return(true);
            }
            catch (Exception ex)
            {
                if (ex is MenuNotFoundException)
                {
                    throw ex;
                }
                return(false);
            }
        }
Beispiel #5
0
        public bool AddMenu(Menu menu)
        {
            try
            {
                using (LCDBEntities model = new LCDBEntities())
                {
                    Menu _menu = new Menu();

                    _menu.ID          = menu.ID;
                    _menu.Name        = menu.Name;
                    _menu.Description = menu.Description;
                    _menu.ParentID    = menu.ParentID;
                    _menu.EventName   = menu.EventName;
                    _menu.OrderNumber = menu.OrderNumber;
                    _menu.IsActive    = menu.IsActive;

                    model.Menus.Add(_menu);

                    model.SaveChanges();
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #6
0
        public bool SetStatusAsActive(int id, bool isActive)
        {
            try
            {
                using (var model = new LCDBEntities())
                {
                    var catalog = model.Catalogs.FirstOrDefault(c => c.ID == id);
                    if (catalog == null)
                    {
                        throw new CatalogNotFoundException("Catalog with this id not found in database");
                    }

                    catalog.IsActive = isActive;
                    model.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                if (ex is CatalogNotFoundException)
                {
                    throw ex;
                }

                return(false);
            }
        }
Beispiel #7
0
        public List <ICatalog> GetCatalogsByParentId(int?parentID)
        {
            try
            {
                using (var model = new LCDBEntities())
                {
                    var catalogs = model.Catalogs.Where(c => c.ParentID == parentID);
                    if (catalogs == null)
                    {
                        return(new List <ICatalog>());
                    }

                    var result = catalogs.Select(c => new CatalogClass
                    {
                        Id       = c.ID,
                        IsActive = c.IsActive,
                        Name     = c.Name,
                        ParentId = c.ParentID
                    }).ToList <ICatalog>();

                    return(result);
                }
            }
            catch (Exception)
            {
                return(new List <ICatalog>());
            }
        }
 public List <RoleClass> GetRoles()
 {
     using (LCDBEntities model = new LCDBEntities())
     {
         return(model.Roles.Select(i =>
                                   new RoleClass {
             ID = i.ID, Name = i.Name, Description = i.Description
         }).ToList());
     }
 }
        public bool AddUser(UserClass user)
        {
            try
            {
                using (LCDBEntities model = new LCDBEntities())
                {
                    using (TransactionScope t = new TransactionScope())
                    {
                        try
                        {
                            if (model.Users.Any(i => i.PersonalNumber.Equals(user.PersonalNumber)))
                            {
                                throw new Exception("This user already exists...");
                            }

                            User u = new User();
                            u.PersonalNumber = user.PersonalNumber;
                            u.FirstName      = user.FirstName;
                            u.LastName       = user.LastName;
                            u.EMail          = user.EMail;
                            u.Password       = user.Password;
                            u.BirthDate      = user.BirthDate;
                            u.NationalityID  = user.NationalityID;
                            u.Avatar         = user.Avatar;
                            u.Address1       = user.Address1;
                            u.Address2       = user.Address2;
                            u.PhoneNumber    = user.PhoneNumber;
                            u.GenderId       = user.GenderId;
                            model.Users.Add(u);

                            UserRole ur = new UserRole();
                            ur.RoleID   = user.RoleId;
                            ur.UserID   = u.ID;
                            ur.IsActive = true;
                            model.UserRoles.Add(ur);

                            model.SaveChanges();

                            t.Complete();
                            return(true);
                        }
                        catch (Exception ex)
                        {
                            t.Dispose();
                            throw new Exception(ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Beispiel #10
0
        public bool BlockUnblock(int userId, int roleId)
        {
            try
            {
                LCDBEntities model = new LCDBEntities();
                if (!model.UserRoles.Any(i => i.UserID == userId && i.RoleID == roleId))
                {
                    throw new Exception("role not found...");
                }

                UserRole u = model.UserRoles.Where(i => i.UserID == userId && i.RoleID == roleId).First();
                u.IsActive = !u.IsActive;
                model.SaveChanges();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Beispiel #11
0
        public UserClass FindUser(string pn)
        {
            try
            {
                LCDBEntities model = new LCDBEntities();

                if (!model.Users.Any(i => i.PersonalNumber.Equals(pn)))
                {
                    throw new Exception("User not found...");
                }

                var result = model.Users.Where(i => i.PersonalNumber.Equals(pn)).Select(i =>
                                                                                        new UserClass
                {
                    Id            = i.ID,
                    FirstName     = i.FirstName,
                    LastName      = i.LastName,
                    EMail         = i.EMail,
                    BirthDate     = i.BirthDate,
                    NationalityID = i.NationalityID,
                    Avatar        = i.Avatar,
                    Address1      = i.Address1,
                    Address2      = i.Address2,
                    PhoneNumber   = i.PhoneNumber,
                    GenderId      = i.GenderId,
                    Roles         = i.UserRoles.Select(k =>
                                                       new RoleClass
                    {
                        ID = k.ID, Name = k.Role.Name, Description = k.Role.Description
                    }).ToList()
                }).First();

                return(result);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Beispiel #12
0
        public bool ChangeMenuIndex(Menu menu, int ordernNUmber)
        {
            try
            {
                using (LCDBEntities model = new LCDBEntities())
                {
                    var _menu = model.Menus.FirstOrDefault(o => o.ID == menu.ID);
                    if (_menu == null)
                    {
                        throw new MenuNotFoundException("Menu not found with this id");
                    }

                    var menuItem = model.Menus.FirstOrDefault(o => o.OrderNumber == ordernNUmber);

                    if (menuItem != null)
                    {
                        //model.Menus.Skip(ordernNUmber).Select(o => { o.OrderNumber++; return true; });

                        //for (int i = ordernNUmber; i < model.Menus.Count(); i++)
                        //{
                        //    model.Menus.ToList()[i].OrderNumber++;
                        //}

                        model.Menus.Skip(ordernNUmber).ToList().ForEach(o => { o.OrderNumber = o.OrderNumber++; });

                        menu.OrderNumber = ordernNUmber;
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                if (ex is MenuNotFoundException)
                {
                    throw ex;
                }
                return(false);
            }
        }
Beispiel #13
0
        public bool UpdateRole(RoleClass role)
        {
            try
            {
                LCDBEntities model = new LCDBEntities();
                if (!model.Roles.Any(i => i.Name.Equals(role.Name)))
                {
                    throw new Exception("This role not found...");
                }

                Role r = model.Roles.Where(i => i.Name.Equals(role.Name)).First();
                r.Name        = role.Name;
                r.Description = role.Description;
                model.SaveChanges();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Beispiel #14
0
        public bool AddNewCatalog(ICatalog catalog)
        {
            try
            {
                using (var model = new LCDBEntities())
                {
                    var newCatalog = new Catalog()
                    {
                        Name     = catalog.Name,
                        ParentID = catalog.ParentId,
                        IsActive = catalog.IsActive
                    };

                    model.Catalogs.Add(newCatalog);
                    model.SaveChanges();
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #15
0
        public bool AddRole(RoleClass role)
        {
            try
            {
                LCDBEntities model = new LCDBEntities();
                if (model.Roles.Any(i => i.Name.Equals(role.Name)))
                {
                    throw new Exception("This Role already exists...");
                }

                Role r = new Role();
                r.Name        = role.Name;
                r.Description = role.Description;
                model.Roles.Add(r);
                model.SaveChanges();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Beispiel #16
0
        public bool AssignNewRole(int userId, int roleId)
        {
            try
            {
                LCDBEntities model = new LCDBEntities();
                if (model.UserRoles.Any(i => i.UserID == userId && i.RoleID == roleId))
                {
                    throw new Exception("The Role is already assigned for this User...");
                }

                UserRole ur = new UserRole();
                ur.UserID   = userId;
                ur.RoleID   = roleId;
                ur.IsActive = true;

                model.UserRoles.Add(ur);
                model.SaveChanges();
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Beispiel #17
0
        public bool UpdateUser(UserClass user)
        {
            try
            {
                using (LCDBEntities model = new LCDBEntities())
                {
                    using (TransactionScope t = new TransactionScope())
                    {
                        try
                        {
                            if (!model.Users.Any(i => i.PersonalNumber.Equals(user.PersonalNumber)))
                            {
                                throw new Exception("This user not found...");
                            }

                            User u = model.Users.Where(i => i.PersonalNumber.Equals(user.PersonalNumber)).First();
                            u.FirstName     = user.FirstName;
                            u.LastName      = user.LastName;
                            u.BirthDate     = user.BirthDate;
                            u.NationalityID = user.NationalityID;
                            u.Avatar        = user.Avatar;
                            u.Address1      = user.Address1;
                            u.Address2      = user.Address2;
                            u.PhoneNumber   = user.PhoneNumber;
                            u.GenderId      = user.GenderId;

                            if (!model.UserRoles.Any(i => i.UserID == u.ID))
                            {
                                throw new Exception("role not found...");
                            }

                            UserRole ur = model.UserRoles.Where(i => i.UserID == u.ID).First();
                            ur.RoleID = user.RoleId;

                            model.SaveChanges();

                            t.Complete();
                            return(true);
                        }
                        catch (Exception ex)
                        {
                            t.Dispose();
                            throw new Exception(ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(false);
            }

            try
            {
                LCDBEntities model = new LCDBEntities();



                model.SaveChanges();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }