74 lines
2.0 KiB
C#
74 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using api.Services;
|
|
using api.DTOs;
|
|
|
|
namespace api.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class PersonController : ControllerBase
|
|
{
|
|
private readonly IPersonService _personService;
|
|
|
|
public PersonController(IPersonService personService)
|
|
{
|
|
_personService = personService;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAll()
|
|
{
|
|
var persons = await _personService.GetAllPersonsAsync();
|
|
return Ok(persons);
|
|
}
|
|
|
|
[HttpGet("username/{username}")]
|
|
public async Task<IActionResult> GetByUsername(string username)
|
|
{
|
|
try
|
|
{
|
|
var person = await _personService.GetPersonByUsernameAsync(username);
|
|
return Ok(person);
|
|
}
|
|
catch (KeyNotFoundException)
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Add(PersonDto personDto)
|
|
{
|
|
await _personService.AddPersonAsync(personDto);
|
|
return CreatedAtAction(nameof(GetByUsername), new { username = personDto.Username }, personDto);
|
|
}
|
|
|
|
[HttpPut("username/{username}")]
|
|
public async Task<IActionResult> Update(string username, PersonDto personDto)
|
|
{
|
|
try
|
|
{
|
|
await _personService.UpdatePersonAsync(username, personDto);
|
|
return NoContent();
|
|
}
|
|
catch (KeyNotFoundException)
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpDelete("username/{username}")]
|
|
public async Task<IActionResult> Delete(string username)
|
|
{
|
|
try
|
|
{
|
|
await _personService.DeletePersonAsync(username);
|
|
return NoContent();
|
|
}
|
|
catch (KeyNotFoundException)
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
}
|
|
} |