Skip to content

Commit 5d1532c

Browse files
Created model, interface and its implementation, symple GET methods: GetAllCommands and GetCommandById. Added controller to manipulate with HTTP requests and responses.
1 parent 4d45027 commit 5d1532c

11 files changed

+282
-0
lines changed

ASP.NET-Core-MVC-REST-API.sln

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30114.105
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commander", "Commander\Commander.csproj", "{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Debug|x64.ActiveCfg = Debug|Any CPU
21+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Debug|x64.Build.0 = Debug|Any CPU
22+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Debug|x86.ActiveCfg = Debug|Any CPU
23+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Debug|x86.Build.0 = Debug|Any CPU
24+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Release|x64.ActiveCfg = Release|Any CPU
27+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Release|x64.Build.0 = Release|Any CPU
28+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Release|x86.ActiveCfg = Release|Any CPU
29+
{4DB8C54B-7DF2-420C-85CE-F416F9D63FAC}.Release|x86.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {617FA7F5-6493-4C9B-9E51-88BF279745B5}
36+
EndGlobalSection
37+
EndGlobal

Commander/Commander.csproj

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
9+
</ItemGroup>
10+
11+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Commander.domain;
2+
using Commander.Interfaces;
3+
using Commander.Models;
4+
using Microsoft.AspNetCore.Mvc;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace Commander.Controllers
11+
{
12+
[Route("api/commands")]
13+
[ApiController]
14+
public class CommandsController : ControllerBase
15+
{
16+
17+
private readonly CommanderRepository repository = new CommanderRepository();
18+
19+
// GET api/commands
20+
[HttpGet]
21+
public ActionResult <IEnumerable<Command>> GetAllCommand()
22+
{
23+
var commandItems = repository.GetAllCommands();
24+
25+
return Ok(commandItems);
26+
}
27+
28+
// GET api/commands/5
29+
[HttpGet("{id}")]
30+
public ActionResult <Command> GetCommandById(int id)
31+
{
32+
var commandItem = repository.GetCommandById(id);
33+
34+
return Ok(commandItem);
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Commander.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace Commander.Interfaces
8+
{
9+
public interface ICommanderRepository
10+
{
11+
IEnumerable<Command> GetAllCommands();
12+
Command GetCommandById(int id);
13+
}
14+
}

Commander/Models/Command.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Commander.Models
7+
{
8+
public class Command
9+
{
10+
public int Id { get; set; }
11+
public string HowTo { get; set; }
12+
public string Line { get; set; }
13+
public string Platform { get; set; }
14+
}
15+
}

Commander/Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace Commander
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:32906",
8+
"sslPort": 44348
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "swagger",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"Commander": {
21+
"commandName": "Project",
22+
"dotnetRunMessages": "true",
23+
"launchBrowser": true,
24+
"launchUrl": "swagger",
25+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}

Commander/Startup.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Commander.domain;
2+
using Commander.Interfaces;
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.HttpsPolicy;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Hosting;
10+
using Microsoft.Extensions.Logging;
11+
using Microsoft.OpenApi.Models;
12+
using System;
13+
using System.Collections.Generic;
14+
using System.Linq;
15+
using System.Threading.Tasks;
16+
17+
namespace Commander
18+
{
19+
public class Startup
20+
{
21+
public Startup(IConfiguration configuration)
22+
{
23+
Configuration = configuration;
24+
}
25+
26+
public IConfiguration Configuration { get; }
27+
28+
// This method gets called by the runtime. Use this method to add services to the container.
29+
public void ConfigureServices(IServiceCollection services)
30+
{
31+
32+
services.AddControllers();
33+
services.AddSwaggerGen(c =>
34+
{
35+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Commander", Version = "v1" });
36+
});
37+
38+
}
39+
40+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
41+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
42+
{
43+
if (env.IsDevelopment())
44+
{
45+
app.UseDeveloperExceptionPage();
46+
app.UseSwagger();
47+
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Commander v1"));
48+
}
49+
50+
app.UseHttpsRedirection();
51+
52+
app.UseRouting();
53+
54+
app.UseAuthorization();
55+
56+
app.UseEndpoints(endpoints =>
57+
{
58+
endpoints.MapControllers();
59+
});
60+
}
61+
}
62+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}

Commander/appsettings.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Commander.Interfaces;
2+
using Commander.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace Commander.domain
9+
{
10+
public class CommanderRepository : ICommanderRepository
11+
{
12+
public IEnumerable<Command> GetAllCommands()
13+
{
14+
var commands = new List<Command>
15+
{
16+
new Command { Id = 0, HowTo = "Boil an egg", Line = "Boil watter", Platform = "Kettle & Pan" },
17+
new Command { Id = 1, HowTo = "Cut bread", Line = "Get a knife", Platform = "knife & chopping board" },
18+
new Command { Id = 2, HowTo = "Make a cup of tea", Line = "Place teabag in cup", Platform = "Kettle & cup" }
19+
};
20+
21+
return commands;
22+
}
23+
24+
25+
public Command GetCommandById(int id)
26+
{
27+
return new Command { Id = 0, HowTo = "Boil an egg", Line = "Boil watter", Platform = "Kettke & Pan" };
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)