发表于: 2019-11-05 23:22:50

1 981


今天完成的事情:

今天开始写复盘的代码,最先写假数据。然后写接口。

我想直接写接口,被老大教育了。

那把写不完的接口先弄点假数据。


后台学习对象接口,不完整,少了获取token里的管理员ID。

package com.happynewyear.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.happynewyear.admin.pojo.StudyObject;
import com.happynewyear.admin.server.StudyObjectServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.happynewyear.admin.utils.CodeMsg.REQUEST_FAILED;
import static com.happynewyear.admin.utils.CodeMsg.SUCCESS;

/**
* @author 张明顺
* 后台学习对你接口,列表查,详细查,增,改,删,上下架
*/
@RestController
public class StudyObjectController {
   private final Logger log = LogManager.getLogger(this.getClass());
   @Autowired
   StudyObjectServer studyObjectServer;

   /**
    * 列表查,(可以详细查,但不用),创建时间倒序
    */
   @GetMapping("/a/u/list/study")
   public Map<String, Object> studyListSelect(StudyObject studyObject,
                                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                                          @RequestParam(value = "size", defaultValue = "10") Integer size) {
       HashMap<String, Object> result = new HashMap<>(16);
       log.info("获取第{}页共{}行学习对象信息{}", page, size, studyObject);
       try {
           /*按照创建时间倒序*/
           PageHelper.startPage(page, size, "create_at desc");
           List<StudyObject> studyObjectList = studyObjectServer.selectSelective(studyObject);
           PageInfo<StudyObject> pageInfo = new PageInfo<>(studyObjectList);
           /*获取总数据量*/
           Long total = pageInfo.getTotal();
           if (!studyObjectList.isEmpty()) {
               result.put("code", SUCCESS.getCode());
               result.put("msg", SUCCESS.getMsg());
               result.put("total", total);
               result.put("data", studyObjectList);
               /*查询到的id集合*/
               List<Long> selectStudyObjectIdList = new ArrayList();
               for (StudyObject studyObject1 : studyObjectList) {
                   selectStudyObjectIdList.add(studyObject1.getId());
               }
               log.info("查到的学习对象ID是{}", selectStudyObjectIdList);
           } else {
               result.put("code", SUCCESS.getCode());
               result.put("msg", SUCCESS.getMsg());
               result.put("data", studyObjectList);
               log.info("未查询到此学习对象信息");
           }
           return result;
       } catch (Exception e) {
           e.printStackTrace();
           result.put("code", REQUEST_FAILED.getCode());
           result.put("msg", REQUEST_FAILED.getMsg());
           return result;
       }
   }
   /**
    * 详细查学习对象
    */
   @GetMapping("/a/u/study")
   public Map<String, Object> studySelect(Long id) {
       HashMap<String, Object> result = new HashMap<>(16);
       try {
           log.info("要查询的学习对象ID:{}", id);
           StudyObject studyObject = studyObjectServer.selectByPrimaryKey(id);
           result.put("code", SUCCESS.getCode());
           result.put("msg", SUCCESS.getMsg());
           result.put("data", studyObject);
           return result;
       } catch (Exception e) {
           e.printStackTrace();
           result.put("code", REQUEST_FAILED.getCode());
           result.put("msg", REQUEST_FAILED.getMsg());
           return result;
       }
   }
   /**
    * 后台学习对象新增
    */
   @PostMapping("/a/u/study")
   public Map<String, Object> studyInsert(StudyObject studyObject) {
       HashMap<String, Object> result = new HashMap<>(16);
       log.info("新增的学习对象信息{}", studyObject);
       try {
           /*新增默认为下架状态*/
           studyObject.setStatus(0);
           int studyInsertSuccess = studyObjectServer.insertSelective(studyObject);
           if (studyInsertSuccess == 1) {
               result.put("id", studyObject.getId());
               result.put("code", SUCCESS.getCode());
               result.put("msg", SUCCESS.getMsg());
               log.info("新增的学习对象ID:{}", studyObject.getId());
           }
           return result;
       } catch (Exception e) {
           e.printStackTrace();
           result.put("code", REQUEST_FAILED.getCode());
           result.put("msg", REQUEST_FAILED.getMsg());
           return result;
       }
   }
   /**
    * 后台学习对象删除
    */
   @DeleteMapping("/a/u/study")
   public Map<String, Object> studyInsert(Long id) {
       HashMap<String, Object> result = new HashMap<>(16);
       try {
           log.info("删除的学习对象id为:{}", id);
           studyObjectServer.deleteByPrimaryKey(id);
           result.put("code", SUCCESS.getCode());
           result.put("msg", SUCCESS.getMsg());
           log.info("成功删除学习对象的对应id:{}", id);
           return result;
       } catch (Exception e) {
           e.printStackTrace();
           result.put("code", REQUEST_FAILED.getCode());
           result.put("msg", REQUEST_FAILED.getMsg());
           return result;
       }
   }
   /**
    * 后台学习对象修改
    */
   @PutMapping("/a/u/study")
   public Map<String, Object> studyUpdate(StudyObject studyObject) {
       HashMap<String, Object> result = new HashMap<>(16);
       log.info("修改的学习对象信息{}", studyObject);
       try {
           int studyUpdateSuccess = studyObjectServer.updateByPrimaryKeySelective(studyObject);
           if (studyUpdateSuccess == 1) {
               result.put("code", SUCCESS.getCode());
               result.put("msg", SUCCESS.getMsg());
               log.info("成功修改学习对象的对应id:{}", studyObject.getId());
           }
           return result;
       } catch (Exception e) {
           e.printStackTrace();
           result.put("code", REQUEST_FAILED.getCode());
           result.put("msg", REQUEST_FAILED.getMsg());
           return result;
       }
   }
   /**
    * 后台学习对象上下架
    * */
   @PutMapping("/a/u/study/status")
   public Map<String, Object> studyStatus(Long id,int status) {
       HashMap<String, Object> result = new HashMap<>(16);
       log.info("上下架操作:{}", status);
       try {
           StudyObject studyObject =new StudyObject();
           studyObject.setStatus(status);
           studyObject.setId(id);
           int studyUpdateSuccess = studyObjectServer.updateByPrimaryKeySelective(studyObject);
           if (studyUpdateSuccess == 1) {
               result.put("code", SUCCESS.getCode());
               result.put("msg", SUCCESS.getMsg());
               log.info("上下架操作{}成功", status);
           }
           return result;
       } catch (Exception e) {
           e.printStackTrace();
           result.put("code", REQUEST_FAILED.getCode());
           result.put("msg", REQUEST_FAILED.getMsg());
           return result;
       }
   }
}

还有,点赞的,就不贴了。

贴个假代码的,对于假代码我还是没弄的太懂,也不是说不懂,主要是层级结构上的问题。如果假代码里的层级结构和真实数据有出入的话,不知道会不会影响到前端这边。


这是后台留言接口的假数据

package com.happynewyear.admin.controller;

import com.happynewyear.admin.pojo.Comment;
import com.happynewyear.admin.pojo.StudyObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.happynewyear.admin.utils.CodeMsg.SUCCESS;

@RestController
public class CommentController {

   /*评论列表*/
   @GetMapping("/a/u/list/comment")
   public Map<String, Object> commentListSelect() {
       HashMap<String, Object> result = new HashMap<>(16);
       List list = new ArrayList();
       HashMap user = new HashMap();
       user.put("name", "小明");
       Comment comment = new Comment();
       comment.setId(1L);
       comment.setCreateAt(System.currentTimeMillis());
       comment.setContent("这是留言内容,随便说点什么");
       list.add(user);
       list.add(comment);
       result.put("code", SUCCESS.getCode());
       result.put("msg", SUCCESS.getMsg());
       result.put("total", 10);
       result.put("data", list);
       return result;
   }

   /*评论查看*/
   @GetMapping("/a/u/comment")
   public Map<String, Object> commentSelect() {
       HashMap<String, Object> result = new HashMap<>(16);
       List list = new ArrayList();
       HashMap user = new HashMap();
       user.put("name", "小明");
       user.put("id", 10L);
       user.put("mail", "1234567@qq.com");
       user.put("phone", "13888888888");
       list.add(user);
       /*留言表内容*/
       Comment comment = new Comment();
       comment.setContent("这是留言内容,内容");
       comment.setStar(1);
       comment.setCreateAt(System.currentTimeMillis());
       list.add(comment);
       /*学习对象表内容*/
       StudyObject studyObject = new StudyObject();
       studyObject.setGrade(11);
       studyObject.setCourse(1);
       studyObject.setType(10);
       list.add(studyObject);
       result.put("code", SUCCESS.getCode());
       result.put("msg", SUCCESS.getMsg());
       result.put("data", list);
       return result;
   }

   /*评论删除*/
   @DeleteMapping("/a/u/comment")
   public Map<String, Object> commentDelete() {
       HashMap<String, Object> result = new HashMap<>(16);
       result.put("code", SUCCESS.getCode());
       result.put("msg", SUCCESS.getMsg());
       return result;
   }
}



明天计划的事情:

还有个小课堂要讲。争取把接口写完。
遇到的问题:

层级结构我不知道怎么处理。
收获:

学会了点怎么做假数据。


返回列表 返回列表
评论

    分享到