コード例 #1
0
ファイル: SqlDbService.cs プロジェクト: s16651/APBD10
        public async Task <EnrollmentStudentResponse> EnrollStudentAsync(EnrollmentStudenRequest request)
        {
            var studiesExists = _studentContext.Studies.Any(s => s.Name == request.Studies);

            if (!studiesExists)
            {
                return(null);
            }
            var studiesList      = _studentContext.Studies.Where(s => s.Name == request.Studies).ToList();
            int idStudy          = studiesList[0].IdStudy;
            int enrollmentID     = 1;
            var enrollmentExists = _studentContext.Enrollment.Any(e => e.IdStudy == idStudy && e.Semester == 1);

            if (enrollmentExists)
            {
                var enrollmentsList = _studentContext.Enrollment.Where(e => e.IdStudy == idStudy && e.Semester == 1).ToList();
                enrollmentID = enrollmentsList[0].IdEnrollment;
            }
            else
            {
                var m = new Enrollment
                {
                    Semester  = 1,
                    IdStudy   = idStudy,
                    StartDate = DateTime.Now
                };
                _studentContext.Enrollment.Add(e);
                _studentContext.SaveChanges();
                enrollmentID = m.IdEnrollment;
            }
            var exists = _studentContext.Student.Any(s => s.IndexNumber == request.IndexNumber);

            if (exists)
            {
                return(null);
            }
            var isq = new InsertStudentRequest
            {
                IndexNumber  = request.IndexNumber,
                FirstName    = request.FirstName,
                LastName     = request.LastName,
                BirthDate    = request.BirthDate,
                IdEnrollment = enrollmentID,
                Password     = request.Password
            };

            await AddStudentAsync(isq);

            var enrollStudentList = _studentContext.Enrollment.Where(e => e.IdEnrollment == enrollmentID).ToList();
            var esr = new EnrollmentStudentResponse
            {
                IdEnrollment = enrollStudentList[0].IdEnrollment,
                IdStudy      = enrollStudentList[0].IdStudy,
                Semester     = enrollStudentList[0].Semester,
                StartDate    = enrollStudentList[0].StartDate
            };

            return(esr);
        }
コード例 #2
0
        public async Task <IActionResult> EnrollStudentAsync(EnrollmentStudenRequest request)
        {
            EnrollmentStudentResponse enrollStudentResponse = await _db.EnrollStudentAsync(request);

            if (enrollStudentResponse == null)
            {
                return(BadRequest());
            }
            return(this.StatusCode(201, enrollStudentResponse));
        }
コード例 #3
0
ファイル: EfDbService.cs プロジェクト: s17180/DbFirst
        public EnrollmentStudentResponse EnrollStudent(EnrollStudentRequest request)
        {
            Studies studies = _context.Studies.Where(s => s.Name == request.Studies).First();

            if (studies == null)
            {
                throw new Exception("nie ma takich studiów");
            }

            Student st = _context.Student.Where(s => s.IndexNumber == request.IndexNumber).First();

            if (st != null)
            {
                throw new Exception("student o podanym indeksie jest juz w bazie");
            }

            var enrollment = _context.Enrollment
                             .OrderBy(e => e.StartDate)
                             .Where(e => e.IdStudy == studies.IdStudy && e.Semester == 1).First();

            if (enrollment == null)
            {
                int maxId = _context.Enrollment.Max(e => e.IdEnrollment);

                enrollment = new Enrollment
                {
                    IdEnrollment = maxId,
                    Semester     = 1,
                    IdStudy      = studies.IdStudy,
                    StartDate    = DateTime.Now
                };

                _context.Enrollment.Add(enrollment);
                _context.SaveChanges();
            }
            string   pattern = "MM-dd-yyyy";
            DateTime parsedDate;

            if (!DateTime.TryParseExact(request.BirthDate, pattern, null, DateTimeStyles.None, out parsedDate))
            {
                throw new Exception("niepoprawna data");
            }

            Student student = new Student
            {
                IndexNumber  = request.IndexNumber,
                FirstName    = request.FirstName,
                LastName     = request.LastName,
                BirthDate    = parsedDate,
                IdEnrollment = enrollment.IdEnrollment
            };

            _context.Student.Add(student);

            _context.SaveChanges();

            EnrollmentStudentResponse response = new EnrollmentStudentResponse
            {
                IdEnrollment = enrollment.IdEnrollment,
                IdStudy      = enrollment.IdStudy,
                Semester     = enrollment.Semester,
                StartDate    = enrollment.StartDate
            };

            return(response);
        }