public static async Task <string> GetNeededSubjects(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request,
            ILogger log)
        {
            OperationResult operationResult = new OperationResult();
            SubjectRequest  subjectRequest  = await Utilities.ExtractContent <SubjectRequest>(request);

            var neededSubjects = SubjectsTableUtilities.GetNeededSubject(subjectRequest.tableName);

            operationResult.UpdateData(neededSubjects);
            return(JsonConvert.SerializeObject(operationResult));
        }
        public static async Task RunAsync([TimerTrigger("0 00 14 * * *")] TimerInfo myTimer, [SignalR(HubName = "Pack2SchoolSignalR1")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            var TodayDateTime   = DateTime.Today;
            var classesTable    = CloudTableUtilities.OpenTable(ProjectConsts.classesTableName);
            var classesEntities = CloudTableUtilities.getTableEntityAsync <ClassesTable>(classesTable).Result;

            foreach (var classEntity in classesEntities)
            {
                if (classEntity.ScheduleTableName == null)
                {
                    continue;
                }

                if (classEntity.LastTeacherUpdate.Date != TodayDateTime)
                {
                    var day            = System.DateTime.Now.DayOfWeek.ToString();
                    var scheduleTable  = CloudTableUtilities.OpenTable(classEntity.ScheduleTableName);
                    var subjectsEntity = CloudTableUtilities.getTableEntityAsync <ScheduleTable>(scheduleTable, day).Result.First();
                    var neededSubjects = typeof(ScheduleTable).GetProperties().Where(propInfo => propInfo.Name.Contains(SubjectsTableUtilities.SubjectPropertyPrefix) && propInfo.GetValue(subjectsEntity) != null)
                                         .Select(propInfo => propInfo.GetValue(subjectsEntity)?.ToString()).ToList();

                    var subjectsTable     = CloudTableUtilities.OpenTable(classEntity.subjectsTableName);
                    var subjectsNames     = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.ClassSubjects).Result.First();
                    var subjectsNecessity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.Necessity).Result.First();
                    SubjectsTableUtilities.UpdateSubjectNecessity(subjectsNames, subjectsNecessity, neededSubjects);
                    await CloudTableUtilities.AddTableEntity(subjectsTable, subjectsNecessity);

                    neededSubjects = SubjectsTableUtilities.GetNeededSubject(classEntity.subjectsTableName);
                    var studentsIds = SubjectsTableUtilities.GetAllStudentsIds(classEntity.subjectsTableName);

                    foreach (var studentId in studentsIds)
                    {
                        var missingSubjects = SubjectsTableUtilities.GetMissingSubejcts(classEntity.subjectsTableName, studentId);

                        var dataBaseAndScanUpdates = new DataBaseAndScanUpdates(studentId, neededSubjects, missingSubjects);

                        await signalRMessages.AddAsync(
                            new SignalRMessage
                        {
                            UserId    = studentId,
                            Target    = ProjectConsts.SignalRTarget,
                            Arguments = new object[] { dataBaseAndScanUpdates }
                        });
                    }
                }
            }
        }
        public static async Task <string> UpdateSubjectNecessity(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request, [SignalR(HubName = "Pack2SchoolSignalR1")] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            var            operationResult = new OperationResult();
            SubjectRequest subjectRequest  = await Utilities.ExtractContent <SubjectRequest>(request);

            var subjectsTable          = CloudTableUtilities.OpenTable(subjectRequest.tableName);
            var classSubjectsNames     = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.ClassSubjects).Result.First();
            var classSubjectsNecessity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.Necessity).Result.First();

            SubjectsTableUtilities.UpdateSubjectNecessity(classSubjectsNames, classSubjectsNecessity, subjectRequest.neededForTomorrow);
            await CloudTableUtilities.AddTableEntity(subjectsTable, classSubjectsNecessity);

            var neededSubjects = SubjectsTableUtilities.GetNeededSubject(subjectRequest.tableName);
            var studentsIds    = SubjectsTableUtilities.GetAllStudentsIds(subjectRequest.tableName);

            foreach (var studentId in studentsIds)
            {
                var missingSubjects = SubjectsTableUtilities.GetMissingSubejcts(subjectRequest.tableName, studentId);

                var dataBaseAndScanUpdates = new DataBaseAndScanUpdates(studentId, neededSubjects, missingSubjects);

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    UserId    = studentId,
                    Target    = ProjectConsts.SignalRTarget,
                    Arguments = new object[] { dataBaseAndScanUpdates }
                });
            }

            var classesTable    = CloudTableUtilities.OpenTable(ProjectConsts.classesTableName);
            var classesEntities = CloudTableUtilities.getTableEntityAsync <ClassesTable>(classesTable).Result;

            foreach (var classEntity in classesEntities)
            {
                if (classEntity.subjectsTableName == subjectRequest.tableName)
                {
                    classEntity.LastTeacherUpdate = DateTime.Now;
                    await CloudTableUtilities.AddTableEntity(classesTable, classEntity);
                }
            }

            return(JsonConvert.SerializeObject(operationResult));
        }
        public static async Task <string> EditSubject(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request, [SignalR(HubName = "Pack2SchoolSignalR1")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            OperationResult operationResult    = new OperationResult();
            SubjectRequest  editSubjectRequest = await Utilities.ExtractContent <SubjectRequest>(request);

            var    subjectsTable          = CloudTableUtilities.OpenTable(editSubjectRequest.tableName);
            var    classSubjectsNames     = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.ClassSubjects).Result.First();
            var    classSubjectsNecessity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.Necessity).Result.First();
            var    classesTable           = CloudTableUtilities.OpenTable(ProjectConsts.classesTableName);
            var    classesEntities        = CloudTableUtilities.getTableEntityAsync <ClassesTable>(classesTable).Result;
            string scheduleTableName      = null;

            foreach (var classEntity in classesEntities)
            {
                if (classEntity.subjectsTableName == editSubjectRequest.tableName)
                {
                    scheduleTableName = classEntity.ScheduleTableName;
                    break;
                }
            }

            if (editSubjectRequest.requestType == ProjectConsts.AddSubjectOperation)
            {
                SubjectsTableUtilities.AddNewSubject(editSubjectRequest, classSubjectsNames, classSubjectsNecessity, operationResult);
            }

            if (editSubjectRequest.requestType == ProjectConsts.RenameSubjectOperation)
            {
                await SubjectsTableUtilities.RenameSubjectAsync(editSubjectRequest, classSubjectsNames, scheduleTableName, classSubjectsNecessity, operationResult);
            }

            if (editSubjectRequest.requestType == ProjectConsts.DeleteSubjectOperation)
            {
                await SubjectsTableUtilities.DeleteSubjectAsync(editSubjectRequest, classSubjectsNames, scheduleTableName, classSubjectsNecessity, operationResult);
            }

            await CloudTableUtilities.AddTableEntity(subjectsTable, classSubjectsNames);

            await CloudTableUtilities.AddTableEntity(subjectsTable, classSubjectsNecessity);

            var neededSubjects = SubjectsTableUtilities.GetNeededSubject(editSubjectRequest.tableName);
            var allSubjects    = SubjectsTableUtilities.GetAllSubjects(editSubjectRequest.tableName);
            var studentsIds    = SubjectsTableUtilities.GetAllStudentsIds(editSubjectRequest.tableName);

            foreach (var studentId in studentsIds)
            {
                var missingSubjects = SubjectsTableUtilities.GetMissingSubejcts(editSubjectRequest.tableName, studentId);

                var dataBaseAndScanUpdates = new DataBaseAndScanUpdates(studentId, neededSubjects, missingSubjects, allSubjects);

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    UserId    = studentId,
                    Target    = ProjectConsts.SignalRTarget,
                    Arguments = new object[] { dataBaseAndScanUpdates }
                });
            }

            return(JsonConvert.SerializeObject(operationResult));
        }