准备文件API文档:
[YM]课设-C#-WebApi-Vue-员工管理系统 Api接口文档&SQL脚本-CSDN博客
响应类ResultModel:
注意到返回的数据是有格式的
用Model封装一下
打开心爱的idea
定义属性和构造方法
作为响应的返回类
定义成功和失败的方法
相当于是一个工具类
code=1操作成功
code=0操作失败
查询所有员工信息
功能已经在上一节连接数据库中的小案例实现了
[YM]课设-C#-WebApi-Vue-员工管理系统 (二)连接数据库-CSDN博客
根据id删除部门
请求路径:/depts/{id}
请求方式:DELETE
接口描述:该接口用于根据ID删除部门数据
controller:
//根据id删除部门
[HttpDelete]
[Route("/depts/{id}")]
public ResultModel DeleteById(int id){
bool res=deptService.DeleteById(id);
if(res==false)return ResultModel.error("删除错误");
return ResultModel.success();
}
res是标记是否删除成功,不成功要返回error信息
service:
IDeptService接口中声明
public bool DeleteById(int Id);
再到实现类DeptService中实现
public bool DeleteById(int Id){
return deptMapper.DeleteById(Id);
}
mapper:
public bool DeleteById(int Id){
var res=context.Dept.Find(Id);
if(res is null)return false;
context.Dept.Remove(res);
context.SaveChanges();
return true;
}
根据id从数据库中查找部门
res是null说明没有此部门,返回false
如果有此部门,调用函数Remove删除掉这个实体
再调用SaveChanges去更新现在的操作到数据库
一定一定要有此操作
不然只会在context中修改数据不会在数据库中修改
添加部门:
请求路径:/depts
请求方式:POST
接口描述:该接口用于添加部门数据
controller:
//添加部门
[HttpPost]
[Route("/depts")]
public ResultModel AddDept([FromBody]DeptModel dept){
deptService.AddDept(dept);
return ResultModel.success();
}
后端函数内参数是一个实体
从前端获取数据包装进DeptModel类中
在新版中[FromBody]是可以省略的,自动会转化成实体
为了不必要的麻烦还是手动加上了
service:
在IDeptService中声明
public void AddDept(DeptModel dept);
再到DeptService中实现
public void AddDept(DeptModel dept){
dept.createTime=DateTime.Now;
dept.updateTime=DateTime.Now;
deptMapper.AddDept(dept);
}
类中的创建时间和更改时间都设置为当前时间
传递给mapper层
mapper:
public void AddDept(DeptModel dept){
context.Dept.Add(dept);
context.SaveChanges();
}
拿到实体类
进行数据库更新操作
根据id查询部门
请求路径:/depts/{id}
请求方式:GET
接口描述:该接口用于根据ID查询部门数据
controller:
//根据id查询部门
[HttpGet]
[Route("/depts/{id}")]
public ResultModel FindById(int id){
var res=deptService.FindById(id);
if(res is null)return ResultModel.error("没有此用户");
return ResultModel.success(res);
}
没有此id部门就返回错误信息
存在的话就返回这个部门的信息
service:
接口声明:
public DeptModel FindById(int id);
实现类实现:
public bool DeleteById(int Id){
return deptMapper.DeleteById(Id);
}
mapper:
public DeptModel FindById(int id){
return context.Dept.Find(id);
}
find函数就是根据id查询
根据id修改部门
请求路径:/depts
请求方式:PUT
接口描述:该接口用于修改部门数据
controller:
//根据id修改部门
[HttpPut]
[Route("/depts")]
public ResultModel UpdateDept([FromBody]DeptModel dept){
var res=deptService.UpdateDept(dept);
if(res==false)return ResultModel.error("错误");
return ResultModel.success();
}
根据id修改,为什么这边获取的是实体类呢?
因为部门名称是唯一列,不允许重复
其它要更改的内容一起打包进实体类中
最后根据部门名称查询到id
再把当前这个获取的实体类赋值到根据id查询在数据库的实体类
service:
接口说明
public bool UpdateDept(DeptModel dept);
实现类实现
public bool UpdateDept(DeptModel dept){
return deptMapper.UpdateDept(dept);
}
mapper :
public bool UpdateDept(DeptModel dept){
var res=context.Dept.Find(dept.id);
if(res is null)return false;
var NewDept=context.Dept.Where(c=>c.name==dept.name);
if(NewDept is null){
res.updateTime=DateTime.Now;
res.name=dept.name;
context.SaveChanges();
return true;
}
return false;
}
controller层已经讲解了原理
看到这应该已经理解了
注意就是要把更新时间改成当前的时间
测试:
打开终端,启动应用
dotnet run
打开后端端口
地址后边加上swagger
依次测试即可
————————————
持续更新中...
关注[YM]课设专栏[YM]课设_夜喵YM的博客-CSDN博客
文章:
[YM]课设-C#-WebApi-Vue-员工管理系统 (前言)-CSDN博客