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" ) ;
}
}
当用户进行身份验证时,他会获得一个 JWT 令牌,该令牌的有效期为 30 分钟。它在请求标头中发送。如果令牌中的数据不匹配,服务器将响应 401 Not Authorized。