Create a REST service

This commit is contained in:
Manuel Thalmann 2024-06-06 16:54:24 +02:00
parent 0a7f3890e7
commit 2b25012888
9 changed files with 188 additions and 0 deletions

View file

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34928.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RankingService", "RankingService\RankingService.csproj", "{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93368568-85F1-41B9-983B-B459F5C738D4}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,8 @@
namespace RankingService
{
public class Competitor
{
public string? Name { get; set; }
public string? Time { get; set; }
}
}

View file

@ -0,0 +1,33 @@
using Microsoft.OpenApi.Models;
using System.Net;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(
(options) =>
{
options.SwaggerDoc(
"v1",
new OpenApiInfo
{
Version = "v1",
Title = "Ranking API",
Description = "List rankings"
});
});
// Add services to the container.
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();

View file

@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:31447",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "Rankings",
"applicationUrl": "http://localhost:5072",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "Rankings",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.6.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.6.2" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,6 @@
@RankingService_HostAddress = http://localhost:5072
GET {{RankingService_HostAddress}}/weatherforecast/
Accept: application/json
###

View file

@ -0,0 +1,54 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace RankingService
{
[Route("[controller]")]
[ApiController]
public class RankingsController : ControllerBase
{
private string[] ranks = {
"Mueller Stefan,02:31:14",
"Marti Adrian,2:30:09",
"Kiptum Daniel,2:11:31",
"Ancay Tarcis,2:20:02",
"Kreibuhl Christian,2:21:47",
"Ott Michael,2:33:48",
"Menzi Christoph,2:27:26",
"Oliver Ruben,2:32:12",
"Elmer Beat,2:33:53",
"Kuehni Martin,2:33:36"
};
private Lazy<List<Competitor>> competitors;
public RankingsController()
{
competitors = new Lazy<List<Competitor>>(
() =>
{
List<Competitor> result = new();
foreach (string rank in ranks)
{
string[] items = rank.Split(',');
result.Add(
new Competitor
{
Name = items[0],
Time = items[1]
});
}
return result;
});
}
[HttpGet]
public ActionResult<IEnumerable<Competitor>> Get()
{
return competitors.Value;
}
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}