Skip to content

Commit 5f84db1

Browse files
committed
Added API and began fleshing our controllers
1 parent 7c2240e commit 5f84db1

40 files changed

+19870
-18
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace PostmanDelivers.API.ControllerModels
2+
{
3+
public class GameMoveRequestModel
4+
{
5+
public string xoro { get; set; }
6+
public int whichSquare { get; set; }
7+
public string playerID { get; set; }
8+
public long gameId { get; set; }
9+
}
10+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.Extensions.Configuration;
3+
using System.Collections.Generic;
4+
using TikTakToe.Engine;
5+
6+
namespace PostmanDelivers.API.Controllers
7+
{
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class GameController : ControllerBase
11+
{
12+
IConfiguration _config;
13+
public GameController(IConfiguration config)
14+
{
15+
_config = config;
16+
}
17+
18+
[HttpPost]
19+
public ActionResult<Game> Post(string[] playerNames)
20+
{
21+
try
22+
{
23+
Game g = new Game();
24+
string cnxn = _config.GetValue<string>("ConnectionStr");
25+
var newGame = g.GenerateNewGame(playerNames[0], playerNames[1], cnxn);
26+
return CreatedAtAction(nameof(GetById), new { id = newGame.Id }, newGame);
27+
}
28+
catch (System.Exception ex)
29+
{
30+
throw;
31+
}
32+
}
33+
34+
[HttpGet]
35+
public ActionResult<IEnumerable<Game>> Get()
36+
{
37+
Game g = new Game();
38+
string cnxn = _config.GetValue<string>("ConnectionStr");
39+
var allGames = g.GetAllGames(cnxn);
40+
return Ok(allGames);
41+
}
42+
43+
[HttpGet("{id}")]
44+
public ActionResult<string> GetById(int id)
45+
{
46+
Game g = new Game();
47+
string cnxn = _config.GetValue<string>("ConnectionStr");
48+
var theGame = g.GetGameByID(id, cnxn);
49+
return Ok(theGame);
50+
}
51+
}
52+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Extensions.Configuration;
8+
using PostmanDelivers.API.ControllerModels;
9+
using TikTakToe.Engine;
10+
11+
namespace PostmanDelivers.API.Controllers
12+
{
13+
[Route("api/[controller]")]
14+
[ApiController]
15+
public class MoveController : ControllerBase
16+
{
17+
IConfiguration _config;
18+
19+
public MoveController(IConfiguration config)
20+
{
21+
_config = config;
22+
}
23+
24+
[HttpPost]
25+
public ActionResult<MoveResult> Post(GameMoveRequestModel mdl)
26+
{
27+
try
28+
{
29+
Game g = new Game();
30+
string cnxn = _config.GetValue<string>("ConnectionStr");
31+
MoveResult result = g.Move(mdl.xoro, mdl.whichSquare, mdl.playerID, mdl.gameId, cnxn);
32+
return Ok(result);
33+
}
34+
catch (ArgumentException ae)
35+
{
36+
return BadRequest(ae.Message);
37+
}
38+
catch (InvalidOperationException ioe)
39+
{
40+
return BadRequest(ioe.Message);
41+
}
42+
}
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace PostmanDelivers.API.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
public class ValuesController : ControllerBase
12+
{
13+
// GET api/values
14+
[HttpGet]
15+
public ActionResult<IEnumerable<string>> Get()
16+
{
17+
return new string[] { "value1", "value2" };
18+
}
19+
20+
// GET api/values/5
21+
[HttpGet("{id}")]
22+
public ActionResult<string> Get(int id)
23+
{
24+
return "value";
25+
}
26+
27+
// POST api/values
28+
[HttpPost]
29+
public void Post([FromBody] string value)
30+
{
31+
}
32+
33+
// PUT api/values/5
34+
[HttpPut("{id}")]
35+
public void Put(int id, [FromBody] string value)
36+
{
37+
}
38+
39+
// DELETE api/values/5
40+
[HttpDelete("{id}")]
41+
public void Delete(int id)
42+
{
43+
}
44+
}
45+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.AspNetCore.App" />
10+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
11+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\TikTakToe.Engine\TikTakToe.Engine.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
5+
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
6+
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth>
7+
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
8+
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
9+
<WebStackScaffolding_IsReferencingScriptLibrariesSelected>True</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
10+
<WebStackScaffolding_LayoutPageFile />
11+
<WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected>
12+
<ActiveDebugProfile>PostmanDelivers.API</ActiveDebugProfile>
13+
</PropertyGroup>
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
15+
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
16+
</PropertyGroup>
17+
</Project>

PostmanDelivers.API/Program.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 PostmanDelivers.API
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
CreateWebHostBuilder(args).Build().Run();
18+
}
19+
20+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>();
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
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:52538",
7+
"sslPort": 44351
8+
}
9+
},
10+
"$schema": "http://json.schemastore.org/launchsettings.json",
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "api/values",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"PostmanDelivers.API": {
21+
"commandName": "Project",
22+
"launchUrl": "api/values",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
},
26+
"applicationUrl": "https://localhost:5001;http://localhost:5000"
27+
}
28+
}
29+
}

PostmanDelivers.API/Startup.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.HttpsPolicy;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Logging;
12+
using Microsoft.Extensions.Options;
13+
14+
namespace PostmanDelivers.API
15+
{
16+
public class Startup
17+
{
18+
public Startup(IConfiguration configuration)
19+
{
20+
Configuration = configuration;
21+
}
22+
23+
public IConfiguration Configuration { get; }
24+
25+
// This method gets called by the runtime. Use this method to add services to the container.
26+
public void ConfigureServices(IServiceCollection services)
27+
{
28+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
29+
}
30+
31+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
32+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
33+
{
34+
if (env.IsDevelopment())
35+
{
36+
app.UseDeveloperExceptionPage();
37+
}
38+
else
39+
{
40+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
41+
app.UseHsts();
42+
}
43+
44+
app.UseHttpsRedirection();
45+
app.UseMvc();
46+
}
47+
}
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)