public async Task Delete(string id) { T obj = await GetById(id); _context.Set <T>().Remove(obj); await SaveChange(); }
public User CheckLogin(string username, string password) { var user = DataDbContext.Set <User>().FirstOrDefault(t => t.AccountName == username); if (user != null) { if (user.UserState == Models.Enum.UserStateEnum.Enable) { string dbPassword = Encrypt(password); if (dbPassword == user.Password) { user.LogOnCount += 1; user.LastModifyTime = DateTime.Now; DataDbContext.SaveChanges(); return(user); } else { user.ErrorTimes += 1; user.LastErrorDateTime = DateTime.Now; DataDbContext.SaveChanges(); throw new Exception("密码不正确,请重新输入"); } } else { throw new Exception("账户不可用,请联系管理员"); } } else { throw new Exception("账户不存在,请重新输入"); } }
private void Add(CustomerEditDto dto) { ValidateEditDto(dto); if (DataDbContext.Set <Customer>().Any(c => c.MobilePhoneNumber != null && c.MobilePhoneNumber == dto.MobilePhoneNumber && c.RealName == dto.RealName)) { throw new Exception($"错误,用户名:{dto.RealName},手机号:{dto.MobilePhoneNumber}的用户已经存在,请检查后重试!"); } var customer = dto.MapTo <Customer>(); if (dto.CustomerCategoryId > 0) { var category = DataDbContext.Set <CustomerCategory>().FirstOrDefault(cc => cc.Id == dto.CustomerCategoryId); if (category == null) { throw new Exception($"错误,Id={dto.CustomerCategoryId} 的客户分类不存在!"); } customer.CustomerCategory = category; } customer.CreatorTime = DateTime.Now; customer.LastModifyTime = DateTime.Now; DataDbContext.Set <Customer>().Add(customer); DataDbContext.SaveChanges(); }
public List <Customer> Search(CustomerSearchDto dto) { var dataSource = DataDbContext.Set <Customer>().AsQueryable(); if (dto.Gender.HasValue) { dataSource = dataSource.Where(c => c.Gender != null && c.Gender.Value == dto.Gender.Value); } if (dto.CustomerCategoryId > 0) { dataSource = dataSource.Where(c => c.CustomerCategory != null && c.CustomerCategory.Id == dto.CustomerCategoryId); } dataSource = dataSource.WhereDateTime(nameof(Customer.CreatorTime), dto.StartCreatorTime, dto.EndCreatorTime); if (!string.IsNullOrWhiteSpace(dto.Keywords)) { dataSource = dataSource.Where(c => c.RealName != null && c.RealName.Contains(dto.Keywords) || c.NickName != null && c.NickName.Contains(dto.Keywords) || c.MobilePhoneNumber != null && c.MobilePhoneNumber.Contains(dto.Keywords)); } dataSource = dataSource.OrderByDescending(a => a.LastModifyTime); if (dto.IsGetTotalCount) { dto.TotalCount = dataSource.Count(); } return(dataSource.Skip(dto.StartIndex).Take(dto.PageSize).ToList()); }
public List <TelephoneRecording> Search(TelephoneRecordingSearchDto dto) { var dataSource = DataDbContext.Set <TelephoneRecording>().AsQueryable(); if (dto.CustomerId > 0) { dataSource = dataSource.Where(c => c.CustomerId == dto.CustomerId); } if (dto.VisitorId > 0) { dataSource = dataSource.Where(c => c.VisitorId == dto.VisitorId); } dataSource = dataSource.WhereDateTime(nameof(TelephoneRecording.CreatorTime), dto.StartCreatorTime, dto.EndCreatorTime); if (!string.IsNullOrWhiteSpace(dto.Keywords)) { dataSource = dataSource.Where(c => c.AudioFileName != null && c.AudioFileName.Contains(dto.Keywords) || c.CustomerRealName != null && c.CustomerRealName.Contains(dto.Keywords) || c.VisitorNickName != null && c.VisitorNickName.Contains(dto.Keywords)); } dataSource = dataSource.OrderByDescending(a => a.LastModifyTime); if (dto.IsGetTotalCount) { dto.TotalCount = dataSource.Count(); } return(dataSource.Skip(dto.StartIndex).Take(dto.PageSize).ToList()); }
protected bool UserHasRole(Guid folderId, AccessLevel accessLevel) { var relation = _db.Set <UsersFolders>().FirstOrDefault(uf => uf.UserId == UserId && uf.FolderId == folderId); if (relation == null) { return(false); } return((relation.AccessLevel & accessLevel) == accessLevel); }
private void Add(TelephoneRecordingEditDto dto) { ValidateEditDto(dto); if (DataDbContext.Set <TelephoneRecording>().Any(tr => tr.AudioFileName != null && tr.AudioFileName == dto.AudioFileName)) { throw new Exception($"错误,录音文件:{dto.AudioFileName}已经存在,请检查后重试!"); } Customer customer = null; if (dto.CustomerId > 0) { customer = DataDbContext.Set <Customer>().FirstOrDefault(c => c.Id == dto.CustomerId); if (customer == null) { throw new Exception($"错误,Id={dto.CustomerId}的客户不存在,请检查后重试!"); } } var telephoneRecording = new TelephoneRecording { AudioFileName = dto.AudioFileName, Description = dto.Description }; if (customer != null) { telephoneRecording.CustomerId = customer.Id; telephoneRecording.CustomerRealName = customer.RealName; } User visitor = null; if (dto.VisitorId > 0) { visitor = DataDbContext.Set <User>().FirstOrDefault(c => c.Id == dto.VisitorId); if (visitor == null) { throw new Exception($"错误,Id={dto.VisitorId}的采访人不存在,请检查后重试!"); } } if (visitor != null) { telephoneRecording.VisitorId = visitor.Id; telephoneRecording.VisitorNickName = visitor.NickName; } telephoneRecording.CreatorTime = DateTime.Now; telephoneRecording.LastModifyTime = DateTime.Now; DataDbContext.Set <TelephoneRecording>().Add(telephoneRecording); DataDbContext.SaveChanges(); }
public void Update(UserEditDto dto) { var user = DataDbContext.Set <User>().FirstOrDefault(m => m.Id == dto.UpdateId); if (user == null) { throw new Exception($"错误:指定Id {dto.UpdateId} 的用户不存在!"); } ValidateUpdateDto(dto); dto.MapTo <User>(user); user.LastModifyTime = DateTime.Now; DataDbContext.SaveChanges(); }
private void Add(CustomerCategoryEditDto dto) { ValidateEditDto(dto); if (DataDbContext.Set <CustomerCategory>().Any(cc => cc.Name != null && cc.Name == dto.Name)) { throw new Exception($"错误,新增失败,名称:{dto.Name}的{_modelDescription}已经存在!"); } var customerCategory = dto.MapTo <CustomerCategory>(); customerCategory.CreatorTime = DateTime.Now; customerCategory.LastModifyTime = DateTime.Now; DataDbContext.Set <CustomerCategory>().Add(customerCategory); DataDbContext.SaveChanges(); }
public void Update(TelephoneRecordingEditDto dto) { var telephoneRecording = DataDbContext.Set <TelephoneRecording>().FirstOrDefault(tr => tr.Id == dto.UpdateId); if (telephoneRecording == null) { throw new Exception($"错误,Id={dto.UpdateId}的采访记录不存在,修改记录失败!"); } Customer customer = null; if (dto.CustomerId > 0) { customer = DataDbContext.Set <Customer>().FirstOrDefault(c => c.Id == dto.CustomerId); if (customer == null) { throw new Exception($"错误,Id={dto.CustomerId}的客户不存在,请检查后重试!"); } } if (customer != null) { telephoneRecording.CustomerId = customer.Id; telephoneRecording.CustomerRealName = customer.RealName; } User visitor = null; if (dto.VisitorId > 0) { visitor = DataDbContext.Set <User>().FirstOrDefault(c => c.Id == dto.VisitorId); if (visitor == null) { throw new Exception($"错误,Id={dto.VisitorId}的采访人不存在,请检查后重试!"); } } if (visitor != null) { telephoneRecording.VisitorId = visitor.Id; telephoneRecording.VisitorNickName = visitor.NickName; } telephoneRecording.Description = dto.Description; telephoneRecording.LastModifyTime = DateTime.Now; DataDbContext.SaveChanges(); }
public string GetFullPath(Guid currentFolderId) { if (currentFolderId == Guid.Empty) { return(string.Empty); } var currentFolder = _db.Set <Folder>().FirstOrDefault(f => f.Id == currentFolderId); return(GetFullPath(currentFolder.ParentFolderId) + @"\" + currentFolder.FolderName); }
public void Remove(params long[] ids) { if (ids == null || ids.Length == 0) { throw new Exception("错误,删除的序号为空!"); } foreach (var id in ids) { var data = DataDbContext.Set <User>().FirstOrDefault(b => b.Id == id); if (data == null) { throw new Exception($"错误,{_modelDescription}不存在!(Id:{id})"); } DataDbContext.Set <User>().Remove(data); } DataDbContext.SaveChanges(); }
public async Task Invoke(HttpContext context, ILogger <AuthMiddleware> logger) { Debugger.Log(1, "", $"{context.Request.Path}{context.Request.QueryString}"); var userId = context.Request.Headers["userId"]; if (!string.IsNullOrWhiteSpace(userId)) { var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, userId) }; ClaimsPrincipal userContext = new ClaimsPrincipal(new ClaimsIdentity(claims, "MyAuth")); context.User = userContext; await _next.Invoke(context); } else if (context.Request.Path.Value.Contains("auth")) { await _next.Invoke(context); } else { _db.Set <LogEntry>().Add(new LogEntry { Title = "Ошибка авторизации!", Message = $"UserId: {userId}", OperationName = "AuthService" }); await _db.SaveChangesAsync(); if (Boolean.TryParse(context.Request.Headers["godmode"], out var isGodMode) && isGodMode) { await _next.Invoke(context); } else { context.Response.StatusCode = 401; return; } } }
public void Remove(params long[] ids) { if (ids == null || ids.Length == 0) { throw new Exception("错误,删除的序号为空!"); } foreach (var id in ids) { var data = DataDbContext.Set <TelephoneRecording>().FirstOrDefault(b => b.Id == id); if (data == null) { throw new Exception($"错误,{_modelDescription}不存在!(Id:{id})"); } TelephoneRecordingFileHelper.RemoveFile(data.AudioFileName); DataDbContext.Set <TelephoneRecording>().Remove(data); } DataDbContext.SaveChanges(); }
public void Test_RoleAuth() { /* * drop table if exists RoleAuth; * create table RoleAuth (RoleId int not null comment '角色Id', * AuthId int not null comment '权限Id', * AuthFlag int not null comment '权限标志,0-AuthId代表权限, 1-AuthId代表角色', * primary key(RoleId, AuthId, AuthFlag)) * comment '角色中包含的权限和角色'; * -- 总经理角色 * insert into RoleAuth values(1, 1, 0); * insert into RoleAuth values(1, 2, 0); * -- 部门经理角色 * insert into RoleAuth values(2, 2, 0); * insert into RoleAuth values(2, 3, 1); -- 部门经理角色包含普通职员角色 * -- 普通职员角色 * insert into RoleAuth values(3, 3, 0); */ using (var dao = new DataDbContext()) { var roldAuths = new List <RoleAuth>() { new RoleAuth { RoleId = 1, AuthId = 1, RoleAuthFlag = 0 }, //总经理角色,可以查看公司财务报表 new RoleAuth { RoleId = 1, AuthId = 2, RoleAuthFlag = 0 }, //总经理角色,可以批阅请示报告 new RoleAuth { RoleId = 2, AuthId = 2, RoleAuthFlag = 0 }, //部门经理角色,可以批阅请示报告 new RoleAuth { RoleId = 2, AuthId = 3, RoleAuthFlag = 1 }, //部门经理角色,包含普通职员角色 new RoleAuth { RoleId = 3, AuthId = 3, RoleAuthFlag = 0 }, //普通职员角色,可以编写请示报告 }; dao.Set <RoleAuth>().AddRange(roldAuths); dao.SaveChanges(); } }
public List <CustomerCategory> Search(CustomerCategorySearchDto dto) { var dataSource = DataDbContext.Set <CustomerCategory>().AsQueryable(); if (!string.IsNullOrWhiteSpace(dto.Keywords)) { dataSource = dataSource.Where(cc => cc.Name != null && cc.Name.Contains(dto.Keywords) || cc.Description != null && cc.Description.Contains(dto.Keywords)); } dataSource = dataSource.WhereDateTime(nameof(Customer.CreatorTime), dto.StartCreatorTime, dto.EndCreatorTime); dataSource = dataSource.OrderByDescending(a => a.LastModifyTime); if (dto.IsGetTotalCount) { dto.TotalCount = dataSource.Count(); } return(dataSource.Skip(dto.StartIndex).Take(dto.PageSize).ToList()); }
public void OnException(ExceptionContext context) { var actionName = context.ActionDescriptor.DisplayName; var exceptionStack = context.Exception.StackTrace; var exceptionMessage = context.Exception.Message; var message = $"При выполнении {actionName} возникло исключение: \n {exceptionMessage}"; _db.Set <LogEntry>().Add(new LogEntry { Title = "Произошла ошибка!", Message = $"{message} \n {exceptionStack}", OperationName = actionName }); _db.SaveChanges(); context.HttpContext.Response.StatusCode = 500; context.Result = new ObjectResult($"Message: {message} \nStackTrace: {exceptionStack}"); context.ExceptionHandled = true; }
public void Test_Role() { using (var dao = new DataDbContext()) { var roles = new List <Role>() { new Role { RoleName = "总经理" }, new Role { RoleName = "部门经理" }, new Role { RoleName = "普通职员" }, }; dao.Set <Role>().AddRange(roles); dao.SaveChanges(); } }
public void Update(CustomerCategoryEditDto dto) { ValidateEditDto(dto); var customerCategory = DataDbContext.Set <CustomerCategory>().FirstOrDefault(c => c.Id == dto.UpdateId); if (customerCategory == null) { throw new Exception($"错误,Id={dto.UpdateId} 的{_modelDescription}不存在!"); } if (DataDbContext.Set <CustomerCategory>().Any(cc => cc.Id != customerCategory.Id && cc.Name != null && cc.Name == dto.Name)) { throw new Exception($"错误,修改失败,名称:{dto.Name}的{_modelDescription}已经存在!"); } dto.MapTo <CustomerCategory>(customerCategory); customerCategory.LastModifyTime = DateTime.Now; DataDbContext.SaveChanges(); }
public void Test_Auth() { using (var dao = new DataDbContext()) { var auths = new List <Auth>() { new Auth { AuthName = "查看公司财务报表", AuthUrl = "/Pages/Finance/ViewRpt", AuthMemo = "公司高层和财务经理可以查看公司财务报告" }, new Auth { AuthName = "批阅请示报告", AuthUrl = "/Pages/Dept/ViewRequest", AuthMemo = "公司高层和部门经理可以批阅请示" }, new Auth { AuthName = "编写请示报告", AuthUrl = "/Pages/Dept/WriteRequest", AuthMemo = "部门内的人员、部门经理都可以编写请示报告" }, }; dao.Set <Auth>().AddRange(auths); dao.SaveChanges(); } }
public List <User> Search(UserSearchDto dto) { var dataSource = DataDbContext.Set <User>().AsQueryable(); if (!string.IsNullOrEmpty(dto.Keywords)) { dataSource = dataSource.Where(m => (m.RealName != null && m.RealName.Contains(dto.Keywords)) || (m.NickName != null && m.NickName.Contains(dto.Keywords)) || (m.MobilePhoneNumber != null && m.MobilePhoneNumber.Contains(dto.Keywords))); } dataSource = dataSource.WhereDateTime(nameof(Customer.CreatorTime), dto.StartCreatorTime, dto.EndCreatorTime); dataSource = dataSource.OrderByDescending(m => m.LastModifyTime); if (dto.IsGetTotalCount) { dto.TotalCount = dataSource.Count(); } return(dataSource.Skip(dto.StartIndex).Take(dto.PageSize).ToList()); }
public void Test_UserAuth() { /* * drop table if exists UserAuth; * create table UserAuth (UserId int not null comment '用户Id', * AuthId int not null comment '权限Id', * AuthFlag int not null comment '权限标志,0-AuthId代表权限, 1-AuthId代表角色', * primary key(UserId, AuthId, AuthFlag)) * comment '用户所拥有的权限和角色'; * insert into UserAuth values(1, 1, 1); * insert into UserAuth values(2, 2, 1); * insert into UserAuth values(3, 2, 1); * insert into UserAuth values(3, 1, 0); -- 财务部经理,直接分配权限 * insert into UserAuth values(4, 3, 0); */ using (var dao = new DataDbContext()) { var userAuths = new List <UserAuth>() { new UserAuth { UserId = 1, AuthId = 1, RoleAuthFlag = 1 }, new UserAuth { UserId = 2, AuthId = 2, RoleAuthFlag = 1 }, new UserAuth { UserId = 3, AuthId = 2, RoleAuthFlag = 1 }, new UserAuth { UserId = 3, AuthId = 1, RoleAuthFlag = 0 }, new UserAuth { UserId = 4, AuthId = 3, RoleAuthFlag = 0 }, }; dao.Set <UserAuth>().AddRange(userAuths); dao.SaveChanges(); } }
public void Add(UserEditDto dto) { ValidateUpdateDto(dto); if (string.IsNullOrEmpty(dto.LoginPassword)) { throw new Exception("错误:用户密码不能为空!"); } if (DataDbContext.Set <User>().Any(u => u.AccountName == dto.AccountName)) { throw new Exception($"添加用户失败,{dto.AccountName}已存在!"); } var user = dto.MapTo <User>(); user.Password = Encrypt(dto.LoginPassword); user.CreatorTime = DateTime.Now; user.LastModifyTime = DateTime.Now; DataDbContext.Set <User>().Add(user); DataDbContext.SaveChanges(); }
public void Test_User() { /* * create table User (UserId int primary key comment '用户Id', * LoginName varchar(20) not null comment '登录名', * LoginPwd varchar(20) not null comment '登录密码', * UserName varchar(20) not null comment '用户姓名', * DeptId varchar(20) not null comment '所在部门Id', * PositionId int not null comment '职位Id', * Status int not null default 1 comment '用户状态,1-正常,0-停用', * Memo varchar(200) comment '备注') * comment '用户表'; * insert into user values(1, 'wangzong', 'wz123', '王总', '00', 1, 1, '公司总经理'); * insert into user values(2, 'zhangfei', 'zf123', '张飞', '0002', 2, 1, '研发部经理'); * insert into user values(3, 'huangyun', 'hy123', '黄云', '0003', 2, 1, '财务部经理'); * insert into user values(4, 'lilan', 'pw123', '李兰', '0002', 3, 1, '研发部工程师'); */ using (var dao = new DataDbContext()) { var users = new List <UserInfo>() { new UserInfo { LoginName = "wangzong", LoginPwd = "wz123", UserName = "******", DeptId = "00", Status = 1, Memo = "公司总经理" }, new UserInfo { LoginName = "zhangfei", LoginPwd = "zf123", UserName = "******", DeptId = "00", Status = 1, Memo = "研发部经理" }, new UserInfo { LoginName = "huangyun", LoginPwd = "hy123", UserName = "******", DeptId = "00", Status = 1, Memo = "财务部经理" }, new UserInfo { LoginName = "lilan", LoginPwd = "pw123", UserName = "******", DeptId = "00", Status = 1, Memo = "研发部工程师" }, }; dao.Set <UserInfo>().AddRange(users); dao.SaveChanges(); } }
public void Remove(params long[] ids) { if (ids == null || ids.Length == 0) { throw new Exception("错误,删除的序号为空!"); } foreach (var id in ids) { var data = DataDbContext.Set <CustomerCategory>().FirstOrDefault(b => b.Id == id); if (data == null) { throw new Exception($"错误,{_modelDescription}不存在!(Id:{id})"); } if (DataDbContext.Set <Customer>().Any(c => c.CustomerCategory != null && c.CustomerCategory.Id == data.Id)) { throw new Exception($"错误,有客户记录引用了{_modelDescription},请先删除客户资料重试!)"); } DataDbContext.Set <CustomerCategory>().Remove(data); } DataDbContext.SaveChanges(); }
public Repository(DataDbContext context) { _context = context; DbSet = _context.Set <T>(); }
// generic public async Task <IReadOnlyList <T> > ListAllAsync() { return(await _context.Set <T>().ToListAsync()); }
public void Add(T entity) { _dataDbContext.Set <T>().Add(entity); }
public async Task <List <T> > GetAll() { //await Task.Run(() => InitiateConnection()); return(_datadbContext.Set <T>().ToList()); }
public T Add(T entity) { Context.Set <T>().Add(entity); Context.SaveChangesAsync(); return(entity); }