Exemplo n.º 1
0
        public async Task <IActionResult> PutTitle([FromRoute] string id, [FromBody] Title title)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != title.Id)
            {
                return(BadRequest());
            }

            _context.Entry(title).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TitleExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async void ToLike(int num_of_likes, int bookId)
        {
            UserContext user_db  = new UserContext();
            var         userName = User.Identity.GetUserName();
            User        user     = await user_db.Users.FirstOrDefaultAsync(x => x.Login == userName); // UserId found

            LikesContext likes_db          = new LikesContext();
            var          real_num_of_likes = likes_db.Likes.ToArray().Length;

            Books b = db.Books.FirstOrDefault(b => b.id == bookId);
            // если в бд лайков не найден ни один лайк от пользователя
            Likes like = new Likes()
            {
                id      = (++real_num_of_likes),
                book_id = bookId,
                user_id = user.Id
            };

            likes_db.Likes.Add(like);

            b.likes = num_of_likes;

            await db.SaveChangesAsync();

            await likes_db.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public async Task <Author> AddAuthorAsync(Author author)
        {
            _context.Authors.Add(author);
            await _context.SaveChangesAsync();

            return(author);
        }
Exemplo n.º 4
0
        public async Task <ActionResult> CreateAsync([FromForm] CreateBookRequest book)
        {
            //Convert to the model
            var bookEntity = _mapper.Map <BookEntity>(book);

            if (book.CoverImage != null)
            {
                // Saving Image on Server
                var coverImage     = book.CoverImage;
                var serverFileName = bookEntity.CoverImageFileName;
                var path           = Path.Combine(_env.WebRootPath + "\\Images\\", serverFileName);
                if (coverImage.Length > 0)
                {
                    using var fileStream = new FileStream(path, FileMode.Create);
                    await coverImage.CopyToAsync(fileStream);
                }
            }

            _context.Books.Add(bookEntity);
            await _context.SaveChangesAsync();

            var bookResponse = _mapper.Map <BookResponse>(bookEntity);

            return(CreatedAtRoute("GetByIdAsync", new { id = bookEntity.Id }, bookResponse));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> PutMagazine(int id, Magazine magazine)
        {
            if (id != magazine.Id)
            {
                return(BadRequest());
            }

            _context.Entry(magazine).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MagazineExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Create(BookADS bookADS)
        {
            var image = bookADS.ImageFile;

            if (image != null)
            {
                string ImageName = Guid.NewGuid().ToString() + Path.GetExtension(image.FileName);

                //Get url To Save
                string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", ImageName);

                using (var stream = new FileStream(SavePath, FileMode.Create))
                {
                    image.CopyTo(stream);
                }
                bookADS.ImagePath = ImageName;
            }
            bookADS.ImageFile = null;

            if (ModelState.IsValid)
            {
                _context.Add(bookADS);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(bookADS));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> PutBook(int id, [FromBody] Book book)
        {
            if (id != book.BookId)
            {
                return(BadRequest());
            }

            _context.Entry(book).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 8
0
        public async Task <Book> AddBookAsync(Book task)
        {
            _context.Books.Add(task);
            await _context.SaveChangesAsync();

            return(task);
        }
Exemplo n.º 9
0
        public async Task <IActionResult> PutTodoItem(long id, TodoItem todoItem)
        {
            if (id != todoItem.Id)
            {
                return(BadRequest());
            }

            _context.Entry(todoItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TodoItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 10
0
        public async Task <IActionResult> PutGenre(long id, Genre genre)
        {
            if (id != genre.Id)
            {
                return(BadRequest());
            }

            _context.Entry(genre).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GenreExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutAuthor([FromRoute] int id, [FromBody] Author author)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != author.ID)
            {
                return(BadRequest());
            }

            _context.Entry(author).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuthorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 12
0
        public async Task <IActionResult> PutPublisher(int id, Publisher publisher)
        {
            if (id != publisher.Id)
            {
                return(BadRequest());
            }

            _context.Entry(publisher).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PublisherExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 13
0
        public async Task <Books> Create(Books book)
        {
            context.Books.Add(book);
            await context.SaveChangesAsync();

            return(book);
        }
Exemplo n.º 14
0
        private async Task LoadGenresAsync(string fileName, CancellationToken cancellationToken)
        {
            using (GenresReader reader = new GenresReader(fileName))
            {
                reader.Fb2Only = true;

                foreach (GenreRecord record in reader.ReadGenres(cancellationToken))
                {
                    Genre genre = await _genresCache.GetAsync(record.Fb2Code);

                    if (genre != null)
                    {
                        continue;
                    }

                    genre = new Genre()
                    {
                        Fb2Code = record.Fb2Code,
                        Name    = record.Name,
                        Search  = record.Name.ToUpper(),
                    };

                    await _db.AddAsync(genre);

                    _genresCache.Add(genre);
                }

                await _db.SaveChangesAsync();
            }
        }
Exemplo n.º 15
0
        public async Task <IHttpActionResult> PutPublisher(int id, Publisher publisher)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != publisher.PublisherId)
            {
                return(BadRequest());
            }

            db.Entry(publisher).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PublisherExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 16
0
        public async Task <ActionResult <BookItem> > PostBookItem(BookItem book)
        {
            _context.BookItems.Add(book);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetBookItem), new { id = book.BookItemId }, book));
        }
Exemplo n.º 17
0
        public async Task <IActionResult> PutAuthors(long id, Authors authors)
        {
            if (id != authors.id)
            {
                return(BadRequest());
            }

            _context.Entry(authors).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuthorsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 18
0
 public async Task <IActionResult> AddOrEdit(int id, [Bind("Id,NameBook,GenreBook,AuthorBook,Pages,Data")] MyBooksModel booksModel)
 {
     if (ModelState.IsValid)
     {
         if (id == 0)
         {
             booksModel.Date = DateTime.Now;
             db.Add(booksModel);
             await db.SaveChangesAsync();
         }
         // Обнова
         else
         {
             try
             {
                 booksModel.Date = DateTime.Now;
                 db.Update(booksModel);
                 await db.SaveChangesAsync();
             }
             catch (DbUpdateConcurrencyException)
             {
                 if (!BooksModelExists(booksModel.Id))
                 {
                     return(NotFound());
                 }
                 else
                 {
                     throw;
                 }
             }
         }
         return(Json(new { isValid = true, html = RenderRazorViewToString(this, "Index", db.Books.ToList()) }));
     }
     return(Json(new { isValid = false, html = RenderRazorViewToString(this, "AddOrEdit", booksModel) }));
 }
Exemplo n.º 19
0
        public async Task CreateAsync(Book book)
        {
            if (!string.IsNullOrWhiteSpace(book.Title) && !string.IsNullOrWhiteSpace(book.Author))
            {
                await booksContext.Books.AddAsync(book);

                await booksContext.SaveChangesAsync();
            }
        }
Exemplo n.º 20
0
        public async Task <IActionResult> Create([Bind("Id,CustomerID,Title,Author,BorrowDate,ReturnDate,BooksAmount")] Books books)
        {
            if (ModelState.IsValid)
            {
                _context.Add(books);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(books));
        }
Exemplo n.º 21
0
        public async Task <IActionResult> Create([Bind("Id,Title")] Collection collection)
        {
            if (ModelState.IsValid)
            {
                _context.Add(collection);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(collection));
        }
Exemplo n.º 22
0
        public async Task <IActionResult> Create([Bind("ID,Name,Email,Score,Comments")] Review review)
        {
            if (ModelState.IsValid)
            {
                _context.Add(review);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Books"));
            }
            return(View(review));
        }
Exemplo n.º 23
0
        public async Task <IActionResult> Create([Bind("id,name,email,password,readingCardId")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
Exemplo n.º 24
0
        public async Task <IActionResult> Create([Bind("Id,Name,Surname")] Author author)
        {
            if (ModelState.IsValid)
            {
                _context.Add(author);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
        public async Task <IActionResult> Create([Bind("Isbn,Title,Publisher")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(book));
        }
Exemplo n.º 26
0
        public async Task <IActionResult> Create([Bind("Id,Title,Author,ISBN,ReleaseYear")] Books books)
        {
            if (ModelState.IsValid)
            {
                _context.Add(books);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(books));
        }
Exemplo n.º 27
0
        public async Task <IActionResult> Create([Bind("id,name")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                _context.Add(genre);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(genre));
        }
    public async Task AddBookAsync(string title, string publisher)
    {
        Book book = new(title, publisher);
        await _booksContext.Books.AddAsync(book);

        int records = await _booksContext.SaveChangesAsync();

        Console.WriteLine($"{records} record added with id {book.BookId}");

        Console.WriteLine();
    }
Exemplo n.º 29
0
        public async Task <IActionResult> Create([Bind("ID,Title,ReleaseDate,Genre,Price,Rating")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(book));
        }
Exemplo n.º 30
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Book book)
        {
            if (ModelState.IsValid)
            {
                _db.Add(book);
                await _db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(book));
        }