Esempio n. 1
0
        public IActionResult EnrollStudent(EnrollStudentRequest request)
        {
            var studies = Db_context.Studies.Single(s => s.Name == request.Studies);

            if (studies == null)
            {
                return(NotFound("Studia " + request.Studies + " nie istnieja"));
            }

            var enrollment = Db_context.Enrollment.Single(e => e.IdStudy == studies.IdStudy && e.Semester == 1);

            if (enrollment == null)
            {
                enrollment = new Enrollment
                {
                    Semester  = 1,
                    IdStudy   = studies.IdStudy,
                    StartDate = DateTime.Now
                };

                Db_context.Enrollment.Add(enrollment);
                Db_context.SaveChanges();
            }

            if (Db_context.Student.Any(s => s.IndexNumber == request.IndexNumber))
            {
                return(BadRequest("Id " + request.IndexNumber + " jest przypisane do innego studenta!"));
            }

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

            Db_context.Student.Add(student);
            Db_context.SaveChanges();

            return(Ok(student));
        }
Esempio n. 2
0
        public IActionResult UpdateStudent(string id, UpdateStudentRequest request)
        {
            var s = Db_context.Student.Single(s => s.IndexNumber == id);

            if (s == null)
            {
                return(NotFound("Student o nr " + id + " nie został odnaleziony"));
            }

            else
            {
                s.FirstName    = request.FirstName;
                s.LastName     = request.LastName;
                s.BirthDate    = request.BirthDate;
                s.IdEnrollment = request.IdEnrollment;

                Db_context.SaveChanges();
                return(Ok(s));
            }
        }