/// <summary> /// Adds the world protection /// </summary> public override RestResponse OnPost(Query query, object payloadObject) { //get the manager var manager = GetWhitelistManager(); if (manager == null) { return(new RestResponse(RestStatus.ResourceNotFound, msg: "Could not find the protection manager!")); } //Get the payload and make sure it svalid OptionalProtectedWorld patch = (OptionalProtectedWorld)payloadObject; if (!patch.AllowAnonymous.HasValue) { return(new RestResponse(RestStatus.BadRequest, msg: "Cannot create a new protection. The 'AllowAnonymous' is null or missing.")); } if (!patch.Mode.HasValue) { return(new RestResponse(RestStatus.BadRequest, msg: "Cannot create a new protection. The 'Mode' is null or missing.")); } var task = Task.Run(async() => { //Add the protection var protection = await manager.SetProtectionAsync(World, patch.Mode.Value, patch.AllowAnonymous.Value); //Add the users if (patch.AccountList != null) { foreach (var accname in patch.AccountList) { var acc = await Starbound.Configurator.GetAccountAsync(accname); await protection.SetAccountAsync(acc, ""); } } }); //If we are async, return instantly if (query.GetBool(Query.AsyncKey, false)) { return(RestResponse.Async); } //Return the newly added route task.Wait(); return(OnGet(query)); }
/// <summary> /// Edits the world protection /// </summary> public override RestResponse OnPatch(Query query, object payloadObject) { //get the manager var manager = GetWhitelistManager(); if (manager == null) { return(new RestResponse(RestStatus.ResourceNotFound, msg: "Could not find the protection manager!")); } //Get the payload OptionalProtectedWorld patch = (OptionalProtectedWorld)payloadObject; //Run the patch var task = Task.Run(async() => { //Get the protection var protection = await manager.GetProtectionAsync(World); if (protection == null) { return(OnPost(query, payloadObject)); } //update values if (patch.AllowAnonymous.HasValue) { protection.AllowAnonymous = patch.AllowAnonymous.Value; } if (patch.Mode.HasValue) { protection.Mode = patch.Mode.Value; } if (patch.Name != null) { protection.Name = patch.Name; } //Add the users if (patch.AccountList != null) { foreach (var accname in patch.AccountList) { var acc = await Starbound.Configurator.GetAccountAsync(accname); await protection.SetAccountAsync(acc, ""); } } await protection.SaveAsync(manager.DbContext); return(OnGet(query)); }); //If we are async, return instantly if (query.GetBool(Query.AsyncKey, false)) { return(RestResponse.Async); } //Return the newly added route task.Wait(); return(task.Result); ////get the manager //var manager = GetWhitelistManager(); //if (manager == null) return new RestResponse(RestStatus.ResourceNotFound, msg: "Could not find the protection manager!"); // ////Get the world //var protection = manager.GetWorld(World); //if (protection == null) return new RestResponse(RestStatus.ResourceNotFound, msg: "Could not find the protection for the world."); // ////Get the payload and make sure its valid //OptionalProtectedWorld patch = (OptionalProtectedWorld)payloadObject; //protection.Name = patch.Name ?? protection.Name; //protection.AllowAnonymous = patch.AllowAnonymous.GetValueOrDefault(protection.AllowAnonymous); //protection.Mode = patch.Mode.GetValueOrDefault(protection.Mode); // ////Add the members //if (patch.AccountList != null) //{ // foreach (var user in patch.AccountList) // { // protection. // } //} // ////Save the new lists //manager.Save(); // ////Return the newly patched route //return OnGet(query); }