diff --git a/VideoGameCharacterApi/Controllers/VideoGameCharactersController.cs b/VideoGameCharacterApi/Controllers/VideoGameCharactersController.cs index 9e0dc7d..4ea7785 100644 --- a/VideoGameCharacterApi/Controllers/VideoGameCharactersController.cs +++ b/VideoGameCharacterApi/Controllers/VideoGameCharactersController.cs @@ -4,22 +4,42 @@ using VideoGameCharacterApi.Dtos; using VideoGameCharacterApi.Models; using VideoGameCharacterApi.Services; -namespace VideoGameCharacterApi.Controllers +namespace VideoGameCharacterApi.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class VideoGameCharactersController(IVideoGameCharacterService service ) : ControllerBase { - [Route("api/[controller]")] - [ApiController] - public class VideoGameCharactersController(IVideoGameCharacterService service ) : ControllerBase + + [HttpGet] + public async Task>> GetCharacters() + => Ok(await service.GetCharactersAsync()); + + [HttpGet("{id}")] + public async Task> GetCharacter(int id) { - - [HttpGet] - public async Task>> GetCharacters() - => Ok(await service.GetCharactersAsync()); - - [HttpGet("{id}")] - public async Task> GetCharacter(int id) - { - var character = await service.GetCharacterByIdAsync(id); - return character is null ? NotFound("Character with given id was not found") : Ok(character); - } + var character = await service.GetCharacterByIdAsync(id); + return character is null ? NotFound("Character with given id was not found") : Ok(character); } -} + + [HttpPost] + public async Task> AddCharacter(CreateCharacterRequest character) + { + var createdCharacter = await service.AddCharacterAsync(character); + return CreatedAtAction(nameof(GetCharacter), new { id = createdCharacter.Id }, createdCharacter); + } + + [HttpPut("{id}")] + public async Task 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 DeleteCharacter(int id) + { + var isDeleted = await service.DeleteCharacterAsync(id); + return isDeleted ? NoContent() : NotFound("Character with given id was not found"); + } +} \ No newline at end of file diff --git a/VideoGameCharacterApi/Dtos/CharacterResponse.cs b/VideoGameCharacterApi/Dtos/CharacterResponse.cs index 978fd76..d4fc6c4 100644 --- a/VideoGameCharacterApi/Dtos/CharacterResponse.cs +++ b/VideoGameCharacterApi/Dtos/CharacterResponse.cs @@ -2,7 +2,7 @@ { public class CharacterResponse { - //public int Id { get; set; } + 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; diff --git a/VideoGameCharacterApi/Dtos/CreateCharacterRequest.cs b/VideoGameCharacterApi/Dtos/CreateCharacterRequest.cs new file mode 100644 index 0000000..64f973e --- /dev/null +++ b/VideoGameCharacterApi/Dtos/CreateCharacterRequest.cs @@ -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; + + } +} diff --git a/VideoGameCharacterApi/Dtos/UpdateCharacterRequest.cs b/VideoGameCharacterApi/Dtos/UpdateCharacterRequest.cs new file mode 100644 index 0000000..2e469a7 --- /dev/null +++ b/VideoGameCharacterApi/Dtos/UpdateCharacterRequest.cs @@ -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; + } +} diff --git a/VideoGameCharacterApi/Services/IVideoGameCharacterService.cs b/VideoGameCharacterApi/Services/IVideoGameCharacterService.cs index bb299b6..36fb85d 100644 --- a/VideoGameCharacterApi/Services/IVideoGameCharacterService.cs +++ b/VideoGameCharacterApi/Services/IVideoGameCharacterService.cs @@ -7,7 +7,7 @@ public interface IVideoGameCharacterService { Task> GetCharactersAsync(); Task GetCharacterByIdAsync(int id); - Task AddCharacterAsync(Character character); - Task UpdateCharacterAsync(int id,Character character); + Task AddCharacterAsync(CreateCharacterRequest character); + Task UpdateCharacterAsync(int id, UpdateCharacterRequest character); Task DeleteCharacterAsync(int id); } diff --git a/VideoGameCharacterApi/Services/VideoGameCharacterService.cs b/VideoGameCharacterApi/Services/VideoGameCharacterService.cs index 4c1c083..ee6cd92 100644 --- a/VideoGameCharacterApi/Services/VideoGameCharacterService.cs +++ b/VideoGameCharacterApi/Services/VideoGameCharacterService.cs @@ -15,14 +15,35 @@ namespace VideoGameCharacterApi.Services } - public Task AddCharacterAsync(Character character) + public async Task 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 DeleteCharacterAsync(int id) + public async Task 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 GetCharacterByIdAsync(int id) @@ -31,6 +52,7 @@ namespace VideoGameCharacterApi.Services .Where(c => c.Id == id) .Select(c => new CharacterResponse { + Id = c.Id, Name = c.Name, Game = c.Game, Role = c.Role @@ -42,14 +64,23 @@ namespace VideoGameCharacterApi.Services public async Task> GetCharactersAsync() => await _context.Characters.Select(c => new CharacterResponse { + Id = c.Id, Name = c.Name, Game = c.Game, Role = c.Role }).ToListAsync(); - public Task UpdateCharacterAsync(int id, Character character) + public async Task 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; + } };