Beispiel #1
0
        public virtual JsonResult FavouritePost(EntityIdViewModel viewModel)
        {
            var returnValue = new FavouriteJsonReturnModel();

            User.GetMembershipUser(MembershipService);
            if (Request.IsAjaxRequest() && LoggedOnReadOnlyUser != null)
            {
                try
                {
                    var post  = _postService.Get(viewModel.Id);
                    var topic = _topicService.Get(post.Topic.Id);

                    // See if this is a user adding or removing the favourite
                    Guid?user;
                    if (LoggedOnReadOnlyUser == null)
                    {
                        user = null;
                    }
                    else
                    {
                        user = LoggedOnReadOnlyUser.Id;
                    }
                    ;
                    var loggedOnUser      = MembershipService.GetUser(user);
                    var existingFavourite = _favouriteService.GetByMemberAndPost(loggedOnUser.Id, post.Id);
                    if (existingFavourite != null)
                    {
                        _favouriteService.Delete(existingFavourite);
                        returnValue.Message = LocalizationService.GetResourceString("Post.Favourite");
                    }
                    else
                    {
                        var favourite = new Favourite
                        {
                            DateCreated = DateTime.UtcNow,
                            Member      = loggedOnUser,
                            Post        = post,
                            Topic       = topic
                        };
                        _favouriteService.Add(favourite);
                        returnValue.Message = LocalizationService.GetResourceString("Post.Favourited");
                        returnValue.Id      = favourite.Id;
                    }

                    Context.SaveChanges();
                    return(Json(returnValue, JsonRequestBehavior.DenyGet));
                }
                catch (Exception ex)
                {
                    Context.RollBack();
                    LoggingService.Error(ex);
                    throw new Exception(LocalizationService.GetResourceString("Errors.Generic"));
                }
            }
            return(Json(returnValue));
        }
Beispiel #2
0
        public JsonResult FavouritePost(FavouritePostViewModel viewModel)
        {
            var returnValue = new FavouriteJsonReturnModel();

            if (Request.IsAjaxRequest() && LoggedOnReadOnlyUser != null)
            {
                using (var unitOfwork = UnitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        var post  = _postService.Get(viewModel.PostId);
                        var topic = _topicService.Get(post.Topic.Id);

                        // See if this is a user adding or removing the favourite
                        var loggedOnUser      = MembershipService.GetUser(LoggedOnReadOnlyUser.Id);
                        var existingFavourite = _favouriteService.GetByMemberAndPost(loggedOnUser.Id, post.Id);
                        if (existingFavourite != null)
                        {
                            _favouriteService.Delete(existingFavourite);
                            returnValue.Message = LocalizationService.GetResourceString("Post.Favourite");
                        }
                        else
                        {
                            var favourite = new Favourite
                            {
                                DateCreated = DateTime.UtcNow,
                                Member      = loggedOnUser,
                                Post        = post,
                                Topic       = topic
                            };
                            _favouriteService.Add(favourite);
                            returnValue.Message = LocalizationService.GetResourceString("Post.Favourited");
                            returnValue.Id      = favourite.Id;
                        }

                        unitOfwork.Commit();
                        return(Json(returnValue, JsonRequestBehavior.DenyGet));
                    }
                    catch (Exception ex)
                    {
                        unitOfwork.Rollback();
                        LoggingService.Error(ex);
                        throw new Exception(LocalizationService.GetResourceString("Errors.Generic"));
                    }
                }
            }
            return(Json(returnValue));
        }