public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "insertlibrarybook")] HttpRequest req, ILogger log) { try { var result = _tokenProvider.ValidateToken(req); if (result.Status == AccessTokenStatus.Valid) { log.LogInformation($"Request received for {result.Principal.Identity.Name}."); } else { return(new UnauthorizedResult()); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); LibraryBookApiModel model = JsonConvert.DeserializeObject <LibraryBookApiModel>(requestBody); if (model == null) { return(new BadRequestObjectResult("Please pass LibraryBookApiModel in the request body")); } int retVal = 0; string libraryBookCode = string.Empty; if (model != null) { model.CreatedBy = _tokenProvider.User; model.DateCreated = DateTime.Now; model.ModifiedBy = _tokenProvider.User; model.DateModified = DateTime.Now; retVal = _libraryBookWebApiManager.InsertLibraryBook(model, out libraryBookCode); } if (retVal < 1) { return(new BadRequestObjectResult("Failed to insert record")); } return((ActionResult) new OkObjectResult(new ContentResult { Content = libraryBookCode, ContentType = "text/plain", StatusCode = 200 })); } catch (Exception ex) { log.LogError($"Caught exception: {ex.Message}"); return(new BadRequestObjectResult(ex.Message)); } }
public IActionResult Insert([FromBody] LibraryBookApiModel model) { int result = 0; var libraryBookCode = ""; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (model != null) { model.CreatedBy = GetCurrentUser(); model.DateCreated = DateTime.Now; model.ModifiedBy = GetCurrentUser(); model.DateModified = DateTime.Now; result = _libraryBookWebApiManager.InsertLibraryBook(model, out libraryBookCode); } switch (result) { case 1: return(new ContentResult { Content = libraryBookCode, ContentType = "text/plain", StatusCode = 200 }); default: ModelState.AddModelError(string.Empty, "Failed to insert record"); break; } return(BadRequest(ModelState)); }