Пример #1
0
        public static int Delete(int id)
        {
            try
            {
                using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
                {
                    Post Delete = context.Posts.Include("Likes").First(s => s.Id == id);

                    //Delete picture
                    if (Delete.Picture != null)
                    {
                        try
                        {
                            System.IO.File.Delete(Delete.Picture);
                        }
                        catch (System.IO.IOException e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                    context.Posts.Remove(Delete);
                    return(context.SaveChanges());
                }
            }
            catch (Exception)
            {
                return(-1);
            }
        }
Пример #2
0
        public static ICollection <Post> GetPostByName(String name, User user)
        {
            try
            {
                using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
                {
                    List <Post> postSearchResult = new List <Post>();

                    foreach (var group in user.Group.ToList())
                    {
                        List <Post> postSearchTmp = context.Posts.Where(p => p.GroupId == group.Id).ToList();
                        foreach (var post in postSearchTmp)
                        {
                            if (post.Content.ToUpper().Contains(name.ToUpper()))
                            {
                                postSearchResult.Add(post);
                            }
                        }
                    }
                    return(postSearchResult);
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Пример #3
0
        public static ICollection <Post> GetPostFollow(int id)
        {
            try
            {
                using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
                {
                    User        usersTmp = context.Users.Include("Follow").Include("Group").Include("Group.Users").Where(u => u.Id == id).Single();
                    List <Post> posts    = new List <Post>();

                    // Foreach user's groups
                    foreach (var item in usersTmp.Group.ToList())
                    {
                        // user's follow
                        foreach (var item2 in usersTmp.Follow)
                        {
                            // If share group
                            if (item2.Group.FirstOrDefault(u => u.Id == item.Id) != null)
                            {
                                List <Post> postTmps = context.Posts.Include("Likes").Where(u => u.UserId == item2.Id && u.GroupId == item.Id).ToList();
                                foreach (var post in postTmps)
                                {
                                    posts.Add(post);
                                }
                            }
                        }
                    }
                    return(posts.OrderByDescending(p => p.DateOfCreation).ToList());
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Пример #4
0
        public static int Create(Group group)
        {
            try
            {
                using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
                {
                    if (context.Groups.FirstOrDefault(g => g.Name == group.Name) == null)
                    {
                        context.Groups.Add(group);
                        context.SaveChanges();

                        var UserSearch  = context.Users.Include("Group").First(u => u.Id == group.UserOwnerId);
                        var GroupSearch = context.Groups.Include("Users").First(g => g.Id == group.Id);
                        UserSearch.Group.Add(GroupSearch);
                        return(context.SaveChanges());
                    }
                    else
                    {
                        return(-1);
                    }
                }
            }
            catch (Exception)
            {
                return(-1);
            }
        }
Пример #5
0
        public static User Create(User user)
        {
            try
            {
                using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
                {
                    // check user already exist
                    user.Group = null;
                    user.Likes = null;
                    user.Posts = null;
                    if (context.Users.Where(s => s.Email == user.Email).FirstOrDefault() == null)
                    {
                        // create user
                        context.Users.Add(user);

                        // assign group to user
                        string   email      = user.Email;
                        string[] split      = email.Split('@');
                        string   nomDomaine = split[1].ToString();
                        split = nomDomaine.Split('.');
                        string nomGroupe = split[0].ToString();

                        // check group already exist
                        Group existGroup = context.Groups.Where(s => s.Name == nomGroupe).FirstOrDefault();

                        if (existGroup == null)
                        {
                            Group groupForUser = new Group();
                            groupForUser.Name        = nomGroupe;
                            groupForUser.UserOwnerId = user.Id;
                            context.Groups.Add(groupForUser);
                            context.SaveChanges();
                            User_Repo.AddUserToGroup(user.Id, groupForUser.Id);
                        }
                        else
                        {
                            context.SaveChanges();
                            User_Repo.AddUserToGroup(user.Id, existGroup.Id);
                        }

                        context.SaveChanges();
                        return(user);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Пример #6
0
 public static ICollection <Post> GetPostByGroup(Group group)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             return(context.Posts.Include("User").Include("Likes").Include("Comments").Include("Comments.User").Where(s => s.GroupId == group.Id).OrderByDescending(p => p.DateOfCreation).ToList());
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #7
0
 public static Group GetGroupById(int id)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             return(context.Groups.Include("Users").Include("User.Follow").First(s => s.Id == id));
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #8
0
 public static User GetUserById(int id)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             return(context.Users.Include("Group").Include("Follow").Where(s => s.Id == id).First());
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #9
0
 public static User GetUserByEmailAndPass(String email, String pass)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             return(context.Users.Where(s => s.Email == email && s.Password == pass).First());
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #10
0
 public static Like GetLikeById(int id)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             return(context.Likes.Find(id));
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #11
0
 public static ICollection <User> GetAllUser()
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             return(context.Users.Include("Group").ToList());
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #12
0
 public static Comment GetCommentById(int id)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             return(context.Comments.Include("User").First(p => p.Id == id));
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #13
0
 public static Post GetPostById(int id)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             return(context.Posts.Include("Likes").First(s => s.Id == id));
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #14
0
 public static int Create(Like like)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             context.Likes.Add(like);
             return(context.SaveChanges());
         }
     }
     catch (Exception)
     {
         return(-1);
     }
 }
Пример #15
0
 public static int Create(Comment comment)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             context.Comments.Add(comment);
             return(context.SaveChanges());
         }
     }
     catch (Exception)
     {
         return(-1);
     }
 }
Пример #16
0
 public static int AddPictureToAPost(Post post, String pathPicture)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             Post postEdit = context.Posts.Find(post.Id);
             postEdit.Picture = pathPicture;
             return(context.SaveChanges());
         }
     }
     catch (Exception)
     {
         return(-1);
     }
 }
Пример #17
0
 public static int Delete(int id)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             Like delete = context.Likes.Find(id);
             context.Likes.Remove(delete);
             return(context.SaveChanges());
         }
     }
     catch (Exception)
     {
         return(-1);
     }
 }
Пример #18
0
 public static int RemoveFollow(int idUser, int IdUserDestinataire)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             User userDestinataire = context.Users.Include("Users").Where(s => s.Id == IdUserDestinataire).First();
             User user             = context.Users.Include("Follow").Where(s => s.Id == idUser).First();
             user.Follow.Remove(userDestinataire);
             return(context.SaveChanges());
         }
     }
     catch (Exception)
     {
         return(-1);
     }
 }
Пример #19
0
        public static int RemoveUserFromGroup(int idUser, int idGroup)
        {
            try
            {
                using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
                {
                    User  userSearch  = context.Users.Include("Group").First(s => s.Id == idUser);
                    Group groupSearch = context.Groups.Include("Users").First(s => s.Id == idGroup);

                    userSearch.Group.Remove(groupSearch);

                    return(context.SaveChanges());
                }
            }
            catch (Exception)
            {
                return(-1);
            }
        }
Пример #20
0
        public static Post Create(Post post, int idUser, int idGroup)
        {
            try
            {
                using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
                {
                    User  userSearch  = context.Users.Include("Group").Single(s => s.Id == idUser);
                    Group groupSearch = context.Groups.Include("User").Single(s => s.Id == idGroup);
                    post.Group = groupSearch;
                    post.User  = userSearch;

                    context.Posts.Add(post);
                    context.SaveChanges();
                    return(post);
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Пример #21
0
 public static List <User> GetUserByName(String name)
 {
     try
     {
         using (ZoombuM1ServiceContext context = new ZoombuM1ServiceContext())
         {
             List <User> userSearchResult = new List <User>();
             foreach (var user in context.Users.ToList())
             {
                 String nameUser = user.Firstname + " " + user.Lastname;
                 if (nameUser.ToUpper().Contains(name.ToUpper()))
                 {
                     userSearchResult.Add(user);
                 }
             }
             return(userSearchResult);
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }