Пример #1
0
        public async Task <IActionResult> CreateOrg()
        {
            var user = await GetCurrentUserAsync();

            var model = new CreateOrgViewModel(user);

            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> CreateOrg(CreateOrgViewModel model)
        {
            var user = await GetCurrentUserAsync();

            if (!ModelState.IsValid)
            {
                model.ModelStateValid = false;
                model.Recover(user);
                return(View(model));
            }
            // Ensure no org with the same name.
            if (await _dbContext.Organizations.AnyAsync(t => t.DisplayName.ToLower() == model.OrganizationName.ToLower()))
            {
                ModelState.AddModelError("", $"There is already an organization with name: '{model.OrganizationName.ToLower()}'");
                model.ModelStateValid = false;
                model.Recover(user);
                return(View(model));
            }
            // Ensure site can be created.
            try
            {
                await _sitesService.CreateNewSiteAsync(await accesstoken, model.OrganizationSiteName, true, true);
            }
            catch (AiurUnexceptedResponse e)
            {
                ModelState.AddModelError(string.Empty, e.Response.Message);
                model.ModelStateValid = false;
                model.Recover(user);
                return(View(model));
            }
            // Create org.
            var newOrg = new Organization
            {
                DisplayName = model.OrganizationName,
                Description = model.OrganizationDescription,
                SiteName    = model.OrganizationSiteName
            };

            _dbContext.Organizations.Add(newOrg);
            await _dbContext.SaveChangesAsync();

            var newRelation = new UserRelationship
            {
                OrganizationId = newOrg.Id,
                UserId         = user.Id
            };

            _dbContext.UserRelationships.Add(newRelation);
            await _dbContext.SaveChangesAsync();

            return(RedirectToAction(nameof(OrgHome), new OrgHomeAddressModel
            {
                OrgName = model.OrganizationName
            }));
        }