public async Task <IActionResult> Create(CreateOrUpdateBookViewModel book, IFormFile fImage) { if (ModelState.IsValid) { if (fImage == null || fImage.Length == 0) { ModelState.AddModelError("", "Uploaded file is empty or null."); DropdownForm(null); return(View(nameof(CreateOrUpdate), book)); } string image = UploadFileExtensions.UploadFile(fImage, "books"); if (!string.IsNullOrEmpty(image)) { book.Image = image; } book.Alias = book.Title.ToFriendlyUrl(); var viewModel = _mapper.Map <Book>(book); _context.Add(viewModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } DropdownForm(book); return(View(nameof(CreateOrUpdate), book)); }
public async Task <Category> CreateAsync(Category item) { var result = await _context.Categories.AddAsync(item); await _context.SaveChangesAsync(); return(result.Entity); }
public async Task <Book> CreateAsync(Book item) { var result = await _context.Books.AddAsync(item); await _context.SaveChangesAsync(); return(result.Entity); }
public async Task <int> AddUser(UsersInformation user) { if (_context != null) { await _context.UserInfos.AddAsync(user); await _context.SaveChangesAsync(); } return(0); }
public async Task <int> AddBook(BooksInformation book) { if (_context != null) { await _context.BookInfos.AddAsync(book); await _context.SaveChangesAsync(); return(book.BookId); } return(0); }
public async Task <CartModel> Handle(AddBookToCartCommand request, CancellationToken cancellationToken) { var book = await _dbContext.Books.SingleOrDefaultAsync( x => x.Id == request.BookId, cancellationToken); if (book == null) { throw NotFoundError.CreateForResource(nameof(Book), request.BookId).ToException(); } Cart cart = null; if (request.CartId != null) { cart = await _dbContext.Carts.Include(x => x.Books).SingleOrDefaultAsync( x => x.Id == request.CartId.Value, cancellationToken); } if (cart == null) { cart = new Cart(book, request.CartId); await _dbContext.Carts.AddAsync(cart, cancellationToken); } else { cart.AddBook(book); } await _dbContext.SaveChangesAsync(cancellationToken); var cartModel = _mapper.Map <CartModel>(cart); return(cartModel); }
public async Task <IActionResult> Create(CreateOrUpdateAuthorViewModel Author) { if (ModelState.IsValid) { Author.Alias = Author.Name.ToFriendlyUrl(); Author.UpdateDate = DateTime.Now; var viewModel = _mapper.Map <Author>(Author); _context.Add(viewModel); Alert("Lưu danh mục thành công!"); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(nameof(CreateOrUpdate), Author)); }
public async Task <BookModel> Handle(AddBookCommand request, CancellationToken cancellationToken) { var book = new Book(request.Genre, request.Title, request.Author, request.Price); await _dbContext.Books.AddAsync(book, cancellationToken); await _dbContext.SaveChangesAsync(cancellationToken); var bookModel = _mapper.Map <BookModel>(book); return(bookModel); }
public async Task <IActionResult> Create(CreateOrUpdateViewModel category) { if (ModelState.IsValid) { category.Alias = category.Name.ToFriendlyUrl(); category.UpdateDate = DateTime.Now; var viewModel = _mapper.Map <Category>(category); _context.Add(viewModel); Alert("Lưu danh mục thành công!"); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(nameof(CreateOrUpdate), category)); }
public async Task <int> PurchaseBook(PurchaseInfo entity) { if (_context != null) { await _context.PurchaseInfos.AddAsync(entity); await _context.SaveChangesAsync(); return(entity.PurchaseId); } return(0); }
public async Task <int> CreateAsync(string firstName, string lastName) { var author = new Author { FirstName = firstName, LastName = lastName }; _dbContext.Authors.Add(author); await _dbContext.SaveChangesAsync(); return(author.AuthorId); }
public async Task <int> CreateAsync(int authorId, string title, string description, decimal price, int copies, int?edition, int?ageRestriction, DateTime releaseDate, string categories) { var categoryNames = categories.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var existingCategories = await _dbContext .Categories .Where(c => categoryNames.Contains(c.Name)) .ToListAsync(); var allCategories = new List <Category>(existingCategories); foreach (var categoryName in categoryNames) { if (existingCategories.All(c => c.Name != categoryName)) { var category = new Category { Name = categoryName }; this._dbContext.Add(category); allCategories.Add(category); } } await this._dbContext.SaveChangesAsync(); var book = new Book { AuthorId = authorId, Title = title, Description = description, Price = price, Copies = copies, Edition = edition, AgeRestriction = ageRestriction, ReleaseDate = releaseDate }; allCategories.ForEach(c => book.Categories.Add(new BookCategory { CategoryId = c.CategoryId })); _dbContext.Books.Add(book); await _dbContext.SaveChangesAsync(); return(book.BookId); }
public async Task <OrderModel> Handle(SubmitOrderCommand request, CancellationToken cancellationToken) { var cart = await _dbContext.Carts .Include(x => x.Books) .SingleOrDefaultAsync( x => x.Id == request.CartId, cancellationToken); if (cart == null) { throw NotFoundError.CreateForResource(nameof(Cart), request.CartId).ToException(); } var order = new Order(cart, request.PhoneNumber); await _dbContext.AddAsync(order, cancellationToken); _dbContext.Remove(cart); await _dbContext.SaveChangesAsync(cancellationToken); return(_mapper.Map <OrderModel>(order)); }
public BookShopMutation(BookShopDbContext dbContext, ReviewMessageService reviewMessageService) { FieldAsync <BookReviewType>( name: "createReview", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <BookReviewInputType> > { Name = "review" }), resolve: async context => { var review = context.GetArgument <BookReview>("review"); return(await context.TryAsyncResolve(async _ => { await dbContext.BookReviews.AddAsync(review); await dbContext.SaveChangesAsync(); reviewMessageService.AddReviewAddedMessage(review); return review; })); }); }
public Task <int> SaveChangesAsync() { return(_context.SaveChangesAsync()); }
public async Task FilterShouldReturnCorrectResultsWithSeachTextAndOrder() { // Arrange var dbContextOptions = new DbContextOptionsBuilder <BookShopDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var bookShopDbContext = new BookShopDbContext(dbContextOptions); var firstBook = new Book { Id = 1, Title = "Test Title 1", Description = "test description 1", AuthorId = 1 }; var secondBook = new Book { Id = 2, Title = "Test Title 2", Description = "test description 2", AuthorId = 1 }; var thirdBook = new Book { Id = 3, Title = "Test Title 3", Description = "A description that contains title 1", AuthorId = 1 }; var category = new Category { Id = 1, Name = "TestCategory" }; var bookCategory = new BookCategory { BookId = 1, CategoryId = 1 }; var author = new Author { Id = 1, FirstName = "Test", LastName = "User" }; bookShopDbContext.Add(category); bookShopDbContext.Add(bookCategory); bookShopDbContext.Add(author); bookShopDbContext.Add(firstBook); bookShopDbContext.Add(secondBook); bookShopDbContext.Add(thirdBook); await bookShopDbContext.SaveChangesAsync(); var configuration = new MapperConfiguration(config => config.AddProfile <AutoMapperServiceProfile>()); var mapper = new Mapper(configuration); var bookService = new BookService(bookShopDbContext, mapper); // Act var result = await bookService.Filter("title 1"); // Assert result .Should() .HaveCount(2); var firstResultBook = result.ElementAt(0); var secondResultBook = result.ElementAt(1); firstResultBook.Id.Should().Be(firstBook.Id); firstResultBook.Categories.Count().Should().Be(1); firstResultBook.Categories.First().Should().Be(category.Name); firstResultBook.Author.Id.Should().Be(author.Id); firstResultBook.Author.FirstName.Should().Be(author.FirstName); firstResultBook.Author.LastName.Should().Be(author.LastName); secondResultBook.Id.Should().Be(thirdBook.Id); secondResultBook.Categories.Should().BeNullOrEmpty(); secondResultBook.Author.Id.Should().Be(author.Id); secondResultBook.Author.FirstName.Should().Be(author.FirstName); secondResultBook.Author.LastName.Should().Be(author.LastName); }