コード例 #1
0
        public IActionResult Insert([FromBody] LibraryBookStatusApiModel model)
        {
            int result = 0;
            var libraryBookStatusCode = "";

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            if (model != null)
            {
                int?retVal = _libraryBookStatusWebApiManager.GetCountOfBookCurrentLent(model?.LibraryUser.LibraryUserCode);
                if (retVal != null && retVal.Value > 4)
                {
                    ModelState.AddModelError(string.Empty, "Maximum number of books exceeded");
                    return(BadRequest(ModelState));
                }

                LibraryBookApiModel libraryBookApiModel = _libraryBookWebApiManager.GetLibraryBookByLibraryBookCode(model.LibraryBook?.LibraryBookCode);
                if (libraryBookApiModel == null)
                {
                    ModelState.AddModelError(string.Empty, "Library book not found");
                    return(BadRequest(ModelState));
                }

                if (_libraryBookStatusWebApiManager.HasMoreThanOneBookWithSameISBN(libraryBookApiModel.ISBN, model?.LibraryUser.LibraryUserCode))
                {
                    ModelState.AddModelError(string.Empty, "Library User already has that book");
                    return(BadRequest(ModelState));
                }


                model.CreatedBy    = GetCurrentUser();
                model.DateCreated  = DateTime.Now;
                model.ModifiedBy   = GetCurrentUser();
                model.DateModified = DateTime.Now;

                result = _libraryBookStatusWebApiManager.InsertLibraryBookStatus(model, out libraryBookStatusCode);
            }

            switch (result)
            {
            case 1:
                return(new ContentResult
                {
                    Content = libraryBookStatusCode,
                    ContentType = "text/plain",
                    StatusCode = 200
                });

            default:
                ModelState.AddModelError(string.Empty, "Failed to insert record");
                break;
            }

            return(BadRequest(ModelState));
        }
コード例 #2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "insertlibrarybookstatus")] 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();
                LibraryBookStatusApiModel model = JsonConvert.DeserializeObject <LibraryBookStatusApiModel>(requestBody);

                if (model == null)
                {
                    return(new BadRequestObjectResult("Please pass LibraryBookApiModel in the request body"));
                }

                int    retVal          = 0;
                string libraryBookCode = string.Empty;
                if (model != null)
                {
                    int?returnVal = _libraryBookStatusWebApiManager.GetCountOfBookCurrentLent(model?.LibraryUser.LibraryUserCode);
                    if (returnVal != null && returnVal.Value > 4)
                    {
                        return(new BadRequestObjectResult("Maximum number of books exceeded"));
                    }

                    LibraryBookApiModel libraryBookApiModel = _libraryBookWebApiManager.GetLibraryBookByLibraryBookCode(model.LibraryBook?.LibraryBookCode);
                    if (libraryBookApiModel == null)
                    {
                        return(new BadRequestObjectResult("Library book not found"));
                    }

                    if (_libraryBookStatusWebApiManager.HasMoreThanOneBookWithSameISBN(libraryBookApiModel.ISBN, model?.LibraryUser.LibraryUserCode))
                    {
                        return(new BadRequestObjectResult("Library User already has that book"));
                    }

                    model.CreatedBy    = _tokenProvider.User;
                    model.DateCreated  = DateTime.Now;
                    model.ModifiedBy   = _tokenProvider.User;
                    model.DateModified = DateTime.Now;

                    retVal = _libraryBookStatusWebApiManager.InsertLibraryBookStatus(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));
            }
        }