コード例 #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());
        }
コード例 #2
0
        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();
        }
コード例 #3
0
        public async Task <Author> AddAuthorAsync(Author author)
        {
            _context.Authors.Add(author);
            await _context.SaveChangesAsync();

            return(author);
        }
コード例 #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));
        }
コード例 #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());
        }
コード例 #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));
        }
コード例 #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());
        }
コード例 #8
0
        public async Task <Book> AddBookAsync(Book task)
        {
            _context.Books.Add(task);
            await _context.SaveChangesAsync();

            return(task);
        }
コード例 #9
0
ファイル: TodoItemsController.cs プロジェクト: Oqular/Sait
        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());
        }
コード例 #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());
        }
コード例 #11
0
        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());
        }
コード例 #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());
        }
コード例 #13
0
        public async Task <Books> Create(Books book)
        {
            context.Books.Add(book);
            await context.SaveChangesAsync();

            return(book);
        }
コード例 #14
0
ファイル: ImportService.cs プロジェクト: ksandr/Books.NET
        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();
            }
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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());
        }
コード例 #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) }));
 }
コード例 #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();
            }
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #24
0
ファイル: AuthorsController.cs プロジェクト: san-kov/project
        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));
        }
コード例 #25
0
        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));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #28
0
    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();
    }
コード例 #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));
        }
コード例 #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));
        }