public async Task <StudentUpdate> Create(StudentCreate model) { var student = new Student { Firstname = model.Firstname, Lastname = model.Lastname, Jmbag = model.Jmbag, IndexNmb = model.IndexNmb }; var errors = student.Validate(); if (errors.Any()) { throw new ValidationPropertyException(errors); } await context.Student.AddAsync(student); await context.SaveChangesAsync(); return(new StudentUpdate { Id = student.Id, Firstname = student.Firstname, Lastname = student.Lastname, Jmbag = student.Jmbag, IndexNmb = student.IndexNmb }); }
public async Task<SemesterUpdate> Create(SemesterCreate model) { var semester = new Semester { }; UpdateValues(semester, model); Validate(semester); await context.Semester.AddAsync(semester); await context.SaveChangesAsync(); return new SemesterUpdate { Id = semester.Id, StartDate = semester.StartDate, EndDate = semester.EndDate, IsWinter = semester.IsWinter }; }
public async Task <EnrolmentUpdate> Create(EnrolmentCreate model) { var enrolment = new Enrolment { SemesterId = model.CourseInstance.SemesterId, CourseId = model.CourseInstance.CourseId, StudentId = model.StudentId, FinalGrade = model.FinalGrade.ConvertGrade(), GradeDate = model.GradeDate }; Validate(enrolment); await context.Enrolment.AddAsync(enrolment); await context.SaveChangesAsync(); return(new EnrolmentUpdate { Id = enrolment.Id, CourseInstance = new CourseInstanceBase { CourseId = enrolment.CourseId, SemesterId = enrolment.SemesterId }, StudentId = enrolment.StudentId, FinalGrade = enrolment.FinalGrade.ConvertToGrade(), GradeDate = enrolment.GradeDate }); }
public async Task <CourseUpdate> CreateBasic(CourseCreate model) { var course = new Course { Name = model.Name }; var errors = course.Validate(); if (errors.Any()) { throw new ValidationPropertyException(errors); } if (context.Course.Any(_ => _.Name == course.Name)) { throw new ValidationException("Course with the same name already exists!"); } await context.Course.AddAsync(course); await context.SaveChangesAsync(); return(new CourseUpdate { Id = course.Id, Name = course.Name }); }
public async Task Create(List <StudentExamCreate> models) { var studentExams = models.Select(_ => new StudentExam { EnrolmentId = _.EnrolmentId, ExamId = _.ExamId }).ToList(); studentExams.ForEach(_ => Validate(_)); await context.StudentExam.AddRangeAsync(studentExams); await context.SaveChangesAsync(); }
public async Task <Guid> CreateOralExam(OralExamCreate oralExamForm) { var errors = new List <string>(); if (!(await context.Course.AnyAsync(_ => _.Id == oralExamForm.CourseId))) { errors.Add("Odabran je nepostojeći predmet!"); } if (!(await context.Semester.AnyAsync(_ => _.Id == oralExamForm.SemesterId))) { errors.Add("Odabran je nepostojeći semestar!"); } if (!(await context.CourseInstance.AnyAsync(_ => _.CourseId == oralExamForm.CourseId && _.SemesterId == oralExamForm.SemesterId))) { errors.Add("Odabrani predmet se ne održava u odabranom semestru!"); } if (!(await context.Enrolment.AnyAsync(_ => _.Id == oralExamForm.EnrolmentId))) { errors.Add("Odabrani student nije upisan na odabranu instancu predmeta!"); } if (oralExamForm.ExamId.HasValue) { if (!(await context.Exam.AnyAsync(_ => _.Id == oralExamForm.ExamId.Value))) { errors.Add("Odabran je nepostojeći ispit!"); } } else if (oralExamForm.Exam == null) { errors.Add("Nevaljali podaci za kreiranje usmenog ispita!"); } if (context.StudentExam.Any(_ => _.ExamId == oralExamForm.ExamId && _.EnrolmentId == oralExamForm.EnrolmentId)) { var student = context.StudentExam .Include(_ => _.Enrolment) .ThenInclude(_ => _.Student) .FirstOrDefault(_ => _.ExamId == oralExamForm.ExamId && _.EnrolmentId == oralExamForm.EnrolmentId) .Enrolment .Student; throw new ValidationException(string.Format($"Odabrani student {student.Firstname} {student.Lastname} već je prijavljen na dani ispit.")); } if (errors.Any()) { throw new ValidationException(errors); } if (!oralExamForm.ExamId.HasValue) { var exam = new Exam { Type = (int)ExamType.Oral, Date = GetDateTime(oralExamForm.Exam.Date, oralExamForm.Exam.Time), CourseId = oralExamForm.CourseId, SemesterId = oralExamForm.SemesterId }; await context.Exam.AddAsync(exam); oralExamForm.ExamId = exam.Id; } if (oralExamForm.ExistingExamDateTime != null) { var exam = await context.Exam.FirstOrDefaultAsync(_ => _.Id == oralExamForm.ExamId); exam.Date = GetDateTime(oralExamForm.ExistingExamDateTime.Date, oralExamForm.ExistingExamDateTime.Time); context.Exam.Update(exam); } var studentExam = new StudentExam { EnrolmentId = oralExamForm.EnrolmentId, ExamId = oralExamForm.ExamId.Value }; await context.StudentExam.AddAsync(studentExam); await context.SaveChangesAsync(); return(studentExam.Id); }