/// <summary> /// Method to add disciplinary - SS /// </summary> /// <param name="addDisciplinary">disciplinary</param> /// <param name="loggedInUser">logged in user</param> /// <returns>response</returns> public async Task <DisciplinaryManagementResponse> AddDisciplinaryAsync(AddDisciplinaryManagementAc addDisciplinary, ApplicationUser loggedInUser) { if (string.IsNullOrEmpty(addDisciplinary.Subject.Trim())) { return new DisciplinaryManagementResponse() { HasError = true, Message = "Subject can't be empty", ErrorType = DisciplinaryManagementResponseType.Subject } } ; else if (string.IsNullOrEmpty(addDisciplinary.Description.Trim())) { return new DisciplinaryManagementResponse() { HasError = true, Message = "Description can't be empty", ErrorType = DisciplinaryManagementResponseType.Description } } ; else { var instituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(loggedInUser.Id, true); var studentsCount = await _iMSDbContext.StudentBasicInformation.CountAsync(x => addDisciplinary.StudentIds .Contains(x.Id) && x.InstituteId == instituteId); if (studentsCount != addDisciplinary.StudentIds.Count) { return new DisciplinaryManagementResponse() { HasError = true, Message = "Student not found", ErrorType = DisciplinaryManagementResponseType.StudentId } } ; else { if (!await _iMSDbContext.DisciplinaryStatuses.AnyAsync(x => x.InstituteId == instituteId && x.Id == addDisciplinary.StatusId)) { return new DisciplinaryManagementResponse() { HasError = true, Message = "Status not found", ErrorType = DisciplinaryManagementResponseType.StatusId } } ; else { List <Disciplinary> disciplinaries = new List <Disciplinary>(); foreach (var studentId in addDisciplinary.StudentIds) { disciplinaries.Add(new Disciplinary() { CreatedOn = DateTime.UtcNow, Date = addDisciplinary.Date, Description = addDisciplinary.Description, Remarks = addDisciplinary.Remarks, StatusId = addDisciplinary.StatusId, StudentId = studentId, Subject = addDisciplinary.Subject, UpdatedById = loggedInUser.Id, UpdatedOn = DateTime.UtcNow }); } using (var db = await _iMSDbContext.Database.BeginTransactionAsync()) { await _iMSDbContext.BulkInsertAsync(disciplinaries); db.Commit(); } return(new DisciplinaryManagementResponse() { HasError = false, Message = "Disciplinary added successfully", Data = disciplinaries.Select(s => s.Id) }); } } } }
/// <summary> /// Method to add or update homework - SS /// </summary> /// <param name="homeWork">home work</param> /// <param name="loggedInUser">logged in user</param> /// <returns>response</returns> public async Task <HomeworkManagementResponse> AddOrUpdateHomeworkAsync(AddHomeworkManagementAc homeWork, ApplicationUser loggedInUser) { var instituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(loggedInUser.Id, true); if (!await _iMSDbContext.StaffBasicPersonalInformation.AnyAsync(x => x.Id == homeWork.StaffId && x.InstituteId == instituteId)) { return new HomeworkManagementResponse() { Message = "Staff not found", HasError = true, ErrorType = HomeworkManagementReponseType.StaffId } } ; else if (!await _iMSDbContext.InstituteClasses.AnyAsync(x => x.Id == homeWork.ClassId && x.InstituteId == instituteId)) { return new HomeworkManagementResponse() { Message = "Class not found", HasError = true, ErrorType = HomeworkManagementReponseType.ClassId } } ; else if (!await _iMSDbContext.Sections.AnyAsync(x => x.Id == homeWork.SectionId && x.InstituteId == instituteId)) { return new HomeworkManagementResponse() { Message = "Section not found", HasError = true, ErrorType = HomeworkManagementReponseType.SectionId } } ; else { var subjectIds = homeWork.HomeworkSubjectMappings.Select(x => x.SubjectId).Distinct().ToList(); var subjectCount = await _iMSDbContext.InstituteClassSubjectMappings.CountAsync(x => subjectIds.Contains(x.SubjectId) && x.ClassId == homeWork.ClassId && x.FacultyId == homeWork.StaffId || x.AlternateFacultyId == homeWork.StaffId); if (subjectIds.Count != subjectCount) { return new HomeworkManagementResponse() { Message = "Subject not found", HasError = true, ErrorType = HomeworkManagementReponseType.SubjectId } } ; else { var homeworkTexts = homeWork.HomeworkSubjectMappings.Select(x => x.HomeworkData).ToList(); if (homeworkTexts.Any(x => string.IsNullOrEmpty(x.Trim()))) { return new HomeworkManagementResponse() { Message = "Home can't be empty", HasError = true, ErrorType = HomeworkManagementReponseType.HomeworkData } } ; else { var homework = await _iMSDbContext.Homeworks.FirstOrDefaultAsync(x => x.ClassId == homeWork.ClassId && x.StaffId == homeWork.StaffId && x.SectionId == homeWork.SectionId && x.HomeworkDate == homeWork.HomeworkDate); if (homework == null) { homework = new Homework() { ClassId = homeWork.ClassId, CreatedOn = DateTime.UtcNow, HomeworkDate = homeWork.HomeworkDate, SectionId = homeWork.SectionId, StaffId = homeWork.StaffId, UpdatedById = loggedInUser.Id, UpdatedOn = DateTime.UtcNow }; _iMSDbContext.Homeworks.Add(homework); await _iMSDbContext.SaveChangesAsync(); } else { homework.UpdatedById = loggedInUser.Id; homework.UpdatedOn = DateTime.UtcNow; _iMSDbContext.Homeworks.Update(homework); await _iMSDbContext.SaveChangesAsync(); var previousHomeWorkSubjects = await _iMSDbContext.HomeworkSubjectMappings.Where(x => x.HomeworkId == homework.Id).ToListAsync(); if (previousHomeWorkSubjects.Count != 0) { using (var db = await _iMSDbContext.Database.BeginTransactionAsync()) { await _iMSDbContext.BulkDeleteAsync(previousHomeWorkSubjects); db.Commit(); } } } List <HomeworkSubjectMapping> homeworkSubjects = new List <HomeworkSubjectMapping>(); foreach (var homeworkSubject in homeWork.HomeworkSubjectMappings) { homeworkSubjects.Add(new HomeworkSubjectMapping() { CreatedOn = DateTime.UtcNow, HomeworkData = homeworkSubject.HomeworkData, HomeworkId = homework.Id, IsSelected = homeworkSubject.IsSelected, SubjectId = homeworkSubject.SubjectId }); } using (var db = await _iMSDbContext.Database.BeginTransactionAsync()) { await _iMSDbContext.BulkInsertAsync(homeworkSubjects); db.Commit(); } // Set bell notification await SendBellNotificationOnHomewordCreation(homeWork, loggedInUser, instituteId); return(new HomeworkManagementResponse() { HasError = false, Message = "Homework updated successfully", Data = new { Id = homework.Id } }); } } } }
/// <summary> /// Method to add Leave type - SS /// </summary> /// <param name="addLeaveType">leave type</param> /// <param name="loggedInUser">logged in user</param> /// <returns>response</returns> public async Task <LeaveTypeManagementResponse> AddLeaveType(AddLeaveTypeManagementAc addLeaveType, ApplicationUser loggedInUser) { if (string.IsNullOrEmpty(addLeaveType.Code.Trim())) { return new LeaveTypeManagementResponse() { HasError = true, Message = "Code can't be empty", ErrorType = LeaveTypeManagementResponseType.Code } } ; else if (string.IsNullOrEmpty(addLeaveType.Name.Trim())) { return new LeaveTypeManagementResponse() { HasError = true, Message = "Code can't be empty", ErrorType = LeaveTypeManagementResponseType.Name } } ; else { var instituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(loggedInUser.Id, true); var assignedToCount = await _iMSDbContext.UserInstituteMappings.CountAsync(x => addLeaveType.LeaveAssignedTos.Contains(x.UserId) && x.InstituteId == instituteId); if (assignedToCount != addLeaveType.LeaveAssignedTos.Count) { return new LeaveTypeManagementResponse() { HasError = true, Message = "User not found", ErrorType = LeaveTypeManagementResponseType.LeaveAssignedTos } } ; else { if (await _iMSDbContext.LeaveTypes.AnyAsync(x => x.InstituteId == instituteId && x.Code.ToLowerInvariant() == addLeaveType.Code.ToLowerInvariant())) { return new LeaveTypeManagementResponse() { HasError = true, Message = "Leave type with same code already exist. Please use unique one", ErrorType = LeaveTypeManagementResponseType.Code } } ; else { var leaveType = new LeaveType() { Code = addLeaveType.Code, CreatedOn = DateTime.UtcNow, Description = addLeaveType.Description, InstituteId = instituteId, LeaveAssignedTypeEnum = EnumHelperService.GetValueFromDescription <LeaveAssignedTypeEnum>(addLeaveType.LeaveAssignedTypeEnumDescription), Name = addLeaveType.Name, NumberOfAllowedLeave = addLeaveType.NumberOfAllowedLeave, UpdatedById = loggedInUser.Id, UpdatedOn = DateTime.UtcNow }; _iMSDbContext.LeaveTypes.Add(leaveType); await _iMSDbContext.SaveChangesAsync(); List <LeaveAssignedTo> assignedTos = new List <LeaveAssignedTo>(); foreach (var user in addLeaveType.LeaveAssignedTos) { assignedTos.Add(new LeaveAssignedTo() { CreatedOn = DateTime.UtcNow, LeaveTypeId = leaveType.Id, UserId = user }); } using (var db = await _iMSDbContext.Database.BeginTransactionAsync()) { await _iMSDbContext.BulkInsertAsync(assignedTos); db.Commit(); } return(new LeaveTypeManagementResponse() { HasError = false, Message = "Leave type added succesfully" }); } } } }