implemented person controller
This commit is contained in:
1 parent
27a1ed624e
commit
fad2d19098
1 file changed
+68
-9
@@ -1,15 +1,74 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using api.Services;
|
||||||
|
using api.DTOs;
|
||||||
|
|
||||||
namespace api.Controllers;
|
namespace api.Controllers
|
||||||
|
|
||||||
[ApiController]
|
|
||||||
[Route("[controller]")]
|
|
||||||
|
|
||||||
public class PersonController : ControllerBase
|
|
||||||
{
|
{
|
||||||
[HttpGet(Name = "GetPerson")]
|
[ApiController]
|
||||||
public Person Get()
|
[Route("api/[controller]")]
|
||||||
|
public class PersonController : ControllerBase
|
||||||
{
|
{
|
||||||
return new Person{};
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user