Everything works

This commit is contained in:
2026-02-08 17:29:03 +01:00
parent 585cac9d9c
commit 1c79d20d05
6 changed files with 96 additions and 25 deletions

View File

@ -4,22 +4,42 @@ using VideoGameCharacterApi.Dtos;
using VideoGameCharacterApi.Models; using VideoGameCharacterApi.Models;
using VideoGameCharacterApi.Services; using VideoGameCharacterApi.Services;
namespace VideoGameCharacterApi.Controllers namespace VideoGameCharacterApi.Controllers;
[Route("api/[controller]")]
[ApiController]
public class VideoGameCharactersController(IVideoGameCharacterService service ) : ControllerBase
{ {
[Route("api/[controller]")]
[ApiController] [HttpGet]
public class VideoGameCharactersController(IVideoGameCharacterService service ) : ControllerBase public async Task<ActionResult<List<CharacterResponse>>> GetCharacters()
=> Ok(await service.GetCharactersAsync());
[HttpGet("{id}")]
public async Task<ActionResult<CharacterResponse>> GetCharacter(int id)
{ {
var character = await service.GetCharacterByIdAsync(id);
[HttpGet] return character is null ? NotFound("Character with given id was not found") : Ok(character);
public async Task<ActionResult<List<CharacterResponse>>> GetCharacters()
=> Ok(await service.GetCharactersAsync());
[HttpGet("{id}")]
public async Task<ActionResult<CharacterResponse>> GetCharacter(int id)
{
var character = await service.GetCharacterByIdAsync(id);
return character is null ? NotFound("Character with given id was not found") : Ok(character);
}
} }
}
[HttpPost]
public async Task<ActionResult<CharacterResponse>> AddCharacter(CreateCharacterRequest character)
{
var createdCharacter = await service.AddCharacterAsync(character);
return CreatedAtAction(nameof(GetCharacter), new { id = createdCharacter.Id }, createdCharacter);
}
[HttpPut("{id}")]
public async Task<ActionResult> UpdateCharacter(int id, UpdateCharacterRequest character)
{
var updatedCharacter = await service.UpdateCharacterAsync(id, character);
return updatedCharacter is false ? NotFound("Character with given id was not found") : Ok(updatedCharacter);
}
[HttpDelete("{id}")]
public async Task<ActionResult> DeleteCharacter(int id)
{
var isDeleted = await service.DeleteCharacterAsync(id);
return isDeleted ? NoContent() : NotFound("Character with given id was not found");
}
}

View File

@ -2,7 +2,7 @@
{ {
public class CharacterResponse public class CharacterResponse
{ {
//public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public string Game { get; set; } = string.Empty; public string Game { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty; public string Role { get; set; } = string.Empty;

View File

@ -0,0 +1,10 @@
namespace VideoGameCharacterApi.Dtos
{
public class CreateCharacterRequest
{
public string Name { get; set; } = string.Empty;
public string Game { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,10 @@
namespace VideoGameCharacterApi.Dtos
{
public class UpdateCharacterRequest
{
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;
}
}

View File

@ -7,7 +7,7 @@ public interface IVideoGameCharacterService
{ {
Task<List<CharacterResponse>> GetCharactersAsync(); Task<List<CharacterResponse>> GetCharactersAsync();
Task<CharacterResponse> GetCharacterByIdAsync(int id); Task<CharacterResponse> GetCharacterByIdAsync(int id);
Task<CharacterResponse> AddCharacterAsync(Character character); Task<CharacterResponse> AddCharacterAsync(CreateCharacterRequest character);
Task<Character> UpdateCharacterAsync(int id,Character character); Task<bool> UpdateCharacterAsync(int id, UpdateCharacterRequest character);
Task<bool> DeleteCharacterAsync(int id); Task<bool> DeleteCharacterAsync(int id);
} }

View File

@ -15,14 +15,35 @@ namespace VideoGameCharacterApi.Services
} }
public Task<CharacterResponse> AddCharacterAsync(Character character) public async Task<CharacterResponse> AddCharacterAsync(CreateCharacterRequest character)
{ {
throw new NotImplementedException(); var newCharacter = new Character
{
Name = character.Name,
Game = character.Game,
Role = character.Role
};
_context.Characters.Add(newCharacter);
await _context.SaveChangesAsync();
return new CharacterResponse
{
Id = newCharacter.Id,
Name = newCharacter.Name,
Game = newCharacter.Game,
Role = newCharacter.Role
};
} }
public Task<bool> DeleteCharacterAsync(int id) public async Task<bool> DeleteCharacterAsync(int id)
{ {
throw new NotImplementedException(); var deleteCharacter = await _context.Characters.FindAsync(id);
if (deleteCharacter == null)
return false;
_context.Characters.Remove(deleteCharacter);
await _context.SaveChangesAsync();
return true;
} }
public async Task<CharacterResponse> GetCharacterByIdAsync(int id) public async Task<CharacterResponse> GetCharacterByIdAsync(int id)
@ -31,6 +52,7 @@ namespace VideoGameCharacterApi.Services
.Where(c => c.Id == id) .Where(c => c.Id == id)
.Select(c => new CharacterResponse .Select(c => new CharacterResponse
{ {
Id = c.Id,
Name = c.Name, Name = c.Name,
Game = c.Game, Game = c.Game,
Role = c.Role Role = c.Role
@ -42,14 +64,23 @@ namespace VideoGameCharacterApi.Services
public async Task<List<CharacterResponse>> GetCharactersAsync() public async Task<List<CharacterResponse>> GetCharactersAsync()
=> await _context.Characters.Select(c => new CharacterResponse => await _context.Characters.Select(c => new CharacterResponse
{ {
Id = c.Id,
Name = c.Name, Name = c.Name,
Game = c.Game, Game = c.Game,
Role = c.Role Role = c.Role
}).ToListAsync(); }).ToListAsync();
public Task<Character> UpdateCharacterAsync(int id, Character character) public async Task<bool> UpdateCharacterAsync(int id, UpdateCharacterRequest character)
{ {
throw new NotImplementedException(); var existingCharacter = await _context.Characters.FindAsync(id);
if (existingCharacter == null)
return false;
existingCharacter.Name = character.Name;
existingCharacter.Game = character.Game;
existingCharacter.Role = character.Role;
await _context.SaveChangesAsync();
return true;
} }
}; };