Пример #1
0
        public async Task <ActionResult> CreateAstudent([FromBody] StudentAddDto dto)
        {
            var @class = await _classRepository.LoadEntitiesAsIQueryable(x => x.ClassId == dto.ClassId).FirstOrDefaultAsync();

            var college = await _collegeRepository.LoadEntitiesAsIQueryable(x => x.CollegeId == dto.CollegeId).FirstOrDefaultAsync();

            if (@class == null || college == null)
            {
                return(NotFound());
            }
            var lastStudent = await _studentRepository.GetAllEntitiesAsIQueryable().OrderBy(x => x.StudentId).LastOrDefaultAsync();

            int studentId;

            if (lastStudent != null)
            {
                studentId = lastStudent.StudentId + 1;
            }
            else
            {
                studentId = Convert.ToInt32(DateTime.Now.ToString("yyyyMMss") + DateTime.Now.Day.ToString().Substring(DateTime.Now.Day.ToString().Length - 1));
            }
            Student student = new Student
            {
                StudentId   = studentId,
                StudentName = dto.StudentName,
                Class       = @class,
                Password    = studentId.ToString(),
                Year        = DateTime.Now.Year,
            };
            await _studentRepository.AddEntityAsync(student);

            return(NoContent());
        }
        public ActionResult <Student> CreateStudent([FromBody] StudentAddDto studentDto)
        {
            if (!ModelState.IsValid)
            {
                var errorMessages = new List <KeyValuePair <string, string> >();
                foreach (var key in ModelState.Keys)
                {
                    errorMessages.AddRange(ModelState[key].Errors
                                           .Select(error => new KeyValuePair <string, string>(key, error.ErrorMessage)));
                }

                return(BadRequest(GeneralResponse <Student> .ValidationError(errorMessages)));
            }

            var studentEntity = new Student
            {
                Name        = studentDto.Name,
                StudentNo   = studentDto.StudentNo,
                Age         = studentDto.Age,
                PhoneNumber = studentDto.PhoneNumber
            };

            var studentAfterAdd = _studentService.AddStudent(studentEntity);

            return(Created(new Uri($"{Request.Path}/{studentAfterAdd.Id}", UriKind.Relative),
                           GeneralResponse <Student> .Ok(studentAfterAdd)));
        }
 private async Task AddAddress(StudentAddDto studentAddDto, Student addedStudent)
 {
     await _addressService.AddAddressAsync(
         new Address
     {
         ProvinceId    = studentAddDto.ProvinceId,
         DistrictId    = studentAddDto.DistrictId,
         AddressDetail = studentAddDto.AddressDetail,
         StudentId     = addedStudent.Id
     });
 }
Пример #4
0
        public async Task <ActionResult <StudentDto> > CreateStudent(Guid ClassId, [FromBody] StudentAddDto studentAddDto)
        {
            if (!await _classRoomRepository.ClassRoomExists(ClassId))
            {
                return(NotFound());
            }
            var enitity = _mapper.Map <Student>(studentAddDto);

            _studentRepository.AddStudent(ClassId, enitity);
            await _studentRepository.SaveAsync();

            var StudentDto = _mapper.Map <StudentDto>(enitity);

            return(CreatedAtRoute(nameof(GetStudent), new { ClassId, StudentId = enitity.Id }, StudentDto));
        }
Пример #5
0
        public async Task <IActionResult> Add([FromBody] StudentAddDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(this.GetErrors(model)));
            }

            OperationResult result = await _service.Add(model);

            if (result.HasError)
            {
                return(this.Unprocessable(result));
            }

            return(Ok(result));
        }
        CreateStudentForInstitute(Guid instituteId, StudentAddDto student)
        {
            if (!await _instituteRepository.InstituteExistsAsync(instituteId))
            {
                return(NotFound());
            }
            var entity = _mapper.Map <Student>(student);

            _instituteRepository.AddStudent(instituteId, entity);
            await _instituteRepository.SaveAsync();

            var dtoToReturn = _mapper.Map <StudentDto>(entity);

            return(CreatedAtRoute(nameof(GetStudentForInstitute), new
            {
                instituteId,
                studentId = dtoToReturn.Id
            }, dtoToReturn));
        }
        public async Task <IDataResult <StudentAddDto> > AddStudentWithAddressAsync(StudentAddDto studentAddDto)
        {
            if (studentAddDto == null)
            {
                return(new ErrorDataResult <StudentAddDto>(studentAddDto, HttpStatusCode.NotAcceptable));
            }

            var existingProvinceWithDistrict = await _provinceService.GetByProvinceIdAndDistrictId(
                studentAddDto.ProvinceId,
                studentAddDto.DistrictId);

            if (existingProvinceWithDistrict == null)
            {
                return(new ErrorDataResult <StudentAddDto>(studentAddDto, HttpStatusCode.NotFound));
            }

            var newStudent   = _mapper.Map <Student>(studentAddDto);
            var addedStudent = await AddStudentAsync(newStudent);

            await AddAddress(studentAddDto, addedStudent);

            return(new SuccessfulDataResult <StudentAddDto>(studentAddDto, HttpStatusCode.OK));
        }
Пример #8
0
        public async Task <ActionResult <StudentDto> > CreateStudent(Guid classId, [FromBody] StudentAddDto student)
        {
            if (!await _classRepository.ClassExitAsync(classId))
            {
                return(NotFound());
            }

            var entity = _mapper.Map <Student>(student);

            entity.StudentId = Guid.NewGuid();

            _studentRepository.AddStudent(classId, entity);
            await _studentRepository.SaveAsync();

            var dtoToReturn = _mapper.Map <StudentDto>(entity);

            return(CreatedAtRoute(
                       nameof(GetStudent),
                       new{
                classId = classId,
                studentId = dtoToReturn.StudentId
            }, dtoToReturn
                       ));
        }
Пример #9
0
        public async Task <ActionResult <StudentDto> > CreateStudent(Guid professionId, [FromBody] StudentAddDto studentAddDto)
        {
            //ApiController在遇到studentAddDto为空时可以自动返回400错误
            var student = _mapper.Map <Student>(studentAddDto);

            student.ProfessionId = professionId;
            student.Profession   = await _professionRepository.GetProfessionAsync(professionId);

            student.Profession.Academy = await _academyRepository.GetAcademyAsync(student.Profession.AcademyId);

            _studentRepository.AddStudent(student);//只是被添加到DbContext里

            await _studentRepository.SaveAsync();

            var studentDto = _mapper.Map <StudentDto>(student);

            return(CreatedAtRoute(nameof(GetStudent), new { studentId = student.StudentId }, studentDto));
        }
Пример #10
0
        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());
        }
        public async Task <IActionResult> Add([FromBody] StudentAddDto studentAddDto)
        {
            var result = await _studentService.AddStudentWithAddressAsync(studentAddDto);

            return(StatusCode(result.HttpStatusCode, result.Data));
        }