|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Builder; |
| 8 | +using Microsoft.AspNetCore.Hosting; |
| 9 | +using Microsoft.AspNetCore.Http; |
| 10 | +using Microsoft.AspNetCore.Routing; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Newtonsoft.Json; |
| 13 | + |
| 14 | +namespace OperationResult.NinjaWars |
| 15 | +{ |
| 16 | + public class Startup |
| 17 | + { |
| 18 | + public void ConfigureServices(IServiceCollection services) |
| 19 | + { |
| 20 | + services.AddRouting(); |
| 21 | + } |
| 22 | + |
| 23 | + public void Configure(IApplicationBuilder app, IHostingEnvironment env) |
| 24 | + { |
| 25 | + app.UseRouter(builder => |
| 26 | + { |
| 27 | + var clanService = new ClanWarService(); |
| 28 | + builder.MapGet("api/clans/{clanId}/warstatus", async (request, response, data) => |
| 29 | + { |
| 30 | + // Read param |
| 31 | + var clanId = data.Values["clanId"].ToString(); |
| 32 | + |
| 33 | + // Execute operation |
| 34 | + var result = clanService.ReadAllWarStatusOf(clanId); |
| 35 | + |
| 36 | + // Handle the result |
| 37 | + string jsonResponse; |
| 38 | + if (result.Succeeded) |
| 39 | + { |
| 40 | + // Do something with the value |
| 41 | + jsonResponse = JsonConvert.SerializeObject(result.Value); |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + // Do something with the error |
| 46 | + jsonResponse = JsonConvert.SerializeObject(new { error = result.Error }); |
| 47 | + } |
| 48 | + await response.WriteAsync(jsonResponse); |
| 49 | + }); |
| 50 | + |
| 51 | + builder.MapVerb("PATCH", "api/clans/{clanId}/warstatus", async (request, response, data) => |
| 52 | + { |
| 53 | + // Read param |
| 54 | + var clanId = data.Values["clanId"].ToString(); |
| 55 | + |
| 56 | + // Deserialize the JSON body |
| 57 | + using (StreamReader reader = new StreamReader(request.Body, Encoding.UTF8)) |
| 58 | + { |
| 59 | + var jsonText = await reader.ReadToEndAsync(); |
| 60 | + var warStatus = JsonConvert.DeserializeObject<WarStatus>(jsonText); |
| 61 | + |
| 62 | + // Execute operation |
| 63 | + var result = clanService.SetWarStatus(clanId, warStatus.TargetClanId, warStatus.IsAtWar); |
| 64 | + |
| 65 | + // Handle the result |
| 66 | + if (result.Succeeded) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + response.StatusCode = StatusCodes.Status404NotFound; |
| 71 | + var jsonResponse = JsonConvert.SerializeObject(new { error = result.Error }); |
| 72 | + await response.WriteAsync(jsonResponse); |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public class ClanWarService |
| 80 | + { |
| 81 | + public ReadWarStatusOperationResult ReadAllWarStatusOf(string clanId) |
| 82 | + { |
| 83 | + if (clanId == "c810e13c-1083-4f39-aebc-e150c82dc770") |
| 84 | + { |
| 85 | + return new ReadWarStatusOperationResult |
| 86 | + { |
| 87 | + Value = new WarStatus[] |
| 88 | + { |
| 89 | + new WarStatus |
| 90 | + { |
| 91 | + TargetClanId = "6155d646-98c9-492e-a17d-335b1b69898e", |
| 92 | + IsAtWar = true |
| 93 | + }, |
| 94 | + new WarStatus |
| 95 | + { |
| 96 | + TargetClanId = "f43d6384-4419-4e41-b561-9f96e0620779", |
| 97 | + IsAtWar = true |
| 98 | + }, |
| 99 | + new WarStatus |
| 100 | + { |
| 101 | + TargetClanId = "84712492-1b0a-40b7-b1cd-20661fc6b6b6", |
| 102 | + IsAtWar = true |
| 103 | + }, |
| 104 | + new WarStatus |
| 105 | + { |
| 106 | + TargetClanId = "16446317-3CCE-431E-AEBE-8A5511312594", |
| 107 | + IsAtWar = false |
| 108 | + }, |
| 109 | + new WarStatus |
| 110 | + { |
| 111 | + TargetClanId = "8C0AE03F-202D-4352-BD59-2F794779F3E9", |
| 112 | + IsAtWar = false |
| 113 | + }, |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + return new ReadWarStatusOperationResult |
| 120 | + { |
| 121 | + Error = $"Clan {clanId} not found" |
| 122 | + }; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public SetWarStatusOperationResult SetWarStatus(string clanId, string targetClanId, bool isAtWar) |
| 127 | + { |
| 128 | + if (clanId == "c810e13c-1083-4f39-aebc-e150c82dc770" && targetClanId == "002A8E50-E39B-4AC6-9411-2F02AAE6C845") |
| 129 | + { |
| 130 | + return new SetWarStatusOperationResult(); |
| 131 | + } |
| 132 | + |
| 133 | + return new SetWarStatusOperationResult |
| 134 | + { |
| 135 | + Error = $"The clan {clanId} or the target clan {targetClanId} was not found" |
| 136 | + }; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public class SetWarStatusOperationResult |
| 141 | + { |
| 142 | + [JsonProperty("succeeded")] |
| 143 | + public bool Succeeded => string.IsNullOrWhiteSpace(Error); |
| 144 | + |
| 145 | + [JsonProperty("error", DefaultValueHandling = DefaultValueHandling.Ignore)] |
| 146 | + public string Error { get; set; } |
| 147 | + } |
| 148 | + |
| 149 | + public class ReadWarStatusOperationResult |
| 150 | + { |
| 151 | + [JsonProperty("succeeded")] |
| 152 | + public bool Succeeded => string.IsNullOrWhiteSpace(Error); |
| 153 | + |
| 154 | + [JsonProperty("value", DefaultValueHandling = DefaultValueHandling.Ignore)] |
| 155 | + public WarStatus[] Value { get; set; } |
| 156 | + |
| 157 | + [JsonProperty("error", DefaultValueHandling = DefaultValueHandling.Ignore)] |
| 158 | + public string Error { get; set; } |
| 159 | + } |
| 160 | + |
| 161 | + public class WarStatus |
| 162 | + { |
| 163 | + [JsonProperty("targetClanId")] |
| 164 | + public string TargetClanId { get; set; } |
| 165 | + |
| 166 | + [JsonProperty("isAtWar")] |
| 167 | + public bool IsAtWar { get; set; } |
| 168 | + } |
| 169 | +} |
0 commit comments