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