Esempio n. 1
0
        public async Task <IActionResult> CreateTenantAsyncAndInviteAdmin([FromBody] CreateTenantCmd createTenantCmd)
        {
            BackofficeWriteContext ctx = new BackofficeWriteContext(
                new EFList <Tenant>(_dbContext.Tenant),
                new EFList <TenantUser>(_dbContext.TenantUser),
                new EFList <User>(_dbContext.User));

            var dependencies = new BackofficeDependencies();

            dependencies.GenerateInvitationToken = () => Guid.NewGuid().ToString();
            dependencies.SendInvitationEmail     = SendEmail;

            var expr = from createTenantResult in BackofficeDomain.CreateTenant(createTenantCmd)
                       let adminUser = createTenantResult.SafeCast <CreateTenantResult.TenantCreated>().Select(p => p.AdminUser)
                                       let inviteAdminCmd = new InviteTenantAdminCmd(adminUser)
                                                            from inviteAdminResult in BackofficeDomain.InviteTenantAdmin(inviteAdminCmd)
                                                            select new { createTenantResult, inviteAdminResult };

            var r = await _interpreter.Interpret(expr, ctx, dependencies);

            _dbContext.SaveChanges();
            return(r.createTenantResult.Match(
                       created => (IActionResult)Ok(created.Tenant.TenantId),
                       notCreated => StatusCode(StatusCodes.Status500InternalServerError, "Tenant could not be created."),//todo return 500 (),
                       invalidRequest => BadRequest("Invalid request.")));
        }
        public async Task <IActionResult> CreateTenantAsyncAndInviteAdmin([FromBody] CreateTenantCmd createTenantCmd)
        {
            BackofficeWriteContext ctx = new BackofficeWriteContext(new List <Tenant>(), new List <TenantUser>(), new List <User>());

            /* BackofficeWriteContext ctx = new BackofficeWriteContext(
             *   new EFList<Tenant>(_dbContext.Tenant),
             *   new EFList<TenantUser>(_dbContext.TenantUser),
             *   new EFList<User>(_dbContext.User));*/


            var dependencies = new BackofficeDependencies();

            dependencies.GenerateInvitationToken = () => Guid.NewGuid().ToString();
            dependencies.SendInvitationEmail     = (InvitationLetter letter) => async() => new InvitationAcknowledgement(Guid.NewGuid().ToString());

            var expr = from createTenantResult in BackofficeDomain.CreateTenant(createTenantCmd)
                       let adminUser = createTenantResult.SafeCast <CreateTenantResult.TenantCreated>().Select(p => p.AdminUser)
                                       let inviteAdminCmd = new InviteTenantAdminCmd(adminUser)
                                                            from inviteAdminResult in BackofficeDomain.InviteTenantAdmin(inviteAdminCmd)
                                                            select new { createTenantResult, inviteAdminResult };

            var r = await _interpreter.Interpret(expr, ctx, dependencies);

            return(r.createTenantResult.Match(
                       created => (IActionResult)Ok(created.Tenant.TenantId),
                       notCreated => BadRequest("Tenant could not be created."),
                       invalidRequest => BadRequest("Invalid request.")));
        }
Esempio n. 3
0
        public async Task <IActionResult> CreateTenantAsyncAndInviteAdmin([FromBody] CreateTenantCmd createTenantCmd)
        {
            var ctx = await _dbContext.LoadAsync("dbo.BackofficeHttpController", new
            {
                OrganisationId = createTenantCmd.OrganisationId
            }, async reader =>
            {
                var tenants     = await reader.ReadAsync <Tenant>();
                var tenantUsers = await reader.ReadAsync <TenantUser>();
                var users       = await reader.ReadAsync <User>();

                _dbContext.AttachRange(tenants);
                _dbContext.AttachRange(tenantUsers);
                _dbContext.AttachRange(users);
                return(new BackofficeWriteContext(
                           new EFList <Tenant>(_dbContext.Tenant),
                           new EFList <TenantUser>(_dbContext.TenantUser),
                           new EFList <User>(_dbContext.User)));
            });

            var expr = from createResult in BackofficeDomain.CreateTenant(createTenantCmd.OrganisationId,
                                                                          createTenantCmd.TenantName, createTenantCmd.Description, createTenantCmd.AdminEmail, createTenantCmd.AdminName, createTenantCmd.UserId)
                       let adminUser = createResult.SafeCast <CreateTenantResult.TenantCreated>().Select(p => p.User)
                                       from u in BackofficeDomain.InviteTenantAdmin(adminUser)
                                       select new { createResult, u };

            var r = await _interpreter.Interpret(expr, ctx);

            await _dbContext.SaveChangesAsync();

            return(r.createResult.Match(
                       created => (IActionResult)Ok(new CreateTenantAndAdminResponse(true, created.Tenant.TenantId, created.User.DisplayName)),
                       notCreated => BadRequest(new CreateTenantAndAdminResponse(false, 0, string.Empty)),
                       invalidRequest => BadRequest(new CreateTenantAndAdminResponse(false, 0, string.Empty))));
        }
        public async Task <IActionResult> CreateTenantAsyncAndInviteAdmin([FromBody] CreateTenantCmd createTenantCmd)
        {
            BackofficeWriteContext ctx = new BackofficeWriteContext(new List <Tenant>(), new List <TenantUser>(), new List <User>());
            var dependencies           = new BackofficeDependencies();

            dependencies.GenerateInvitationToken = () => Guid.NewGuid().ToString();
            dependencies.SendInvitationEmail     = (InvitationLetter letter) => async() => new InvitationAcknowledgement(Guid.NewGuid().ToString());

            var expr = from createTenantResult in BackofficeDomain.CreateTenant(createTenantCmd)
                       let adminUser = createTenantResult.SafeCast <CreateTenantResult.TenantCreated>().Select(p => p.AdminUser)
                                       let inviteAdminCmd = new InviteTenantAdminCmd(adminUser)
                                                            from inviteAdminResult in BackofficeDomain.InviteTenantAdmin(inviteAdminCmd)
                                                            select new { createTenantResult, inviteAdminResult };

            var r = await _interpreter.Interpret(expr, ctx, dependencies);

            _dbContext.Tenant.Add(new EF.Models.Tenant {
                RowGuid = Guid.NewGuid(), OrganisationId = createTenantCmd.OrganisationId, Name = createTenantCmd.TenantName, Description = createTenantCmd.Description
            });
            //var reply = await _dbContext.QuestionModel.Where(r => r.Title == "intrebTest").SingleOrDefaultAsync();
            //_dbContext.Question.Update(reply);
            await _dbContext.SaveChangesAsync();

            return(r.createTenantResult.Match(
                       created => (IActionResult)Ok(created.Tenant.TenantId),
                       notCreated => BadRequest("Tenant could not be created."),
                       invalidRequest => BadRequest("Invalid request.")));
        }
        public override Task Assertions(object[] path, CreateTenantCmd op, CreateTenantResult.ICreateTenantResult result, BackofficeWriteContext state)
        {
            var withInput = from inputCase in path.OfType <CreateTenantCmdInput>().HeadOrNone()
                            from stateCase in path.OfType <BackofficeWriteContextInput>().HeadOrNone()
                            select AssertionsOnInput(inputCase, stateCase, state, op, result);

            return(Task.CompletedTask);
        }
Esempio n. 6
0
        public async Task <IActionResult> CreateTenantAsyncAndInviteAdmin([FromBody] CreateTenantCmd createTenantCmd)
        {
            BackofficeWriteContext ctx = new BackofficeWriteContext(
                new EFList <Tenant>(_dbContext.Tenant),

                new EFList <User>(_dbContext.User));


            var r = await _interpreter.Interpret(expr, dependencies);

            _dbContext.SaveChanges();
            return(r.createTenantResult.Match(
                       created => (IActionResult)Ok(created.Tenant.TenantId),
                       notCreated => StatusCode(StatusCodes.Status500InternalServerError, "Tenant could not be created."),//todo return 500 (),
                       invalidRequest => BadRequest("Invalid request.")));
        }
        public Task OnInvalidInput(CreateTenantCmd op, CreateTenantResult.ICreateTenantResult result, BackofficeWriteContext state)
        {
            result.Match(created =>
            {
                Assert.True(op.ValidateObject(), "The command is expected to be invalid");
                Assert.True(false, "We shouldn't create a tenant with invalid input");
                return(created);
            },
                         notCreated =>
            {
                Assert.True(false, "Received 'NotCreated', expected InvalidRequest");
                return(notCreated);
            }, invalidRequest =>
            {
                Assert.True(true);
                Assert.False(op.ValidateObject(), "The command should be invalid");
                return(invalidRequest);
            });

            return(Task.CompletedTask);
        }
 public override Task PostConditions(CreateTenantCmd op, CreateTenantResult.ICreateTenantResult result, BackofficeWriteContext state)
 {
     result.Match(created =>
     {
         Assert.True(true);
         Assert.True(op.ValidateObject());
         Assert.Contains(state.Tenants, tenant => tenant.OrganisationId.Equals(op.OrganisationId));
         return(created);
     }, notCreated =>
     {
         Assert.True(false);
         Assert.DoesNotContain(state.Tenants, tenant => tenant.OrganisationId.Equals(op.OrganisationId));
         return(notCreated);
     },
                  invalidRequest =>
     {
         Assert.False(op.ValidateObject());
         Assert.True(false);
         return(invalidRequest);
     });
     return(Task.CompletedTask);
 }
Esempio n. 9
0
 public static Port <ICreateTenantResult> CreateTenant(CreateTenantCmd command)
 {
     return(NewPort <CreateTenantCmd, ICreateTenantResult>(command));
 }
Esempio n. 10
0
 public static Port <ICreateTenantResult> CreateTenant(CreateTenantCmd command) => NewPort <CreateTenantCmd, ICreateTenantResult>(command);
        private Task AssertionsOnInput(CreateTenantCmdInput inputCase, BackofficeWriteContextInput stateCase, BackofficeWriteContext state, CreateTenantCmd op, CreateTenantResult.ICreateTenantResult result)
        {
            var _ = (inputCase, stateCase) switch
            {
                (CreateTenantCmdInput.Valid, BackofficeWriteContextInput.Empty) => PostConditions(op, result, state),
                (CreateTenantCmdInput.Valid, BackofficeWriteContextInput.Nulls) => PostConditions(op, result, state),
                _ => OnInvalidInput(op, result, state)
            };

            return(Task.CompletedTask);
        }
Esempio n. 12
0
        public static async Task <GraphQLResponse <dynamic> > CreateTenantAndAdmin(this GraphQLHttpClient client, CreateTenantCmd createTenantCmd)
        {
            const string query =
                @"mutation createTenant($cmd: CreateTenantCmd!) {
                      createTenant(cmd: $cmd) {
                        successful 
                        tenantId
                        adminName
                      }
                }";

            var request = new GraphQLRequest(query, new Dictionary <string, object>()
            {
                { "cmd", createTenantCmd }
            });

            var result = await client.SendMutationAsync <dynamic>(request);

            Assert.IsNull(result.Errors);

            return(result);
        }