コード例 #1
0
        public ActionResult Promote(string id)
        {
            id = id.NullSafe();

            JsonViewData viewData = Validate <JsonViewData>(
                new Validation(() => string.IsNullOrEmpty(id), "Identyfikator artyku³u nie mo¿e byæ pusty."),
                new Validation(() => id.ToGuid().IsEmpty(), "Niepoprawny identyfikator artyku³u."),
                new Validation(() => !IsCurrentUserAuthenticated, "Nie jesteœ zalogowany.")
                );

            if (viewData == null)
            {
                try
                {
                    using (IUnitOfWork unitOfWork = UnitOfWork.Get())
                    {
                        IStory story = _storyRepository.FindById(id.ToGuid());

                        if (story == null)
                        {
                            viewData = new JsonViewData {
                                errorMessage = "Podany artyku³ nie istnieje."
                            };
                        }
                        else
                        {
                            if (!story.CanPromote(CurrentUser))
                            {
                                viewData = story.HasPromoted(CurrentUser) ?
                                           new JsonViewData {
                                    errorMessage = "Ju¿ wypromowa³eœ ten artyku³."
                                } :
                                new JsonViewData {
                                    errorMessage = "Nie mo¿esz promowaæ tego artyku³u."
                                };
                            }
                            else
                            {
                                _storyService.Promote(story, CurrentUser, CurrentUserIPAddress);
                                unitOfWork.Commit();

                                viewData = new JsonVoteViewData {
                                    isSuccessful = true, votes = story.VoteCount, text = GetText(story.VoteCount)
                                };
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Exception(e);

                    viewData = new JsonViewData {
                        errorMessage = FormatStrings.UnknownError.FormatWith("promowaniu artyku³u")
                    };
                }
            }

            return(Json(viewData));
        }
コード例 #2
0
ファイル: StoryController.cs プロジェクト: aoki1210/kigg
        public ActionResult Promote(string id)
        {
            id = id.NullSafe();

            JsonViewData viewData = Validate <JsonViewData>(
                new Validation(() => string.IsNullOrEmpty(id), "Story identifier cannot be blank."),
                new Validation(() => id.ToGuid().IsEmpty(), "Invalid story identifier."),
                new Validation(() => !IsCurrentUserAuthenticated, "You are currently not authenticated.")
                );

            if (viewData == null)
            {
                try
                {
                    IStory story = _storyRepository.FindById(id.ToGuid());

                    if (story == null)
                    {
                        viewData = new JsonViewData {
                            errorMessage = "Specified story does not exist."
                        };
                    }
                    else
                    {
                        if (!story.CanPromote(CurrentUser))
                        {
                            viewData = story.HasPromoted(CurrentUser) ?
                                       new JsonViewData {
                                errorMessage = "You have already promoted this story."
                            } :
                            new JsonViewData {
                                errorMessage = "You are not allowed to promote this story."
                            };
                        }
                        else
                        {
                            _storyService.Promote(story, CurrentUser, CurrentUserIPAddress);

                            viewData = new JsonVoteViewData {
                                isSuccessful = true, votes = story.VoteCount
                            };
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Exception(e);

                    viewData = new JsonViewData {
                        errorMessage = FormatStrings.UnknownError.FormatWith("promoting story")
                    };
                }
            }

            return(Json(viewData));
        }