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;
}
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;
}
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;
}
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;
}
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));
}
Aggregations