public async Task <IActionResult> AddBook([FromBody] SimpleBook vm)
        {
            if (ModelState.IsValid)
            {
                var book = new Book()
                {
                    Id          = vm.Id,
                    Title       = vm.Title,
                    Price       = vm.Price,
                    Description = vm.Description,
                    CoverPage   = vm.CoverPage,
                    Tags        = vm.Tags,
                    Author      = authorManager.GetAuthorById(vm.Author)
                };

                bookManager.AddBook(book);
                vm.Id = book.Id;
                await bookHub.Clients.All.SendAsync("BookAdded", vm);

                return(Created($"/api/books/{book.Id}", vm));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
        [NullIsError404(Reason = "Author Not Found")]  //action level attribute overrides controller level attribute
        //[RequiredParameter("id")] <--- if id is null or not given send Error 400
        public ActionResult Details(string id)
        {
            //business logic shouldn't be part of the controller
            //var author = dummyAuthors.FirstOrDefault(a => a.Id == id);

            var author = authorManager.GetAuthorById(id);

            //what if author is null?
            if (author == null)
            {
                //return E404
            }
            return(View(author));
        }
Esempio n. 3
0
        [Route("old/{id}")]  //this path fragment is in combination with controller level Route[]
        public IActionResult GetAuthorByIdOldVersion(string id)
        {
            var author = authorManager.GetAuthorById(id);

            if (author != null)
            {
                return(Ok(author));   //Happy Path
            }
            else
            {
                //return NoFound(); //return status 404 without anydata
                //return NotFound($"No Author with {id} found");   //return with simple message --> text/plain
                return(NotFound(new { Error = "No Author With Given Id", Id = id })); //return with structure message --> application/json
            }
        }
Esempio n. 4
0
        public GetAuthorOutput GetAuthorById(GetAuthorInput input)
        {
            var getAuthor = _authorManager.GetAuthorById(input.Id);
            var output    = ObjectMapper.Map <GetAuthorOutput>(getAuthor);

            return(output);
        }
Esempio n. 5
0
        public GetAuthorOutput GetAuthorById(GetAuthorInput input)
        {
            var author = _authorManager.GetAuthorById(input.Id);
            var result = ObjectMapper.Map <GetAuthorOutput>(author);

            return(result);
        }
Esempio n. 6
0
 public IActionResult Get(int id)
 {
     try
     {
         return(Ok(_authorManager.GetAuthorById(id)));
     }
     catch (ArgumentException ex)
     {
         return(BadRequest(ex));
     }
 }
        public async Task <ActionResult> UpdateAuthor(int id)
        {
            try {
                var entity = await _manager.GetAuthorById(id);

                return(View(entity));
            } catch (ArgumentNullException) {
                return(RedirectToAction("ErrorPage", nameof(Main), new { message = "Error: can not find author with this id", call = nameof(Author) }));
            }
            catch (DbUpdateException) {
                return(RedirectToAction("ErrorPage", nameof(Main), new { message = "Error: invalid input", call = nameof(Author) }));
            }
        }