Search in sources :

Example 1 with Module

use of models.Module in project modules.playframework.org by playframework.

the class Categories method categoryDetails.

public static Result categoryDetails(String name) {
    Category category = Category.findByName(name);
    List<Module> modules = Module.findByCategory(category);
    return ok(categoryDetails.render(currentUser(), category, modules));
}
Also used : Category(models.Category) Module(models.Module)

Example 2 with Module

use of models.Module in project modules.playframework.org by playframework.

the class Modules method details.

public static Result details(String moduleKey) {
    Result result;
    final Module module = Module.findByModuleKey(moduleKey);
    if (module == null) {
        result = notFound("Module not found: " + moduleKey);
    } else {
        List<ModuleVersion> moduleVersions = ModuleVersion.findByModule(module);
        User user = currentUser();
        Rate rate = null;
        Vote vote = null;
        if (user != null) {
            rate = CollectionUtils.filterFirst(user.rates, new Filter<Rate>() {

                public boolean isAcceptable(Rate rate) {
                    return rate.playModule.id.equals(module.id);
                }
            });
            vote = CollectionUtils.filterFirst(user.votes, new Filter<Vote>() {

                public boolean isAcceptable(Vote vote) {
                    return vote.playModule.id.equals(module.id);
                }
            });
        }
        Set<PlayVersion.MajorVersion> majorVersions = new HashSet<PlayVersion.MajorVersion>();
        for (ModuleVersion moduleVersion : moduleVersions) {
            majorVersions.addAll(moduleVersion.getMajorVersions());
        }
        Ebean.refresh(module.rating);
        result = ok(moduleDetails.render(user, module, moduleVersions, majorVersions, rate, vote));
    }
    return result;
}
Also used : Vote(models.Vote) CurrentUser.currentUser(actions.CurrentUser.currentUser) CurrentUser(actions.CurrentUser) User(models.User) ModuleVersion(models.ModuleVersion) Filter(utils.Filter) Rate(models.Rate) PlayVersion(models.PlayVersion) Module(models.Module) Result(play.mvc.Result) HashSet(java.util.HashSet)

Example 3 with Module

use of models.Module 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 4 with Module

use of models.Module 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 5 with Module

use of models.Module 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)

Aggregations

Module (models.Module)9 CurrentUser (actions.CurrentUser)6 CurrentUser.currentUser (actions.CurrentUser.currentUser)6 User (models.User)6 Result (play.mvc.Result)6 RoleHolderPresent (be.objectify.deadbolt.actions.RoleHolderPresent)5 Vote (models.Vote)3 VoteResponseForm (forms.modules.VoteResponseForm)2 ModuleVersion (models.ModuleVersion)2 PlayVersion (models.PlayVersion)2 Rate (models.Rate)2 Filter (utils.Filter)2 RatingForm (forms.modules.RatingForm)1 RatingResponseForm (forms.modules.RatingResponseForm)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Category (models.Category)1 Rating (models.Rating)1 Sitemap (models.memory.Sitemap)1