Skip to content

Commit

Permalink
♻️ use vue.js
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Jun 8, 2018
1 parent 60d308e commit c13b611
Show file tree
Hide file tree
Showing 11 changed files with 635 additions and 70 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/tale/controller/ArticleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ public class ArticleController extends BaseController {
*/
@GetRoute(value = {"/:cid", "/:cid.html"})
public String page(@PathParam String cid, Request request) {
Optional<Contents> contentsOptional = contentsService.getContents(cid);
if (!contentsOptional.isPresent()) {
Contents contents = contentsService.getContents(cid);
if (null == contents) {
return this.render_404();
}
Contents contents = contentsOptional.get();
if (contents.getAllowComment()) {
int cp = request.queryInt("cp", 1);
request.attribute("cp", cp);
Expand All @@ -72,11 +71,10 @@ public String page(@PathParam String cid, Request request) {
*/
@GetRoute(value = {"article/:cid", "article/:cid.html"})
public String post(Request request, @PathParam String cid) {
Optional<Contents> contentsOptional = contentsService.getContents(cid);
if (!contentsOptional.isPresent()) {
Contents contents = contentsService.getContents(cid);
if (null == contents) {
return this.render_404();
}
Contents contents = contentsOptional.get();
if (Types.DRAFT.equals(contents.getStatus())) {
return this.render_404();
}
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/com/tale/controller/admin/AdminApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@
import com.tale.service.MetasService;
import com.tale.service.SiteService;
import com.tale.validators.CommonValidator;
import io.github.biezhi.anima.enums.OrderBy;
import io.github.biezhi.anima.page.Page;

import java.util.List;
import java.util.Optional;

import static io.github.biezhi.anima.Anima.select;

/**
* @author biezhi
* @date 2018/6/9
*/
@Path(value = "admin", suffix = ".json", restful = true)
@Path(value = "admin/api", restful = true)
public class AdminApiController extends BaseController {

@Inject
Expand All @@ -40,8 +37,8 @@ public class AdminApiController extends BaseController {

@GetRoute("articles/:cid")
public RestResponse article(@PathParam String cid) {
Optional<Contents> contents = contentsService.getContents(cid);
return RestResponse.ok(contents.orElse(null));
Contents contents = contentsService.getContents(cid);
return RestResponse.ok(contents);
}

@PostRoute("article/new")
Expand Down Expand Up @@ -83,15 +80,17 @@ public RestResponse updateArticle(@BodyParam Contents contents) {

@GetRoute("articles")
public RestResponse articleList(ArticleParam articleParam) {
Page<Contents> articles = select().from(Contents.class).where(Contents::getType, Types.ARTICLE)
.order(Contents::getCreated, OrderBy.DESC).page(articleParam.getPage(), articleParam.getLimit());
articleParam.setType(Types.ARTICLE);
articleParam.setOrderBy("created desc");
Page<Contents> articles = contentsService.findArticles(articleParam);
return RestResponse.ok(articles);
}

@GetRoute("pages")
public RestResponse pageList(ArticleParam articleParam) {
Page<Contents> articles = select().from(Contents.class).where(Contents::getType, Types.PAGE)
.order(Contents::getCreated, OrderBy.DESC).page(articleParam.getPage(), articleParam.getLimit());
articleParam.setType(Types.PAGE);
articleParam.setOrderBy("created desc");
Page<Contents> articles = contentsService.findArticles(articleParam);
return RestResponse.ok(articles);
}

Expand Down
25 changes: 15 additions & 10 deletions src/main/java/com/tale/controller/admin/PagesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public String commonPage(@PathParam String module, @PathParam String page) {
return "admin/" + module + "/" + page + ".html";
}

@GetRoute("/article/edit/:cid")
public String editArticle() {
return "admin/article/edit.html";
}

@GetRoute("login")
public String login(Response response) {
if (null != this.user()) {
Expand Down Expand Up @@ -115,16 +120,16 @@ public String newPage(Request request) {
return "admin/page_edit";
}

@GetRoute("page/:cid")
public String editPage(@PathParam String cid, Request request) {
Optional<Contents> contents = contentsService.getContents(cid);
if (!contents.isPresent()) {
return render_404();
}
request.attribute("contents", contents.get());
request.attribute(Types.ATTACH_URL, Commons.site_option(Types.ATTACH_URL, Commons.site_url()));
return "admin/page_edit";
}
// @GetRoute("page/:cid")
// public String editPage(@PathParam String cid, Request request) {
// Optional<Contents> contents = contentsService.getContents(cid);
// if (!contents.isPresent()) {
// return render_404();
// }
// request.attribute("contents", contents.get());
// request.attribute(Types.ATTACH_URL, Commons.site_option(Types.ATTACH_URL, Commons.site_url()));
// return "admin/page_edit";
// }

@GetRoute("template")
public String index(Request request) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/tale/model/params/ArticleParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
@ToString(callSuper = true)
public class ArticleParam extends PageParam {

private String type;
private String orderBy;

}
33 changes: 26 additions & 7 deletions src/main/java/com/tale/service/ContentsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import com.tale.model.entity.Comments;
import com.tale.model.entity.Contents;
import com.tale.model.entity.Relationships;
import com.tale.model.params.ArticleParam;
import com.tale.utils.TaleUtils;
import com.vdurmont.emoji.EmojiParser;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.AnimaQuery;
import io.github.biezhi.anima.enums.OrderBy;
import io.github.biezhi.anima.page.Page;

import java.util.Optional;
Expand All @@ -37,15 +40,19 @@ public class ContentsService {
*
* @param id 唯一标识
*/
public Optional<Contents> getContents(String id) {
public Contents getContents(String id) {
Contents contents = null;
if (StringKit.isNotBlank(id)) {
if (StringKit.isNumber(id)) {
return Optional.ofNullable(select().from(Contents.class).byId(id));
contents = select().from(Contents.class).byId(id);
} else {
return Optional.ofNullable(select().from(Contents.class).where(Contents::getSlug, id).one());
contents = select().from(Contents.class).where(Contents::getSlug, id).one();
}
if (null != contents) {
return this.mapContent(contents);
}
}
return Optional.empty();
return contents;
}

/**
Expand Down Expand Up @@ -107,12 +114,12 @@ public void updateArticle(Contents contents) {
* @param cid 文章id
*/
public void delete(int cid) {
Optional<Contents> contents = this.getContents(cid + "");
contents.ifPresent(content -> {
Contents contents = this.getContents(cid + "");
if (null != contents) {
deleteById(Contents.class, cid);
Anima.delete().from(Relationships.class).where(Relationships::getCid, cid).execute();
Anima.delete().from(Comments.class).where(Comments::getCid, cid).execute();
});
}
}

/**
Expand All @@ -127,4 +134,16 @@ public Page<Contents> getArticles(Integer mid, int page, int limit) {
return select().bySQL(Contents.class, SQL_QUERY_ARTICLES, mid).page(page, limit);
}

public Page<Contents> findArticles(ArticleParam articleParam) {
AnimaQuery<Contents> query = select().from(Contents.class);
query.where(Contents::getType, articleParam.getType());
query.order(articleParam.getOrderBy());
Page<Contents> articles = query.page(articleParam.getPage(), articleParam.getLimit());
return articles.map(this::mapContent);
}

private Contents mapContent(Contents contents) {
contents.setContent(contents.getContent().replaceAll("\\\\\"", "\\\""));
return contents;
}
}
Loading

0 comments on commit c13b611

Please sign in to comment.