Search in sources :

Example 1 with RoleHolderPresent

use of be.objectify.deadbolt.actions.RoleHolderPresent in project modules.playframework.org by playframework.

the class Modules method voteDown.

@RoleHolderPresent
public static Result voteDown(String moduleKey) {
    Result result;
    final Module module = Module.findByModuleKey(moduleKey);
    if (module == null) {
        result = notFound("Module not found: " + moduleKey);
    } else {
        User user = currentUser();
        Vote vote = CollectionUtils.filterFirst(user.votes, new Filter<Vote>() {

            @Override
            public boolean isAcceptable(Vote vote) {
                return vote.playModule.id.equals(module.id);
            }
        });
        if (vote != null) {
            if (vote.voteType == Vote.VoteType.UP) {
                --module.upVoteCount;
                ++module.downVoteCount;
                vote.voteType = Vote.VoteType.DOWN;
                vote.save();
                module.save();
            } else {
                Logger.info(String.format("User [%s] tried to downvote module [%s] but vote already existed", user.userName, module.key));
            }
        } else {
            vote = new Vote();
            vote.playModule = module;
            vote.voteType = Vote.VoteType.DOWN;
            // support for public voting comes later
            vote.publicVote = false;
            user.votes.add(vote);
            user.save();
            ++module.downVoteCount;
            module.save();
        }
        VoteResponseForm responseForm = new VoteResponseForm();
        responseForm.upVotes = module.upVoteCount;
        responseForm.downVotes = module.downVoteCount;
        result = ok(Json.toJson(responseForm));
    }
    return result;
}
Also used : Vote(models.Vote) CurrentUser.currentUser(actions.CurrentUser.currentUser) CurrentUser(actions.CurrentUser) User(models.User) Module(models.Module) VoteResponseForm(forms.modules.VoteResponseForm) Result(play.mvc.Result) RoleHolderPresent(be.objectify.deadbolt.actions.RoleHolderPresent)

Example 2 with RoleHolderPresent

use of be.objectify.deadbolt.actions.RoleHolderPresent in project modules.playframework.org by playframework.

the class Modules method rate.

// If a user has already rated, then change the rate, don't add a new one
@RoleHolderPresent
public static Result rate(String moduleKey) {
    Result result;
    Form<RatingForm> form = form(RatingForm.class).bindFromRequest();
    if (form.hasErrors()) {
        result = badRequest(form.errorsAsJson());
    } else {
        RatingForm ratingForm = form.get();
        final Module module = Module.findById(ratingForm.id);
        if (module == null) {
            result = badRequest("Module does not exist");
        } else {
            User user = currentUser();
            Rate rate = null;
            if (user != null) {
                // user shouldn't be null because of @RoleHolderPresent
                rate = CollectionUtils.filterFirst(user.rates, new Filter<Rate>() {

                    @Override
                    public boolean isAcceptable(Rate rate) {
                        return rate.playModule.id.equals(module.id);
                    }
                });
                if (rate != null) {
                    module.rating.subtract(rate);
                } else {
                    rate = new Rate();
                    rate.playModule = module;
                    user.rates.add(rate);
                }
                rate.rating = ratingForm.rating;
                module.rating.add(rate);
                module.rating.calculateAverage();
                module.save();
                user.save();
            }
            RatingResponseForm responseForm = new RatingResponseForm();
            responseForm.totalRatings = module.rating.totalRatings();
            responseForm.averageRating = module.rating.averageRating;
            result = ok(Json.toJson(responseForm));
        }
    }
    return result;
}
Also used : RatingResponseForm(forms.modules.RatingResponseForm) CurrentUser.currentUser(actions.CurrentUser.currentUser) CurrentUser(actions.CurrentUser) User(models.User) Filter(utils.Filter) Rate(models.Rate) Module(models.Module) RatingForm(forms.modules.RatingForm) Result(play.mvc.Result) RoleHolderPresent(be.objectify.deadbolt.actions.RoleHolderPresent)

Example 3 with RoleHolderPresent

use of be.objectify.deadbolt.actions.RoleHolderPresent in project modules.playframework.org by playframework.

the class Modules method voteUp.

@RoleHolderPresent
public static Result voteUp(String moduleKey) {
    Result result;
    final Module module = Module.findByModuleKey(moduleKey);
    if (module == null) {
        result = notFound("Module not found: " + moduleKey);
    } else {
        User user = currentUser();
        Vote vote = CollectionUtils.filterFirst(user.votes, new Filter<Vote>() {

            @Override
            public boolean isAcceptable(Vote vote) {
                return vote.playModule.id.equals(module.id);
            }
        });
        if (vote != null) {
            if (vote.voteType == Vote.VoteType.DOWN) {
                --module.downVoteCount;
                ++module.upVoteCount;
                vote.voteType = Vote.VoteType.UP;
                vote.save();
                module.save();
            } else {
                Logger.info(String.format("User [%s] tried to upvote module [%s] but vote already existed", user.userName, module.key));
            }
        } else {
            vote = new Vote();
            vote.playModule = module;
            vote.voteType = Vote.VoteType.UP;
            // support for public voting comes later
            vote.publicVote = false;
            user.votes.add(vote);
            user.save();
            ++module.upVoteCount;
            module.save();
        }
        VoteResponseForm responseForm = new VoteResponseForm();
        responseForm.upVotes = module.upVoteCount;
        responseForm.downVotes = module.downVoteCount;
        result = ok(Json.toJson(responseForm));
    }
    return result;
}
Also used : Vote(models.Vote) CurrentUser.currentUser(actions.CurrentUser.currentUser) CurrentUser(actions.CurrentUser) User(models.User) Module(models.Module) VoteResponseForm(forms.modules.VoteResponseForm) Result(play.mvc.Result) RoleHolderPresent(be.objectify.deadbolt.actions.RoleHolderPresent)

Example 4 with RoleHolderPresent

use of be.objectify.deadbolt.actions.RoleHolderPresent in project modules.playframework.org by playframework.

the class Modules method submitModuleRegistrationForm.

@RoleHolderPresent
public static Result submitModuleRegistrationForm() {
    Form<Module> form = form(Module.class).bindFromRequest();
    Result result;
    User user = currentUser();
    if (form.hasErrors()) {
        result = badRequest(moduleRegistrationForm.render(user, form));
    } else {
        Module module = form.get();
        module.owner = user;
        module.rating = new Rating(true);
        module.save();
        createHistoricalEvent("New module - " + module.name, String.format("%s (%s) created a new module - %s", user.displayName, user.userName, module.name));
        result = redirect(routes.Modules.myModules());
    }
    return result;
}
Also used : CurrentUser.currentUser(actions.CurrentUser.currentUser) CurrentUser(actions.CurrentUser) User(models.User) Rating(models.Rating) Module(models.Module) Result(play.mvc.Result) RoleHolderPresent(be.objectify.deadbolt.actions.RoleHolderPresent)

Example 5 with RoleHolderPresent

use of be.objectify.deadbolt.actions.RoleHolderPresent in project modules.playframework.org by playframework.

the class Modules method showVersionManagement.

@RoleHolderPresent
public static Result showVersionManagement(String moduleKey) {
    Form<ModuleVersion> form = form(ModuleVersion.class);
    Module module = Module.findByModuleKey(moduleKey);
    return ok(manageVersionsForm.render(currentUser(), module, PlayVersion.getAll(), form));
}
Also used : ModuleVersion(models.ModuleVersion) Module(models.Module) RoleHolderPresent(be.objectify.deadbolt.actions.RoleHolderPresent)

Aggregations

RoleHolderPresent (be.objectify.deadbolt.actions.RoleHolderPresent)6 CurrentUser (actions.CurrentUser)5 CurrentUser.currentUser (actions.CurrentUser.currentUser)5 Module (models.Module)5 User (models.User)5 Result (play.mvc.Result)5 VoteResponseForm (forms.modules.VoteResponseForm)2 ModuleVersion (models.ModuleVersion)2 Vote (models.Vote)2 RatingForm (forms.modules.RatingForm)1 RatingResponseForm (forms.modules.RatingResponseForm)1 Date (java.util.Date)1 BinaryContent (models.BinaryContent)1 Rate (models.Rate)1 Rating (models.Rating)1 Filter (utils.Filter)1