Esempio n. 1
0
        public bool AddFriend(int firstuserId, int secondUserId)
        {
            using (var db = new groubel_dbEntities1())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var item = db.Friendships.FirstOrDefault(i => i.UserId == firstuserId && i.FriendId == secondUserId);

                        if (item != null)
                        {
                            return(false);
                        }

                        db.Friendships.Add(new Friendship
                        {
                            UserId   = firstuserId,
                            FriendId = secondUserId,
                            Date     = DateTime.Now,
                            Status   = true
                        });

                        db.Friendships.Add(new Friendship
                        {
                            UserId   = secondUserId,
                            FriendId = firstuserId,
                            Date     = DateTime.Now,
                            Status   = true
                        });

                        db.SaveChanges();

                        _notificationService.SetApproved(firstuserId, secondUserId);

                        _notificationService.AddNotification(firstuserId, secondUserId, NotificationTypeEnum.AddEdYouAsFriend, 0, "", null);

                        transaction.Commit();

                        return(true);
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();

                        return(false);
                    }
                }
            }
        }
Esempio n. 2
0
        public PostEntity AddPost(PostEntity post, HttpPostedFileBase file)
        {
            using (var db = new groubel_dbEntities1())
            {
                var upload = new IOHandler("~/Uploads/PostAttachements/");

                var name = upload.Save(file);

                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var newPost = new Post
                        {
                            Attachement = name,
                            Text        = post.Text,
                            Date        = DateTime.Now,
                            Anonoimus   = post.Anonoimus,
                            Ip          = post.Ip,
                            UserId      = post.UserId,
                            Image       = "",
                            Video       = "",
                        };

                        db.Posts.Add(newPost);

                        db.SaveChanges();

                        var postInterests = _interestService.GetInterestObjectsByName(post.InterestNames, post.UserId);

                        foreach (var intere in postInterests)
                        {
                            db.PostInterests.Add(new PostInterest
                            {
                                PostId     = newPost.Id,
                                InterestId = intere.Id
                            });

                            _notificationService.AddNotification(post.UserId, 0, NotificationTypeEnum.PostAddedOnInterest, intere.Id, newPost.Ip, newPost.Anonoimus);
                        }

                        db.SaveChanges();

                        transaction.Commit();

                        return(new PostEntity
                        {
                            Id = newPost.Id,
                            Attachement = newPost.Attachement,
                            Text = newPost.Text,
                            Anonoimus = newPost.Anonoimus,
                            Ip = newPost.Ip,
                            UserId = newPost.UserId,
                            PostInterests = postInterests,
                            PostComments = new List <PostCommentEntity>(),
                            OldUser = null,
                            Rate = null,
                            User = _userService.GetUserById(newPost.UserId, newPost.UserId),
                            Saved = 0,
                            Date = DateTime.Now,
                            CommentsCount = 0,
                            Reported = false,
                            PostLikers = GetPostLikers(newPost.Id, newPost.UserId)
                        });
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();

                        return(null);
                    }
                }
            }
        }
Esempio n. 3
0
        public ChatEntity AddRoom(ChatEntity chat, HttpPostedFileBase file)
        {
            using (var db = new groubel_dbEntities1())
            {
                //  var upload = new IOHandler("~/Uploads/RoomFile/");

                //  var name = upload.Save(file);

                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var newChat = new Chat
                        {
                            Image      = chat.Image,
                            Anonimus   = chat.Anonimus,
                            IsArchive  = false,
                            IsRoom     = chat.IsRoom,
                            Date       = DateTime.Now,
                            MaxUsers   = chat.MaxUsers,
                            Name       = chat.Name,
                            Visibility = chat.Visibility,
                            UserId     = chat.UserId
                        };

                        db.Chats.Add(newChat);

                        db.SaveChanges();

                        var roomInterests = _interestService.GetInterestObjectsByName(chat.Interests.Select(i => i.Name).ToList(), chat.UserId);

                        foreach (var intere in roomInterests)
                        {
                            db.ChatInterests.Add(new ChatInterest
                            {
                                ChatId     = newChat.Id,
                                InterestId = intere.Id
                            });
                        }

                        db.SaveChanges();

                        db.ChatMembers.Add(new ChatMember
                        {
                            UserId   = chat.UserId,
                            RoomId   = newChat.Id,
                            Date     = DateTime.Now,
                            Agree    = true,
                            SenderId = chat.UserId,
                            Status   = 1,
                            Avatar   = "",
                        });

                        foreach (var us in chat.Members)
                        {
                            db.ChatMembers.Add(new ChatMember
                            {
                                UserId   = us.Id,
                                RoomId   = newChat.Id,
                                Date     = DateTime.Now,
                                Agree    = true,
                                SenderId = chat.UserId,
                                Status   = 1,
                                Avatar   = "",
                            });
                        }

                        db.SaveChanges();

                        _notificationService.AddNotification(chat.UserId, 0, NotificationTypeEnum.AddedYouInRoom, newChat.Id, "", null);

                        transaction.Commit();

                        chat.Id        = newChat.Id;
                        chat.Interests = roomInterests;
                        chat.Members   = GetChatMembers(chat.Id, chat.UserId);
                        chat.Comments  = new List <ChatCommentEntity>();

                        return(chat);
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();

                        return(null);
                    }
                }
            }
        }