public void Vote(ClaimsPrincipal user, long vorfallId, VoteType?type) { var userId = _accountService.GetIdOfUser(user); if (!_context.Vorfall.Any(n => n.Id == vorfallId)) { throw new AffairNotFoundException(); } if (type.HasValue) { var dbObj = _context.Vote.SingleOrDefault(n => n.UserId == userId && n.VorfallId == vorfallId); if (dbObj == null) { _context.Add(new db.Affair.Vote { UserId = userId, VorfallId = vorfallId, VoteType = _mapper.Map <db.Affair.VoteType>(type), }); } else { dbObj.VoteType = _mapper.Map <db.Affair.VoteType>(type); } } else { _context.Remove(_context.Vote.Single(n => n.UserId == userId && n.VorfallId == vorfallId)); } _context.SaveChanges(); }
public void Like(ClaimsPrincipal user, long postId, LikeType?type) { var userId = _accountService.GetIdOfUser(user); if (!_context.Post.Any(n => n.Id == postId)) { throw new PostNotFoundException(); } if (type.HasValue) { var dbObj = _context.Like.SingleOrDefault(n => n.UserId == userId && n.PostId == postId); if (dbObj == null) { _context.Add(new db.Like { UserId = userId, PostId = postId, LikeType = _mapper.Map <db.LikeType>(type), }); } else { dbObj.LikeType = _mapper.Map <db.LikeType>(type); } } else { _context.Remove(_context.Like.Single(n => n.UserId == userId && n.PostId == postId)); } _context.SaveChanges(); }