public static async Task Run([EventHubTrigger("pack2schoolhub", Connection = "myConnection")] EventData[] events, [SignalR(HubName = "Pack2SchoolSignalR1")] IAsyncCollector <SignalRMessage> signalRMessages,
                                     ILogger log)
        {
            var exceptions = new List <Exception>();

            foreach (EventData eventData in events)
            {
                List <string>          missingSubjects = null;
                DataBaseAndScanUpdates dataBaseAndScanUpdates;

                eventData.SystemProperties.TryGetValue(deviceIdProperty, out var deviceIdObj);
                string userId = (string)deviceIdObj;

                string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");

                JObject obj          = JObject.Parse(messageBody);
                var     errorMessage = obj[error]?.ToString();

                if (errorMessage != null)
                {
                    dataBaseAndScanUpdates = new DataBaseAndScanUpdates(errorMessage);
                }
                else
                {
                    var subjects       = obj[stickers].Select(x => x.ToString()).ToList();
                    var subjectsUpdate = await SubjectsTableUtilities.UpdateBagContent(userId, subjects);

                    var extraSubjects = subjectsUpdate[0];
                    missingSubjects        = subjectsUpdate[1];
                    dataBaseAndScanUpdates = new DataBaseAndScanUpdates(userId, missingSubjects: missingSubjects, extraSubjects: extraSubjects);
                }

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


                var parentsEntities = UsersTableUtilities.GetParentsEntitiesFromChildId(userId);

                foreach (var parentEntity in parentsEntities)
                {
                    if (parentEntity.UserEmail != null && dataBaseAndScanUpdates.missingSubjects != null && dataBaseAndScanUpdates.missingSubjects.Count > 0)
                    {
                        await EmailSender.sendEmailAsync(userId, parentEntity.UserEmail, missingSubjects);
                    }
                }
            }
        }
        public static async Task <string> GetAllSubjects(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request, ILogger log)
        {
            {
                OperationResult operationResult = new OperationResult();
                SubjectRequest  subjectsRequest = await Utilities.ExtractContent <SubjectRequest>(request);

                var allSubjects = SubjectsTableUtilities.GetAllSubjects(subjectsRequest.tableName);
                operationResult.UpdateData(allSubjects);
                return(JsonConvert.SerializeObject(operationResult));
            }
        }
示例#3
0
        public static async Task <string> AddNewClass(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request,
            [Table("ClassesTable")] CloudTable classesTable, ILogger log)
        {
            List <string>   teacherClassesIds;
            OperationResult result = new OperationResult();

            SchoolClass newClassInfo = await Utilities.ExtractContent <SchoolClass>(request);

            var usersTable    = CloudTableUtilities.OpenTable(ProjectConsts.UsersTableName);
            var teacherEntity = CloudTableUtilities.getTableEntityAsync <UsersTable>(usersTable, newClassInfo.teacherId).Result.First();
            TableQuerySegment <ClassesTable> teacherClasses = await CloudTableUtilities.getTableEntityAsync <ClassesTable>(classesTable, teacherEntity.RowKey, newClassInfo.classId);

            if (!teacherClasses.Any())
            {
                string subjectsTableName = $"{teacherEntity.RowKey}{newClassInfo.classId}";
                SubjectsTableUtilities.CreateClassTable(subjectsTableName);
                var newClassEntity = new ClassesTable {
                    subjectsTableName = newClassInfo.ToString()
                };
                newClassEntity.LastTeacherUpdate = defaultDateTime;
                TableOperation insertOperation = TableOperation.InsertOrReplace(newClassEntity);
                newClassEntity.PartitionKey      = teacherEntity.RowKey;
                newClassEntity.RowKey            = newClassInfo.classId;
                newClassEntity.subjectsTableName = subjectsTableName;
                await classesTable.ExecuteAsync(insertOperation);

                result.UpdateData(newClassEntity.subjectsTableName);

                if (teacherEntity.ClassId != null)
                {
                    teacherClassesIds = teacherEntity.ClassId.Split(ProjectConsts.delimiter).ToList();
                }
                else
                {
                    teacherClassesIds = new List <string>();
                }

                teacherClassesIds.Add(newClassInfo.classId);
                teacherEntity.ClassId = string.Join(ProjectConsts.delimiter, teacherClassesIds);
                await CloudTableUtilities.AddTableEntity <UsersTable>(usersTable, teacherEntity);
            }
            else
            {
                result.UpdateFailure(ErrorMessages.classAlreadyExist);
            }

            return(JsonConvert.SerializeObject(result));
        }
        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));
        }
示例#6
0
        public static async Task <string> SignUp(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request,
            [Table("UsersTable")] CloudTable usersTable)
        {
            OperationResult operationResult = new OperationResult();
            string          childrenIds     = null;

            UserRequest newUserRequest = await Utilities.ExtractContent <UserRequest>(request);

            TableQuery <UsersTable> query = new TableQuery <UsersTable>();

            TableQuerySegment <UsersTable> usersQueryResult = await usersTable.ExecuteQuerySegmentedAsync(query, null);

            if (!UsersTableUtilities.ValidateUserNotExist(usersQueryResult, newUserRequest, operationResult))
            {
                return(JsonConvert.SerializeObject(operationResult));
            }

            if (newUserRequest.userType == ProjectConsts.ParentType)
            {
                if (newUserRequest.childrenIds == null || newUserRequest.childrenIds.Count == 0)
                {
                    operationResult.UpdateFailure(ErrorMessages.NoChildIdProvided);
                    return(JsonConvert.SerializeObject(operationResult));
                }

                childrenIds = string.Join(ProjectConsts.delimiter, newUserRequest.childrenIds);

                if (!UsersTableUtilities.ValidateChildrenIdExist(usersQueryResult, newUserRequest, operationResult))
                {
                    return(JsonConvert.SerializeObject(operationResult));
                }
            }


            if (newUserRequest.userType == ProjectConsts.TeacherType)
            {
                newUserRequest.userName = UsersTableUtilities.GetUniqueName(newUserRequest.userName);
            }

            if (newUserRequest.userType == ProjectConsts.StudentType)
            {
                var tableExist = CloudTableUtilities.TableExist(newUserRequest.teacherUser + newUserRequest.classId);

                if (!tableExist)
                {
                    operationResult.UpdateFailure(string.Format(ErrorMessages.subjectTableNotExist));
                    return(JsonConvert.SerializeObject(operationResult));
                }
            }

            var newUserEntity = new UsersTable()
            {
                UserType     = newUserRequest.userType,
                UserEmail    = newUserRequest.userEmail,
                UserPassword = newUserRequest.userPassword,
                TeacherName  = newUserRequest.teacherUser,
                ClassId      = newUserRequest.classId,
                ChildrenIds  = childrenIds
            };

            await CloudTableUtilities.AddTableEntity(usersTable, newUserEntity, newUserRequest.userId, newUserRequest.userName);

            if (newUserRequest.userType == ProjectConsts.TeacherType)
            {
                operationResult.UpdateData(newUserEntity.RowKey);
            }


            if (newUserRequest.userType == ProjectConsts.StudentType)
            {
                var deviceConnectionString = await IotDeviceFunctions.AddDeviceAsync(newUserRequest.userId);

                var subjectsTablesNames = UsersTableUtilities.GetSubjectsTableNamesForStudent(newUserEntity);
                await SubjectsTableUtilities.AddStuentToClassTableAsync(subjectsTablesNames.First(), newUserRequest, operationResult);

                operationResult.UpdateData(new { deviceConnectionString, subjectsTablesNames });
            }

            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));
        }