-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharacterController.cs
75 lines (62 loc) · 2.24 KB
/
CharacterController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using dot_net_api_rpg.Dtos.Character;
using dot_net_api_rpg.Models;
using dot_net_api_rpg.Services.CharacterService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace dot_net_api_rpg.Controllers
{
[Authorize]
[ApiController]
[Route("[controller]")]
public class CharacterController : ControllerBase
{
private readonly ICharacterService _characterService;
public CharacterController(ICharacterService characterService)
{
_characterService = characterService;
}
[HttpGet("GetAll")]
public async Task<IActionResult> Get()
{
//Forma vieja de conseguir el ID sin injection de HTTP Context
/* int id = int.Parse(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
*/
return Ok(await _characterService.GetAllCharacters());
}
/* [AllowAnonymous]*/ //Para permitir sin token
[HttpGet("{id}")]
public async Task<IActionResult> GetSingle(int id)
{
return Ok(await _characterService.GetCharacterById(id));
}
[HttpPost]
public async Task<IActionResult> AddCharacter(AddCharacterDto newCharacter)
{
return Ok(await _characterService.AddCharacter(newCharacter));
}
[HttpPut]
public async Task<IActionResult> UpdateCharacter(UpdateCharacterDto updatedCharacter)
{
ServiceResponse<GetCharacterDto> response = await _characterService.UpdateCharacter(updatedCharacter);
if (response.Data == null)
{
return NotFound(response);
}
return Ok(await _characterService.UpdateCharacter(updatedCharacter));
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
ServiceResponse<List<GetCharacterDto>> response = await _characterService.DeleteCharacter(id);
if (response.Data == null)
{
return NotFound(response);
}
return Ok(response);
}
}
}