/// <summary> /// 更新课程 /// </summary> /// <param name="courseId"></param> /// <param name="dto"></param> /// <returns></returns> public async Task <(bool sucess, string errMessage)> UpdateCourseAsync(AddOrUpdateCourseDto dto) { //先判断用户是不是存在了课程 var course = await db.Courses.FirstOrDefaultAsync(e => e.Id == dto.Id && e.UserId == dto.UserId); if (course == null) { return(false, "不存在该课程"); } course.Desc = dto.Desc; course.Name = dto.Name; course.LastModifyDatetime = DateTime.UtcNow; try { int row = await db.SaveChangesAsync(); return(row > 0, null); } catch (Exception ex) { return(false, ex.InnerException?.Message ?? ex.Message); } }
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); }
/// <summary> /// 添加或者更新课程 /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <Result> AddOrUpdateCourseAsync(AddOrUpdateCourseDto dto) { (bool sucess, string errMessage)result; if (string.IsNullOrEmpty(dto.Id)) { result = await AddCourseAsync(dto); } else { result = await UpdateCourseAsync(dto); } return(new Result { Succeeded = result.sucess, ErrorMessage = result.errMessage }); }
public async Task <IActionResult> OnPost(AddOrUpdateCourseDto AddOrUpdateCourseDto) { if (!TryValidateModel(AddOrUpdateCourseDto)) { return(BadRequest(ModelState)); } var result = await CourseService.AddOrUpdateCourseAsync(AddOrUpdateCourseDto).ConfigureAwait(false); if (!result.Succeeded) { ModelState.AddModelError(string.Empty, result.ErrorMessage); return(BadRequest(ModelState)); } return(Content("/Teacher/Course/CourseList")); }
public async Task <IActionResult> OnGetAsync([ModelBinder(Name = "CourseId")] CourseDto course) { if (course != null) { AddOrUpdateCourseDto = new AddOrUpdateCourseDto { Desc = course.Desc, Id = course.Id, Name = course.Name, UserId = course.UserId }; } else { AddOrUpdateCourseDto = new AddOrUpdateCourseDto { UserId = CurUserId }; } return(Page()); }