FoodRest.ProductsApi
1.0.0
//GET api/items
[ HttpGet ]
public IEnumerable < Item > Get ( ) => itemRepository . Items . ToList ( ) ;
//GET api/items/F74349D5-52B4-4A4A-0382-08DA02C684C5
[ HttpGet ( "{id:guid}" ) ]
public ActionResult < Item > Get ( Guid id )
{
var item = itemRepository . Items . First ( g => g . Id == id ) ;
if ( item != null )
return item ;
return BadRequest ( ) ;
}
//GET api/items/take/2
[ HttpGet ( "take/{productPage:int}" ) ]
public IEnumerable < Item > Get ( int productPage ) => itemRepository . Items
. Skip ( ( productPage - 1 ) * PageSize )
. Take ( PageSize ) ;
//POST api/items/add/{item}
[ HttpPost ( "add" ) ]
public async Task < ActionResult < Item > > Post ( Item item )
{
try
{
if ( item == null )
{
return BadRequest ( ) ;
}
var repitem = itemRepository . Items . FirstOrDefault ( i => i . Id == item . Id ) ;
if ( repitem != null )
{
repitem . Description = item . Description ;
repitem . Category = item . Category ;
repitem . Price = item . Price ;
repitem . Name = item . Name ;
repitem . addedTime = item . addedTime ;
itemRepository . SaveItem ( repitem ) ;
}
itemRepository . SaveItem ( item ) ;
return Ok ( ) ;
}
catch ( Exception ex )
{
return StatusCode ( StatusCodes . Status500InternalServerError , "Error retrieving data from the database" ) ;
}
}
When a user authenticates, he is issued a JWT Token, which is valid for 30 minutes. It is sent in the request header. If the data in the token does not match, the server responds with 401 Not Authorized.