Skip to content

Commit 95341fe

Browse files
authored
Operation Result - Ninja Wars (#7)
* Part 1: Read all war status of a clan * Part 2: update the war status of a clan
1 parent 087b75d commit 95341fe

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio 15
3+
VisualStudioVersion = 15.0.27130.2036
4+
MinimumVisualStudioVersion = 15.0.26124.0
5+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F2D93F61-DBF4-46FA-8F46-55F215091DD0}"
6+
EndProject
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OperationResult.NinjaWars", "src\OperationResult.NinjaWars\OperationResult.NinjaWars.csproj", "{3E26B24F-E710-4961-8DCE-4F5E161EF64E}"
8+
EndProject
9+
Global
10+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
11+
Debug|Any CPU = Debug|Any CPU
12+
Debug|x64 = Debug|x64
13+
Debug|x86 = Debug|x86
14+
Release|Any CPU = Release|Any CPU
15+
Release|x64 = Release|x64
16+
Release|x86 = Release|x86
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Debug|x64.ActiveCfg = Debug|Any CPU
22+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Debug|x64.Build.0 = Debug|Any CPU
23+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Debug|x86.ActiveCfg = Debug|Any CPU
24+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Debug|x86.Build.0 = Debug|Any CPU
25+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Release|x64.ActiveCfg = Release|Any CPU
28+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Release|x64.Build.0 = Release|Any CPU
29+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Release|x86.ActiveCfg = Release|Any CPU
30+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E}.Release|x86.Build.0 = Release|Any CPU
31+
EndGlobalSection
32+
GlobalSection(SolutionProperties) = preSolution
33+
HideSolutionNode = FALSE
34+
EndGlobalSection
35+
GlobalSection(NestedProjects) = preSolution
36+
{3E26B24F-E710-4961-8DCE-4F5E161EF64E} = {F2D93F61-DBF4-46FA-8F46-55F215091DD0}
37+
EndGlobalSection
38+
GlobalSection(ExtensibilityGlobals) = postSolution
39+
SolutionGuid = {A94A064A-F7B4-4CCA-B1C3-937312B655A2}
40+
EndGlobalSection
41+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Folder Include="wwwroot\" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace OperationResult.NinjaWars
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
BuildWebHost(args).Run();
18+
}
19+
20+
public static IWebHost BuildWebHost(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>()
23+
.Build();
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:65013/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"launchUrl": "api/clans/c810e13c-1083-4f39-aebc-e150c82dc770/warstatus",
15+
"environmentVariables": {
16+
"ASPNETCORE_ENVIRONMENT": "Development"
17+
}
18+
},
19+
"OperationResult.NinjaWars": {
20+
"commandName": "Project",
21+
"launchBrowser": true,
22+
"launchUrl": "api/clans/ASDF-1234/wars",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
},
26+
"applicationUrl": "http://localhost:65014/"
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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

Comments
 (0)