PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/b13/nooote/controllers/NoteController.java

https://github.com/Eric-Sun/nooote
Java | 313 lines | 167 code | 28 blank | 118 comment | 1 complexity | d3d61a7ca20c0d947467eae688b60f9b MD5 | raw file
  1. package com.b13.nooote.controllers;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.swing.plaf.multi.MultiFileChooserUI;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import com.alibaba.fastjson.JSON;
  13. import com.b13.nooote.core.ResultDTO;
  14. import com.b13.nooote.dtos.NoteDTO;
  15. import com.b13.nooote.note.services.NoteService;
  16. import com.b13.nooote.utils.ResponseWritter;
  17. import com.b13.nooote.utils.session.SessionUtil;
  18. @Controller
  19. @RequestMapping("/note")
  20. public class NoteController {
  21. @Autowired
  22. NoteService noteServ;
  23. /**
  24. * /note/post <p>
  25. * 上传一个note<p>
  26. * 请求:
  27. * <pre>
  28. * noteTitle note的标题*
  29. * noteContent note的内容(fck)*
  30. * userId 创建者用户id *
  31. * </pre>
  32. * 返回
  33. * <pre>
  34. * {
  35. * "result":0 0成功1失败
  36. * "noteId":3 note的id,用于主键索引
  37. * }
  38. * </pre>
  39. * @param req
  40. * @param resp
  41. * @return
  42. * @throws Exception
  43. */
  44. @RequestMapping("/post")
  45. public String post(HttpServletRequest req, HttpServletResponse resp) throws Exception{
  46. String noteTitle = req.getParameter("noteTitle");
  47. String noteContent = req.getParameter("noteContent");
  48. long userId = (Long)new SessionUtil(req).get("userId");
  49. long noteId = noteServ.create(userId,noteTitle, noteContent);
  50. class NoteUDTO{long noteId;
  51. public long getNoteId() {
  52. return noteId;
  53. }
  54. public void setNoteId(long noteId) {
  55. this.noteId = noteId;
  56. }}
  57. NoteUDTO d = new NoteUDTO();
  58. d.noteId = noteId;
  59. new ResponseWritter(resp).write(JSON.toJSONString(new ResultDTO(d))).end();
  60. return null;
  61. }
  62. /**
  63. * /note/getNote <p>
  64. * 显示一个note的详细内容
  65. * 请求:
  66. * <pre>
  67. * noteId note的ID
  68. * </pre>
  69. * 返回:
  70. * <pre>
  71. * {
  72. * "result":0 0成功1失败
  73. * "noteContent":"b", note的内容(fck)
  74. * "noteCreatetime":1328630400000, note的创建时间
  75. * "noteId":2, note的ID
  76. * "noteTitle":"a", note的标题
  77. * "userId":1 发布者ID
  78. * }
  79. * </pre>
  80. * @param req
  81. * @param resp
  82. * @return
  83. * @throws Exception
  84. */
  85. @RequestMapping("/getNote")
  86. public String getNote(HttpServletRequest req, HttpServletResponse resp) throws Exception{
  87. long noteId = new Long(req.getParameter("noteId"));
  88. NoteDTO n = noteServ.getNoteById(noteId);
  89. class NoteNDTO {
  90. long noteId;
  91. String noteTitle;
  92. String noteContent;
  93. Date noteCreatetime;
  94. long userId;
  95. public long getUserId() {
  96. return userId;
  97. }
  98. public void setUserId(long userId) {
  99. this.userId = userId;
  100. }
  101. public long getNoteId() {
  102. return noteId;
  103. }
  104. public void setNoteId(long noteId) {
  105. this.noteId = noteId;
  106. }
  107. public String getNoteTitle() {
  108. return noteTitle;
  109. }
  110. public void setNoteTitle(String noteTitle) {
  111. this.noteTitle = noteTitle;
  112. }
  113. public String getNoteContent() {
  114. return noteContent;
  115. }
  116. public void setNoteContent(String noteContent) {
  117. this.noteContent = noteContent;
  118. }
  119. public Date getNoteCreatetime() {
  120. return noteCreatetime;
  121. }
  122. public void setNoteCreatetime(Date noteCreatetime) {
  123. this.noteCreatetime = noteCreatetime;
  124. }
  125. }
  126. NoteNDTO ndto = new NoteNDTO();
  127. ndto.noteId = noteId;
  128. ndto.noteTitle = n.getNoteTitle();
  129. ndto.noteContent = n.getNoteContent();
  130. ndto.noteCreatetime = n.getNoteCreatetime();
  131. ndto.userId = n.getUserId();
  132. new ResponseWritter(resp).write(JSON.toJSONString(new ResultDTO(ndto))).end();
  133. return null;
  134. }
  135. /**
  136. * /note/list <p>
  137. * 显示一个玩家发布的note的标题的列表
  138. * 请求:
  139. * <pre>
  140. * userId 用户ID
  141. * pageNum 页数
  142. * sizePerPage 每页条数
  143. * </pre>
  144. * 返回
  145. * <pre>
  146. * [
  147. * {
  148. * "noteCreatetime":1328630400000, note创建时间
  149. * "noteId":1, note的id
  150. * "noteTitle":"a"}, note的标题
  151. * {"noteCreatetime":1328630400000,"noteId":2,"noteTitle":"a"}
  152. * ]
  153. * </pre>
  154. * @param req
  155. * @param resp
  156. * @return
  157. * @throws Exception
  158. */
  159. @RequestMapping("/list")
  160. public String list(HttpServletRequest req, HttpServletResponse resp) throws Exception{
  161. // 通过session获得userId
  162. // 限制必须是登录用户才可以获得
  163. long userId = (Long)new SessionUtil(req).get("userId");
  164. int pageNum = new Integer(req.getParameter("pageNum"));
  165. int sizePerPage = new Integer(req.getParameter("sizePerPage"));
  166. List<NoteDTO> nList = noteServ.getNoteTitleList(userId, pageNum, sizePerPage);
  167. class NoteLDTO{
  168. long noteId;
  169. String noteTitle;
  170. Date noteCreatetime;
  171. public long getNoteId() {
  172. return noteId;
  173. }
  174. public void setNoteId(long noteId) {
  175. this.noteId = noteId;
  176. }
  177. public String getNoteTitle() {
  178. return noteTitle;
  179. }
  180. public void setNoteTitle(String noteTitle) {
  181. this.noteTitle = noteTitle;
  182. }
  183. public Date getNoteCreatetime() {
  184. return noteCreatetime;
  185. }
  186. public void setNoteCreatetime(Date noteCreatetime) {
  187. this.noteCreatetime = noteCreatetime;
  188. }
  189. }
  190. List<NoteLDTO> returnList = new ArrayList<NoteLDTO>();
  191. for( NoteDTO noteDTO : nList ){
  192. NoteLDTO d = new NoteLDTO();
  193. d.noteCreatetime = noteDTO.getNoteCreatetime();
  194. d.noteId = noteDTO.getNoteId();
  195. d.noteTitle = noteDTO.getNoteTitle();
  196. returnList.add(d);
  197. }
  198. new ResponseWritter(resp).write(JSON.toJSONString(new ResultDTO(returnList))).end();
  199. return null;
  200. }
  201. /**
  202. * /note/listSize <p>
  203. * 显示一个用户发布的note的总数
  204. * 请求:
  205. * <pre>
  206. * userId 发布人的ID
  207. * </pre>
  208. * 返回:
  209. * <pre>
  210. * {
  211. * "size":2 列表大小
  212. * }
  213. * </pre>
  214. * @param req
  215. * @param resp
  216. * @return
  217. * @throws Exception
  218. */
  219. @RequestMapping("/listSize")
  220. public String listSize(HttpServletRequest req, HttpServletResponse resp) throws Exception{
  221. long userId = new Long(req.getParameter("userId"));
  222. int size = noteServ.getNoteListSize(userId);
  223. class NoteListSizeDTO{
  224. long size;
  225. public long getSize() {
  226. return size;
  227. }
  228. public void setSize(long size) {
  229. this.size = size;
  230. }
  231. }
  232. NoteListSizeDTO d = new NoteListSizeDTO();
  233. d.size = size;
  234. new ResponseWritter(resp).write(JSON.toJSONString(new ResultDTO(d))).end();
  235. return null;
  236. }
  237. /**
  238. * 上传文件
  239. * 请求:
  240. * <pre>
  241. * fileName 文件内容
  242. * file form中的file类型的对象
  243. * </pre>
  244. * @param req
  245. * @param resp
  246. * @return
  247. * @throws Exception
  248. */
  249. @RequestMapping("/uploadFile")
  250. public String uploadFile(HttpServletRequest req, HttpServletResponse resp) throws Exception{
  251. String fileName = req.getParameter("fileName");
  252. MultipartFile mpf = (MultipartFile)req.getAttribute("file");
  253. return null;
  254. }
  255. /**
  256. * /note/modify <p>
  257. * 修改一个note
  258. * 请求:
  259. * <pre>
  260. * noteId note的id
  261. * </pre>
  262. * 返回:
  263. * <pre>
  264. * {
  265. * "size":2 列表大小
  266. * }
  267. * </pre>
  268. * @param req
  269. * @param resp
  270. * @return
  271. * @throws Exception
  272. */
  273. @RequestMapping("/modify")
  274. public String mofidy( HttpServletRequest req, HttpServletResponse resp ) throws Exception {
  275. long noteId = new Long(req.getParameter("noteId"));
  276. String noteTitle = req.getParameter("noteTitle");
  277. String noteContent = req.getParameter("noteContent");
  278. noteServ.modifyNote(noteId, noteTitle, noteContent);
  279. class noteModifyDTO {
  280. }
  281. noteModifyDTO d = new noteModifyDTO();
  282. new ResponseWritter(resp).write(JSON.toJSONString(new ResultDTO(d))).end();
  283. return null;
  284. }
  285. }