Skip to content

Instantly share code, notes, and snippets.

@masoodwasim
Created March 11, 2020 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masoodwasim/867cdbf1668075691ee3a0063bf78d6c to your computer and use it in GitHub Desktop.
Save masoodwasim/867cdbf1668075691ee3a0063bf78d6c to your computer and use it in GitHub Desktop.
[FunctionName(nameof(CreateBook))]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "CreateBook")] HttpRequest req)
{
IActionResult returnValue = null;
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var input = JsonConvert.DeserializeObject<BookModel>(requestBody);
var book = new BookModel
{
Title= input.Title,
Author=input.Author,
Publisher=input.Publisher,
Category= input.Category,
Price = input.Price,
PublishDate = input.PublishDate
};
try
{
_books.InsertOne(book);
returnValue = new OkObjectResult(book);
}
catch (Exception ex)
{
_logger.LogError($"Exception thrown: {ex.Message}");
returnValue = new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
return returnValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment