use of com.erudika.scoold.ScooldServer.TAGSLINK in project scoold by Erudika.
the class TagsController method delete.
@PostMapping("/delete")
public String delete(@RequestParam String tag, HttpServletRequest req, HttpServletResponse res) {
Profile authUser = utils.getAuthUser(req);
if (utils.isMod(authUser)) {
Tag t = pc.read(Utils.type(Tag.class), new Tag(tag).getId());
if (t != null) {
pc.delete(t);
logger.info("User {} ({}) deleted tag '{}'.", authUser.getName(), authUser.getCreatorid(), t.getTag());
Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
List<Question> questionslist;
do {
questionslist = pc.findTagged(Utils.type(Question.class), new String[] { t.getTag() }, pager);
for (Question q : questionslist) {
t.setCount(t.getCount() + 1);
q.setTags(Optional.ofNullable(q.getTags()).orElse(Collections.emptyList()).stream().filter(ts -> !ts.equals(t.getTag())).distinct().collect(Collectors.toList()));
logger.debug("Removed tag {} from {} out of {} questions.", t.getTag(), questionslist.size(), pager.getCount());
}
if (!questionslist.isEmpty()) {
pc.updateAll(questionslist);
}
} while (!questionslist.isEmpty());
}
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
return "blank";
} else {
return "redirect:" + TAGSLINK + "?" + req.getQueryString();
}
}
use of com.erudika.scoold.ScooldServer.TAGSLINK in project scoold by Erudika.
the class TagsController method rename.
@PostMapping
public String rename(@RequestParam String tag, @RequestParam String newtag, HttpServletRequest req, HttpServletResponse res, Model model) {
Profile authUser = utils.getAuthUser(req);
int count = 0;
if (utils.isMod(authUser)) {
Tag updated;
Tag oldTag = new Tag(tag);
Tag newTag = new Tag(newtag);
Tag t = pc.read(Utils.type(Tag.class), oldTag.getId());
if (t != null && !oldTag.getTag().equals(newTag.getTag())) {
if (oldTag.getTag().equals(newTag.getTag())) {
t.setCount(pc.getCount(Utils.type(Question.class), Collections.singletonMap(Config._TAGS, oldTag.getTag())).intValue());
updated = pc.update(t);
} else {
pc.delete(t);
t.setId(newtag);
logger.info("User {} ({}) is renaming tag '{}' to '{}'.", authUser.getName(), authUser.getCreatorid(), oldTag.getTag(), t.getTag());
t.setCount(pc.getCount(Utils.type(Question.class), Collections.singletonMap(Config._TAGS, newTag.getTag())).intValue());
Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
List<Question> questionslist;
do {
questionslist = pc.findTagged(Utils.type(Question.class), new String[] { oldTag.getTag() }, pager);
for (Question q : questionslist) {
t.setCount(t.getCount() + 1);
q.setTags(Optional.ofNullable(q.getTags()).orElse(Collections.emptyList()).stream().map(ts -> {
if (ts.equals(newTag.getTag())) {
t.setCount(t.getCount() - 1);
}
return ts.equals(oldTag.getTag()) ? t.getTag() : ts;
}).distinct().collect(Collectors.toList()));
logger.debug("Updated {} out of {} questions with new tag {}.", questionslist.size(), pager.getCount(), t.getTag());
}
if (!questionslist.isEmpty()) {
pc.updateAll(questionslist);
}
} while (!questionslist.isEmpty());
// overwrite new tag object
updated = pc.create(t);
}
model.addAttribute("tag", updated);
count = t.getCount();
}
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
res.setContentType("application/json");
try {
res.getWriter().println("{\"count\":" + count + ", \"tag\":\"" + new Tag(newtag).getTag() + "\"}");
} catch (IOException ex) {
}
return "blank";
} else {
return "redirect:" + TAGSLINK + "?" + req.getQueryString();
}
}
Aggregations