Everything works
This commit is contained in:
@ -4,8 +4,8 @@ using VideoGameCharacterApi.Dtos;
|
|||||||
using VideoGameCharacterApi.Models;
|
using VideoGameCharacterApi.Models;
|
||||||
using VideoGameCharacterApi.Services;
|
using VideoGameCharacterApi.Services;
|
||||||
|
|
||||||
namespace VideoGameCharacterApi.Controllers
|
namespace VideoGameCharacterApi.Controllers;
|
||||||
{
|
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class VideoGameCharactersController(IVideoGameCharacterService service ) : ControllerBase
|
public class VideoGameCharactersController(IVideoGameCharacterService service ) : ControllerBase
|
||||||
@ -21,5 +21,25 @@ namespace VideoGameCharacterApi.Controllers
|
|||||||
var character = await service.GetCharacterByIdAsync(id);
|
var character = await service.GetCharacterByIdAsync(id);
|
||||||
return character is null ? NotFound("Character with given id was not found") : Ok(character);
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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;
|
||||||
|
|||||||
10
VideoGameCharacterApi/Dtos/CreateCharacterRequest.cs
Normal file
10
VideoGameCharacterApi/Dtos/CreateCharacterRequest.cs
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
10
VideoGameCharacterApi/Dtos/UpdateCharacterRequest.cs
Normal file
10
VideoGameCharacterApi/Dtos/UpdateCharacterRequest.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user