public CommentModel Insert(string currentUsername, CommentModel input) { UserModel user = _usersService.GetByUsername(currentUsername); var entity = _mapper.Map <CommentModel, Comment>(input, opt => opt.AfterMap((src, dest) => { dest.VotingBox = new VotingBox(); })); _dbContext.Comments.Add(entity); _dbContext.SaveChanges(); _dbContext.Entry(entity).Reference(c => c.User).Load(); // automatic upvote from the poster _upvotesService.Vote(user.Username, entity.Id, this, false); _dbContext.Entry(entity.VotingBox).Collection(c => c.Upvotes).Load(); return(MapToDto(currentUsername)(entity)); }
public void Vote(string username, string upvotableEntityId, IUpvotablesService entityService, bool isDownvote) { IUpvotableEntity upvotable = entityService.ResolveUpvotableEntity(upvotableEntityId); var user = _usersService.GetByUsername(username); if (user == null) { return; } int voteWeight = isDownvote ? -1 : 1; Upvote entity = new Upvote(); entity.UserId = user.UserId; entity.Weight = voteWeight; var votes = _dbContext.Upvotes.Where(uv => uv.VotingBoxId == upvotable.VotingBoxId && uv.UserId == user.UserId).ToList(); if (votes == null) { entity.VotingBoxId = upvotable.VotingBoxId; _dbContext.Upvotes.Add(entity); _dbContext.SaveChanges(); return; } if (votes != null && votes.Count == 1 && votes.First().Weight == voteWeight) { return; } else { _dbContext.RemoveRange(votes); entity.VotingBoxId = upvotable.VotingBoxId; _dbContext.Upvotes.Add(entity); _dbContext.SaveChanges(); } }
public SubmissionModel Insert(string currentUsername, SubmissionModel input) { var user = _usersService.GetByUsername(currentUsername); var entity = _mapper.Map <SubmissionModel, Submission>(input, opt => opt.AfterMap((src, dest) => { dest.CreatedAtUtc = DateTime.UtcNow; dest.UserId = user.UserId; dest.CreatedAtUtc = DateTime.UtcNow; dest.CreatedBy = user.UserId; dest.ModifiedAtUtc = DateTime.UtcNow; dest.ModifiedBy = user.UserId; dest.VotingBox = new VotingBox(); })); _dbContext.Submissions.Add(entity); _dbContext.SaveChanges(); // automatic upvote from the creator of the thread _upvotesService.Vote(currentUsername, entity.Id, this, false); return(_mapper.Map <SubmissionModel>(entity)); }
public ForumSectionModel Insert(string currentUsername, ForumSectionModel input) { var user = _usersService.GetByUsername(currentUsername); var entity = _mapper.Map <ForumSectionModel, ForumSection>(input, opt => opt.AfterMap((src, dest) => { dest.CreatedAtUtc = DateTime.UtcNow; dest.CreatedAtUtc = DateTime.UtcNow; dest.CreatedBy = user.UserId; dest.ModifiedAtUtc = DateTime.UtcNow; dest.ModifiedBy = user.UserId; })); _dbContext.ForumSections.Add(entity); _dbContext.SaveChanges(); return(_mapper.Map <ForumSectionModel>(entity)); }