påbörjat utv
This commit is contained in:
@ -0,0 +1,17 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using VideoGameCharacterApi.Models;
|
||||||
|
using VideoGameCharacterApi.Services;
|
||||||
|
|
||||||
|
namespace VideoGameCharacterApi.Controllers
|
||||||
|
{
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class VideoGameCharactersController(IVideoGameCharacterService service ) : ControllerBase
|
||||||
|
{
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<ActionResult<List<Character>>> GetCharacters()
|
||||||
|
=> Ok(await service.GetCharactersAsync());
|
||||||
|
}
|
||||||
|
}
|
||||||
10
VideoGameCharacterApi/Models/Character.cs
Normal file
10
VideoGameCharacterApi/Models/Character.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
namespace VideoGameCharacterApi.Models
|
||||||
|
{
|
||||||
|
public class Character
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
public string Game { get; set; } = string.Empty;
|
||||||
|
public string Role { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,6 @@
|
|||||||
|
using Scalar.AspNetCore;
|
||||||
|
using VideoGameCharacterApi.Services;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
@ -6,12 +9,15 @@ builder.Services.AddControllers();
|
|||||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||||
builder.Services.AddOpenApi();
|
builder.Services.AddOpenApi();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<IVideoGameCharacterService, VideoGameCharacterService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.MapOpenApi();
|
app.MapOpenApi();
|
||||||
|
app.MapScalarApiReference();
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|||||||
12
VideoGameCharacterApi/Services/IVideoGameCharacterService.cs
Normal file
12
VideoGameCharacterApi/Services/IVideoGameCharacterService.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using VideoGameCharacterApi.Models;
|
||||||
|
|
||||||
|
namespace VideoGameCharacterApi.Services;
|
||||||
|
|
||||||
|
public interface IVideoGameCharacterService
|
||||||
|
{
|
||||||
|
Task<List<Character>> GetCharactersAsync();
|
||||||
|
Task<Character> GetCharacterByIdAsync(int id);
|
||||||
|
Task<Character> AddCharacterAsync(Character character);
|
||||||
|
Task<Character> UpdateCharacterAsync(int id,Character character);
|
||||||
|
Task<bool> DeleteCharacterAsync(int id);
|
||||||
|
}
|
||||||
37
VideoGameCharacterApi/Services/VideoGameCharacterService.cs
Normal file
37
VideoGameCharacterApi/Services/VideoGameCharacterService.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
using VideoGameCharacterApi.Models;
|
||||||
|
|
||||||
|
namespace VideoGameCharacterApi.Services
|
||||||
|
{
|
||||||
|
public class VideoGameCharacterService : IVideoGameCharacterService
|
||||||
|
{
|
||||||
|
static List<Character> characters = new List<Character>
|
||||||
|
{
|
||||||
|
new Character { Id = 1, Name = "Mario", Game = "Super Mario Bros.", Role = "hero" },
|
||||||
|
new Character { Id = 2, Name = "Link", Game = "The Legend of Zelda", Role = "hero" },
|
||||||
|
new Character { Id = 3, Name = "Bowser", Game = "Super Mario Bros.", Role = "villain" },
|
||||||
|
new Character { Id = 3, Name = "Zelda", Game = "The Legend of Zelda", Role = "princess" },
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
public Task<Character> AddCharacterAsync(Character character)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<bool> DeleteCharacterAsync(int id)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Character> GetCharacterByIdAsync(int id)
|
||||||
|
=> await Task.FromResult(characters.FirstOrDefault(c => c.Id == id));
|
||||||
|
|
||||||
|
public async Task<List<Character>> GetCharactersAsync()
|
||||||
|
=> await Task.FromResult(characters);
|
||||||
|
|
||||||
|
public Task<Character> UpdateCharacterAsync(int id, Character character)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net10.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" />
|
||||||
|
<PackageReference Include="Scalar.AspNetCore" Version="2.12.32" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user