public JsonResult JoinChallenge(int contentId) { bool result = true; SessionCustom.Begin(); ChaellengeFollowerRepository follower = new ChaellengeFollowerRepository(SessionCustom); follower.Entity.ChallengeId = contentId; follower.Entity.UserId = ((CustomPrincipal)User).UserId; follower.Entity.Date = DateTime.Now; follower.Insert(); follower.Entity = new Domain.Entities.ChaellengeFollower(); follower.Entity.ChallengeId = contentId; int total = follower.GetAll().Count; ChallengeRepository challenge = new ChallengeRepository(SessionCustom); challenge.Entity.ContentId = contentId; challenge.Entity.Followers = total; challenge.Update(); SessionCustom.Commit(); Business.UserRelation.SaveRelationAction(((CustomPrincipal)User).UserId, null, contentId, "follow", this.SessionCustom); return(this.Json(new { result = result })); }
/// <summary> /// Bind the context and the session with the content /// </summary> /// <param name="context">Context page</param> /// <param name="session">Session object</param> /// <param name="id">Content ID</param> /// <param name="userId">current user ID</param> public void Bind(HttpContextBase context, ISession session, int?id, int?userId, int?LanguageId) { ContentRepository contentrepository = new ContentRepository(session); if (contentrepository.Entity != null) { ChallengeRepository challengepository = new ChallengeRepository(session); FileattachRepository file = new FileattachRepository(session); IdeaRepository idea = new IdeaRepository(session); contentrepository.Entity.ContentId = id; contentrepository.LoadByKey(); if (contentrepository.Entity.Frienlyname != null) { file.Entity.ContentId = challengepository.Entity.ContentId = contentrepository.Entity.ContentId; challengepository.LoadByKey(); this.ObjContent = contentrepository.Entity; this.ObjChallenge = challengepository.Entity; this.CollFiles = file.GetAll(); } contentrepository.Entity = new Content(); contentrepository.Entity.ContentId = this.ObjContent.ContentId; contentrepository.Entity.SectionId = this.ObjContent.SectionId; if (userId.HasValue) { ChaellengeFollowerRepository follower = new ChaellengeFollowerRepository(session); follower.Entity.UserId = userId; follower.Entity.ChallengeId = id; if (follower.GetAll().Count > 0) { this.JoinedChallenge = true; } } if (id.HasValue) { int totalCount = 0; CommentRepository comment = new CommentRepository(session); BlogEntryRepository blog = new BlogEntryRepository(session); idea.Entity.ContentId = blog.Entity.ContentId = id.Value; this.CollIdeas = idea.IdeasPaging(1, 6, out totalCount, id.Value, userId); this.IdeasCount = totalCount; foreach (IdeasPaging item in this.CollIdeas) { item.CollComment = comment.CommentsPaging(1, 3, out totalCount, item.IdeaId.Value); this.CommentsCount = totalCount; } this.BlogEntries = blog.BlogContentEntriesPaging(1, 6, out totalCount); this.BlogEntriesCount = totalCount; foreach (BlogEntriesPaging blogEntry in this.BlogEntries) { blogEntry.CollComment = comment.CommentsPagingContent(1, 3, out totalCount, blogEntry.ContentId.Value); this.CommentsCount = totalCount; } this.TopIdeas = idea.TopIdeas(10); this.Statistics = contentrepository.GetContentStatistics(id.Value); } } this.CollContent = contentrepository.GetNewsRelationFrontEnd(); }
/// <summary> /// prepare the user relation /// </summary> /// <param name="userId">user id</param> /// <param name="userRelationId">user relation id</param> /// <param name="relationId">relation id</param> /// <param name="action">text action</param> /// <param name="session">SQL session</param> /// <returns>true if the relation was created false if not</returns> private static bool AsyncSaveRelationAction(int userId, int?userRelationId, int relationId, string action, ISession session) { bool result = false; ////session.Begin(); try { UserRelationRepository relation = new UserRelationRepository(session); IdeaRepository ideaRepository = new IdeaRepository(session); IdeaVoteRepository ideaVoteRepository = new IdeaVoteRepository(session); CommentRepository commentRepository = new CommentRepository(session); ChaellengeFollowerRepository followerRepository = new ChaellengeFollowerRepository(session); AnswerRepository answerRepository = new AnswerRepository(session); UserRepository userRepository = new UserRepository(session); ContentRepository contentRepository = new ContentRepository(session); int contentId = 0; switch (action) { case "idea": ideaRepository.Entity.IdeaId = relationId; ideaRepository.LoadByKey(); if (ideaRepository.Entity.ContentId.HasValue) { contentId = ideaRepository.Entity.ContentId.Value; } break; case "comment": commentRepository.Entity.CommentId = relationId; commentRepository.LoadByKey(); if (commentRepository.Entity.IdeaId.HasValue && userRelationId.HasValue) { ideaRepository.Entity.IdeaId = commentRepository.Entity.IdeaId; ideaRepository.LoadByKey(); if (ideaRepository.Entity.ContentId.HasValue) { contentId = ideaRepository.Entity.ContentId.Value; } InsertAction(userId, userRelationId.Value, relationId, action, 3, session); InsertAction(userRelationId.Value, userId, relationId, action, 3, session); } break; case "like": ideaRepository.Entity.IdeaId = relationId; ideaRepository.LoadByKey(); if (ideaRepository.Entity.ContentId.HasValue) { InsertAction(userId, userRelationId.Value, relationId, action, 3, session); InsertAction(userRelationId.Value, userId, relationId, action, 3, session); contentId = ideaRepository.Entity.ContentId.Value; } break; case "follow": contentId = relationId; break; case "vote": object tempId = contentRepository.GetContentIdByAnswerId(relationId); if (tempId != null && int.TryParse(tempId.ToString(), out contentId)) { List <Domain.Entities.User> same = userRepository.UserAnswerByAnswerId(relationId, userId); foreach (Domain.Entities.User user in same) { InsertAction(userId, user.UserId.Value, relationId, action, 3, session); InsertAction(user.UserId.Value, userId, relationId, action, 3, session); } } else { contentId = 0; } break; } if (contentId != 0) { // ideas ideaRepository.Entity = new Domain.Entities.Idea(); ideaRepository.Entity.ContentId = contentId; List <Domain.Entities.Idea> ideas = ideaRepository.GetAll(); IEnumerable <IGrouping <int?, Domain.Entities.Idea> > usersIdeas = ideas.Where(i => i.UserId != userId && i.Active == true).GroupBy(g => g.UserId); foreach (IGrouping <int?, Domain.Entities.Idea> userIdeas in usersIdeas) { InsertAction(userId, userIdeas.Key.Value, relationId, action, 1, session); foreach (Domain.Entities.Idea idea in userIdeas) { InsertAction(userIdeas.Key.Value, userId, idea.IdeaId.Value, "idea", 1, session); } } // comentarios commentRepository.Entity = new Domain.Entities.Comment(); commentRepository.Entity.ContentId = contentId; List <Domain.Entities.Comment> comments = commentRepository.GetAll(); IEnumerable <IGrouping <int?, Domain.Entities.Comment> > usersComments = comments.Where(c => c.UserId != userId && c.Active == true).GroupBy(g => g.UserId); foreach (IGrouping <int?, Domain.Entities.Comment> userComments in usersComments) { InsertAction(userId, userComments.Key.Value, relationId, action, 1, session); foreach (Domain.Entities.Comment comment in userComments) { InsertAction(userComments.Key.Value, userId, comment.CommentId.Value, "comment", 1, session); } } // likes - new sp needed List <Domain.Entities.User> likes = userRepository.UserLikeByContentId(contentId, userId); foreach (Domain.Entities.User like in likes) { InsertAction(userId, like.UserId.Value, relationId, action, 1, session); InsertAction(like.UserId.Value, userId, like.Age.Value, "like", 1, session); } // followers followerRepository.Entity = new Domain.Entities.ChaellengeFollower(); followerRepository.Entity.ChallengeId = contentId; List <Domain.Entities.ChaellengeFollower> followers = followerRepository.GetAll(); foreach (Domain.Entities.ChaellengeFollower follower in followers) { InsertAction(userId, follower.UserId.Value, relationId, action, 1, session); InsertAction(follower.UserId.Value, userId, contentId, "follow", 1, session); } // votes - new sp needed => answerId is int the age variable for this action List <Domain.Entities.User> answers = userRepository.UserAnswerByContentId(contentId, userId); foreach (Domain.Entities.User answer in answers) { InsertAction(userId, answer.UserId.Value, relationId, action, 1, session); InsertAction(answer.UserId.Value, userId, answer.Age.Value, "vote", 1, session); } result = true; } else { result = false; } } catch { } finally { if (result) { ////session.Commit(); } else { ////session.RollBack(); } } return(result); }