public async Task <IActionResult> CreateSite(CreateSiteViewModel model)
        {
            var user = await GetCurrentUserAsync();

            if (!ModelState.IsValid)
            {
                model.ModelStateValid = false;
                model.Recover(user);
                return(View(model));
            }
            try
            {
                await _sitesService.CreateNewSiteAsync(await accesstoken, model.SiteName, model.OpenToUpload, model.OpenToDownload);

                user.SiteName = model.SiteName;
                await _dbContext.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch (AiurUnexceptedResponse e)
            {
                ModelState.AddModelError(string.Empty, e.Response.Message);
                model.ModelStateValid = false;
                model.Recover(user);
                return(View(model));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateSite(CreateSiteViewModel model)
        {
            var user = await GetCurrentUserAsync();

            if (!ModelState.IsValid)
            {
                model.Recover(user);
                return(View(model));
            }
            try
            {
                var existing = await _sitesService.ViewMySitesAsync(await AccessToken);

                if (existing.Sites.Any(t => t.SiteName.ToLower().Trim() == model.SiteName.ToLower().Trim()))
                {
                    ModelState.AddModelError(string.Empty, $"The site with name: {model.SiteName} already exists. Please try another name.");
                    model.Recover(user);
                    return(View(model));
                }
                await _sitesService.CreateNewSiteAsync(await AccessToken, model.SiteName, model.OpenToUpload, model.OpenToDownload);

                user.SiteName = model.SiteName;
                await _userManager.UpdateAsync(user);

                return(RedirectToAction(nameof(Index)));
            }
            catch (AiurUnexpectedResponse e)
            {
                ModelState.AddModelError(string.Empty, e.Response.Message);
                model.Recover(user);
                return(View(model));
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> CreateSite(CreateSiteViewModel model)
        {
            var user = await GetCurrentUserAsync();

            if (!ModelState.IsValid)
            {
                model.Recover(user);
                return(View(model));
            }
            var app = await _dbContext.Apps.FindAsync(model.AppId);

            if (app == null)
            {
                return(NotFound());
            }
            if (app.CreatorId != user.Id)
            {
                return(Unauthorized());
            }
            try
            {
                var token = await _appsContainer.AccessToken(app.AppId, app.AppSecret);

                await _sitesService.CreateNewSiteAsync(token, model.SiteName, model.OpenToUpload, model.OpenToDownload);

                return(RedirectToAction(nameof(AppsController.ViewApp), "Apps", new { id = app.AppId, JustHaveUpdated = true }));
            }
            catch (AiurUnexpectedResponse e)
            {
                ModelState.AddModelError(string.Empty, e.Response.Message);
                model.Recover(user);
                return(View(model));
            }
        }
Ejemplo n.º 4
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
            }));
        }