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。