/// <summary> /// Method to add class exam - SS /// </summary> /// <param name="addClassExam">class exam</param> /// <param name="loggedInUser">logged in user</param> /// <returns>response</returns> public async Task <ClassExamResponse> AddClassExamAsync(AddClassExamAc addClassExam, ApplicationUser loggedInUser) { var instituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(loggedInUser.Id, true); if (!await _iMSDbContext.InstituteClasses.AnyAsync(x => x.Id == addClassExam.ClassId && x.InstituteId == instituteId)) { return new ClassExamResponse() { HasError = true, Message = "Class not found", ErrorType = ClassExamResponseType.ClassId } } ; else if (!await _iMSDbContext.Sections.AnyAsync(x => x.Id == addClassExam.SectionId && x.InstituteId == instituteId)) { return new ClassExamResponse() { HasError = true, Message = "Section not found", ErrorType = ClassExamResponseType.SectionId } } ; else if (!await _iMSDbContext.ExamDefinitions.AnyAsync(x => x.Id == addClassExam.ExamId && x.InstituteId == instituteId)) { return new ClassExamResponse() { HasError = true, Message = "Exam not found", ErrorType = ClassExamResponseType.ExamId } } ; else { var subjectIds = addClassExam.ClassExamSubjectMappings.Select(x => x.SubjectId).Distinct().ToList(); var subjectCount = await _iMSDbContext.InstituteSubjects.CountAsync(x => subjectIds.Contains(x.Id)); if (subjectIds.Count != subjectCount) { return new ClassExamResponse() { HasError = true, Message = "Subject not found", ErrorType = ClassExamResponseType.SubjectId } } ; else { var classExam = new ClassExam() { ClassId = addClassExam.ClassId, CreatedOn = DateTime.UtcNow, ExamId = addClassExam.ExamId, SectionId = addClassExam.SectionId, TotalAttendanceDays = addClassExam.TotalAttendanceDays, UpdatedById = loggedInUser.Id, UpdatedOn = DateTime.UtcNow }; _iMSDbContext.ClassExams.Add(classExam); await _iMSDbContext.SaveChangesAsync(); List <ClassExamSubjectMapping> classExamSubjects = new List <ClassExamSubjectMapping>(); foreach (var subject in addClassExam.ClassExamSubjectMappings) { classExamSubjects.Add(new ClassExamSubjectMapping() { ClassExamId = classExam.Id, Content = subject.Content, CreatedOn = DateTime.UtcNow, EndTime = subject.EndTime, MaxScore = subject.MaxScore, MinScore = subject.MinScore, Remark = subject.Remark, StartDate = subject.StartDate, StartTime = subject.StartTime, SubjectId = subject.SubjectId }); } _iMSDbContext.ClassExamSubjectMappings.AddRange(classExamSubjects); await _iMSDbContext.SaveChangesAsync(); return(new ClassExamResponse() { HasError = false, Message = "Class exam detail added successfully" }); } } }
public async Task <IActionResult> AddClassExamAsync([FromBody] AddClassExamAc addClassExam) { var loggedInUser = await _userManager.FindByNameAsync(User.Identity.Name); return(Ok(await _markManagementRepository.AddClassExamAsync(addClassExam, loggedInUser))); }