Skip to content

Commit

Permalink
🤙 upgrade blade jdbc
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Sep 17, 2017
1 parent d603f7d commit 7bf499b
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 287 deletions.
132 changes: 132 additions & 0 deletions src/main/java/com/tale/controller/CategoryController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.tale.controller;

import com.blade.ioc.annotation.Inject;
import com.blade.jdbc.page.Page;
import com.blade.mvc.annotation.GetRoute;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.http.Request;
import com.tale.init.TaleConst;
import com.tale.model.dto.Types;
import com.tale.model.entity.Contents;
import com.tale.model.entity.Metas;
import com.tale.service.ContentsService;
import com.tale.service.MetasService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* 分类、标签控制器
*
* @author biezhi
* @date 2017/9/17
*/
@Slf4j
@Path
public class CategoryController extends BaseController {

@Inject
private ContentsService contentsService;

@Inject
private MetasService metasService;

/**
* 分类列表页
*
* @since 1.3.1
*/
@GetRoute(value = {"categories", "categories.html"})
public String categories(Request request) {
Map<String, List<Contents>> mapping = metasService.getMetaMapping(Types.CATEGORY);
Set<String> categories = mapping.keySet();
request.attribute("categories", categories);
request.attribute("mapping", mapping);
return this.render("categories");
}

/**
* 某个分类详情页
*/
@GetRoute(value = {"category/:keyword", "category/:keyword.html"})
public String categories(Request request, @PathParam String keyword, @Param(defaultValue = "12") int limit) {
return this.categories(request, keyword, 1, limit);
}

/**
* 某个分类详情页分页
*/
@GetRoute(value = {"category/:keyword/:page", "category/:keyword/:page.html"})
public String categories(Request request, @PathParam String keyword,
@PathParam int page, @Param(defaultValue = "12") int limit) {
page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
Metas metaDto = metasService.getMeta(Types.CATEGORY, keyword);
if (null == metaDto) {
return this.render_404();
}

Page<Contents> contentsPaginator = contentsService.getArticles(metaDto.getMid(), page, limit);

request.attribute("articles", contentsPaginator);
request.attribute("meta", metaDto);
request.attribute("type", "分类");
request.attribute("keyword", keyword);
request.attribute("is_category", true);
request.attribute("page_prefix", "/category/" + keyword);

return this.render("page-category");
}

/**
* 标签列表页面
* <p>
* 渲染所有的标签和文章映射
*
* @since 1.3.1
*/
@GetRoute(value = {"tags", "tags.html"})
public String tags(Request request) {
Map<String, List<Contents>> mapping = metasService.getMetaMapping(Types.TAG);
Set<String> tags = mapping.keySet();
request.attribute("tags", tags);
request.attribute("mapping", mapping);
return this.render("tags");
}

/**
* 标签详情页
*
* @param name 标签名
*/
@GetRoute(value = {"tag/:name", "tag/:name.html"})
public String tagPage(Request request, @PathParam String name, @Param(defaultValue = "12") int limit) {
return this.tags(request, name, 1, limit);
}

/**
* 标签下文章分页
*/
@GetRoute(value = {"tag/:name/:page", "tag/:name/:page.html"})
public String tags(Request request, @PathParam String name, @PathParam int page, @Param(defaultValue = "12") int limit) {
page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
Metas metaDto = metasService.getMeta(Types.TAG, name);
if (null == metaDto) {
return this.render_404();
}

Page<Contents> contentsPage = contentsService.getArticles(metaDto.getMid(), page, limit);
request.attribute("articles", contentsPage);
request.attribute("meta", metaDto);
request.attribute("type", "标签");
request.attribute("keyword", name);
request.attribute("is_tag", true);
request.attribute("page_prefix", "/tag/" + name);

return this.render("page-category");
}

}
105 changes: 3 additions & 102 deletions src/main/java/com/tale/controller/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.tale.model.dto.Types;
import com.tale.model.entity.Comments;
import com.tale.model.entity.Contents;
import com.tale.model.entity.Metas;
import com.tale.service.CommentsService;
import com.tale.service.ContentsService;
import com.tale.service.MetasService;
Expand All @@ -30,12 +29,10 @@

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
* 首页、分类、归档、标签页面
* 首页、归档、Feed、评论
*
* @author biezhi
* @since 1.3.1
Expand Down Expand Up @@ -143,103 +140,6 @@ public String post(Request request, @PathParam String cid) {
return this.render("post");
}

/**
* 分类页
*
* @return
*/
@GetRoute(value = {"category/:keyword", "category/:keyword.html"})
public String categories(Request request, @PathParam String keyword, @Param(defaultValue = "12") int limit) {
return this.categories(request, keyword, 1, limit);
}

@GetRoute(value = {"category/:keyword/:page", "category/:keyword/:page.html"})
public String categories(Request request, @PathParam String keyword,
@PathParam int page, @Param(defaultValue = "12") int limit) {
page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
Metas metaDto = metasService.getMeta(Types.CATEGORY, keyword);
if (null == metaDto) {
return this.render_404();
}

Page<Contents> contentsPaginator = contentsService.getArticles(metaDto.getMid(), page, limit);

request.attribute("articles", contentsPaginator);
request.attribute("meta", metaDto);
request.attribute("type", "分类");
request.attribute("keyword", keyword);
request.attribute("is_category", true);
request.attribute("page_prefix", "/category/" + keyword);

return this.render("page-category");
}

/**
* 标签列表页面
* <p>
* 渲染所有的标签和文章映射
*
* @since 1.3.1
*/
@GetRoute(value = {"tags", "tags.html"})
public String tags(Request request) {
Map<String, List<Contents>> mapping = metasService.getMetaMapping(Types.TAG);
Set<String> tags = mapping.keySet();
request.attribute("tags", tags);
request.attribute("mapping", mapping);
return this.render("tags");
}

/**
* 分类列表页
*
* @since 1.3.1
*/
@GetRoute(value = {"categories", "categories.html"})
public String categories() {
return this.render("categories");
}

/**
* 标签详情页
*
* @param name 标签名
*/
@GetRoute(value = {"tag/:name", "tag/:name.html"})
public String tagPage(Request request, @PathParam String name, @Param(defaultValue = "12") int limit) {
return this.tags(request, name, 1, limit);
}


/**
* 标签下文章分页
*
* @param request
* @param name
* @param page
* @param limit
* @return
*/
@GetRoute(value = {"tag/:name/:page", "tag/:name/:page.html"})
public String tags(Request request, @PathParam String name, @PathParam int page, @Param(defaultValue = "12") int limit) {

page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
Metas metaDto = metasService.getMeta(Types.TAG, name);
if (null == metaDto) {
return this.render_404();
}

Page<Contents> contentsPaginator = contentsService.getArticles(metaDto.getMid(), page, limit);
request.attribute("articles", contentsPaginator);
request.attribute("meta", metaDto);
request.attribute("type", "标签");
request.attribute("keyword", name);
request.attribute("is_tag", true);
request.attribute("page_prefix", "/tag/" + name);

return this.render("page-category");
}

/**
* 搜索页
*
Expand Down Expand Up @@ -372,4 +272,5 @@ public RestResponse comment(Request request, Response response,
return RestResponse.fail(msg);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public class ArticleController extends BaseController {
* @return
*/
@GetRoute(value = "")
public String index(@Param(defaultValue = "1") int page,
@Param(defaultValue = "15") int limit, Request request) {
public String index(@Param(defaultValue = "1") int page, @Param(defaultValue = "15") int limit,
Request request) {

Page<Contents> articles = new Contents().where("type", Types.ARTICLE).page(page, limit, "created desc");
request.attribute("articles", articles);
Expand Down Expand Up @@ -104,7 +104,6 @@ public String editArticle(@PathParam String cid, Request request) {
@PostRoute(value = "publish")
@JSON
public RestResponse publishArticle(@Valid Contents contents) {

Users users = this.user();
contents.setType(Types.ARTICLE);
contents.setAuthorId(users.getUid());
Expand Down Expand Up @@ -136,6 +135,9 @@ public RestResponse publishArticle(@Valid Contents contents) {
@JSON
public RestResponse modifyArticle(@Valid Contents contents) {
try {
if(null == contents || null == contents.getCid()){
return RestResponse.fail("缺少参数,请重试");
}
contentsService.updateArticle(contents);
return RestResponse.ok(contents.getCid());
} catch (Exception e) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/tale/controller/admin/PageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ public RestResponse publishPage(@Valid Contents contents) {
@Route(value = "modify", method = HttpMethod.POST)
@JSON
public RestResponse modifyArticle(@Valid Contents contents) {
contents.setType(Types.PAGE);
if(null == contents || null == contents.getCid()){
return RestResponse.fail("缺少参数,请重试");
}
try {
contents.setType(Types.PAGE);
contentsService.updateArticle(contents);
} catch (Exception e) {
String msg = "页面编辑失败";
Expand Down
41 changes: 19 additions & 22 deletions src/main/java/com/tale/init/SqliteJdbc.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.tale.init;

import com.blade.mvc.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -15,17 +15,14 @@
import java.util.stream.Collectors;

/**
* Sqlite 数据库操作
* SQLite 数据库操作
* <p>
* Created by biezhi on 2017/3/4.
*/
@Slf4j
@NoArgsConstructor
public final class SqliteJdbc {

private static final Logger LOGGER = LoggerFactory.getLogger(SqliteJdbc.class);

private SqliteJdbc() {
}

public static final String DB_NAME = "tale.db";
public static String DB_PATH;
public static String DB_SRC;
Expand All @@ -34,7 +31,7 @@ private SqliteJdbc() {
try {
Class.forName("org.sqlite.JDBC");
} catch (Exception e) {
e.printStackTrace();
log.error("load sqlite driver error", e);
}
}

Expand All @@ -52,29 +49,29 @@ public static void importSql(boolean devMode) {
DB_SRC = "jdbc:sqlite://" + DB_PATH;
}

LOGGER.info("blade dev mode: {}", devMode);
LOGGER.info("load sqlite database path [{}]", DB_PATH);
LOGGER.info("load sqlite database src [{}]", DB_SRC);
log.info("blade dev mode: {}", devMode);
log.info("load sqlite database path [{}]", DB_PATH);
log.info("load sqlite database src [{}]", DB_SRC);

Connection con = DriverManager.getConnection(DB_SRC);
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='t_options'");
int count = rs.getInt(1);
Connection con = DriverManager.getConnection(DB_SRC);
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='t_options'");
int count = rs.getInt(1);
if (count == 0) {
String cp = SqliteJdbc.class.getClassLoader().getResource("").getPath();
String cp = SqliteJdbc.class.getClassLoader().getResource("").getPath();
InputStreamReader isr = new InputStreamReader(new FileInputStream(cp + "schema.sql"), "UTF-8");

String sql = new BufferedReader(isr).lines().collect(Collectors.joining("\n"));
int r = statement.executeUpdate(sql);
LOGGER.info("initialize import database - {}", r);
int r = statement.executeUpdate(sql);
log.info("initialize import database - {}", r);
}
rs.close();
statement.close();
con.close();
LOGGER.info("database path is: {}", DB_PATH);
log.info("database path is: {}", DB_PATH);
} catch (Exception e) {
LOGGER.error("initialize database fail", e);
log.error("initialize database fail", e);
}
}

}
}
Loading

0 comments on commit 7bf499b

Please sign in to comment.