public async Task <TEntity> Add(TEntity entity) { Db.Set <TEntity>().Add(entity); await Db.SaveChangesAsync(); return(entity); }
/// <inheritdoc /> public async Task <Product> AddProduct(Product product) { EntityEntry <Product> entity = await _context.Products.AddAsync(product); await _context.SaveChangesAsync(); return(entity.Entity); }
public async Task <IActionResult> MyOffers(MyOffersViewModel model) { if (ModelState.IsValid) { if (model.Image != null && (model.Image.ContentType.Length == 0 || !model.Image.ContentType.Contains("image"))) { ModelState.AddModelError("Image", "Загружено не изображение"); } else { Offer offer = new Offer() { Title = model.Title, Description = model.Description, PostedDate = DateTime.Now, UserId = (await userManager.GetUserAsync(User)).Id, CategoryId = model.CategoryId }; db.Offers.Add(offer); await db.SaveChangesAsync(); Price price = new Price() { Value = model.Price, IsContract = model.IsPriceContract, IsWithDelivery = model.IsWithDelivery, OfferId = offer.Id }; db.Prices.Add(price); if (model.Image != null) { string fileName = Path.ChangeExtension(offer.Id.ToString(), Path.GetExtension(model.Image.FileName)); using (var fileStream = new FileStream(Path.Combine(appEnvironment.WebRootPath, "img", "offers", fileName), FileMode.Create)) { await model.Image.CopyToAsync(fileStream); } offer.ImageUrl = "/img/offers/" + fileName; db.Offers.Update(offer); } await db.SaveChangesAsync(); return(await MyOffers()); } } ViewBag.Categories = new SelectList(await db.Categories.AsNoTracking().ToListAsync(), "Id", "Name"); int userId = (await userManager.GetUserAsync(User)).Id; model.Offers = await db.Offers.Where(o => o.UserId == userId).Include(o => o.Price).Include(o => o.User).AsNoTracking().ToListAsync(); return(View(model)); }
public async Task Add(Employee employee) { _context.Employees.Add(employee); try { await _context.SaveChangesAsync(); } catch (Exception) { throw new ArgumentException($"The employee already exists : {employee}"); } }
public async Task Add(Customer customer) { _context.Customers.Add(customer); try { await _context.SaveChangesAsync(); } catch (Exception ex) { throw new InvalidOperationException($"Add Failed : {ex.Message}"); } }
public async Task Add(Product product) { _context.Products.Add(product); try { await _context.SaveChangesAsync(); } catch (Exception ex) { throw new InvalidOperationException($"Add Failed : {ex.Message}"); } }
public async Task Add(ProductType productType) { _context.ProductTypes.Add(productType); try { await _context.SaveChangesAsync(); } catch (Exception) { throw new ArgumentException($"Add Failed : {productType}"); } }
/// <inheritdoc /> public async Task <User> SaveUser(User user) { context.Users.Add(user); await context.SaveChangesAsync(); return(user); }
/// <inheritdoc /> public async Task <Offer> SaveOffer(Offer offer) { context.Offers.Add(offer); await context.SaveChangesAsync(); return(offer); }
public async Task <TruckDb> Delete(int id) { var truck = _ctx.Trucks.FirstOrDefault(x => x.Id == id); _ctx.Trucks.Remove(truck); await _ctx.SaveChangesAsync(); return(truck); }
public async Task Add(Order order) { var product = await _context.Products.FirstOrDefaultAsync(p => p.Id == order.ProductId); order.Price = order.Count * product.Price; _context.Orders.Add(order); try { await _context.SaveChangesAsync(); } catch (Exception ex) { throw new InvalidOperationException($"Add Failed : {ex.Message}"); } }
public async Task <int> CommitAsync() => await _context.SaveChangesAsync();