示例#1
0
        public async Task <ActionResult <Book> > CreateSucessfully([FromForm] CreateBookRequestModel bookModel)
        {
            var command = new CreateBookCommand(modelConverter.Convert(bookModel));
            await mediatr.Publish(command);

            return(Ok(new { id = command.Id }));
        }
示例#2
0
        public async Task <MiscResponse <Admin_BookResponseModel> > CreateBookAsync(CreateBookRequestModel model)
        {
            var existingBook = this._bookRepo.Find(x => x.Title == model.Title && x.Author == model.Author).ToList();

            if (existingBook.Any())
            {
                throw new ArgumentException($"A book exists with the given title '{model.Title}' and author '{model.Author}'");
            }

            existingBook = this._bookRepo.Find(x => x.ISBN == model.ISBN).ToList();
            if (existingBook.Any())
            {
                throw new ArgumentException($"A book exists with the given ISBN '{model.ISBN}'");
            }

            var bookId = await this._bookRepo.AddAsync(new Book
                                                       { Author = model.Author, ISBN = model.ISBN, Title = model.Title, IsAvailable = true });

            return(new MiscResponse <Admin_BookResponseModel>
            {
                Data = new Admin_BookResponseModel {
                    Author = model.Author, IsAvailable = true, ISBN = model.ISBN, Title = model.Title, Id = bookId
                },
                Message = "Book created successfully"
            });
        }
示例#3
0
        public IActionResult Post([FromBody] CreateBookRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!this.author.Exists(model.AuthorId))
            {
                return(BadRequest("Author don't exist."));
            }

            var id = this.books.Create(
                model.Title,
                model.Descrtioption,
                model.Price,
                model.Copies,
                model.Edition,
                model.AgeRestriction,
                model.ReleaseDate,
                model.AuthorId,
                model.Categories);

            return(Ok(id));
        }
示例#4
0
        public async Task <IActionResult> Post([FromBody] CreateBookRequestModel model)
        {
            if (!await this.authors.Exists(model.AuthorId))
            {
                return(BadRequest("Author does not exist."));
            }

            var id = await this.books.Create(model.Title, model.Description, model.Price, model.Copies, model.Edition, model.AgeRestriction, model.ReleaseDate, model.AuthorId, model.Categories);

            return(Ok(id));
        }
示例#5
0
        public BookDto Convert(CreateBookRequestModel bookModel)
        {
            string tempPath = bookFileStorage.SaveTemp(bookModel.File);

            return(new BookDto
            {
                Author = bookModel.Author,
                Title = bookModel.Title,
                File = tempPath
            });
        }
示例#6
0
 public BookDto Convert(CreateBookRequestModel bookModel)
 {
     using (var memoryStream = new MemoryStream())
     {
         bookModel.File.CopyTo(memoryStream);
         return(new BookDto
         {
             Author = bookModel.Author,
             Title = bookModel.Title,
             File = memoryStream.ToArray()
         });
     }
 }
示例#7
0
        public async Task <IActionResult> Create([FromBody] CreateBookRequestModel bookModel)
        {
            if (!await _authorService.Exist(bookModel.AuthorId))
            {
                return(BadRequest("Author does not exist"));
            }

            var id = await _bookService.Create(
                bookModel.Title,
                bookModel.Description,
                bookModel.Price,
                bookModel.Copies,
                bookModel.Edition,
                bookModel.AgeRestriction,
                bookModel.ReleaseDate,
                bookModel.CategoryNames,
                bookModel.AuthorId);

            return(Ok(id));
        }
        public async Task <IActionResult> Post([FromBody] CreateBookRequestModel model)
        {
            var result = await this.books.CreateAsync(
                model.Title,
                model.Description,
                model.Price,
                model.Copies,
                model.Edition,
                model.ReleaseDate,
                model.AgeRestriction,
                model.AuthorFirstName,
                model.AuthorLastName,
                model.Categories);

            if (result == 0)
            {
                return(this.BadRequest("Fault! This book cannot be created."));
            }

            return(this.Ok(result));
        }
示例#9
0
        public async Task <IActionResult> AddBook([FromBody] CreateBookRequestModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.GetErrorsAsList()));
                }
                var book = await this._bookService.CreateBookAsync(model);

                return(Ok(book));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                // log exception
                return(StatusCode(500, Constants.ServerErrorMessageText));
            }
        }
示例#10
0
 /// <summary>
 /// 添加图书信息
 /// </summary>
 /// <param name="model">添加图书信息请求模型</param>
 /// <returns></returns>
 public Task AddBookInfoAsync(CreateBookRequestModel model)
 {
     throw new NotImplementedException();
 }
示例#11
0
        public ActionResult <Book> CreateFail([FromForm] CreateBookRequestModel bookModel)
        {
            var book = bookService.AddFileFail(modelConverter.Convert(bookModel));

            return(CreatedAtRoute(nameof(GetBook), new { id = book.Id.ToString() }, modelConverter.Convert(book, GetHostUrl())));
        }