Example #1
0
 public IEnumerable<role> GetRoles()
 {
     using (var context = new appsterEntities())
     {
         return context.roles.ToArray();
     }
 }
Example #2
0
 public Models.user GetUser(Func<Models.user, bool> predicate)
 {
     using (var context = new appsterEntities())
     {
         var result = context.users.SingleOrDefault(predicate);
         return result;
     }
 }
Example #3
0
 /// <summary>
 /// Get the role_id of an role by role name from database
 /// </summary>
 /// <param name="roleName"></param>
 /// <returns>role_id of type int, -1 if no role can be found</returns>
 public int GetRole(string roleName)
 {
     using (var context = new appsterEntities())
     {
         var role = context.roles.SingleOrDefault(i => i.name.ToLower() == roleName.ToLower());
         if (role != null) return role.id;
         return -1;
     }
 }
Example #4
0
 public IEnumerable<gift_categories> GetGiftCategories()
 {
     var context = new appsterEntities();
     try
     {
         var data = context.gift_categories.ToArray();
         return data;
     }
     finally
     {
         context.Dispose();
     }
 }
 public int SuspendUser(int userId, bool suspend = true)
 {
     using (var context = new appsterEntities())
     {
         var dbUser = context.users.SingleOrDefault(i => i.id == userId);
         if(dbUser!=null) 
         {
             dbUser.status = suspend ? 0 : 1;
             context.SaveChanges();
             return dbUser.status;
         }
         throw new AppsDataNotFoundException();
     }
 }
Example #6
0
 /// <summary>
 /// Get user access type
 /// </summary>
 /// <param name="role"></param>
 /// <returns></returns>
 /// <exception cref="AppsInvalidUserException"></exception>
 public static AccessType GetAccessType(int role)
 {
     using (var context = new appsterEntities())
     {
         var roles = context.roles;
         var userRole = roles.SingleOrDefault(i => i.id == role);
         if (userRole == null) throw new AppsInvalidUserException("User role is invalid");
         switch (userRole.name.ToLower())
         {
             case SupperAdmin: return AccessType.Supper;
             case "minimum access": return AccessType.Minimum;
             case "average access": return AccessType.Average;
             case "maximum access": return AccessType.Max;
             default: throw new AppsInvalidUserException("User role is invalid");
         }
     }
 }
Example #7
0
        public IEnumerable<Models.user> GetUsers(Func<Models.user, bool> predicate, int? take, int? cursor, bool loadBack = false)
        {
            using (var context = new appsterEntities())
            {
                cursor = cursor.HasValue ? cursor : 0;
                var users = loadBack ?
                     from data in context.users.Where(predicate) 
                     where data.id < cursor.Value
                     select data : 
                     from data in context.users.Where(predicate)
                       where data.id > cursor.Value
                       select data;

                if (take.HasValue && take.Value > 0) return users.Take(take.Value).ToArray();
                return users.ToArray();
            }
        }
 public async Task UpdateUser(user updatedUser)
 {
     if (!GetRoles().Any(i => i.id == updatedUser.role_id)) throw new AppsRequiredDataIsNullException();
     using (var context = new appsterEntities())
     {
         var checkData = context.users.SingleOrDefault(i => i.id != updatedUser.id &&
                         (i.username == updatedUser.username || i.email == updatedUser.email));
         if (checkData == null)
         {
             context.users.Add(updatedUser);
             context.Entry(updatedUser).State = System.Data.Entity.EntityState.Modified;
             await context.SaveChangesAsync();
         }
         else
         {
             if (checkData.username == updatedUser.username) throw new AppsUsernameAlreadyExistException();
             if (checkData.email == updatedUser.email) throw new AppsEmailAlreadyExistException();
         }
     }
 }
 public void SuspendUser(Func<user, bool> predicate)
 {
     using (var context = new appsterEntities())
     {
         context.Configuration.AutoDetectChangesEnabled = false;
         var users = context.users.Where(predicate);
         try
         {
             foreach (var usr in users)
             {
                 usr.status = usr.status == 0 ? 1 : 0;
                 context.Entry(usr).State = System.Data.Entity.EntityState.Modified;
             }
         }
         finally
         {
             context.Configuration.AutoDetectChangesEnabled = true;
             context.SaveChanges();
         }
     }
 }
        public void DeleteUser(Func<user, bool> predicate)
        {
            using (var context = new appsterEntities())
            {
                context.Configuration.AutoDetectChangesEnabled = false;
                var users = context.users.Where(predicate);
                try
                {
                    if (users.Count() > 0)
                    {
                        context.users.RemoveRange(users);
                    }
                }
                finally
                {
                    context.Configuration.AutoDetectChangesEnabled = true;
                    context.SaveChanges();
                }

            }
        }
 public async Task<int> CreateUser(user newUser)
 {
     using (var context = new appsterEntities())
     {
         var checkData = context.users.FirstOrDefault(i => i.username == newUser.username || i.email == newUser.email);
         if (checkData == null)
         {
             context.users.Add(newUser);
             await context.SaveChangesAsync();
             return newUser.id;
         }
         else
         {
             if (checkData.username == newUser.username) throw new AppsUsernameAlreadyExistException();
             if (checkData.email == newUser.email) throw new AppsEmailAlreadyExistException();
         }
         throw new AppsDatabaseExecutionException();
     }
 }
Example #12
0
 public Models.post GetPost(Func<Models.post, bool> predicate)
 {
     using (var context = new appsterEntities())
     {
         var post = context.posts.SingleOrDefault(predicate);
         return post;
     }
 }
 public int SuspendGift(int giftId, bool suspend = true)
 {
     using (var context = new appsterEntities())
     {
         var dbGift = context.gifts.SingleOrDefault(i => i.id == giftId);
         if (dbGift != null)
         {
             dbGift.status = suspend ? 0 : 1;
             context.SaveChanges();
             return dbGift.status;
         }
         throw new AppsDataNotFoundException();
     }
 }
Example #14
0
 public IEnumerable<user> GetUsersByPage(Func<user, bool> predicate, bool loadBack, int take, int page, out PagingHelper pagingInfo)
 {
     var context = new appsterEntities();
     try
     {
         var totalItems = context.users.Count(predicate);
         pagingInfo = PagingHelper.GetPageInfo(totalItems, page, take);
         var skip = pagingInfo.CurentPage == 0 ? 0 : (pagingInfo.CurentPage - 1) * take;
         if (loadBack)
         {
             var data = (from users in context.users.Where(predicate)
                         orderby users.id descending
                         select users).Skip(skip).Take(pagingInfo.Count).ToList();
             return data;
         }
         else
         {
             var data = (from users in context.users.Where(predicate)
                         select users).Skip(skip).Take(pagingInfo.Count).ToList();
             return data;
         }
     }
     finally
     {
         context.Dispose();
     }
 }
Example #15
0
 public IEnumerable<Models.post> GetPosts(Func<Models.post, bool> predicate)
 {
     using (var context = new appsterEntities())
     {
         var posts = context.posts.Where(predicate);
         return posts.ToArray();
     }
 }
Example #16
0
 public IEnumerable<user> GetNewAddedUser(int take, bool getAdminUser = false)
 {
     var roleId = GetRole("user");
     using (var context = new appsterEntities())
     {
         if (getAdminUser)
         {
             var newAddedAdmin = (from users in context.users
                              where users.role_id != roleId
                              orderby users.id descending
                              select users).Take(take).ToArray();
             return newAddedAdmin;
         }
         else
         {
             var newAddedUsers = (from users in context.users
                                  where users.role_id == roleId
                                  orderby users.id descending
                              select users).Take(take).ToList();
             return newAddedUsers;
         }
         
     }
 }
Example #17
0
        public IEnumerable<Models.gift> GetGifts(Func<Models.gift, bool> predicate, int take, int cursor = 0, bool loadBack = false)
        {
            var context = new appsterEntities();
            context.Configuration.AutoDetectChangesEnabled = false;
            try
            {
                var data = loadBack ? (from gifts in context.gifts.Where(predicate).OrderByDescending(i => i.id)
                                       where cursor == 0 || gifts.id < cursor
                                       select gifts).Take(take).ToArray() :
                                      (from gifts in context.gifts.Where(predicate)
                                       where gifts.id > cursor
                                       select gifts).Take(take).ToArray();

                return data;
            }
            finally
            {
                context.Configuration.AutoDetectChangesEnabled = true;
                context.Dispose();
            }
        }
Example #18
0
 public IEnumerable<dynamic> GetSavePushNotifications(Func<save_push_notifications, bool> predicate, int take)
 {
         var context = new appsterEntities();
         context.Configuration.AutoDetectChangesEnabled = false;
         try
         {
             var data = context.save_push_notifications.Where(predicate).OrderByDescending(n => n.id)
                            .Join(context.users, p => p.resiver_user_id, u => u.id, (p, u) => new
                            {
                                Id = p.id,
                                UserId = p.user_id,
                                ReceiverId = p.resiver_user_id,
                                ReceiverName = u.display_name,
                                Message = p.message,
                                CreatedDate = p.created
                            }).Take(take).ToArray();
             return data;
         }
         finally
         {
             context.Configuration.AutoDetectChangesEnabled = true;
             context.Dispose();
         }
 }
Example #19
0
        public async Task<dynamic> GetUserAdditionalInformation(int userId)
        {
            var context = new appsterEntities();
            try
            {
                var followings = context.follows.Count(i => i.id == userId);
                var followers = context.follows.Count(i => i.follow_user_id == userId);
                var posts = context.posts.Count(i => i.user_id == userId);

                return new
                {
                    FollowerCount = followers,
                    FollowingCount = followings,
                    PostCount = posts
                };
            }
            finally
            {
                context.Dispose();
            }
        }