Exemple #1
0
        public static bool CreateRole(string roleName, List <int> selectedPermissionIds)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    Roles dbRole = new Roles {
                        Name = roleName
                    };
                    var selectedPermissions = db.Permission.Where(p => selectedPermissionIds.Contains(p.Id)).ToList();
                    foreach (var sp in selectedPermissions)
                    {
                        dbRole.Permission.Add(sp);
                    }
                    db.Roles.Add(dbRole);
                    db.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                log.Error("CreateRole error", ex);
                return(false);
            }
        }
Exemple #2
0
        public static bool AssignBusinessToUser(int userId, List <int> businessIdList)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var user = db.User.Find(userId);

                    var usersMembership = db.BusinessMember.Where(bm => bm.UserId == userId);
                    db.BusinessMember.RemoveRange(usersMembership);
                    foreach (var bizId in businessIdList)
                    {
                        db.BusinessMember.Add(
                            new BusinessMember
                        {
                            BusinessId = bizId,
                            UserId     = user.Id
                        });
                    }

                    db.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                log.Error("AssignBusinesses error", ex);
                return(false);
            }
        }
Exemple #3
0
        public static bool SaveConfigItems(List <ConfigItem> configItems)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    // Update configuration items
                    foreach (var dbItem in db.ConfigItem)
                    {
                        var match = configItems.FirstOrDefault(i => i.Name.Equals(dbItem.Name));
                        if (match != null)
                        {
                            dbItem.Value = match.Value.Trim();
                        }
                    }

                    db.SaveChanges();
                }
                return(true);
            }
            catch (Exception ex)
            {
                log.Error("SaveConfigItems error", ex);
                return(false);
            }
        }
Exemple #4
0
        public static bool AssignTicketBusinesses(int userId, Guid guidTicketNumber)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var ticket         = db.UserSignupTicket.First(t => t.Guid == guidTicketNumber);
                    var businessIdList = ticket.Business.Select(b => b.Id);

                    var user = db.User.Find(userId);
                    foreach (var bizId in businessIdList)
                    {
                        db.BusinessMember.AddOrUpdate(
                            new BusinessMember
                        {
                            BusinessId = bizId,
                            UserId     = user.Id
                        });
                    }

                    db.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                log.Error("AssignBusinesses error", ex);
                return(false);
            }
        }
Exemple #5
0
        public static bool AddBusinessToSignupTicket(int ticketId, List <int> selectedBusinesses)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var ticket = db.UserSignupTicket.Find(ticketId);
                    ticket.Business.Clear();
                    foreach (int bizId in selectedBusinesses)
                    {
                        var biz = db.Business.Find(bizId);
                        ticket.Business.Add(biz);
                    }

                    db.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteSignupTicket error", ex);
                return(false);
            }
        }
Exemple #6
0
        public static bool DeleteSignupTicket(int ticketId)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var ticket = db.UserSignupTicket.Find(ticketId);
                    foreach (var biz in ticket.Business)
                    {
                        biz.TicketId         = null;
                        biz.UserSignupTicket = null;
                    }
                    db.UserSignupTicket.Remove(ticket);

                    db.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteSignupTicket error", ex);
                return(false);
            }
        }
Exemple #7
0
        public static bool RemoveUser(int id)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var user = db.User.Find(id);

                    foreach (var role in user.Roles.ToList())
                    {
                        user.Roles.Remove(role);
                    }

                    var membership = db.BusinessMember.Where(bm => bm.UserId == id);
                    db.BusinessMember.RemoveRange(membership);

                    db.Set <User>().Remove(user);

                    db.SaveChanges();
                }
                return(true);
            }
            catch (Exception ex)
            {
                log.Error("RemoveUser error", ex);
                return(false);
            }
        }
Exemple #8
0
        public static bool ModifyRole(AdapterDb.Roles role, List <int> selectedPermissionIds)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var dbRole = db.Roles.Find(role.Id);
                    dbRole.Name = role.Name;
                    dbRole.Permission.Clear();

                    var selectedPermissions = db.Permission.Where(p => selectedPermissionIds.Contains(p.Id)).ToList();
                    foreach (var sp in selectedPermissions)
                    {
                        dbRole.Permission.Add(sp);
                    }

                    db.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                log.Error("ModifyRole error", ex);
                return(false);
            }
        }
Exemple #9
0
        public static bool DeleteLine(int lineId)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var ln = db.Line.Find(lineId);
                    db.Line.Remove(ln);

                    db.SaveChanges();
                }
                return(true);
            }
            catch (Exception ex)
            {
                log.Error("DeleteLine error", ex);
                return(false);
            }
        }
Exemple #10
0
        public static bool SaveLine(Line dbLine)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    dbLine.Created = dbLine.Created ?? DateTime.UtcNow;
                    db.Line.AddOrUpdate(dbLine);

                    db.SaveChanges();
                }
                return(true);
            }
            catch (Exception ex)
            {
                log.Error("SaveLine error", ex);
                return(false);
            }
        }
Exemple #11
0
        public static bool SaveSignupTicket(UserSignupTicket signupTicket)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    signupTicket.Created = signupTicket.Created ?? DateTime.UtcNow;
                    db.UserSignupTicket.AddOrUpdate(signupTicket);

                    db.SaveChanges();
                }
                return(true);
            }
            catch (Exception ex)
            {
                log.Error("SaveSignupTicket error", ex);
                return(false);
            }
        }
Exemple #12
0
        public static bool DeleteBusiness(int businessId)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var biz = db.Business.Find(businessId);
                    db.Line.RemoveRange(biz.Line);
                    db.Business.Remove(biz);

                    db.SaveChanges();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteBusiness error", ex);
                return(false);
            }
        }
Exemple #13
0
        public static bool DeleteRole(int roleId)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var role = db.Roles.Find(roleId);
                    role.Permission.Clear();
                    db.Roles.Remove(role);

                    db.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteRole error", ex);
                return(false);
            }
        }
Exemple #14
0
        //public static bool CreateUser(string userName, List<int?> selectedRoleIds)
        //{
        //    try
        //    {
        //        using (var db = new AdapterDbEntities())
        //        {
        //            User dbUser = new User { UserName = userName };
        //            var selectedRoles = db.Roles.Where(p => selectedRoleIds.Contains(p.Id)).ToList();
        //            foreach (var sr in selectedRoles)
        //            {
        //                dbUser.Roles.Add(sr);
        //            }
        //            db.User.Add(dbUser);
        //            db.SaveChanges();

        //            return true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error("CreateUser error", ex);
        //        return false;
        //    }
        //}

        //public static bool ModifyUser(User user, List<int?> selectedRoleIds)
        //{
        //    try
        //    {
        //        using (var db = new AdapterDbEntities())
        //        {
        //            var dbUser = db.User.Find(user.Id);
        //            dbUser.UserName = user.UserName;
        //            dbUser.Roles.Clear();

        //            var selectedRoles = db.Roles.Where(p => selectedRoleIds.Contains(p.Id)).ToList();
        //            foreach (var sr in selectedRoles)
        //            {
        //                dbUser.Roles.Add(sr);
        //            }

        //            db.SaveChanges();

        //            return true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error("ModifyUser error", ex);
        //        return false;
        //    }
        //}

        public static bool UpdateUserActivity(int userId, bool isActive)
        {
            try
            {
                using (var db = new AdapterDbEntities())
                {
                    var dbUser = db.User.Find(userId);

                    if (dbUser.IsActive && !isActive ||
                        !dbUser.IsActive && isActive)
                    {
                        dbUser.IsActive = isActive;
                        db.SaveChanges();
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                log.Error("UpdateUserActivity error", ex);
                return(false);
            }
        }