public ResultInfo Save(UserParam param) { ResultInfo result = null; try { var entity = this.mapper.Map <UserEntity>(param); int userId; if (entity.Id == 0) { //user exists if (this.dbContext.UserRepository.Select.Where(m => m.UserName == entity.UserName).ToOne() != null) { return(new ResultInfo() { ResultCode = OcmStatusCode.USER_ALREADY_EXIST, IsSuccess = false }); } entity.Password = "******"; //insert this.dbContext.UserRepository.Add(entity); } else { var oldEntity = this.dbContext.UserRepository.Select.Where(m => m.Id == entity.Id).ToOne(); entity.UserName = oldEntity.UserName; entity.Password = oldEntity.Password; //update this.dbContext.UserRepository.Update(entity); } userId = entity.Id; if (param.Roles != null) { this.dbContext.UserRoleRepository.Remove(m => m.UserId == userId); //根据userid删除与角色的关联 foreach (var role in param.Roles) { this.dbContext.UserRoleRepository.Add(new UserRoleRelationEntity() { RoleId = role, UserId = userId }); } } this.dbContext.SaveChanges(); result = new ResultInfo(); } catch (Exception ex) { result = new ResultExceptionInfo() { Exception = ex }; } return(result); }
public ResultInfo Save(RoleParam param) { ResultInfo result = null; try { int roleId; var roleEntity = this.mapper.Map <RoleEntity>(param); if (roleEntity.Id == 0) { //insert this.dbContext.RoleRepository.Add(roleEntity); } else { //update this.dbContext.RoleRepository.Update(roleEntity); } roleId = roleEntity.Id; if (param.Resources != null) { this.dbContext.RoleResRepository.Remove(m => m.RoleId == roleId); foreach (var r in param.Resources) { this.dbContext.RoleResRepository.Add(new RoleResourceRelationEntity() { ResourceId = r.Id, Status = r.Status, RoleId = roleId }); } } this.dbContext.SaveChanges(); result = new ResultInfo(); } catch (Exception ex) { result = new ResultExceptionInfo() { Exception = ex }; } return(result); }
public ResultInfo Remove(int id) { ResultInfo result = null; try { this.dbContext.RoleRepository.Remove(m => m.Id == id); this.dbContext.RoleResRepository.Remove(m => m.RoleId == id); this.dbContext.SaveChanges(); result = new ResultInfo(); } catch (Exception ex) { result = new ResultExceptionInfo() { Exception = ex }; } return(result); }