Hi,
My project is in Blazor WASM and ASP.Net Core WebApi
I created crud api and add some business login, call into blazor.
// GET: api/Fiscals/5
[HttpGet("{id}")]
public async Task<ActionResult<Fiscal>> GetFiscal(int id)
{
var fiscal = await _context.Fiscal.FindAsync(id);
if (fiscal == null)
{
_logger.LogError("No fiscal found");
return NotFound();
}
return fiscal;
}
[HttpPost]
public async Task<ActionResult<Fiscal>> PostFiscal(Fiscal fiscal)
{
var fiscals = _context.Fiscals.ToList();
if(fiscals.Any(a=>a.Year == fiscal.Year)
{
error = new Error {
Code = 101,
Message = "Already exists"
};
}
else
{
_context.Fiscal.Add(fiscal);
await _context.SaveChangesAsync();
return CreatedAtAction("GetFiscal", new { id = fiscal.ID }, fiscal);
}
}
On post method when inserting into db I checked is entry already available or not. If already exists then it will return error class. so the return type is fiscal class so It's not possible.
So for this I m managing this on Blazor Client side by calling get api then check is it exists or not and then call post api.
But this same I'm doing business logic and stuff on both side.
my question, is it possible to return error message into api so I can use it and call into front end and manage all logics in API side without changing return class?
or what will be the best approach?