Created
April 13, 2020 22:09
-
-
Save Kikimora/df63df2c4659cd55533cacb4bb29bb69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IBotOptionsVisitor<T> | |
{ | |
T Visit(CopyCatOptions copyCatOptions); | |
T Visit(VolGenOptions volGenOptions); | |
} | |
static class BotOptionsVisitor | |
{ | |
public static bool TryVisit<T>(JObject options, IBotOptionsVisitor<T> visitor, out T result) | |
{ | |
result = default; | |
if (!options.TryGetValue(nameof(BotOptions.Type), out var type)) | |
return false; | |
result = type.ToString() switch | |
{ | |
CopyCatOptions.BotType => visitor.Visit(options.ToObject<CopyCatOptions>()), | |
VolGenOptions.BotType => visitor.Visit(options.ToObject<CopyCatOptions>()), | |
_ => default | |
}; | |
return true; | |
} | |
} | |
class BotOptionsReader : IBotOptionsVisitor<BotOptions> | |
{ | |
public static readonly IBotOptionsVisitor<BotOptions> Instance = new BotOptionsReader(); | |
public BotOptions Visit(CopyCatOptions copyCatOptions) | |
{ | |
return copyCatOptions; | |
} | |
public BotOptions Visit(VolGenOptions volGenOptions) | |
{ | |
return volGenOptions; | |
} | |
} | |
class UpdateBotOptionVisitor : IBotOptionsVisitor<BotOptions> | |
{ | |
private JObject _update; | |
public UpdateBotOptionVisitor([NotNull] JObject update) | |
{ | |
_update = Assert.NotNull(update, nameof(update)); | |
} | |
public BotOptions Visit(CopyCatOptions copyCatOptions) | |
{ | |
//copy data from update to copyCatOptions | |
return copyCatOptions; | |
} | |
public BotOptions Visit(VolGenOptions volGenOptions) | |
{ | |
//copy data from update to volGenOptions | |
return volGenOptions; | |
} | |
} | |
class BotController | |
{ | |
private readonly IQodexConfigStore _configStore; | |
public BotController([NotNull] IQodexConfigStore configStore) | |
{ | |
_configStore = Assert.NotNull(configStore, nameof(configStore)); | |
} | |
[HttpGet] | |
public async Task<BotOptions[]> GetAll() | |
{ | |
var options = await _configStore.Entries.Where(x => x.Domain == "Bots").ToListAsync(); | |
return options.Select(x => | |
{ | |
BotOptionsVisitor.TryVisit(x.Value, BotOptionsReader.Instance, out var botOptions); | |
return botOptions; | |
}).Where(x => x != null).ToArray(); | |
} | |
[HttpPost("{id}")] | |
public async Task Update([FromRoute] string botId, [FromBody] JObject update) | |
{ | |
var options = await _configStore.Entries.FirstOrDefaultAsync(x => x.Key == botId && x.Domain == "Bots"); | |
if (options == null) throw ErrorCode.NotFound.ToException(HttpStatusCode.NotFound); | |
var visitor = new UpdateBotOptionVisitor(update); | |
if (BotOptionsVisitor.TryVisit(options.Value, visitor, out var updated)) | |
{ | |
await _configStore.SetAsync("Bots", botId, JObject.FromObject(updated)); | |
await _configStore.SaveChangesAsync(); | |
} | |
else | |
{ | |
//Unknown option type | |
throw ErrorCode.BadRequest.ToException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment