/// <summary> /// 更新学生 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool UpdateStudent(StudentSingleModel model) { var student = DataRepository.Get <Student>(model.Id); student.Sage = model.Age; student.Sname = model.Name; student.Ssex = model.Sex; var transaction = DataRepository.Session.Begin(); try { var flag = DataRepository.Update(student, transaction); DataRepository.Session.Commit(); return(flag); } catch (Exception ex) { DataRepository.Session.Rollback(); logger.Error(ex); return(false); } }
/// <summary> /// 根据条件查询数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public IEnumerable <StudentDto> GetStudentsByCondition(StudentSingleModel model) { StringBuilder sb = new StringBuilder(); sb.Append("SELECT * FROM DBO.STUDENT "); sb.Append("WHERE 1=1 "); if (!string.IsNullOrEmpty(model.Name)) { sb.Append("AND SNAME LIKE @TMP_NAME "); } sb.Append("ORDER BY SID"); var students = DataRepository.GetList <Student>(sb.ToString(), new { TMP_NAME = $"%{model.Name}%" }); return(AutoMapperHelper <IEnumerable <Student>, IEnumerable <StudentDto> > .AutoConvert(students)); }
public HttpResponseMessage GetStudentScore([FromBody] StudentSingleModel model) { var result = _testService.GetStudentScore(model.Id.ToString()); var data = new JsonResultBuilder() .SetCode(JsonResultCode.OK) .SetMessage("success") .SetResult(result).Build(); return(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(data)) }); }
public HttpResponseMessage DeleteStudent([FromBody] StudentSingleModel model) { throw new ArgumentException("无效的请求参数"); #pragma warning disable IDE0059 // 从不使用分配给符号的值 var result = _testService.DeleteStudent(model); #pragma warning restore IDE0059 // 从不使用分配给符号的值 var data = new JsonResultBuilder() .SetCode(JsonResultCode.OK) .SetMessage("success") .SetEmptyResult().Build(); return(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(data)) }); }
/// <summary> /// 删除学生 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool DeleteStudent(StudentSingleModel model) { var transaction = DataRepository.Session.Begin(); try { var rowsAffected = DataRepository.Delete <Student>(model.Id, transaction); DataRepository.Session.Commit(); return(rowsAffected); } catch (Exception ex) { DataRepository.Session.Rollback(); logger.Error(ex); return(false); } }
/// <summary> /// 添加学生 /// </summary> /// <param name="model"></param> /// <returns></returns> public dynamic AddStudent(StudentSingleModel model) { var transaction = DataRepository.Session.Begin(); try { var student = AutoMapperHelper <StudentSingleModel, Student> .AutoConvert(model); var itemId = DataRepository.Insert <Student>(student, transaction); DataRepository.Session.Commit(); return(itemId); } catch (Exception ex) { DataRepository.Session.Rollback(); logger.Error(ex); return(string.Empty); } }