예제 #1
0
        public object Post([FromBody] dynamic model)
        {
            // Create Site
            Site site = SiteHelper.CreateSite(model, _fileProvider);

            // Check if site with name already exists
            if (ManagementUnit.ServerManager.Sites.Any(s => s.Name.Equals(site.Name, StringComparison.OrdinalIgnoreCase)))
            {
                throw new AlreadyExistsException("name");
            }

            // Save it
            ManagementUnit.ServerManager.Sites.Add(site);
            ManagementUnit.Current.Commit();

            // Refresh
            site = SiteHelper.GetSite(site.Id);

            WaitForSiteStatusResolve(site);

            //
            // Create response
            dynamic website = (dynamic)SiteHelper.ToJsonModel(site, Context.Request.GetFields());

            return(Created((string)SiteHelper.GetLocation(website.id), website));
        }
예제 #2
0
        public object Get(string id)
        {
            Site site = SiteHelper.GetSite(new SiteId(id).Id);

            if (site == null)
            {
                return(NotFound());
            }

            return(SiteHelper.ToJsonModel(site, Context.Request.GetFields()));
        }
예제 #3
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            // Set settings
            Site site = SiteHelper.UpdateSite(new SiteId(id).Id, model, _fileProvider);

            if (site == null)
            {
                return(NotFound());
            }

            // Start/Stop
            if (model.status != null)
            {
                Status status = DynamicHelper.To <Status>(model.status);
                try {
                    switch (status)
                    {
                    case Status.Stopped:
                        site.Stop();
                        break;

                    case Status.Started:
                        site.Start();
                        break;
                    }
                }
                catch (COMException e) {
                    // If site is fresh and status is still unknown then COMException will be thrown when manipulating status
                    throw new ApiException("Error setting site status", e);
                }
                catch (ServerManagerException e) {
                    throw new ApiException(e.Message, e);
                }
            }

            // Update changes
            ManagementUnit.Current.Commit();

            // Refresh data
            site = ManagementUnit.ServerManager.Sites[site.Name];

            //
            // Create response
            dynamic sModel = SiteHelper.ToJsonModel(site, Context.Request.GetFields());

            // The Id could change by changing the sites key
            if (sModel.id != id)
            {
                return(LocationChanged(SiteHelper.GetLocation(sModel.id), sModel));
            }

            return(sModel);
        }