Пример #1
0
        public async Task <IActionResult> Create(BookViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.BookImage != null && model.BookImage.Length > 0)
                {
                    var fullPath = Path.Combine(_webHostEnvironment.WebRootPath, "uploads", model.BookImage.FileName);
                    using var stream = new FileStream(fullPath, FileMode.Create);
                    model.BookImage.CopyTo(stream);
                }

                var book = new Book
                {
                    Title       = model.Title,
                    Discription = model.Discription,
                    AuthorId    = model.AuthorId,
                    ImagePath   = model.BookImage.FileName
                };

                _context.Add(book);
                await _context.SaveChangesAsync();

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

            return(RedirectToAction(nameof(Create)));
        }
Пример #2
0
        public async Task <IActionResult> Create([Bind("Name,Id,AddedDate")] Author author)
        {
            if (ModelState.IsValid)
            {
                _context.Add(author);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
        public async Task <IActionResult> CreateBookstore([FromBody] BookstoreResource bookstoreResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var bookstore = mapper.Map <BookstoreResource, Bookstore>(bookstoreResource);

            context.Bookstores.Add(bookstore);
            await context.SaveChangesAsync();

            return(Ok(bookstore));
        }
Пример #4
0
        public async Task <IActionResult> Create(Book book)
        {
            if (ModelState.IsValid)
            {
                book.ImagePath = UploadImage(book.Image);
                _context.Add(book);
                await _context.SaveChangesAsync();

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

            return(View(book));
        }
 public async Task <T> AddAsync(T item, CancellationToken cancel = default)
 {
     if (item is null)
     {
         throw new ArgumentNullException(nameof(item));
     }
     db.Entry(item).State = EntityState.Added; // если элемент не пустой, то отмечаем сущность как добавленную
     if (AutoSaveChanges)
     {
         await db.SaveChangesAsync(cancel).ConfigureAwait(false);
     }
     return(item);
 }
Пример #6
0
        public async Task Save(Author entity)
        {
            //it is possible to avoid this roundtrip by creating the Author with properties and attaching
            var author = await _dbContext.Authors
                         .FirstOrDefaultAsync(x => x.Id == entity.Id);

            //upsert the author
            if (author == null)
            {
                author = new Model.Author
                {
                    Id = entity.Id
                };
                _dbContext.Authors.Add(author);
            }

            author.Name = entity.Name;

            await _dbContext.SaveChangesAsync();
        }
        private async Task InitializeCategories()
        {
            var timer = Stopwatch.StartNew();

            logger.LogInformation("Инициализация категорий...");

            categories = new Category[categoriesCount];
            for (var i = 0; i < categoriesCount; i++)
            {
                categories[i] = new Category {
                    Name = $"Категория {i + 1}"
                }
            }
            ;

            await db.Categories.AddRangeAsync(categories); // Добавляем в контекст

            await db.SaveChangesAsync();                   // Сохраняем в БД

            logger.LogInformation("Инициализация категорий выполнена за {0} мс", timer.ElapsedMilliseconds);
        }
Пример #8
0
        public async Task <IActionResult> CreateBookstore([FromBody] BookstoreResource bookstoreResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var bookstore = mapper.Map <BookstoreResource, Bookstore>(bookstoreResource);

            try
            {
                context.Bookstores.Add(bookstore);
                await context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            //var books = await FakeRepo();
            //bookstore.Id = books.Count +1;
            // books.Add(bookstore);

            return(Ok(bookstore));
        }
Пример #9
0
 public async Task Save()
 {
     await _context.SaveChangesAsync();
 }