0% found this document useful (0 votes)
5 views3 pages

Person Controller

The document outlines a PersonController class in an ASP.NET Core API, which manages person records with CRUD operations (Create, Read, Update, Delete). It includes methods for retrieving all persons, retrieving a person by ID, adding a new person, updating an existing person, and deleting a person. Each method returns appropriate HTTP responses based on the operation's success or failure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Person Controller

The document outlines a PersonController class in an ASP.NET Core API, which manages person records with CRUD operations (Create, Read, Update, Delete). It includes methods for retrieving all persons, retrieving a person by ID, adding a new person, updating an existing person, and deleting a person. Each method returns appropriate HTTP responses based on the operation's success or failure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

{

[Route("api/[controller]")]

[ApiController]

public class PersonController : ControllerBase

//Routes de acceso o GET, POST, DELETE PUT

private readonly PersonService _personService;

//Aca inicializo mi clase o constructor


public PersonController(PersonService personService)
{
_personService = personService;
}

[HttpGet]
public ActionResult<List<object>> GetPerson()
{
var persons = _personService.GetPerson();

var result = persons.Select(p => new


{
p.Id,
p.Name,
p.Description,
p.DateCreated,
Message = p.Id % 2 != 0 ? "ID impar" : "ID par",
Status = p.Id % 2 == 0 ? "Even" : "Odd"
}).ToList();

return Ok(result);
}

[HttpGet("{id}")]
public ActionResult<object> GetPersonById(int id)
{
var person = _personService.GetPersonById(id);
if (person == null)
{
return NotFound();
}

var result = new


{
person.Id,
person.Name,
person.Description,
person.DateCreated,
Message = person.Id % 2 != 0 ? "ID impar" : "ID par",
Status = person.Id % 2 == 0 ? "Even" : "Odd"
};

return Ok(result);
}

//El POST
[HttpPost]
public ActionResult<Person> AddPerson(Person person)
{

var newProducto = _personService.AddPerson(person);

return
CreatedAtAction(
nameof(GetPerson), new
{
id = newProducto.Id
},
newProducto);

//El PUT
//Permite actualizar el registro
[HttpPut]
public IActionResult UpdatePerson(Person person)
{

if (!_personService.UpdatePerson(person))
{
return NotFound(
new
{
elmensaje = "person not found"
}
);
}
return NoContent();
}

//El Delete
//Borra un registro
[HttpDelete]
public IActionResult DeletePerson(int id)
{

if (!_personService.DeletePerson(id))
{
return NotFound(
new
{
elmensaje = "person not found"
}
);
}
return NoContent();
}

}
}

You might also like