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