Created
August 6, 2024 06:15
-
-
Save mjamesharmon/1fd8b69d05d38b526ea49625616773ae to your computer and use it in GitHub Desktop.
Source code for Pokédex API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.DependencyInjection; | |
namespace Pokedexer.Application; | |
public static class Configuration | |
{ | |
public static IServiceCollection AddPokedexServices(this IServiceCollection services) { | |
services.AddSingleton<Pokedex>(); | |
services.AddKeyedTransient<ISortingStrategy,DefaultSort>("default"); | |
services.AddKeyedTransient<ISortingStrategy,SortByName>("name"); | |
services.AddKeyedTransient<ISortingStrategy,SortByType>("type"); | |
services.AddKeyedTransient<ISortingStrategy,SortyByHp>("hp"); | |
return services; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Pokedexer.Application; | |
public class DefaultSort : ISortingStrategy | |
{ | |
public IEnumerable<Pokemon> Sort(IEnumerable<Pokemon> pokemons, | |
bool sortDescending = false) | |
{ | |
return pokemons.OrderBy(p=>p.Type). | |
ThenBy(p=>p.Name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Pokedexer.Application; | |
namespace Pokedexer.Api; | |
public static class Extensions | |
{ | |
public static IEnumerable<Pokemon> Sort(this ISortingStrategy sortingStrategy, | |
IEnumerable<Pokemon> pokemons, string direction) => | |
sortingStrategy.Sort(pokemons, direction.Equals("desc",StringComparison.OrdinalIgnoreCase)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Pokedexer.Application; | |
public interface ISortingStrategy | |
{ | |
IEnumerable<Pokemon> Sort(IEnumerable<Pokemon> pokemons, bool sortDescending = false); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.ObjectModel; | |
namespace Pokedexer.Application; | |
public class Pokedex : KeyedCollection<string,Pokemon> { | |
override protected string GetKeyForItem(Pokemon pokemon) { | |
return pokemon.Name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Pokedexer.Application; | |
public class Pokemon | |
{ | |
public string Name => _name; | |
public int HP => _hp; | |
public string Type => _type; | |
private readonly string _type; | |
private readonly string _name; | |
private readonly int _hp; | |
public Pokemon(string name, int hp, string type) { | |
_name=name; | |
_hp = hp; | |
_type = type; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
using Microsoft.AspNetCore.Mvc; | |
using Pokedexer.Application; | |
using Pokedexer.Api; | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddEndpointsApiExplorer(); | |
builder.Services.AddSwaggerGen(); | |
builder.Services.AddPokedexServices(); | |
var app = builder.Build(); | |
// Configure the HTTP request pipeline. | |
if (app.Environment.IsDevelopment()) | |
{ | |
app.UseSwagger(); | |
app.UseSwaggerUI(); | |
} | |
app.UseHttpsRedirection(); | |
app.MapPost("add", ([FromBody] Pokemon pokemonToAdd, Pokedex pokedex ) => { | |
try { | |
pokedex.Add(pokemonToAdd); | |
return Results.Ok(); | |
}catch { | |
return Results.BadRequest("Pokemon submitted is not valid for addition"); | |
} | |
}); | |
app.MapGet("list", (Pokedex pokedex, [FromKeyedServices("default")] ISortingStrategy sortMethod) => { | |
try { | |
return Results.Ok(sortMethod.Sort(pokedex)); | |
}catch { | |
return Results.StatusCode((int)HttpStatusCode.InternalServerError); | |
} | |
}); | |
app.MapGet("mostpowerful", (Pokedex pokedex, [FromKeyedServices("hp")] ISortingStrategy sortMethod) => { | |
try { | |
return Results.Ok( | |
sortMethod.Sort(pokedex,true). | |
Take(1)); | |
}catch { | |
return Results.StatusCode((int)HttpStatusCode.InternalServerError); | |
} | |
}); | |
app.MapGet("list/bytype", (Pokedex pokedex, | |
[FromKeyedServices("type")] ISortingStrategy sortMethod, string? order) => { | |
try { | |
return Results.Ok( | |
sortMethod.Sort(pokedex,order ?? "asc")); | |
}catch { | |
return Results.StatusCode((int)HttpStatusCode.InternalServerError); | |
} | |
}); | |
app.MapGet("list/alphabetical", (Pokedex pokedex, | |
[FromKeyedServices("name")] ISortingStrategy sortMethod, string? order) => { | |
try { | |
return Results.Ok( | |
sortMethod.Sort(pokedex,order ?? "asc")); | |
}catch { | |
return Results.StatusCode((int)HttpStatusCode.InternalServerError); | |
} | |
}); | |
app.Run(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Pokedexer.Application; | |
public class SortByName : ISortingStrategy | |
{ | |
public IEnumerable<Pokemon> Sort(IEnumerable<Pokemon> pokemons, bool sortDescending = false) | |
{ | |
return (sortDescending) ? | |
pokemons.OrderByDescending(p=>p.Name) : | |
pokemons.OrderBy(p=>p.Name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Pokedexer.Application; | |
public class SortByType : ISortingStrategy | |
{ | |
public IEnumerable<Pokemon> Sort(IEnumerable<Pokemon> pokemons, bool sortDescending = false) | |
{ | |
return (sortDescending) ? | |
pokemons.OrderByDescending(p=>p.Type) : | |
pokemons.OrderBy(p=>p.Type); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Pokedexer.Application; | |
public class SortyByHp : ISortingStrategy | |
{ | |
public IEnumerable<Pokemon> Sort(IEnumerable<Pokemon> pokemons, bool sortDescending = false) | |
{ | |
return sortDescending ? | |
pokemons.OrderByDescending(p=>p.HP) : | |
pokemons .OrderBy(p=>p.HP); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment