|
|
@@ -1,165 +0,0 @@
|
|
|
-package com.miekir.shibei.controller.api;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.miekir.shibei.bean.NoteBean;
|
|
|
-import com.miekir.shibei.bean.ResponseResult;
|
|
|
-import com.miekir.shibei.bean.ResultCode;
|
|
|
-import com.miekir.shibei.repository.NoteRepository;
|
|
|
-import com.miekir.shibei.repository.UserRepository;
|
|
|
-import com.miekir.shibei.tool.TextUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Controller;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
-import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
-
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-@Controller
|
|
|
-public class NoteController {
|
|
|
-
|
|
|
- // 自动装配
|
|
|
- @Autowired
|
|
|
- private NoteRepository noteRepository;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private UserRepository userRepository;
|
|
|
-
|
|
|
- /**
|
|
|
- * 新增记事,接收的是纯JSON数据 严格来说,也要传递id和token过来校验的
|
|
|
- */
|
|
|
- @RequestMapping(value = "/api/addNote", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
- @ResponseBody
|
|
|
- public String addNote(String email, String token, String note_bean) {
|
|
|
- ResponseResult<String> responseResult = new ResponseResult<String>();
|
|
|
- responseResult.setMessage("保存失败");
|
|
|
-
|
|
|
- NoteBean bean;
|
|
|
- try {
|
|
|
- bean = JSON.parseObject(note_bean, NoteBean.class);
|
|
|
- bean.setUpdateTime(System.currentTimeMillis());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- NoteBean dbNOteBean;
|
|
|
- try {
|
|
|
- dbNOteBean = noteRepository.save(bean);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- responseResult.setContent(JSON.toJSONString(dbNOteBean));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- responseResult.setCode(ResultCode.SUCCESS);
|
|
|
- responseResult.setMessage("保存成功");
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除密码记事
|
|
|
- */
|
|
|
- @RequestMapping(value = "/api/delNote", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
- @ResponseBody
|
|
|
- public String delNote(String email, String token, int noteId) {
|
|
|
- ResponseResult<String> responseResult = new ResponseResult<String>();
|
|
|
- responseResult.setMessage("操作失败");
|
|
|
-
|
|
|
- NoteBean dbNoteBean;
|
|
|
- try {
|
|
|
- dbNoteBean = noteRepository.findNoteBeanById(noteId);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- // 密码不属于该用户
|
|
|
- if (dbNoteBean == null || !TextUtils.equals(email, dbNoteBean.getUserId())) {
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- try {
|
|
|
- noteRepository.delete(noteId);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- responseResult.setCode(ResultCode.SUCCESS);
|
|
|
- responseResult.setMessage("删除成功");
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询密码记事
|
|
|
- */
|
|
|
- @RequestMapping(value = "/api/getNote", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
- @ResponseBody
|
|
|
- public String getNote(String email, String token) {
|
|
|
- ResponseResult<String> responseResult = new ResponseResult<String>();
|
|
|
- responseResult.setMessage("获取失败");
|
|
|
-
|
|
|
- List<NoteBean> noteBeanList;
|
|
|
- try {
|
|
|
- noteBeanList = noteRepository.findNotesByEmail(email);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- responseResult.setContent(JSON.toJSONString(noteBeanList));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- responseResult.setCode(ResultCode.SUCCESS);
|
|
|
- responseResult.setMessage("获取成功");
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新密码记事
|
|
|
- */
|
|
|
- @RequestMapping(value = "/api/updateNote", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
- @ResponseBody
|
|
|
- public String updateNote(String email, String token, String note_bean) {
|
|
|
- ResponseResult<String> responseResult = new ResponseResult<String>();
|
|
|
- responseResult.setMessage("更新失败");
|
|
|
-
|
|
|
- NoteBean clientBean;
|
|
|
- try {
|
|
|
- clientBean = JSON.parseObject(note_bean, NoteBean.class);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- // 发送方的用户和该记事的所有者不是同一个人,没有权限
|
|
|
- NoteBean oldBean = noteRepository.findNoteBeanById(clientBean.getId());
|
|
|
- if (!TextUtils.equals(email, oldBean.getUserId())) {
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- try {
|
|
|
- noteRepository.updateNoteBean(clientBean.getContent(), clientBean.getId(), System.currentTimeMillis());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-
|
|
|
- responseResult.setCode(ResultCode.SUCCESS);
|
|
|
- responseResult.setMessage("更新成功");
|
|
|
- return JSON.toJSONString(responseResult);
|
|
|
- }
|
|
|
-}
|