Пример #1
0
 public async Task <ActionResult <ApiResponse> > Post([FromBody] PublisherDto entity)
 {
     return(new ApiResponse("New publisher added.",
                            await publisherService.Add(mapper.Map <Publisher>(entity))));
 }
Пример #2
0
 public async Task <Publisher> Add()
 {
     return(await _service.Add(Rec, UserInfo.UserId));
 }
 public ActionResult <Publisher> AddPublisher(Publisher publisher)
 {
     _publisherService.Add(publisher);
     return(CreatedAtAction("AddPublisher", new { id = publisher.Id }, publisher));
 }
Пример #4
0
        public Book AddBook(string title,
                            List <string> authorsList,
                            List <string> categoryList,
                            string publisherName,
                            DateTime publishedDate,
                            string isbn,
                            string imabeUrl,
                            string description,
                            string shortDescription,
                            string previewLink)
        {
            var existingbook = _context.Books.FirstOrDefault(b => (b.Title == title && b.Publisher.PublisherName == publisherName && b.PublishedDate == publishedDate));
            var publisher    = _context.Publishers.FirstOrDefault(p => p.PublisherName == publisherName);

            if (existingbook != null)
            {
                throw new ArgumentException($"Book with title: {existingbook.Title}, publisher {publisherName} and date {publishedDate} already exists");
            }
            //The validation does not work correctly. Although existingbook.Title is null it enters in the validation and throws exception
            // Validator.IfNotNull<ArgumentException>(existingbook, $"Book with title: {existingbook.Title} already exists");
            //   Validator.CheckExactLength<ArgumentException>(isbn, 13, $"ISBN should be exactly 13 characters!");

            if (publisher is null)
            {
                publisher = _publisherService.Add(publisherName);
            }

            if (publishedDate.Year > DateTime.Now.Year || publishedDate.Year < (new DateTime(1500, 1, 1).Year))
            {
                throw new ArgumentException($"The date should be bigger than 1500 and smaller than next year.");
            }

            var book = new Book()
            {
                Title            = title,
                PublisherId      = publisher.Id,
                PublishedDate    = publishedDate,
                Isbn             = isbn,
                Description      = description,
                ImageURL         = imabeUrl,
                ShortDescription = shortDescription,
                PreviewLink      = previewLink
            };

            _context.Books.Add(book);


            foreach (var authorName in authorsList)
            {
                var author = _context.Authors.FirstOrDefault(a => a.AuthorName == authorName);

                if (author == null)
                {
                    author = _authorService.Add(authorName);
                }

                var bookAuthorEntry = new BookAuthor()
                {
                    Author = author,
                    Book   = book
                };
                _context.BooksAuthors.Add(bookAuthorEntry);
            }

            foreach (var categoryName in categoryList)
            {
                var category = _context.Categories.FirstOrDefault(c => c.CategoryName == categoryName);

                if (category == null)
                {
                    category = _categoryService.AddCategory(categoryName);
                }

                var bookByAuthorEntry = new BookCategory()
                {
                    Category = category,
                    Book     = book
                };
                _context.BooksCategories.Add(bookByAuthorEntry);
            }

            _context.SaveChanges();
            return(book);
        }
Пример #5
0
 public IActionResult PostPublisher([FromBody] PublisherView publisherView)
 {
     return(Ok(_publisherService.Add(publisherView)));
 }