public static async Task RenameSubjectAsync(SubjectRequest editSubjectRequest, SubjectsTable subjectsNames, string scheduleTableName, SubjectsTable subjectsNecessity, OperationResult operationResult)
        {
            var subjects = typeof(SubjectsTable).GetProperties().Where(x => string.Equals(x.GetValue(subjectsNames, null)?.ToString(), editSubjectRequest.subjectName));

            if (!subjects.Any())
            {
                operationResult.UpdateFailure(ErrorMessages.subjetNotFound);
            }
            else
            {
                subjects.First().SetValue(subjectsNames, editSubjectRequest.newSubjectName);
            }

            if (scheduleTableName != null)
            {
                var scheduleTable = CloudTableUtilities.OpenTable(scheduleTableName);
                var daysEntities  = CloudTableUtilities.getTableEntityAsync <ScheduleTable>(scheduleTable).Result;

                foreach (var dayEntity in daysEntities)
                {
                    var daySubjects = typeof(ScheduleTable).GetProperties().Where(x => x.Name.Contains(SubjectPropertyPrefix) && x.GetValue(dayEntity, null) != null).ToList();

                    foreach (var subjectProperty in daySubjects)
                    {
                        if (subjectProperty.GetValue(dayEntity, null).ToString() == editSubjectRequest.subjectName)
                        {
                            subjectProperty.SetValue(dayEntity, editSubjectRequest.newSubjectName);
                        }
                    }

                    await CloudTableUtilities.AddTableEntity(scheduleTable, dayEntity);
                }
            }
        }
        public static List <string> GetAllStudentsIds(string tableName)
        {
            var subjectsTable  = CloudTableUtilities.OpenTable(tableName);
            var studentsEntity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, null, IsInsideTheBag).Result;

            return(studentsEntity.Select(x => x.PartitionKey).ToList());
        }
        public static async Task AddStuentToClassTableAsync(string subjectsTableName, UserRequest newUserRequest, OperationResult response)
        {
            var table = CloudTableUtilities.OpenTable(subjectsTableName);

            if (table == null)
            {
                response.UpdateFailure(ErrorMessages.subjectTableNotExist);
                response.requestSucceeded = false;
                return;
            }

            SubjectsTable newIsInsideTheBagEntity = new SubjectsTable();
            SubjectsTable newStickersEntity       = new SubjectsTable();
            var           classSubjectsEntity     = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(table, ClassSubjects).Result.First();
            var           classSubjectsProperties = typeof(SubjectsTable).GetProperties().Where(x => x.Name.Contains(SubjectPropertyPrefix) && x.GetValue(classSubjectsEntity, null) != null);

            foreach (var classSubjectProperty in classSubjectsProperties)
            {
                classSubjectProperty.SetValue(newIsInsideTheBagEntity, NotInsideTheBag);
            }

            await CloudTableUtilities.AddTableEntity(table, newIsInsideTheBagEntity, newUserRequest.userId, IsInsideTheBag);

            await CloudTableUtilities.AddTableEntity(table, newStickersEntity, newUserRequest.userId, Stickers);
        }
        public static async Task <List <List <string> > > UpdateBagContent(string studentId, List <string> subjectsInTheBag)
        {
            var usersTable                  = CloudTableUtilities.OpenTable(ProjectConsts.UsersTableName);
            var studentEntity               = CloudTableUtilities.getTableEntityAsync <UsersTable>(usersTable, studentId).Result.First();
            var subjectsTableName           = UsersTableUtilities.GetSubjectsTableNamesForStudent(studentEntity).First();
            var subjectsTable               = CloudTableUtilities.OpenTable(subjectsTableName);
            var studentIsInsideTheBagEntity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, studentId, IsInsideTheBag).Result.First();
            var studentStickersEntity       = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, studentId, Stickers).Result.First();
            var subjectsNamesValuesMapping  = typeof(SubjectsTable).GetProperties().Where(x => x.Name.Contains(SubjectPropertyPrefix) && x.GetValue(studentStickersEntity, null) != null).ToDictionary(prop => prop.Name, prop => prop.GetValue(studentStickersEntity, null)?.ToString());

            foreach (var mapping in subjectsNamesValuesMapping)
            {
                if (subjectsInTheBag.Contains(mapping.Value))
                {
                    typeof(SubjectsTable).GetProperty(mapping.Key).SetValue(studentIsInsideTheBagEntity, InsideTheBag);
                }
                else
                {
                    typeof(SubjectsTable).GetProperty(mapping.Key).SetValue(studentIsInsideTheBagEntity, NotInsideTheBag);
                }
            }

            await CloudTableUtilities.AddTableEntity(subjectsTable, studentIsInsideTheBagEntity);

            return(new List <List <string> >()
            {
                GetExtraSubjects(subjectsTableName, studentId), GetMissingSubejcts(subjectsTableName, studentId)
            });
        }
Exemple #5
0
        internal static string GetUniqueName(string userName)
        {
            var usersTable      = CloudTableUtilities.OpenTable(ProjectConsts.UsersTableName);
            var usersEntities   = CloudTableUtilities.getTableEntityAsync <UsersTable>(usersTable).Result;
            int numberOfTeacher = usersEntities.Where(user => user.RowKey.Contains(userName) && user.UserType == ProjectConsts.TeacherType).Count();

            return($"{userName}{numberOfTeacher}");
        }
        public static List <string> GetAllSubjects(string tableName)
        {
            var subjectsTable            = CloudTableUtilities.OpenTable(tableName);
            var classSubjectsNamesEntity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, ClassSubjects).Result.First();

            return(typeof(SubjectsTable).GetProperties().Where(propInfo => propInfo.Name.Contains(SubjectPropertyPrefix) && propInfo.GetValue(classSubjectsNamesEntity) != null)
                   .Select(propInfo => propInfo.GetValue(classSubjectsNamesEntity)?.ToString()).ToList());
        }
Exemple #7
0
        public static List <string> GetSubjectsTableNamesForStudent(UsersTable userEntity)
        {
            var classesTable = CloudTableUtilities.OpenTable(ProjectConsts.classesTableName);
            var classEntity  = CloudTableUtilities.getTableEntityAsync <ClassesTable>(classesTable, userEntity.TeacherName, userEntity.ClassId).Result.First();

            return(new List <string>()
            {
                classEntity.subjectsTableName
            });
        }
        public static List <string> GetNeededSubject(string tableName)
        {
            var subjectsTable                = CloudTableUtilities.OpenTable(tableName);
            var classSubjectsNamesEntity     = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, ClassSubjects).Result.First();
            var classSubjectsNecessityEntity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, Necessity).Result.First();
            var subjectsNamesValuesMapping   = typeof(SubjectsTable).GetProperties().ToDictionary(prop => prop.Name, prop => prop.GetValue(classSubjectsNamesEntity, null));
            var neededSubjectsAsProperties   = typeof(SubjectsTable).GetProperties().Where(x => x.Name.Contains(SubjectPropertyPrefix) && x.GetValue(classSubjectsNecessityEntity, null)?.ToString() == "true");
            var neededSubjects               = neededSubjectsAsProperties.Select(x => subjectsNamesValuesMapping[x.Name.ToString()].ToString()).ToList();

            return(neededSubjects);
        }
        public static async Task <string> SetClassSchedule(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request,
            [Table("ClassesTable")] CloudTable classesTable, ILogger log)
        {
            OperationResult result = new OperationResult();
            CloudTable      scheduleTable;
            ScheduleSetter  newClassInfo = await Utilities.ExtractContent <ScheduleSetter>(request);

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

            foreach (var classEntity in classesEntities)
            {
                if (classEntity.subjectsTableName == newClassInfo.className)
                {
                    if (!CloudTableUtilities.TableExist($"{classEntity.subjectsTableName}Schedule"))
                    {
                        scheduleTable = await CloudTableUtilities.CreateTableAsync($"{classEntity.subjectsTableName}Schedule");

                        classEntity.ScheduleTableName = $"{classEntity.subjectsTableName}Schedule";
                    }
                    else
                    {
                        scheduleTable = CloudTableUtilities.OpenTable($"{classEntity.subjectsTableName}Schedule");
                    }

                    var daysInfo1 = typeof(ScheduleSetter).GetProperties().ToList();
                    var daysInfo  = typeof(ScheduleSetter).GetProperties().Where(x => x.Name != "className").ToList();

                    foreach (var dayinfo in daysInfo)
                    {
                        var dayEnity     = new ScheduleTable();
                        var tableColumns = typeof(ScheduleTable).GetProperties().Where(x => x.Name.Contains(SubjectsTableUtilities.SubjectPropertyPrefix)).ToList();
                        var subjectsList = (List <string>)dayinfo.GetValue(newClassInfo);

                        foreach (var subject in subjectsList)
                        {
                            var col = tableColumns[0];
                            tableColumns.RemoveAt(0);
                            col.SetValue(dayEnity, subject);
                        }

                        dayEnity.PartitionKey = dayinfo.Name;
                        dayEnity.RowKey       = dayinfo.Name;
                        await CloudTableUtilities.AddTableEntity(scheduleTable, dayEnity);
                    }

                    classEntity.LastTeacherUpdate = new DateTime(2000, 1, 01, 0, 0, 0).ToUniversalTime();
                    await CloudTableUtilities.AddTableEntity(classesTable, classEntity);
                }
            }

            return(JsonConvert.SerializeObject(result));
        }
Exemple #10
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));
        }
Exemple #11
0
        public static async Task <string> GetChildSubjectsTableName(

            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request,
            [Table("UsersTable")] CloudTable usersTable)
        {
            OperationResult operationResult = new OperationResult();

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

            var userChildEntity   = CloudTableUtilities.getTableEntityAsync <UsersTable>(usersTable, userRequest.childrenIds.First()).Result.First();
            var subjectsTableName = UsersTableUtilities.GetSubjectsTableNamesForStudent(userChildEntity);

            operationResult.UpdateData(subjectsTableName);
            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> UpdateSubjectSticker([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request,
                                                               ILogger log)
        {
            OperationResult operationResult = new OperationResult();
            SubjectRequest  subjectRequest  = await Utilities.ExtractContent <SubjectRequest>(request);

            var subjectsTable = CloudTableUtilities.OpenTable(subjectRequest.tableName);
            var userStickers  = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, subjectRequest.userId, SubjectsTableUtilities.Stickers).Result.First();
            var classSubjects = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.ClassSubjects).Result.First();

            var subjectsNamesDict = typeof(SubjectsTable).GetProperties().Where(x => x.Name.Contains(SubjectsTableUtilities.SubjectPropertyPrefix) && x.GetValue(classSubjects, null) != null).ToDictionary(prop => prop.GetValue(classSubjects, null).ToString(), prop => prop.Name);

            typeof(SubjectsTable).GetProperty(subjectsNamesDict.GetValueOrDefault(subjectRequest.subjectName)).SetValue(userStickers, subjectRequest.stickerId);
            await CloudTableUtilities.AddTableEntity(subjectsTable, userStickers);

            return(JsonConvert.SerializeObject(operationResult));
        }
        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 void CreateClassTable(string tableName)
        {
            var subjectsTable = await CloudTableUtilities.CreateTableAsync(tableName);

            var            classSubjectsNamesEntity = new SubjectsTable();
            TableOperation insertOperation          = TableOperation.InsertOrReplace(classSubjectsNamesEntity);

            classSubjectsNamesEntity.PartitionKey = ClassSubjects;
            classSubjectsNamesEntity.RowKey       = ClassSubjects;
            await subjectsTable.ExecuteAsync(insertOperation);

            var classSubjectsNecessityEntity = new SubjectsTable();

            insertOperation = TableOperation.InsertOrReplace(classSubjectsNecessityEntity);
            classSubjectsNecessityEntity.PartitionKey = Necessity;
            classSubjectsNecessityEntity.RowKey       = Necessity;
            await subjectsTable.ExecuteAsync(insertOperation);
        }
Exemple #16
0
        public static List <string> GetSubjectsTableNamesForTeacher(UsersTable userEntity)
        {
            var subjectsTablesNames = new List <string>();

            if (userEntity.ClassId == null)
            {
                return(subjectsTablesNames);
            }

            var classesIds   = userEntity.ClassId.Split(ProjectConsts.delimiter).ToList();
            var classesTable = CloudTableUtilities.OpenTable(ProjectConsts.classesTableName);

            foreach (var classId in classesIds)
            {
                var classEntity = CloudTableUtilities.getTableEntityAsync <ClassesTable>(classesTable, userEntity.RowKey, classId).Result.First();
                subjectsTablesNames.Add(classEntity.subjectsTableName);
            }

            return(subjectsTablesNames);
        }
Exemple #17
0
        public static List <UsersTable> GetParentsEntitiesFromChildId(string childId)
        {
            var parentsIds  = new List <UsersTable>();
            var usersTable  = CloudTableUtilities.OpenTable(ProjectConsts.UsersTableName);
            var usersEntity = CloudTableUtilities.getTableEntityAsync <UsersTable>(usersTable).Result;

            foreach (var userEntity in usersEntity)
            {
                if (userEntity.UserType != ProjectConsts.ParentType)
                {
                    continue;
                }

                var childrenIds = userEntity.ChildrenIds.Split(ProjectConsts.delimiter).ToList();
                if (childrenIds.Contains(childId))
                {
                    parentsIds.Add(userEntity);
                }
            }

            return(parentsIds);
        }
        public static List <string> GetExtraSubjects(string tableName, string userId)
        {
            var subjectsTable                = CloudTableUtilities.OpenTable(tableName);
            var classSubjectsNamesEntity     = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, ClassSubjects).Result.First();
            var classSubjectsNecessityEntity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, Necessity).Result.First();
            var studentIsInsideTheBagEntity  = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, userId, IsInsideTheBag).Result.First();

            List <string> missingSubjects = new List <string>();


            var subjectsNamesValuesMapping          = typeof(SubjectsTable).GetProperties().Where(x => x.Name.Contains(SubjectPropertyPrefix) && x.GetValue(classSubjectsNamesEntity, null) != null).ToDictionary(prop => prop.Name, prop => prop.GetValue(classSubjectsNamesEntity, null).ToString());
            var subjectsNamesNecessityMapping       = typeof(SubjectsTable).GetProperties().Where(x => x.Name.Contains(SubjectPropertyPrefix) && x.GetValue(classSubjectsNamesEntity, null) != null).ToDictionary(prop => prop.Name, prop => prop.GetValue(classSubjectsNecessityEntity, null)?.ToString());
            var subejctsNamesToPresenceInbagMapping = typeof(SubjectsTable).GetProperties().Where(x => x.Name.Contains(SubjectPropertyPrefix) && x.GetValue(classSubjectsNamesEntity, null) != null).ToDictionary(prop => prop.Name, prop => prop.GetValue(studentIsInsideTheBagEntity, null)?.ToString());

            foreach (var subjectNecessity in subjectsNamesNecessityMapping)
            {
                if (subjectNecessity.Value == NotNeededSubject && subejctsNamesToPresenceInbagMapping.GetValueOrDefault(subjectNecessity.Key) == InsideTheBag)
                {
                    missingSubjects.Add(subjectsNamesValuesMapping.GetValueOrDefault(subjectNecessity.Key));
                }
            }

            return(missingSubjects);
        }
Exemple #19
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));
        }