public CommentEditReturnModel EditComment(int commentId, string content, string currUserId) { CommentEditReturnModel ret = new CommentEditReturnModel(); Comment cmd = _dbSet.FirstOrDefault(p => p.Id == commentId && p.AuthorId == currUserId); if (cmd == null) { ret.IsActionSucceed = false; return(ret); } else { cmd.Content = content; _dbSet.Update(cmd); if (_context.SaveChanges() != 0) { // return necessary information ret.Comment = new ReviewCommentDTO() { Id = cmd.Id, Content = TagHelpers.RemoveUnwantedTags(cmd.Content) }; ret.IsActionSucceed = true; } return(ret); } }
public CommentAddReturnModel PublishComment(string content, string currUserId, int reviewId = -1) { CommentAddReturnModel ret = new CommentAddReturnModel(); if (reviewId == -1) { ret.IsActionSucceed = false; return(ret); } Review review = _context.Set <Review>().FirstOrDefault(f => f.Id == reviewId); if (review == null) { ret.IsActionSucceed = false; return(ret); } ret.PostId = review.PostId.Value; ret.ReviewAuthorId = review.UserId; Comment comment = new Comment() { DateUtcAdd = DateTime.UtcNow, DateUtcModified = DateTime.UtcNow, ReviewId = reviewId, AuthorId = currUserId, Content = TagHelpers.RemoveUnwantedTags(content), }; _dbSet.Add(comment); if (_context.SaveChanges() != 0) { ret.Comment = new ReviewCommentDTO { Id = comment.Id, DateUtcPublished = comment.DateUtcAdd, Content = comment.Content, DislikeCount = 0, LikeCount = 0, ReviewId = reviewId, UserInfo = new UserInfoExtraSmall() { AppUserId = currUserId, ProfileImage = _userImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", currUserId) }, LikeStatus = LikeStatus.None, AuthorId = currUserId }; ret.IsActionSucceed = true; return(ret); } ret.IsActionSucceed = false; return(ret); }
public BaseReturnModel SaveUserSettings(ProfileSettingsPostViewModel model, string currUserId) { BaseReturnModel ret = new BaseReturnModel(); UserInfo uInfo = _dbSet.FirstOrDefault(p => p.AppUserId == currUserId); uInfo.PrivacySetting = (UserPrivacySetting)model.SelectedPrivacySettings; uInfo.FollowSetting = (UserFollowSetting)model.SelectedFollowSettings; uInfo.Name = TagHelpers.RemoveUnwantedTags(model.Name); uInfo.Status = TagHelpers.RemoveUnwantedTags(model.Status); // uInfo.UName = model.UName; uInfo.Surname = TagHelpers.RemoveUnwantedTags(model.Surname); _dbSet.Update(uInfo); if (!(_entityContext.SaveChanges() == 0)) { ret.IsActionSucceed = true; } return(ret); }
public SaveEditingPostReturnModel SaveEditingPost(SaveEditingPostParameters model, string currUserId) { SaveEditingPostReturnModel ret = new SaveEditingPostReturnModel() { IsActionSucceed = false, Errors = new Dictionary <string, string>() }; Post post = _postSet.Include(p => p.PostParts).Include(p => p.Groups).Include(p => p.UserInfo).FirstOrDefault(p => p.Id == model.Id && p.UserInfoId == currUserId); if (post == null) { return(ret); } post.Title = TagHelpers.RemoveUnwantedTags(model.Title); post.DateUtcModified = DateTime.UtcNow; post.Description = model.Description; post.IsPublished = true; post.Content = TagHelpers.RemoveUnwantedTags(model.Content); if (post.PostParts.Count < 1) { ret.Errors.Add("custom", "Please add your pictures"); return(ret); } if (model.SelectedCollectionId.HasValue) { if (model.SelectedCollectionId > 0) { PostCollection pcol = _postCollectionDbSet.FirstOrDefault(p => p.Id == model.SelectedCollectionId && p.UserInfoId == currUserId); if (pcol == null) { ret.Errors.Add("custom", "That collection does not belongs to you buddy"); return(ret); } post.Collection = pcol; } } if (model.SelectedInterestIds.Length < 1) { ret.Errors.Add("custom", "Please add at least one interest"); return(ret); } List <Group> groups = _groupDbSet.Where(p => model.SelectedInterestIds.Contains(p.Id)).ToList(); int[] groupIds = groups.Select(p => p.Id).ToArray(); int[] prevGroupIds = post.Groups.Select(f => f.GroupId).ToArray(); int[] alreadyFollowIds = _userGroupDbSet .Where(p => p.GroupFollowState == GroupFollowState.Followed && p.UserId == currUserId && groupIds.Contains(p.GroupId)).Select(p => p.GroupId).ToArray(); foreach (var gId in prevGroupIds) { if (!groupIds.Contains(gId)) { post.Groups.Remove(post.Groups.FirstOrDefault(p => p.GroupId == gId)); } } foreach (var item in groupIds) { if (!post.Groups.Any(f => f.GroupId == item)) { post.Groups.Add(new GroupPost() { GroupId = item, DateUtcAdded = DateTime.UtcNow, PostPopularityLevel = 0, }); } } IEnumerable <Group> unFolloweds = groups.Where(p => !alreadyFollowIds.Contains(p.Id)); foreach (var unfollowedGroup in unFolloweds) { UserGroup ug = _userGroupDbSet.FirstOrDefault(p => p.GroupId == unfollowedGroup.Id && p.UserId == currUserId); if (ug == null) { ug = new UserGroup() { DateUtcFollowed = DateTime.UtcNow, Group = unfollowedGroup, UserInfo = post.UserInfo, GroupFollowState = GroupFollowState.Followed, UserReputationInGroup = 0 }; _userGroupDbSet.Add(ug); } else { ug.GroupFollowState = GroupFollowState.Followed; _userGroupDbSet.Update(ug); } } _postSet.Update(post); if (_context.SaveChanges() < 1) { // if ef-sql exception ret.IsActionSucceed = false; ret.Errors.Add("custom", "Oops.. Something bad happened. Try again later"); } else { // reset group-post caches foreach (var item in groupIds) { var postIdRels = _groupCacheService.GetPostRelationships(item); if (postIdRels != null) { if (postIdRels.Count() > 0) { // Remove Deleted Interests from cache if (prevGroupIds != null) { postIdRels = postIdRels.Where(p => !prevGroupIds.Contains(p.GroupId)).ToArray(); } postIdRels = postIdRels.Append(new GroupPost() { DateUtcAdded = DateTime.UtcNow, GroupId = item, PostId = post.Id }).ToArray(); _groupCacheService.SetPostRelationships(item, postIdRels, 30); } } } // set return to succeed ret.IsActionSucceed = true; ret.PublishedPostId = post.Id; } return(ret); }
/// <summary> /// Add review to post /// </summary> /// <param name="postId">PostId</param> /// <param name="rating">Assigned Review Rate</param> /// <param name="content">Content of review</param> /// <param name="currUserId">Current User Id from Claims</param> /// <returns> AddReviewReturnModel which contains information needed for return to user and publish to eventbus</returns> public AddReviewReturnModel AddReview(int postId, double rating, string content, string currUserId, string currUsername) { AddReviewReturnModel ret = new AddReviewReturnModel(); Post post = _postSet.Include(p => p.Reviews).FirstOrDefault(p => p.Id == postId); UserInfo reviewer = _context.Set <UserInfo>().FirstOrDefault(p => p.AppUserId == currUserId); UserInfo reviewed = _context.Set <UserInfo>().FirstOrDefault(p => p.AppUserId == post.UserInfoId); if (post.UserInfoId == currUserId) { ret.IsActionSucceed = false; ret.ErrorInformation.UserInformation = "You cannot review your own post"; //TODO: UNCOMMENT THIS LINE return ret; } bool isPreReviewed = _reviewSet.Any(p => p.PostId == postId && p.UserId == currUserId); // Check if user already reviewed this post // TODO: remove !isPreReviewed if (isPreReviewed && !isPreReviewed) { ret.IsActionSucceed = false; ret.ErrorInformation.UserInformation = "You can only have one review each post"; return(ret); } var reviewerReputation = GetUserReputation(reviewer.AppUserId, int.MaxValue); Review newReview = new Review() { Content = TagHelpers.RemoveUnwantedTags(content), DateUtcPublished = DateTime.UtcNow, PostId = postId, UserId = currUserId, PostRate = rating, UserReputation = reviewerReputation }; _reviewSet.Add(newReview); if (_context.SaveChanges() != 0) { // Reputation for adding review ReputationGain rg = new ReputationGain() { DateUtcAdd = DateTime.UtcNow, DateUtcModified = DateTime.UtcNow, GainedReputationValue = _reputationSettings.InitialReviewReputationValue, UserInfoId = currUserId, PostId = postId, ReviewId = newReview.Id, }; // Reputatin gain of post owner ReputationGain rgofPost = new ReputationGain() { DateUtcAdd = DateTime.UtcNow, DateUtcModified = DateTime.UtcNow, GainedReputationValue = rating, UserInfoId = post.UserInfoId, PostId = postId, ReviewId = newReview.Id, }; _reputationSet.Add(rg); _reputationSet.Add(rgofPost); _postSet.Update(post); ret.PostAuthorId = post.UserInfoId; // Remove post rate to calculate when needed _postCacheService.RemovePostRateCache(post.Id); _context.SaveChanges(); ret.IsActionSucceed = true; ret.Review = new ReviewActivityEntity() { DateUtcPublished = newReview.DateUtcPublished, DislikeCount = 0, Id = newReview.Id, Content = newReview.Content, LikeCount = 0, PostId = newReview.PostId.Value, PostRate = newReview.PostRate.Value, CommentCount = 0, LikeStatus = LikeStatus.None, AuthorInfo = new BaseUserInfoDisplay() { AppUserId = currUserId, ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", currUserId), Username = currUsername } }; return(ret); } ret.IsActionSucceed = false; return(ret); }