Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions OriginDotNetChallenge.Unit/OriginDotNetChallenge.Unit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OriginDotNetChallenge\OriginDotNetChallenge.csproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions OriginDotNetChallenge.Unit/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using OriginDotNetChallenge.Controllers;

namespace OriginDotNetChallenge.Unit;

public class UnitTest1
{
[Fact]
public void Test1()
{
var controller = new PokemonController(new HttpClient());
var expectedUrl = "v2/pokemon?offset=0&limit=20";

var actualUrl = controller.BuildGetUrl(1);

Assert.Equal(expectedUrl, actualUrl);
}

[Fact]
public void Test2()
{
var controller = new PokemonController(new HttpClient());
var expectedUrl = "v2/pokemon?offset=20&limit=20";

var actualUrl = controller.BuildGetUrl(2);

Assert.Equal(expectedUrl, actualUrl);
}
}
1 change: 1 addition & 0 deletions OriginDotNetChallenge.Unit/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
6 changes: 6 additions & 0 deletions OriginDotNetChallenge.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OriginDotNetChallenge", "OriginDotNetChallenge\OriginDotNetChallenge.csproj", "{90CC368F-58C8-4801-8428-6A3F715CF588}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OriginDotNetChallenge.Unit", "OriginDotNetChallenge.Unit\OriginDotNetChallenge.Unit.csproj", "{C7C815CE-A900-445D-886E-91CCE2EF01B3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{90CC368F-58C8-4801-8428-6A3F715CF588}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90CC368F-58C8-4801-8428-6A3F715CF588}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90CC368F-58C8-4801-8428-6A3F715CF588}.Release|Any CPU.Build.0 = Release|Any CPU
{C7C815CE-A900-445D-886E-91CCE2EF01B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7C815CE-A900-445D-886E-91CCE2EF01B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7C815CE-A900-445D-886E-91CCE2EF01B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7C815CE-A900-445D-886E-91CCE2EF01B3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
46 changes: 44 additions & 2 deletions OriginDotNetChallenge/Controllers/PokemonController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net.Http.Headers;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;

namespace OriginDotNetChallenge.Controllers;
Expand All @@ -6,9 +8,49 @@ namespace OriginDotNetChallenge.Controllers;
[Route("pokemon")]
public class PokemonController : ControllerBase
{
public IActionResult Get()
public readonly HttpClient _client;

public PokemonController(HttpClient client)
{
return Ok("hello world");
_client = client;
}

public async Task<IActionResult> Get(int page = 1)
{
_client.BaseAddress = new Uri("https://pokeapi.co/api/");
_client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("origin-user",
"eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI");
var response = await _client.GetAsync(BuildGetUrl(page));

var result = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
var deserializedObject = JsonSerializer.Deserialize<PokeApiResponse>(result, options);


return Ok(deserializedObject?.Results);
}

public string BuildGetUrl(int page)
{
var limit = 20;
var offset = page - 1;

return $"v2/pokemon?offset={offset}&limit={limit}";
}

}

public class PokeApiResponse
{
public List<PokemonDTO> Results { get; set; }
}

public class PokemonDTO
{
public string Name { get; set; }
public string Url { get; set; }
}
1 change: 1 addition & 0 deletions OriginDotNetChallenge/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
$"appsettings.{builder.Environment.EnvironmentName}.json", optional: false
);
builder.Services.AddControllers();
builder.Services.AddSingleton<HttpClient>();

var app = builder.Build();

Expand Down