/*
* Copyright 2012 XueSong Guo.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wbbs.web;
import cn.webwheel.Action;
import cn.webwheel.results.ErrorResult;
import cn.webwheel.results.TemplateResult;
import com.google.inject.Inject;
import wbbs.domain.Reply;
import wbbs.domain.Topic;
import wbbs.domain.User;
import wbbs.service.PaginatedList;
import wbbs.service.TopicService;
import java.sql.SQLException;
import java.sql.Timestamp;
public class topic extends BaseAction {
public Topic topic;
@Inject
TopicService topicService;
User author;
PaginatedList<Reply> replies;
@Action
public Object html(int id, int pg) throws SQLException {
topic = topicService.findTopic(id);
if (topic == null) {
return new ErrorResult(404);
}
author = userService.findUser(topic.authorId);
replies = topicService.listReply(topic.id, pg, 30);
for (Reply reply : replies) {
reply.author = userService.findUser(reply.authorId);
}
return new TemplateResult(this);
}
@Action
public Object update() throws SQLException {
ensureLoginAction();
range(notNull(trim(topic.title)), 1, 32, "标题不能为空且最长32个字");
range(notNull(trim(topic.content)), 1, 4000, "正文不能为空且最长4000个字");
if (topic.id == 0) {
topic.authorId = loginUser.id;
topic.postDate = new Timestamp(System.currentTimeMillis());
topic.updateDate = topic.postDate;
topicService.insertTopic(topic);
} else {
Topic t = topicService.findTopic(topic.id);
notNull(t, "主题不存在");
equal(t.authorId, loginUser.id, "权限错误");
topicService.updateTopic(topic);
}
return ok();
}
@Action
public Object reply(int topicId, String content) throws SQLException {
ensureLoginAction();
Topic topic = topicService.findTopic(topicId);
notNull(topic, "主题不存在");
Reply reply = new Reply();
reply.topicId = topicId;
reply.content = range(notNull(trim(content)), 1, 4000, "回复不能为空且最长4000个字");
reply.authorId = loginUser.id;
reply.postDate = new Timestamp(System.currentTimeMillis());
topicService.insertReply(topicId, reply);
return ok();
}
public User getAuthor() {
return author;
}
public PaginatedList<Reply> getReplies() {
return replies;
}
}