private async Task <(bool sucess, string errMessage)> AddCourseAsync(AddOrUpdateCourseDto dto) { //先判断用户是不是存在了课程名称 bool existCourse = await db.Courses.AnyAsync(e => e.UserId == dto.UserId && e.Name == dto.Name); if (existCourse) { return(false, "添加课程失败,已经存在了该课程"); } var now = DateTime.UtcNow; var id = Guid.NewGuid().ToString("N"); await db.Courses.AddAsync(new Course { Id = id, Desc = dto.Desc, AddDateTime = now, UserId = dto.UserId, Name = dto.Name, LastModifyDatetime = now }); int rows = await db.SaveChangesAsync(); return(rows > 0, null); }
public async Task <IActionResult> PutUser(int id, User user) { if (id != user.Id) { return(BadRequest()); } _context.Entry(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ResultModel> CreateRoles(RolesEditDto rolesEditDto) { if (string.IsNullOrWhiteSpace(rolesEditDto.RoleName)) { return(new ResultModel(ResultCode.Fail, "请输入角色名称")); } if (_userDatabaseContext.Roles.Any(x => x.RoleName == rolesEditDto.RoleName)) { return(new ResultModel(ResultCode.Fail, "角色已存在")); } var result = new ResultModel(); var resultStatus = new ResultStatus(); try { var roles = new Roles(); roles.RoleName = rolesEditDto.RoleName; roles.Id = Guid.NewGuid(); _userDatabaseContext.Roles.Add(roles); await _userDatabaseContext.SaveChangesAsync(); resultStatus.text = "创建角色成功"; resultStatus.code = ResultCode.Success; result.status = resultStatus; return(result); } catch (Exception ex) { return(new ResultModel(ResultCode.Fail, ex.Message)); } }
/// <summary> /// Adding the specified employee details /// </summary> /// <param name="employee">The employee</param> /// <returns></returns> public Task <int> AddEmployee(GreetingModel employee) { userDbContext.Employee.Add(employee); var result = userDbContext.SaveChangesAsync(); return(result); }
/// <summary> /// 添加文章分类 /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <Result> AddArticalClassificationAsync(AddArticalClassificationDto dto) { Result respone = new Result(); do { //using(var step= MiniProfiler.Current.Step("添加文章分类")) { var exist = await db.ArticalClassifications.AnyAsync(e => e.Name == dto.Name); if (exist) { respone.ErrorMessage = "已经存在相同名称分类"; break; } await db.ArticalClassifications.AddAsync(new ArticleClassification { Id = GuidEx.NewGuid(), Name = dto.Name, }); await db.SaveChangesAsync(); //移除cache await cacheService.RemoveAsync(CacheStringArticlesTypes); respone.Succeeded = true; } } while (false); return(respone); }
public async Task <ResultModel> ConfigClientCorsOrigin(List <ClientCorsOriginEditDto> clientCorsOriginCreateDtos, int clientId) { var client = await _userDatabaseContext.Clients.Where(x => x.Id == clientId) .Include(x => x.AllowedCorsOrigins) .FirstOrDefaultAsync(); List <ClientCorsOrigin> clientCorsOrigins = new List <ClientCorsOrigin>(); foreach (var item in clientCorsOriginCreateDtos) { clientCorsOrigins.Add(new ClientCorsOrigin { Origin = item.Origin }); } client.AllowedCorsOrigins = clientCorsOrigins; try { _userDatabaseContext.Clients.Update(client); await _userDatabaseContext.SaveChangesAsync(); return(new ResultModel(ResultCode.Success, "更新客户端跨域配置成功")); } catch (Exception ex) { return(new ResultModel(ResultCode.Fail, ex.Message)); } }
public async Task <bool> CreateAsync(User user) { var success = false; userDatabaseContext.users.Add(user); var numRows = await userDatabaseContext.SaveChangesAsync(); if (numRows == 1) { success = true; } return(success); }
/// <summary> /// 删除教师班级 /// </summary> /// <param name="classId"></param> /// <param name="m"></param> /// <returns></returns> public async Task <IActionResult> OnPostDeleteTeacherClassAsync([FromBody] DeleteTeacherClassViewModel model) { if (!TryValidateModel(model)) { return(BadRequest(ModelState)); } var entity = await UserDatabaseContext.TeacherClasses .FirstOrDefaultAsync(e => e.ClassId == model.ClassId && e.TeacherId == CurUserId).ConfigureAwait(false); if (entity == null) { return(NotFound()); } var classInfo = await UserClient.GetSchoolClassByIdAsync(new UserGrpcService.IdReq { Id = entity.ClassId }); if (!classInfo.Name.Equals(model.ClassName)) { AddModelError("不正确的班级名称输入"); return(BadRequest(ModelState)); } UserDatabaseContext.TeacherClasses.Remove(entity); await UserDatabaseContext.SaveChangesAsync().ConfigureAwait(false); return(new OkResult()); }
/// <summary> /// 添加教师班级 /// </summary> /// <param name="m"></param> /// <returns></returns> public async Task <IActionResult> OnPostAddTeacherClassAsync([FromBody] AddTeacherClassDto dto) { if (!TryValidateModel(dto)) { return(BadRequest(ModelState)); } var exist = await UserDatabaseContext.TeacherClasses .AnyAsync(e => e.TeacherId == CurUserId && e.ClassId == dto.ClassId) .ConfigureAwait(false); if (exist) { ModelState.AddModelError(string.Empty, "重复添加"); return(BadRequest(ModelState)); } await UserDatabaseContext.TeacherClasses.AddAsync(new Database.Entity.UserEntities.TeacherClass { TeacherId = CurUserId, ClassId = dto.ClassId }); int rows = await UserDatabaseContext.SaveChangesAsync().ConfigureAwait(false); if (rows == 0) { ModelState.AddModelError(string.Empty, "没有添加任何记录"); return(BadRequest(ModelState)); } return(new OkResult()); }
/// <summary> /// 添加教师班级 /// </summary> /// <param name="m"></param> /// <returns></returns> public async Task <IActionResult> OnPostAddTeacherClassAsync( [FromBody] Study.Services.UserService.Req.AddTeacherClassDto addTeacherClassModel) { addTeacherClassModel.TeacherId = CurUserId; if (!TryValidateModel(addTeacherClassModel)) { return(BadRequest(ModelState)); } var exist = await UserDatabaseContext.TeacherClasses .AnyAsync(e => e.ClassId == addTeacherClassModel.ClassId && e.TeacherId == CurUserId) .ConfigureAwait(false); if (exist) { AddModelError("已经存在班级"); return(BadRequest(ModelState)); } await UserDatabaseContext.TeacherClasses.AddAsync(new TeacherClass { TeacherId = addTeacherClassModel.TeacherId, ClassId = addTeacherClassModel.ClassId }); await UserDatabaseContext.SaveChangesAsync().ConfigureAwait(false); return(new OkResult()); }
public async Task <ResultModel> CreateApiScope(ApiScopeEditDto dto) { try { List <ApiScopeClaim> apiScopeClaims = new List <ApiScopeClaim>(); foreach (var item in dto.UserClaims) { apiScopeClaims.Add(new ApiScopeClaim { Type = item.Type }); } var apiScope = new ApiScope { Enabled = dto.Enabled, Name = dto.Name, DisplayName = dto.DisplayName, Description = dto.Description, Required = dto.Required, Emphasize = dto.Emphasize, ShowInDiscoveryDocument = dto.ShowInDiscoveryDocument, UserClaims = apiScopeClaims }; //List<ApiScopeProperty> apiScopeProperties = new List<ApiScopeProperty>(); //foreach (var item in apiScopeCreateDto.Properties) //{ // apiScopeProperties.Add(new ApiScopeProperty // { // Key = item.Key, // Value = item.Value // }); //} //apiScope.Properties = apiScopeProperties; _userDatabaseContext.ApiScopes.Add(apiScope); await _userDatabaseContext.SaveChangesAsync(); return(new ResultModel(ResultCode.Success, "创建API域成功")); } catch (Exception ex) { return(new ResultModel(ResultCode.Fail, ex.Message)); } }
public async Task <ResultModel> CreateConfig(OcelotConfigEditDto dto) { if (string.IsNullOrWhiteSpace(dto.GlobalConfiguration.BaseUrl)) { return(new ResultModel(ResultCode.Fail, "请输入网关地址")); } var globalConfiguration = new GlobalConfiguration { BaseUrl = dto.GlobalConfiguration.BaseUrl }; var ocelotConfig = new OcelotConfig { IsEnable = dto.IsEnable, GlobalConfiguration = globalConfiguration }; try { _userDatabaseContext.OcelotConfigs.Add(ocelotConfig); await _userDatabaseContext.SaveChangesAsync(); return(new ResultModel(ResultCode.Success, "创建网关配置成功")); } catch (Exception ex) { return(new ResultModel(ResultCode.Fail, ex.Message)); } }
public async Task <ResultModel> CreateIdentityResource(IdentityResourceCreateDto identityResourceCreateDto) { try { var identityResource = new IdentityResource(); identityResource.Created = DateTime.Now; identityResource.Enabled = identityResourceCreateDto.Enabled; identityResource.Name = identityResourceCreateDto.Name; identityResource.DisplayName = identityResourceCreateDto.DisplayName; identityResource.Required = identityResourceCreateDto.Required; identityResource.Emphasize = identityResourceCreateDto.Emphasize; identityResource.ShowInDiscoveryDocument = identityResourceCreateDto.ShowInDiscoveryDocument; identityResource.NonEditable = identityResourceCreateDto.NonEditable; List <IdentityResourceClaim> identityResourceClaims = new List <IdentityResourceClaim>(); foreach (var item in identityResourceCreateDto.UserClaims) { identityResourceClaims.Add(new IdentityResourceClaim { Type = item.Type }); } identityResource.UserClaims = identityResourceClaims; List <IdentityResourceProperty> identityResourceProperties = new List <IdentityResourceProperty>(); foreach (var item in identityResourceCreateDto.Properties) { identityResourceProperties.Add(new IdentityResourceProperty { Key = item.Key, Value = item.Value, }); } identityResource.Properties = identityResourceProperties; _userDatabaseContext.IdentityResources.Add(identityResource); await _userDatabaseContext.SaveChangesAsync(); return(new ResultModel(ResultCode.Success, "创建认证资源成功")); } catch (Exception ex) { return(new ResultModel(ResultCode.Fail, ex.Message)); } }
public async Task <ResultModel> CreateUser(UserEditDto userCreateDto) { if (string.IsNullOrWhiteSpace(userCreateDto.Username)) { return(new ResultModel(ResultCode.Fail, "请输入用户名")); } if (string.IsNullOrWhiteSpace(userCreateDto.Password)) { return(new ResultModel(ResultCode.Fail, "请输入密码")); } if (userCreateDto.Enabled == null) { return(new ResultModel(ResultCode.Fail, "请输入是否启用")); } if (_userDatabaseContext.Users.Any(x => x.Username == userCreateDto.Username)) { return(new ResultModel(ResultCode.Fail, "此用户已存在")); } var result = new ResultModel(); var resultStatus = new ResultStatus(); try { var users = new Users(); users.Password = MD5Helper.GetMD5Hash(userCreateDto.Password); users.Username = userCreateDto.Username; users.Id = Guid.NewGuid(); users.Enabled = userCreateDto.Enabled.Value; _userDatabaseContext.Users.Add(users); await _userDatabaseContext.SaveChangesAsync(); resultStatus.text = "创建用户成功"; resultStatus.code = ResultCode.Success; result.status = resultStatus; return(result); } catch (Exception ex) { return(new ResultModel(ResultCode.Fail, ex.Message)); } }
/// <summary> /// 添加用户朋友 /// </summary> /// <param name="friendId"></param> /// <returns></returns> public async Task <Result> AddUserFriendAsync(string userId, string friendId) { Result result = new Result(); do { var existFriend = await context.UserFriends.AnyAsync(e => e.UserId == userId && e.FriendId == friendId); if (existFriend) { result.ErrorMessage = "已经存在"; break; } await context.UserFriends.AddAsync(new UserFriend { UserId = userId, FriendId = friendId }); await context.SaveChangesAsync(); result.Succeeded = true; } while (false); return(result); }
/// <summary> /// 添加或者更新教师任务同时发布给学生 /// </summary> public async Task <Result <string> > AddOrUpdateTeacherTaskAsync(AddOrUpdateTeacherTaskDto dto) { Result <string> response = new Result <string>(); do { DateTime startDateTime = DateTime.Parse(dto.TaskStartTime); DateTime endDateTime = DateTime.Parse(dto.TaskEndTime); if (startDateTime >= endDateTime) { response.ErrorMessage = "输入时间错误,开始时间不能快于结束时间"; break; } if (endDateTime <= DateTime.Now) { response.ErrorMessage = "输入时间错误,结束时间不能早于现在"; break; } if (string.IsNullOrEmpty(dto.TaskId)) { //判断是否存在这个任务,不存在就添加 //教师下面的课程下的作业不能同名 var existTask = await context.TeacherTasks.AnyAsync(e => e.TeacherId == CurUserId && e.CourseId == dto.CourseId && e.TaskName == dto.TaskName); if (existTask) { response.ErrorMessage = "任务名称已经存在"; break; } if (dto.StudentIds == null || dto.StudentIds.Count == 0) { response.ErrorMessage = "没有选择学生或班级"; break; } string taskId = Guid.NewGuid().ToString(); var entity = new TeacherTask { Id = taskId, CourseId = dto.CourseId, TeacherId = CurUserId, TaskName = dto.TaskName.Trim(), TaskContent = dto.TaskContent.Trim(), TaskWriteTime = DateTime.Now, TaskStartTime = startDateTime, TaskEndTime = endDateTime, HelperId = dto.HelperId, Files = dto.Files }; await context.TeacherTasks.AddAsync(entity); //任务推送给学生 foreach (var sid in dto.StudentIds) { await context.StudentTasks.AddAsync(new StudentTask { TaskId = taskId, StudentId = sid, Grade = 0, StudentAnswer = "", DateTime = DateTime.MinValue, IsAlreadyAnswered = false, IsTeacherModified = false, TeacherReply = "", }); } response.Data = taskId; } else { var task = await context.TeacherTasks.FirstOrDefaultAsync(e => e.Id == dto.TaskId && e.TeacherId == CurUserId); if (task == null) { response.ErrorMessage = "作业不存在,无法修改"; break; } task.TaskContent = dto.TaskContent; task.TaskName = dto.TaskName; task.CourseId = dto.CourseId; task.HelperId = dto.HelperId; task.TaskStartTime = startDateTime; task.TaskEndTime = endDateTime; task.Files = dto.Files; response.Data = task.Id; } try { await context.SaveChangesAsync(); response.Succeeded = true; } catch (Exception ex) { response.ErrorMessage = ex.GetEntityFrameworkErrorMsg(); } } while (false); return(response); }