public void init() { if (flag == 0) { flag = 1; var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; var order = _context.Order .FirstOrDefaultAsync(m => m.OwnerId == userId); if (order.Result == null) { Order tempOrder = new Order(); tempOrder.OwnerId = userId; _context.Add(tempOrder); _context.SaveChanges(); order = _context.Order .FirstOrDefaultAsync(m => m.OwnerId == userId); orderid = order.Result.Orderid; // OrderList tempList=new OrderList(); // tempList.Orderid = orderid; // tempList.Bookid = _context.Book.First().id; // _context.Add(tempList); // _context.SaveChanges(); return; } order = _context.Order .FirstOrDefaultAsync(m => m.OwnerId == userId); orderid = order.Result.Orderid; } }
public async Task <IActionResult> Create([Bind("LanguageID,LanguageName")] Language language) { if (ModelState.IsValid) { _context.Add(language); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(language)); }
public async Task <IActionResult> Create([Bind("Id,Title,Type,From,TO,Description")] Books books) { if (ModelState.IsValid) { _context.Add(books); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(books)); }
public async Task <IActionResult> Create([Bind("IdProducts,ProductsName,Image,Price,Unit")] Products products) { if (ModelState.IsValid) { _context.Add(products); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(products)); }
public async Task <IActionResult> Create([Bind("")] Order order) { if (ModelState.IsValid) { _context.Add(order); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(order)); }
public async Task <IActionResult> Create([Bind("ContactId,Name,Email,Message,DateCreate")] ContactMe contactMe) { if (ModelState.IsValid) { _context.Add(contactMe); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(contactMe)); }
public async Task <IActionResult> Create([Bind("Name,Price")] Want want) { if (ModelState.IsValid) { var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; want.OwnerId = userId; _context.Add(want); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(want)); }
public static string ImportAuthors(BookShopContext context, string jsonString) { var jsonAuthors = JsonConvert.DeserializeObject <ImportAuthorDTO[]>(jsonString); StringBuilder sb = new StringBuilder(); foreach (var author in jsonAuthors) { if (!IsValid(author) || context.Authors.Any(x => x.Email == author.Email)) { sb.AppendLine(ErrorMessage); continue; } Author currentAuthor = new Author { FirstName = author.FirstName, LastName = author.LastName, Phone = author.Phone, Email = author.Email }; foreach (var bookId in author.Books) { if (!context.Books.Any(x => x.Id == bookId.Id)) { continue; } currentAuthor.AuthorsBooks.Add(new AuthorBook { BookId = bookId.Id.Value }); } if (currentAuthor.AuthorsBooks.Count == 0) { sb.AppendLine(ErrorMessage); continue; } context.Add(currentAuthor); context.SaveChanges(); sb.AppendLine(string.Format(SuccessfullyImportedAuthor, currentAuthor.FirstName + " " + currentAuthor.LastName, currentAuthor.AuthorsBooks.Count)); } return(sb.ToString()); }
public async Task <IActionResult> Create([Bind("Name,Price,SoldPrice,Content,Author")] Book book) { if (ModelState.IsValid) { var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; book.OwnerId = userId; // ViewBag.userId = userId; _context.Add(book); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(book)); }
public static string ImportAuthors(BookShopContext context, string jsonString) { var sb = new StringBuilder(); AuthorJsonImportModel[] authors = JsonConvert.DeserializeObject <AuthorJsonImportModel[]>(jsonString); foreach (var author in authors) { List <Book> booksToAddToAuthor = new List <Book>(); if (!IsValid(author) || !author.Books.Any() || !author.Books.All(IsValid)) { sb.AppendLine(ErrorMessage); continue; } if (context.Authors.Any(a => a.Email == author.Email)) { sb.AppendLine(ErrorMessage); continue; } Author authorToDb = new Author { FirstName = author.FirstName, LastName = author.LastName, Phone = author.Phone, Email = author.Email, }; foreach (var book in author.Books) { if (!book.Id.HasValue) { continue; } Book bookToAdd = context.Books.FirstOrDefault(b => b.Id == book.Id); if (bookToAdd == null) { continue; } booksToAddToAuthor.Add(bookToAdd); } if (booksToAddToAuthor.Count == 0) { sb.AppendLine(ErrorMessage); continue; } foreach (var currentBook in booksToAddToAuthor) { authorToDb.AuthorsBooks.Add(new AuthorBook { Book = currentBook, }); } context.Add(authorToDb); context.SaveChanges(); sb.AppendLine(string.Format(SuccessfullyImportedAuthor, authorToDb.FirstName + " " + authorToDb.LastName, authorToDb.AuthorsBooks.Count)); } return(sb.ToString().TrimEnd()); }