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); } } } }
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); } } } }