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

            var operation = JsonConvert.SerializeObject(new { command = ProjectConsts.scanOperaion });
            await IotDeviceFunctions.SendCloudToDeviceMessageAsync(operation, subjectRequest.userId);

            return(JsonConvert.SerializeObject(operationResult));
        }
Пример #2
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));
        }