Completion of the management system (6)---Improve the back-end interface and front-end access (add and modify teachers, exams, and scores similar to students when connected to students’ queries)

The development of the back-end interface has been completed. The
back-end interface and front-end access have been improved. The
code has been updated https://github.com/dmhsq/edusys
github code is not yet available Fully commented (sleepy) The
current effect is as follows
Insert picture description here

Perfect backend

New modifications and new interfaces

@PostMapping("/admin/change")
    public RespBean change(@RequestParam("type") Integer type,
                           @RequestBody Object object){
    
    
        return reviseService.change(type,object);
    }


    @PostMapping("/admin/add")
    public RespBean add(@RequestParam("type") Integer type,
                           @RequestBody Object object){
    
    
        return addService.add(type,object);
    }

Modify service

Description

Determine the type according to the type and then convert the
conversion statement as follows

 ObjectMapper objectMapper =new ObjectMapper();
        Student student = objectMapper.convertValue(object,Student.class);
        Optional<Student> studentOptional = studentRepository.findById(student.getUserId());

Code

The addition and modification are similar but the following statements are deleted

Optional<Student> studentOptional = studentRepository.findById(student.getUserId());
        if (!studentOptional.isPresent()){
    
    
            return RespBean.error("失败","不存在学生信息");
        }

Modify the overall code block of the service as follows

package edusys.one.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import edusys.one.dao.ExamRepository;
import edusys.one.dao.ResultRepository;
import edusys.one.dao.StudentRepository;
import edusys.one.dao.TeacherRepository;
import edusys.one.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;


/**
 * @Author: 张灿
 * @Time: 2021/1/10 15:41
 */
@Service
public class ReviseService {
    
    

    @Autowired
    ExamRepository examRepository;

    @Autowired
    ResultRepository resultRepository;

    @Autowired
    StudentRepository studentRepository;

    @Autowired
    TeacherRepository teacherRepository;


    public RespBean change(Integer type,Object object){
    
    
        Integer zero=0,one=1,two=2,three=3;
        if (type.equals(zero)){
    
    
            return changeStu(object);
        }else if (type.equals(one)){
    
    
            return changeTch(object);
        }else if (type.equals(two)){
    
    
            return changeExam(object);
        }else if (type.equals(three)){
    
    
            return changeRust(object);
        }
        return RespBean.error("错误","错误");
    }


    private RespBean changeStu(Object object){
    
    
        ObjectMapper objectMapper =new ObjectMapper();
        Student student = objectMapper.convertValue(object,Student.class);
        Optional<Student> studentOptional = studentRepository.findById(student.getUserId());
        if (!studentOptional.isPresent()){
    
    
            return RespBean.error("失败","不存在学生信息");
        }
        studentRepository.saveAndFlush(student);
        return RespBean.ok("成功",true);
    }

    private RespBean changeTch(Object object){
    
    
        ObjectMapper objectMapper =new ObjectMapper();
        Teacher teacher = objectMapper.convertValue(object, Teacher.class);
        Optional<Teacher> teacherOptional = teacherRepository.findById(teacher.getTeacherId());
        if (!teacherOptional.isPresent()){
    
    
            return RespBean.error("失败","不存在老师信息");
        }
        teacherRepository.saveAndFlush(teacher);
        return RespBean.ok("成功",true);
    }

    private RespBean changeExam(Object object){
    
    
        ObjectMapper objectMapper =new ObjectMapper();
        Exam exam = objectMapper.convertValue(object, Exam.class);
        Optional<Exam> examOptional = examRepository.findById(exam.getExamId());
        if (!examOptional.isPresent()){
    
    
            return RespBean.error("失败","不存在老师信息");
        }
        examRepository.saveAndFlush(exam);
        return RespBean.ok("成功",true);
    }

    private RespBean changeRust(Object object){
    
    
        ObjectMapper objectMapper =new ObjectMapper();
        Result result = objectMapper.convertValue(object, Result.class);
        Optional<Result> resultOptional = resultRepository.findByUserIdEqualsAndExamIdEquals(result.getUserId(),result.getExamId());
        if (!resultOptional.isPresent()){
    
    
            return RespBean.error("失败","不存在考试/考生信息");
        }
        resultRepository.saveAndFlush(result);
        return RespBean.ok("成功",true);
    }

}

Front-end access

Configure proxy

Insert picture description here
Add the following code (will be modified after the current test)

devServer:{
    
    
    proxy:{
    
    
      "/api": {
    
    
        target: "http://localhost:8086/edusys/admin",
        changeOrigin: true,
        pathRewrite: {
    
    
          "^/api": ""
        }
      },
      "/login": {
    
    
        target:
          "http://localhost:8086/edusys/oauth/token?grant_type=password&client_id=password&client_secret=123&scope=all",
        changeOrigin: true,
        pathRewrite: {
    
    
          "^/login": ""
        }
      }
    }
  }

Insert picture description here

Configure interface access

Insert picture description here

Configure axios

Unified package has not been tested only three external interfaces Interface landed
in Insert picture description here
carrying access_token before each send

import axioes from "axios";
const axios = axioes.create({
    
    
  timeout: 3000,
  headers: {
    
    
    "Content-Type": "multipart/form-data"
  }
});

const login = (username, password) => {
    
    
  return axioes.post(`/login&username=${
      
      username}&password=${
      
      password}`);
};
//配置请求拦截器 在发送前携带access_token
axios.interceptors.request.use(
  config => {
    
    
    let token = localStorage.getItem("access_token");
    if (token) {
    
    
      config.headers.authorization = "bearer" + token;
    }
    return config;
  },
  error => {
    
    
    return Promise.reject(error);
  }
);


export {
    
     login, axios };

Configuration request method

Insert picture description here

import {
    
     login, axios } from "./axiosFun";

//查询信息
export const find = params => {
    
    
  return axios.get("/api/find", {
    
     params: params });
};
//修改信息
export const change = (type, data) => {
    
    
  return axios.post(`/api/change?type=${
      
      type}`, JSON.stringify(data),{
    
    headers:{
    
    "Content-Type": "application/json"}});
};
//新增
export const add = (type, data) => {
    
    
  return axios.post(`/api/add?type=${
      
      type}`, JSON.stringify(data),{
    
    headers:{
    
    "Content-Type": "application/json"}});
};

//登陆
export const doLogin = (username, password) => {
    
    
  return login(username, password);
};

Access

Insert picture description here

Description

The paging function is used here. The
code will be commented when placing the warehouse.
Insert picture description here

Access new

Insert picture description here

The content of the form is as follows
Insert picture description here

The front-end code will be placed on github after the basic function development is completed







  Hello, everyone, I am a code husky, a student of network engineering in the Software College, because I am a "dog", and I can eat meat for thousands of miles. I want to share what I learned during university and make progress with everyone. However, due to the limited level, there will inevitably be some mistakes in the blog. If there are any omissions, please let me know! For the time being, only update on the csdn platform, the blog homepage: https://blog.csdn.net/qq_42027681 .

未经本人允许,禁止转载

Insert picture description here


Will be launched later

Front-end: vue entry vue development applet, etc.
Back-end: java entry springboot entry, etc.
Server: MySQL entry server simple instructions cloud server to run the project
python: recommended not to warm up, be sure to see
the use of some plug-ins, etc.

The way of university is also in oneself, study hard, youth
with passion. If you are interested in programming, you can join our qq group to communicate together: 974178910
Insert picture description here

If you have any questions, you can leave a message below, I will reply if you see it

Guess you like

Origin blog.csdn.net/qq_42027681/article/details/112726376