package wblog.web;
import cn.webwheel.Action;
import cn.webwheel.results.ErrorResult;
import cn.webwheel.results.TemplateResult;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import wblog.domain.Article;
import wblog.domain.User;
import java.sql.SQLException;
import java.util.List;
public class home extends BaseAction {
public String id;
public String tag;
User user;
List<Article> articles;
@Action(value = "?id=$1&tag=$3", rest = "/user/([^/]+)(/(.+)?)?")
public Object html() throws SQLException {
user = qr.query("select * from User where id=?", new BeanHandler<User>(User.class), id);
if (user == null) {
return new ErrorResult(404);
}
if ((tag = tag.trim()).isEmpty()) tag = null;
if (tag == null) {
articles = qr.query("select * from Article where authorId=? order by postDate desc limit ? offset ?", new BeanListHandler<Article>(Article.class),
user.id, pageSize, (pg - 1) * pageSize);
itemCount = user.getArticleCount();
} else {
articles = qr.query("select * from Article join ArticleTag on Article.id=ArticleTag.articleId where tag=? and authorId=? order by postDate desc limit ? offset ?", new BeanListHandler<Article>(Article.class),
tag, user.id, pageSize, (pg - 1) * pageSize);
Integer n = user.getTagList().get(tag);
itemCount = n == null ? 0 : n;
}
return new TemplateResult(this);
}
public User getUser() {
return user;
}
public List<Article> getArticles() {
return articles;
}
public boolean isHost() throws SQLException {
return user.equals(getLoginUser());
}
}