public int Update(ClassInfo t) { using (var scope = _db.Database.BeginTransaction()) { try { ClassInfo c = _db.ClassInfo.Where(u => u.Id == t.Id).SingleOrDefault <ClassInfo>(); if (c == null) { throw new ClassNotFoundException(); } c.Name = t.Name; c.Course = t.Course; c.Site = t.Site; c.ClassTime = t.ClassTime; c.ReportPercentage = t.ReportPercentage; c.PresentationPercentage = t.PresentationPercentage; c.FivePointPercentage = t.FivePointPercentage; c.FourPointPercentage = t.FourPointPercentage; c.ThreePointPercentage = t.ThreePointPercentage; _db.Entry(c).State = EntityState.Modified; _db.SaveChanges(); scope.Commit(); return(0); } catch (ClassNotFoundException e) { scope.Rollback(); throw e; } } }
//更新topic表中的记录 public void Update(long topicId, Topic topic) { using (var scope = _db.Database.BeginTransaction()) { var updatetopic = _db.Topic.FirstOrDefault(t => t.Id == topicId); if (updatetopic == null) { throw new TopicNotFoundException(); } try { updatetopic.Description = topic.Description; updatetopic.GroupNumberLimit = topic.GroupNumberLimit; updatetopic.GroupStudentLimit = topic.GroupStudentLimit; updatetopic.Name = topic.Name; _db.Entry(updatetopic).State = EntityState.Modified; _db.SaveChanges(); } catch (System.Exception e) { scope.Rollback(); throw e; } } }
public void UpdateUserByUserId(long userId, UserInfo newUserInfo) { using (var scope = _db.Database.BeginTransaction()) { try { UserInfo userInfo = _db.UserInfo.First(u => u.Id == userId); if (userInfo == null) { throw new UserNotFoundException(); } //用修改后的值给修改前的值赋值 userInfo.Avatar = newUserInfo.Avatar; userInfo.Education = newUserInfo.Education; userInfo.Email = newUserInfo.Email; userInfo.Gender = newUserInfo.Gender; userInfo.Name = newUserInfo.Name; userInfo.Number = newUserInfo.Number; userInfo.Password = newUserInfo.Password; userInfo.Phone = newUserInfo.Phone; userInfo.School = newUserInfo.School; userInfo.Title = newUserInfo.Title; userInfo.Type = newUserInfo.Type; _db.Entry(userInfo).State = EntityState.Modified; _db.SaveChanges(); } catch (System.Exception e) { scope.Rollback(); throw e; } } }
public IActionResult GetClassInfo([FromRoute] long classId) { try { var user = _userService.GetUserByUserId(User.Id()); if (user.Type != Shared.Models.Type.Teacher) { return(StatusCode(403, new { msg = "权限不足" })); } var classInfo = _classService.GetClassByClassId(classId); return(Json(new { id = classId, classname = classInfo.Name, numStudent = _db.Entry(classInfo).Collection(cl => cl.CourseSelections).Query().Count() })); } catch (ClassNotFoundException) { return(StatusCode(404, new { msg = "班级不存在" })); } }
public async Task <IActionResult> GetUserClasses([FromQuery] string courseName, [FromQuery] string courseTeacher) { //List<ClassInfo> classes = new List<ClassInfo>(); try { IList <ClassInfo> classes; if (string.IsNullOrEmpty(courseName) && string.IsNullOrEmpty(courseTeacher)) { classes = await _classService.ListClassByUserIdAsync(User.Id()); } else if (string.IsNullOrEmpty(courseTeacher)) { classes = await _courseService.ListClassByCourseNameAsync(courseName); } else if (string.IsNullOrEmpty(courseName)) { classes = await _courseService.ListClassByTeacherNameAsync(courseTeacher); } else { var c = (await _courseService.ListClassByCourseNameAsync(courseName)).ToHashSet(); c.IntersectWith(await _courseService.ListClassByTeacherNameAsync(courseTeacher)); classes = c.ToList(); } return(Json(await Task.WhenAll(classes.Select(async c => { var co = await _courseService.GetCourseByCourseIdAsync(c.CourseId); return new { id = c.Id, name = c.Name, site = c.Site, time = c.ClassTime, courseId = c.CourseId, courseName = co.Name, courseTeacher = (await _userService .GetUserByUserIdAsync(co.TeacherId)) .Name, numStudent = _db.Entry(c).Collection(cl => cl.CourseSelections).Query().Count() }; })))); } catch (ArgumentException) { return(StatusCode(400, new { msg = "用户ID输入格式错误" })); } }
public void UpdateUserByUserId(long userId, UserInfo newUserInfo) { //using (var scope = _db.Database.BeginTransaction()) //{ //try //{ UserInfo userInfo = _db.UserInfo.SingleOrDefault(u => u.Id == userId); if (userInfo == null) { throw new UserNotFoundException(); } //用修改后的值给修改前的值赋值 //userInfo.Avatar = newUserInfo.Avatar; if (newUserInfo.Education != null) { userInfo.Education = newUserInfo.Education; } userInfo.Email = newUserInfo.Email; userInfo.Gender = newUserInfo.Gender; userInfo.Name = newUserInfo.Name; School school = _db.School.Where(s => s.Name == newUserInfo.School.Name).FirstOrDefault(); userInfo.School = school ?? throw new Exception("SchoolNotFound"); userInfo.Number = newUserInfo.Number; userInfo.Phone = newUserInfo.Phone; if (newUserInfo.Title != null) { userInfo.Title = newUserInfo.Title; } if (userInfo.Type == Shared.Models.Type.Unbinded) { userInfo.Type = newUserInfo.Type; } _db.Entry(userInfo).State = EntityState.Modified; _db.SaveChanges(); //} //catch(System.Exception e) //{ // scope.Rollback(); // throw e; //} //} }
public async Task <IActionResult> GetStudentGroupBySeminarId([FromRoute] long seminarId) { if (User.Type() != Type.Student) { return(StatusCode(403, new { msg = "权限不足" })); } try { var groups = await _seminargroupService.ListSeminarGroupBySeminarIdAsync(seminarId); var group = groups.SelectMany(grp => _db.Entry(grp).Collection(gp => gp.SeminarGroupMembers).Query() .Include(gm => gm.SeminarGroup) .Where(gm => gm.StudentId == User.Id()).Select(gm => gm.SeminarGroup)) .SingleOrDefault(sg => sg.SeminarId == seminarId) ?? throw new GroupNotFoundException(); var leader = group.Leader ?? await _userService.GetUserByUserIdAsync(group.LeaderId); var members = await _seminargroupService.ListSeminarGroupMemberByGroupIdAsync(group.Id); var topics = await Task.WhenAll((await _topicService.ListSeminarGroupTopicByGroupIdAsync(group.Id)) .Select(gt => _topicService.GetTopicByTopicIdAsync(gt.TopicId))); return(Json(new { id = group.Id, name = group.Id + "组", leader = new { id = leader.Id, name = leader.Name }, members = members.Select(u => new { id = u.Id, name = u.Name }), topics = topics.Select(t => new { id = t.Id, name = t.Name }) })); } catch (SeminarNotFoundException) { return(StatusCode(404, new { msg = "讨论课不存在" })); } catch (ArgumentException) { return(StatusCode(400, new { msg = "讨论课ID输入格式有误" })); } }
public async Task <IActionResult> GetUserCourses() { var userlogin = await _userService.GetUserByUserIdAsync(User.Id()); if (userlogin.Type != Type.Teacher) { return(StatusCode(403, new { msg = "权限不足" })); } var courses = await _courseService.ListCourseByUserIdAsync(User.Id()); return(Json(courses.Select(c => new { id = c.Id, name = c.Name, numClass = _classService.ListClassByCourseIdAsync(c.Id).Result.Count, numStudent = _classService.ListClassByCourseIdAsync(c.Id).Result.Aggregate(0, (total, cls) => _db.Entry(cls).Collection(cl => cl.CourseSelections).Query().Count() + total), startTime = c.StartDate.ToString("yyyy-MM-dd"), endTime = c.EndDate.ToString("yyyy-MM-dd") }))); }