public async Task <ActionResult <CollegeDto> > GetCollege(int collegeId) { var college = await _collegeRepository.LoadEntitiesAsIQueryable(x => x.CollegeId == collegeId).FirstOrDefaultAsync(); if (college == null) { return(NotFound()); } var dto = _mapper.Map <CollegeDto>(college); return(dto); }
public async Task <ActionResult> Add([FromBody] StudentAddDto dto) { var college = await _collegeRepository.LoadEntitiesAsIQueryable(x => x.CollegeId == dto.CollegeId) .Include(x => x.Classes) .FirstOrDefaultAsync(); if (college == null) { return(BadRequest()); } var classCount = college.Classes.Count(); var @class = await _classRepository.LoadEntitiesAsIQueryable(x => x.CollegeId == dto.CollegeId) .Include(x => x.Students).OrderBy(x => x.Students.Count()).FirstOrDefaultAsync(); if (!(@class != null && @class.Students.Count() < 50)) { int classId = Convert.ToInt32(DateTime.Now.Year.ToString() + dto.CollegeId.ToString() .Substring(dto.CollegeId.ToString().Length - 2, 2) + (college.Classes.Count() + 1)); @class = new Class { ClassId = classId, ClassName = college.CollegeName + (classCount + 1) + "班", College = college, }; } int studentCount = @class.Students.Count(); var lastTwoOfClass = ""; var lastTwoOfStudent = ""; if (studentCount.ToString().Length >= 2) { lastTwoOfStudent = studentCount.ToString().Substring(studentCount.ToString().Length - 2); } else { lastTwoOfStudent = "0" + studentCount.ToString(); } if (classCount.ToString().Length >= 2) { lastTwoOfClass = classCount.ToString().Substring(classCount.ToString().Length - 2); } else { lastTwoOfClass = "0" + classCount.ToString(); } var strstudentId = DateTime.Now.Year.ToString().Substring(2, 2) + college.CollegeId.ToString() .Substring(college.CollegeId.ToString().Length - 3) + lastTwoOfClass + lastTwoOfStudent; var studentId = Convert.ToInt32(strstudentId); while ((await _studentRepository.LoadEntitiesAsIQueryable(x => x.StudentId == studentId).FirstOrDefaultAsync()) != null) { studentId++; } Student student = new Student { StudentId = studentId, StudentName = dto.StudentName, Class = @class, Year = DateTime.Now.Year, Password = studentId.ToString(), }; await _studentRepository.AddEntityAsync(student); return(NoContent()); }