/// <summary> /// 修改密码 /// </summary> /// <param name="id">读者 ID</param> /// <param name="oldPw">原密码</param> /// <param name="newPw">新密码</param> public void ReaderPasswordChange(int id, string newPw, string oldPw = null) { if (!GlobalFunc.IsValidPassword(newPw)) { throw new Exception("Password Invalid"); } if (DbContext.DBstatic.Queryable <ReaderInfo>().InSingle(id) == null) { throw new Exception("Invalid ID"); } if (oldPw == null || GlobalFunc.VerifyPassword(id, oldPw)) { var salt = Guid.NewGuid().ToString(); var pwHash = GlobalFunc.EncryptPassword(newPw, salt); var newInfo = new RegisterModel { ID = id, PasswordHash = pwHash, Salt = salt }; ReaderInfoModel tmpDic; try { tmpDic = GetReaderInfo(id); } catch (MySqlException ex) { throw ex; } catch (Exception ex) { throw ex; } newInfo.UpdateInfo(tmpDic); var readerPw = new ReaderInfo().UpdatePassword(newInfo); try { DbContext.DBstatic.Updateable(readerPw).IgnoreColumns(true, ignoreAllDefaultValue: true) .ExecuteCommand(); } catch (MySqlException ex) { throw ex; } catch (Exception ex) { throw ex; } if (!GlobalFunc.VerifyPassword(id, newPw)) { throw new Exception("Failed Change"); } } else { throw new Exception("Wrong Password"); } }
/// <summary> /// 接受注册信息,并将用户信息写入数据库 /// </summary> /// <param name="ls">注册信息参数列表</param> /// <exception cref="MySqlException"></exception> /// <exception cref="Exception"></exception> /// <returns>用户 ID</returns> public int AccepteRegister(RegisterModel reg) { try { _ = GlobalFunc.CheckRegisterInput(reg); } catch (Exception ex) { throw ex; } if (reg != null) { var newReader = new ReaderInfo(); reg.Salt = Guid.NewGuid().ToString(); reg.PasswordHash = GlobalFunc.EncryptPassword(reg.Password, reg.Salt); newReader.SetInitial(reg); try { var id = DbContext.DBstatic.Insertable(newReader).ExecuteReturnIdentity(); Console.WriteLine("注册成功"); return(id); } catch (MySqlException ex) { throw ex; } catch (Exception ex) { throw ex; } } throw new Exception("Failed to register"); }
/// <summary> /// 获取读者信息 /// </summary> /// <param name="userID">用户 ID</param> /// <returns>信息字典{ 信息类型, 信息内容 }</returns> public ReaderInfoModel GetReaderInfo(int userID) { ReaderInfoModel info = null; if (userID < 10000) { throw new Exception("Permission Denied"); } if (GlobalFunc.FindPersonById(userID)) { try { info = DbContext.DBstatic.Queryable <ReaderInfo>() .Select(f => new ReaderInfoModel { ID = f.ID, Contact = f.Contact, Name = f.Name, Gender = f.Gender, Credit_Score = f.Credit_Score }) .Where(it => it.ID == userID).Single(); } catch { throw new Exception($"Failed to query user information with id: {userID}"); } } return(info); }
/// <summary> /// 借书 /// </summary> /// <param name="userId"></param> /// <param name="bookId"></param> /// <returns></returns> public Borrows BorrowBook(int userId, int bookId) { if (!GetBookState(bookId)) { throw new Exception("Books lent out"); } if (!GlobalFunc.GetBorrowState(userId)) { throw new Exception("Low credit score"); } //修改书的状态,前端应该已经完成对book的状态的检测 DbContext.DBstatic.Updateable <Book>().SetColumns(it => new Book { State = 1 }).Where(it => it.Id == bookId) .ExecuteCommand(); //插入borrow表 var result = DbContext.DBstatic.Saveable(new Borrows { Reader_id = userId, Book_id = bookId, Renew = 0, Borrow_Time = DateTime.Now.Date, Expire_Time = DateTime.Now.Date.AddMonths(1), State = 0 }).ExecuteReturnEntity(); return(result); throw new NotImplementedException(); }
/// <summary> /// 创建管理员账户 /// </summary> /// <param name="ls"></param> /// <exception cref="MySqlException"></exception> /// <exception cref="Exception"></exception> /// <returns>用户 ID</returns> public int CreateAdminAccount(RegisterModel reg) { int newAdId; try { _ = GlobalFunc.CheckRegisterInput(reg); } catch (Exception ex) { throw ex; } if (reg != null) { var newAdmin = new AdminInfo(); reg.Salt = Guid.NewGuid().ToString(); reg.PasswordHash = GlobalFunc.EncryptPassword(reg.Password, reg.Salt); newAdmin.SetInitial(reg); try { newAdId = DbContext.DBstatic.Insertable(newAdmin).ExecuteReturnIdentity(); } catch (MySqlException ex) { throw ex; } } else { throw new Exception("Failed to create account"); } return(newAdId); }
/// <summary> /// 获取读者信息 /// </summary> /// <param name="userID">用户 ID</param> /// <returns>用户信息模型</returns> public ReaderInfoModel GetReaderInfo(int userID) { ReaderInfoModel info = null; if (GlobalFunc.FindPersonById(userID)) { info = DbContext.DBstatic.Queryable <ReaderInfo>() .Select(f => new ReaderInfoModel { ID = f.ID, Contact = f.Contact, Name = f.Name, Gender = f.Gender, Credit_Score = f.Credit_Score }) .Where(it => it.ID == userID).Single(); } return(info); }
/// <summary> /// 修改读者信息 /// </summary> /// <param name="mod"></param> /// <exception cref="MySqlException">更新失败</exception> /// <exception cref="Exception">输入错误</exception> public void ReaderModifyInfo(ReaderInfoModel mod) { try { GlobalFunc.InfoCheck(mod, mod.Contact != null); } catch (Exception ex) { throw ex; } if (mod.ID < 10000) { throw new Exception("Permission Denied"); } ReaderInfoModel tmpDic; try { tmpDic = GetReaderInfo(mod.ID); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return; } tmpDic.UpdateInfo(mod); var newInfo = new ReaderInfo().UpdateInfo(tmpDic); try { DbContext.DBstatic.Updateable(newInfo).IgnoreColumns(true, ignoreAllDefaultValue: true) .ExecuteCommand(); } catch (MySqlException ex) { throw ex; } catch (Exception ex) { throw ex; } }
/// <summary> /// 还书后根据借阅 ID 查询是否超期,若超期则扣信用分 /// </summary> /// <param name="borrowId"></param> public void ReturnResult(int borrowId) { try { GlobalFunc.RefreshBookState(); var state = DbContext.DBstatic.Queryable <Borrows>() .Where(br => br.Borrow_id == borrowId) .Select(br => br.State) .First(); if (state == -1) { var userId = DbContext.DBstatic.Queryable <Borrows>() .Where(br => br.Borrow_id == borrowId) .Select(rd => rd.Reader_id) .First(); var score = DbContext.DBstatic.Queryable <ReaderInfo>() .Where(rd => rd.ID == userId) .Select(rd => rd.Credit_Score) .First(); var setScore = score - 30; if (setScore < 0) { setScore = 0; } DbContext.DBstatic.Updateable <Reader>() .SetColumns(rd => new Reader { credit_score = setScore }) .Where(rd => rd.id == userId) .ExecuteCommand(); } } catch { throw new Exception("Credit modification failed"); } }
public void Do() { GlobalFunc.RefreshBookState(); }