public async Task <bool> SetReaction(SetReactionViewModel reactionViewModel, Guid userId) { var eventModel = await _unitOfWork.EventRepository.GetEventWithReactions(reactionViewModel.EventId); var userReacted = await _unitOfWork.EventReactionRepository.GetUserReactionAsync(userId, reactionViewModel.EventId); if (eventModel == null) { throw new Exception("Reaction set on non existent event"); } if (userReacted != null) { var isOwner = await _unitOfWork.UserRepository.IsUserReactionOwner(userId, reactionViewModel.EventId); if (isOwner) { await UpdateReaction(reactionViewModel, userReacted, eventModel); } return(true); } await CreateReaction(reactionViewModel, eventModel, userId); return(false); }
private async Task <EventReaction> UpdateReaction(SetReactionViewModel reactionViewModel, EventReaction userReaction, Event _event) { if (userReaction.Type == reactionViewModel.ReactionType) { _unitOfWork.EventReactionRepository.RemoveReaction(userReaction); return(null); } userReaction.Type = reactionViewModel.ReactionType; return(userReaction); }
public async Task <JsonResult> SetReaction(SetReactionViewModel reactionViewModel) { try { return(Json(await _eventsFacade.SetReaction(reactionViewModel))); } catch (Exception e) { Response.StatusCode = 500; return(Json(e.Message + e.InnerException)); } }
private async Task <EventReaction> CreateReaction(SetReactionViewModel reactionViewModel, Event _event, Guid userId) { var reaction = new EventReaction() { EventId = reactionViewModel.EventId, Type = reactionViewModel.ReactionType, UserId = userId }; _event.Reactions.Add(reaction); return(reaction); }
public async Task <SortedReactionsViewModel> SetReaction(SetReactionViewModel reactionViewModel) { var userId = _httpContextAccessor.HttpContext.User.GetClaim(JwtTypes.jti); var user = await _userService.GetUserById(userId); var userRoles = await _userService.GetUserRoles(user); var hasPreviouslyReacted = await _reactionService.SetReaction(reactionViewModel, user.Id); var _newEventReactions = await _eventService.GetEventReactionsWAuthor(reactionViewModel.EventId); var targetList = new List <Guid>() { _newEventReactions.Id }; if (!hasPreviouslyReacted) { await _notificationService.CreateNotificationsAsync($"{user.UserName} zareagował na twoje wydarzenie! {_newEventReactions.EventName}" + $"{reactionViewModel.ReactionType}", targetList, reactionViewModel.EventId, _newEventReactions.EventSlug); } await _reactionService.Commit(); return(new SortedReactionsViewModel(_newEventReactions.Reactions)); }