最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

【独家】ASP.NET Core+Element+SQL Server开发校园图书管理系统(四)

来源:博客园

随着技术的进步,跨平台开发已经成为了标配,在此大背景下,ASP.NET Core也应运而生。本文主要基于ASP.NET Core+Element+Sql Server开发一个校园图书管理系统为例,简述基于MVC三层架构开发的常见知识点,前三篇篇文章简单介绍了如何搭建开发框架,登录功能,主页面功能,以及书室管理,书架管理功能的实现,本篇文章继续讲解书籍管理以及借还功能相关功能的开发,仅供学习分享使用,如有不足之处,还请指正。


(资料图片仅供参考)

涉及知识点

在本示例中,应用最多的就是如何Element中提供的组件,和控制器中业务逻辑处理,涉及知识点如下所示:

  • MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式,其中Controller(控制器)处理输入(写入数据库记录)。控制器Controller,是应用程序中处理用户交互的部分,通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。
  • Element组件库,一套为开发者、设计师和产品经理准备的基于 Vue 2.0 的桌面端组件库。可以大大提高开发效率,减少工作量。在主页面中,主要用到如下几种:
    • 表单控件el-form,由输入框、选择器、单选框、多选框等控件组成,用以收集、校验、提交数据到后台。
    • 列表控件el-table,用于展示多条结构类似的数据,可对数据进行排序、筛选、对比或其他自定义操作。主要用户显示结构化列表的数据。
    • 分页控件el-pagination,当数据量过多时,使用分页分解数据。
    • 弹出窗口el-dialog,在保留当前页面状态的情况下,告知用户并承载相关操作。主要用于弹出新建或编辑窗口。
  • axios组件,是一个基于promise 的网络请求库,axios本质上也是对原生XHR的封装,只不过它是Promise的实现版本,符合最新的ES规范。在本示例中,所有的前后端交互,均是通过axios库。

功能介绍

本文主要介绍书籍管理和借还管理两个功能,如下所示:

  1. 图书管理,主要包括书籍的检索,新增,修改,删除等基本功能,另外书籍的存放位置和书架ID相关,书架又与书室相关,所有相对比较复杂。
  2. 借还管理,主要根据用于输入或扫描的ISBN号,进行借阅以及归还,主要记录借阅人,借阅时间,借阅经手人,归还时间,归还经手人等信息。

图书管理

1. Book表结构

图书管理,主要是对Book表的CRUD操作,表结构如下所示:

其中BookRackId为书架ID,与BookRack表的外键。

2. Book表实体类

Book表实体类是数据表的数据映射,和数据表一一对应,如下所示:

1 namespace CLMS.Entity 2 { 3     ///  4     /// 图书实体 5     ///  6     public class BookEntity 7     { 8         ///  9         /// 唯一标识10         /// 11         public int Id { get; set; }12 13         /// 14         /// 图书编号15         /// 16         public string ISBN { get; set; }17 18         /// 19         /// 图书名称20         /// 21         public string Name { get; set; }22 23         /// 24         /// 图书作者25         /// 26         public string Author { get; set; }27 28         /// 29         /// 图书出版社30         /// 31         public string Publisher { get; set; }32 33         /// 34         /// 出版时间35         /// 36         public DateTime PublishDate { get; set; }37 38         /// 39         /// 图书类型40         /// 41         public string BookType { get; set; }42 43         /// 44         /// 描述45         /// 46         public string Description { get; set; }47 48         /// 49         /// 书架ID50         /// 51         public long BookRackId { get; set; }52 53         /// 54         /// 创建时间55         /// 56         public DateTime CreateTime { get; set; }57 58         /// 59         /// 当前登录的账号的ID60         /// 61         public int CreateUser { get; set; }62 63         /// 64         /// 最后编辑时间65         /// 66         public DateTime? LastEditTime { get; set; }67 68         /// 69         /// 最后修改人70         /// 71         public int LastEditUser { get; set; }72     }73 }

3. 图书管理页面布局

图书管理页面主要包括对书籍的查询,新增,编辑,删除等操作,页面布局如下所示:

1 
2 110

4. 图书管理数据交互

数据交互通过JS脚本进行,书写格式和VUE2.0保持一致,在页面启动时,加载所有的书室信息,并绑定到el-table对象,所以需要在mounted函数中增加调用向服务器端发出请求,当用户新增或编辑保存时,通过axios发送请求到服务端接口。

1 <script>  2     var app= new Vue({  3          el: "#app",  4          data:function() {  5            return {  6              queryCondition:{  7                 Name:"",  8                 Publisher:""  9              }, 10              formLabelWidth: "120px", 11              addOrEditForm:{ 12                 Id:0, 13                 ISBN: "", 14                 Name: "", 15                 Author: "", 16                 Publisher: "", 17                 PublishDate: "", 18                 BookType: "", 19                 Description: "", 20                 BookRackId:"", 21                 Location:"" 22              }, 23              total:0, 24              pageSize:10, 25              currentPage:1, 26              dialogFormVisible: false, 27              dialogLocationVisible:false, 28              tableData: [], 29              queryLocationCondition:{ 30                 Name:"", 31                 Publisher:"" 32              }, 33              locationData:[], 34              locationTotal:0, 35              locationPageSize:5, 36              locationCurrentPage:1, 37              locationCurrentRow: null, 38            } 39          }, 40          mounted:function(){ 41             this.query(1); 42          }, 43          methods: { 44            handleOpen(key, keyPath) { 45              console.log(key, keyPath); 46            }, 47            handleClose(key, keyPath) { 48              console.log(key, keyPath); 49            }, 50            formatter(row, column) { 51              return row.address; 52            }, 53            handleQuery(){ 54             this.query(1); 55            }, 56            handlePageChanged(val){ 57             this.query(val); 58            }, 59            handlePrevClick(){ 60             //query(this.currentPage); 61            }, 62            handleNextClick(){ 63             //query(this.currentPage); 64            }, 65            handleLocationPageChanged(val){ 66             this.queryLocation(val); 67            }, 68            handleLocationPrevClick(){ 69             //query(this.currentPage); 70            }, 71            handleLocationNextClick(){ 72             //query(this.currentPage); 73            }, 74            handleAdd(){ 75              this.addOrEditForm.Id=0; 76              this.addOrEditForm.ISBN= ""; 77              this.addOrEditForm.Name= ""; 78              this.addOrEditForm.Author= ""; 79              this.addOrEditForm.Publisher= ""; 80              this.addOrEditForm.PublishDate= ""; 81              this.addOrEditForm.BookType= ""; 82              this.addOrEditForm.Description= ""; 83              this.addOrEditForm.BookRackId=""; 84              this.addOrEditForm.Location=""; 85              this.dialogFormVisible=true; 86              console.log("add"); 87            }, 88            handleEdit(index,row){ 89             console.log("当前index="+index); 90             console.log(row); 91             this.addOrEditForm.Id=row.Id; 92             this.addOrEditForm.ISBN=row.ISBN; 93             this.addOrEditForm.Name=row.Name; 94             this.addOrEditForm.Author=row.Author; 95             this.addOrEditForm.Publisher=row.Publisher; 96             this.addOrEditForm.PublishDate=row.PublishDate; 97             this.addOrEditForm.BookType=row.BookType; 98             this.addOrEditForm.Description=row.Description; 99             this.addOrEditForm.BookRackId=row.BookRackId;100             this.addOrEditForm.Location=row.LibraryName+"-"+row.LibrarySubName+"-"+row.Row+"排"+row.Column+"列";;101             this.dialogFormVisible=true;102            },103            handleDelete(index,row){104             console.log("当前index="+index);105             console.log(row);106             this.$confirm("确定要删除编号为"+row.Id+"的书籍吗?", "提示", {107                 confirmButtonText: "确定",108                 cancelButtonText: "取消",109                 type: "warning"110             }).then(() => {111                 var that=this;112                 axios.post("/Book/Delete", {113                     Id:row.Id114                 }).then(function (response) {115                     if(response.status==200){116                         var msg = response.data;117                         console.log(msg);118                         if(msg.code=="0"){119                             //刷新页面120                             that.$message({121                                 type: "success",122                                 message: "删除成功!"123                               });124                             that.query(1);125                         }else{126                             that.$message.error(msg.message);127                         }128                     }129                     console.log(response);130                 }).catch(function (error) {131                     that.$message.error(error);132                 });133                  console.log("delete");134             }).catch(() => {135                 this.$message({136                 type: "info",137                 message: "已取消删除"138                 });          139             });140            },141            query(pageNum){142             var that = this;143             this.tableData=[];144             console.log("query");145             axios.get("/Book/Query", {params:{146                 Name:this.queryCondition.Name,147                 Publisher:this.queryCondition.Publisher,148                 PageSize:this.pageSize,149                 PageNum:pageNum150             }}).then(function (response) {151                 if(response.status==200){152                     var data = response.data;153                     var count=data.count;154                     that.total = count;155                     for (let i = 0; i < data.items.length; i++) {156                         that.tableData.push({157                             Id:data.items[i].id,158                             ISBN: data.items[i].isbn,159                             Name: data.items[i].name,160                             Author:  data.items[i].author,161                             Publisher: data.items[i].publisher,162                             PublishDate: data.items[i].publishDate,163                             Description:data.items[i].description,164                             BookType: data.items[i].bookType,165                             CreateTime: data.items[i].createTime,166                             LibraryName:data.items[i].libraryName,167                             LibrarySubName:data.items[i].librarySubName,168                             Row:data.items[i].row,169                             Column:data.items[i].column,170                         });171                     }172                 }173                 console.log(response);174             }).catch(function (error) {175                 console.log(error);176             });177            },178            handleLocation(){179             this.queryLocation(1);180             this.dialogLocationVisible=true;181            },182            handleLocationCurrentChange(row){183             this.locationCurrentRow=row;184            },185            queryLocation(pageNum){186             this.locationData=[];187             var that=this;188             console.log("location query");189             axios.get("/BookRack/Query",{params: {190                 Name:this.queryLocationCondition.Name,191                 SubName:this.queryLocationCondition.SubName,192                 PageSize:this.locationPageSize,193                 PageNum:pageNum194             }}).then(function (response) {195                 if(response.status==200){196                     var data = response.data;197                     var count=data.count;198                     that.locationTotal = count;199                     for (let i = 0; i < data.items.length; i++) {200                         that.locationData.push({201                             Id: data.items[i].id,202                             libraryId:data.items[i].libraryId,203                             Name: data.items[i].name,204                             SubName:  data.items[i].subName,205                             Row : data.items[i].row,206                             Column : data.items[i].column,207                             Location : data.items[i].location,208                             Description:data.items[i].description,209                             CreateTime: data.items[i].createTime,210                         });211                     }212                 }213                 console.log(that.locationData);214                 console.log(response);215             }).catch(function (error) {216                 console.log(error);217             });218            },219            handleLocationSave(){220             console.log(this.locationCurrentRow);221             if(this.locationCurrentRow!=null){222                 this.addOrEditForm.BookRackId=this.locationCurrentRow.Id;223                 this.addOrEditForm.Location=this.locationCurrentRow.Name+"-"+this.locationCurrentRow.SubName+"-"+this.locationCurrentRow.Row+"排"+this.locationCurrentRow.Column+"列";224             }225             this.dialogLocationVisible=false;226            },227            handleSave(){228             var that=this;229             axios.post("/Book/Add", {230                 Id:this.addOrEditForm.Id,231                 ISBN: this.addOrEditForm.ISBN,232                 Name: this.addOrEditForm.Name,233                 Author: this.addOrEditForm.Author,234                 Publisher: this.addOrEditForm.Publisher,235                 PublishDate: this.addOrEditForm.PublishDate,236                 BookType: this.addOrEditForm.BookType,237                 Description: this.addOrEditForm.Description,238                 BookRackId:this.addOrEditForm.BookRackId,239             }).then(function (response) {240                 if(response.status==200){241                     var msg = response.data;242                     console.log(msg);243                     if(msg.code=="0"){244                         that.dialogFormVisible=false;245                         //刷新页面246                         that.query(1);247                     }else{248                         window.alert(msg.message);249                     }250                     console.log(that.dialogFormVisible);251                 }252                 console.log(response);253             }).catch(function (error) {254                 console.log(error);255             });256              console.log("save");257            },258          }259        });260 </script>

5. 图书控制器逻辑BookController

控制器主要用于响应用户的请求,与数据库交互,并返回执行的结果信息。

1 namespace CLMS.Host.Controllers  2 {  3     public class BookController : Controller  4     {  5         private DataContext dataContext;  6   7         public BookController(DataContext context) {   8             dataContext = context;  9         } 10  11         public IActionResult Index() 12         { 13             return View(); 14         } 15  16         ///  17         /// 获取符合条件的查询 18         ///  19         ///  20         ///  21         ///  22         ///  23         ///  24         [HttpGet] 25         public PagedRequest Query(string Name, string Publisher, int pageNum, int pageSize) 26         { 27             Name = string.IsNullOrEmpty(Name) ? string.Empty : Name; 28             Publisher = string.IsNullOrEmpty(Publisher) ? string.Empty : Publisher; 29             var bookEntities = dataContext.Books.Where(r => r.Name.Contains(Name) && r.Publisher.Contains(Publisher)); 30             var total = bookEntities.Count(); 31             var bookDtos = bookEntities.Skip((pageNum - 1) * pageSize).Take(pageSize).Select(r => new Book() { Id = r.Id, ISBN = r.ISBN, Name = r.Name, Author = r.Author, Publisher = r.Publisher, BookType = r.BookType,BookRackId=r.BookRackId,PublishDate=r.PublishDate, CreateTime = r.CreateTime,Description=r.Description }).ToList(); 32  33             //位置 34             var bookRackIds = bookDtos.Select(r => r.BookRackId).ToList(); 35             var locations = dataContext.BookRacks.Where(r => bookRackIds.Contains(r.Id)).Join(dataContext.Librarys, b => b.LibraryId, l => l.Id, (b, l) => new BookRack() { Name = l.Name, SubName = l.SubName, Location = l.Location, LibraryId = b.LibraryId, Id = b.Id, Row = b.Row, Column = b.Column, Description = b.Description, CreateTime = b.CreateTime }).ToList(); 36  37             bookDtos.ForEach(r => { 38                 var location = locations.FirstOrDefault(l => l.Id == r.BookRackId); 39                 if (location != null) { 40                     r.LibraryName = location.Name; 41                     r.LibrarySubName=location.SubName; 42                     r.Row=location.Row; 43                     r.Column=location.Column; 44                 } 45             }); 46             // 47             return new PagedRequest() 48             { 49                 count = total, 50                 items = bookDtos, 51             }; 52         } 53  54         [Consumes("application/json")] 55         [HttpPost] 56         public Msg Add([FromBody] Book book) 57         { 58             Msg msg = new Msg(); 59             if (book == null) 60             { 61                 msg.code = 1; 62                 msg.message = "对象为空"; 63                 return msg; 64             } 65             else 66             { 67                 var userId = HttpContext.Session.GetInt32("UserId"); 68  69                 if (book.Id > 0) 70                 { 71                     //更新 72                     var entity = dataContext.Books.Where(r => r.Id == book.Id).FirstOrDefault(); 73                     if (entity != null) 74                     { 75                         entity.BookRackId = book.BookRackId; 76                         entity.Author = book.Author; 77                         entity.Publisher = book.Publisher; 78                         entity.Description = book.Description; 79                         entity.BookType = book.BookType; 80                         entity.ISBN = book.ISBN; 81                         entity.Name = book.Name; 82                         entity.LastEditUser = userId.GetValueOrDefault(); 83                         entity.LastEditTime = DateTime.Now; 84                         dataContext.Books.Update(entity); 85                         dataContext.SaveChanges(); 86                     } 87                     else 88                     { 89                         msg.code = 1; 90                         msg.message = "修改失败"; 91                         return msg; 92                     } 93                 } 94                 else 95                 { 96                     //新增 97                     var entity = new BookEntity() 98                     { 99                         BookRackId = book.BookRackId,100                         Author = book.Author,101                         Publisher = book.Publisher,102                         PublishDate = book.PublishDate,103                         Description = book.Description,104                         BookType = book.BookType,105                         ISBN = book.ISBN,106                         Name = book.Name,107                         CreateTime = DateTime.Now,108                         CreateUser = userId.GetValueOrDefault(),109                         LastEditTime = DateTime.Now,110                         LastEditUser = userId.GetValueOrDefault(),111                     };112                     dataContext.Books.Add(entity);113                     dataContext.SaveChanges();114                 }115                 msg.code = 0;116                 msg.message = "success";117                 return msg;118             }119         }120 121         [Consumes("application/json")]122         [HttpPost]123         public Msg Delete([FromBody] Book book) {124             Msg msg = new Msg();125             if (book == null)126             {127                 msg.code = 1;128                 msg.message = "对象为空";129                 return msg;130             }131             else132             {133                 if (book.Id > 0)134                 {135                     var entity = dataContext.Books.Where(r => r.Id == book.Id).FirstOrDefault();136                     if (entity != null)137                     {138                         dataContext.Books.Remove(entity);139                         dataContext.SaveChanges();140                         msg.code = 0;141                         msg.message = "success";142                     }143                     else { 144                         msg.code = 1;145                         msg.message = "对象不存在或已被删除";146                     }147                 }148                 else {149 150                     msg.code = 1;151                     msg.message = "对象Id小于0";152                 }153                 return msg;154             }155         }156     }157 }

6. 图书管理功能测试

经过以上几个步骤,即可完成图书管理的基本操作,主要包括图书的查询,新增,编辑,删除,已经分页等功能,如下所示:

图书借还

1. 图书借还数据表结构

图书借还包括图书的借阅和归还,两个功能,主要记录借阅人,借阅时间,归还时间,以及经手人,数据表结构如下所示:

2. 图书借还实体类

数据表实体类和数据表一一对应,主要通过EntityFrameword与数据库进行映射。如下所示:

1 namespace CLMS.Entity 2 { 3     ///  4     /// 借还记录 5     ///  6     public class CirculateEntity 7     { 8         ///  9         /// 唯一标识10         /// 11         public int Id { get; set; }12 13         /// 14         /// 图书标识15         /// 16         public int BookId { get; set; }17 18         /// 19         /// 是否归还20         /// 21         public bool IsReturn { get; set; }22 23         /// 24         /// 借阅人25         /// 26         public string BorrowUser { get; set; }27 28         /// 29         /// 借阅时间30         /// 31         public DateTime BorrowTime { get; set; }32 33         /// 34         /// 借阅确认人35         /// 36         public string BorrowConfirmor { get; set; }37 38         /// 39         /// 归还时间40         /// 41         public DateTime? ReturnTime { get; set; }42 43         /// 44         /// 归还确认人45         /// 46         public string? ReturnConfirmor { get; set; }47     }48 }

3. 图书借还页面布局

图书借还主要包括信息查询,借阅和归还等功能,页面布局如下所示:

1 
2 64

4. 图书借还数据交互

数据交互通过JS脚本进行,书写格式和VUE2.0保持一致,在页面启动时,加载所有的图书借还信息,并绑定到el-table对象,所以需要在mounted函数中增加调用向服务器端发出请求,当用户借阅或归还保存时,通过axios发送请求到服务端接口。

1 <script>  2     var app= new Vue({  3          el: "#app",  4          data:function() {  5            return {  6                queryCondition:{  7                    Name:""  8                },  9                formLabelWidth: "120px", 10                addOrEditForm:{ 11                 Id:0, 12                 ISBN: "", 13                 Name: "", 14                 BorrowConfirmor:  "", 15                 BorrowTime: "", 16                 BorrowUser: "", 17                 IsReturn:"", 18                 ReturnConfirmor: "", 19                 ReturnTime: "", 20                }, 21                borrowForm:{ 22                 Id:0, 23                 ISBN: "", 24                 Name: "", 25                 BorrowUser:"" 26                }, 27                returnForm:{ 28                 Id:0, 29                 ISBN: "", 30                 Name: "", 31                }, 32                total:0, 33                pageSize:10, 34                currentPage:1, 35                tableData: [], 36                dialogFormBorrowVisible: false, 37                dialogFormReturnVisible: false, 38            } 39          }, 40          mounted:function(){ 41             this.query(1); 42          }, 43          methods: { 44             handleOpen(key, keyPath) { 45                 console.log(key, keyPath); 46             }, 47             handleClose(key, keyPath) { 48                 console.log(key, keyPath); 49             }, 50             formatter(row, column) { 51                 return row.address; 52             }, 53             handleQuery(){ 54                 console.log("query"); 55                 this.query(1); 56             }, 57             handlePageChanged(val){ 58                 this.query(val); 59             }, 60             handlePrevClick(){ 61                 //query(this.currentPage); 62             }, 63             handleNextClick(){ 64                 //query(this.currentPage); 65             }, 66             handleBorrow(){ 67                 this.dialogFormBorrowVisible=true; 68             }, 69             handleReturn(){ 70                 this.dialogFormReturnVisible=true; 71             }, 72             handleSaveBorrow(){ 73                 this.$confirm("确定要借阅编号为"+this.borrowForm.ISBN+"的书籍吗?", "提示", { 74                     confirmButtonText: "确定", 75                     cancelButtonText: "取消", 76                     type: "warning" 77                 }).then(() => { 78                     var that=this; 79                     axios.post("/Circulate/Borrow", { 80                         Id:that.borrowForm.Id, 81                         ISBN:that.borrowForm.ISBN, 82                         Name:that.borrowForm.Name, 83                         BorrowUser:that.borrowForm.BorrowUser, 84                     }).then(function (response) { 85                         if(response.status==200){ 86                             var msg = response.data; 87                             console.log(msg); 88                             if(msg.code=="0"){ 89                                 //刷新页面 90                                 that.dialogFormBorrowVisible=false; 91                                 that.$message({ 92                                     type: "success", 93                                     message: "借阅成功!" 94                                   }); 95                                 that.query(1); 96                             }else{ 97                                 that.$message.error(msg.message); 98                             } 99                         }100                         console.log(response);101                     }).catch(function (error) {102                         that.$message.error(error);103                     });104                      console.log("delete");105                 }).catch(() => {106                     this.$message({107                     type: "info",108                     message: "已取消借阅"109                     });          110                 });111             },112             handleSaveReturn(){113                 this.$confirm("确定要归还编号为"+this.returnForm.ISBN+"的书籍吗?", "提示", {114                     confirmButtonText: "确定",115                     cancelButtonText: "取消",116                     type: "warning"117                 }).then(() => {118                     var that=this;119                     axios.post("/Circulate/Return", {120                         Id:that.returnForm.Id,121                         ISBN:that.returnForm.ISBN,122                         Name:that.returnForm.Name,123                     }).then(function (response) {124                         if(response.status==200){125                             var msg = response.data;126                             console.log(msg);127                             if(msg.code=="0"){128                                 //刷新页面129                                 that.dialogFormReturnVisible=false;130                                 that.$message({131                                     type: "success",132                                     message: "归还成功!"133                                   });134                                 that.query(1);135                             }else{136                                 that.$message.error(msg.message);137                             }138                         }139                         console.log(response);140                     }).catch(function (error) {141                         that.$message.error(error);142                     });143                      console.log("delete");144                 }).catch(() => {145                     this.$message({146                     type: "info",147                     message: "已取消归还"148                     });          149                 });150             },151             query(pageNum){152                 var that = this;153                 this.tableData=[];154                 console.log("query");155                 axios.get("/Circulate/Query", {params:{156                     Name:this.queryCondition.Name,157                     PageSize:this.pageSize,158                     PageNum:pageNum159                 }}).then(function (response) {160                     if(response.status==200){161                         var data = response.data;162                         var count=data.count;163                         that.total = count;164                         for (let i = 0; i < data.items.length; i++) {165                             that.tableData.push({166                                 Id:data.items[i].id,167                                 ISBN: data.items[i].isbn,168                                 Name: data.items[i].name,169                                 BorrowConfirmor:  data.items[i].borrowConfirmor,170                                 BorrowTime: data.items[i].borrowTime,171                                 BorrowUser: data.items[i].borrowUser,172                                 IsReturn:data.items[i].isReturn==true?"已归还":"未归还",173                                 ReturnConfirmor: data.items[i].returnConfirmor,174                                 ReturnTime: data.items[i].returnTime,175                             });176                         }177                     }178                     console.log(response);179                 }).catch(function (error) {180                     console.log(error);181                 });182            },183         }184     });185 </script>

5. 图书借还控制器CirculateController

控制器主要用于响应用户的请求,与数据库交互,并返回执行的结果信息。

1 namespace CLMS.Host.Controllers  2 {  3     ///   4     /// 借还管理  5     ///   6     public class CirculateController : Controller  7     {  8         private DataContext dataContext;  9  10         public CirculateController(DataContext context) 11         { 12             dataContext = context; 13         } 14  15         public IActionResult Index() 16         { 17             return View(); 18         } 19  20         [HttpGet] 21         public PagedRequest Query(string Name, int pageNum, int pageSize) 22         { 23             Name = string.IsNullOrEmpty(Name) ? string.Empty : Name; 24             var dtos = dataContext.Circulates.Join(dataContext.Books, c => c.BookId, b => b.Id, (c, b) => new Circulate() 25             { 26                 Id = c.Id, 27                 Name = b.Name, 28                 BookId = c.BookId, 29                 BorrowConfirmor = c.BorrowConfirmor, 30                 BorrowTime = c.BorrowTime, 31                 BorrowUser = c.BorrowUser, 32                 ISBN = b.ISBN, 33                 IsReturn = c.IsReturn, 34                 ReturnConfirmor = c.ReturnConfirmor, 35                 ReturnTime = c.ReturnTime, 36             }).Where(r=>r.Name.Contains(Name)); 37             var total = dtos.Count(); 38             var dtos2 = dtos.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList(); 39             // 40             return new PagedRequest() 41             { 42                 count = total, 43                 items = dtos2, 44             }; 45         } 46  47         [Consumes("application/json")] 48         [HttpPost] 49         public Msg Borrow([FromBody]Borrow borrow) {  50             Msg msg = new Msg(); 51             if (borrow == null || string.IsNullOrEmpty(borrow.ISBN)) 52             { 53                 msg.code = 1; 54                 msg.message = "书籍为空"; 55                 return msg; 56             } 57             var book = dataContext.Books.FirstOrDefault(r => r.ISBN == borrow.ISBN); 58             if (book == null) 59             { 60                 msg.code = 1; 61                 msg.message = "ISBN有误"; 62                 return msg; 63             } 64             var entity = dataContext.Circulates.FirstOrDefault(r => r.BookId == book.Id && r.IsReturn == false); 65             if (entity != null) 66             { 67                 msg.code = 1; 68                 msg.message = "书籍已被借阅"; 69                 return msg; 70             } 71             var userId = HttpContext.Session.GetInt32("UserId"); 72             if (userId < 0) { 73                 msg.code = 1; 74                 msg.message = "尚未登录"; 75                 return msg; 76             } 77             var borrorConfirmor = dataContext.Users.FirstOrDefault(r => r.Id == userId)?.NickName; 78             var entity2  = new CirculateEntity() 79             { 80                 Id = 0, 81                 BookId = book.Id, 82                 IsReturn = false, 83                 BorrowTime = DateTime.Now, 84                 BorrowUser=borrow.BorrowUser, 85                 BorrowConfirmor= borrorConfirmor, 86             }; 87             this.dataContext.Circulates.Add(entity2); 88             this.dataContext.SaveChanges(); 89             msg.code = 0; 90             msg.message = "success"; 91             return msg; 92         } 93  94         ///  95         /// 归还 96         ///  97         ///  98         ///  99         [Consumes("application/json")]100         [HttpPost]101         public Msg Return([FromBody] Return returns) { 102             Msg msg = new Msg();103             if (returns == null || string.IsNullOrEmpty(returns.ISBN))104             {105                 msg.code = 1;106                 msg.message = "书籍为空";107                 return msg;108             }109             var book = dataContext.Books.FirstOrDefault(r => r.ISBN == returns.ISBN);110             if (book == null)111             {112                 msg.code = 1;113                 msg.message = "ISBN有误";114                 return msg;115             }116             var userId = HttpContext.Session.GetInt32("UserId");117             if (userId < 0)118             {119                 msg.code = 1;120                 msg.message = "尚未登录";121                 return msg;122             }123             var returnConfirmor = dataContext.Users.FirstOrDefault(r => r.Id == userId)?.NickName;124             var entity = dataContext.Circulates.FirstOrDefault(r => r.BookId == book.Id && r.IsReturn == false);125             if (entity != null)126             {127                 entity.IsReturn = true;128                 entity.ReturnTime = DateTime.Now;129                 entity.ReturnConfirmor=returnConfirmor;130                 dataContext.Circulates.Update(entity);131                 dataContext.SaveChanges();132                 msg.code = 0;133                 msg.message = "success";134             }135             else {136                 msg.code = 1;137                 msg.message = "书籍已归还";138             }139             return msg;140         }141     }142 }

6. 图书借还功能测试

图书借还主要包括借阅和归还,如下所示:

以上就是校园图书管理系统的图书管理及图书借还功能实现,功能正在开发完善中,后续功能再继续介绍。旨在抛砖引玉,一起学习,共同进步。

关键词: 图书管理 主要包括 刷新页面