public async Task <ActionResult> Create([Bind(Include = "Id,Name,Description")] Category category) { if (ModelState.IsValid) { db.Categories.Add(category); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(category)); }
public async Task <IActionResult> Create([Bind("Id,Description")] Tag tag) { if (ModelState.IsValid) { _context.Add(tag); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(tag)); }
public async Task <IActionResult> Create([Bind("ProductId,CategoryId,ProductName,Price,ProductDescription")] Product product) { if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "Name", product.CategoryId); return(View(product)); }
public static async Task SeedUsers(UserManager <WebShopUser> userManager, RoleManager <IdentityRole <Guid> > roleManager, WebShopDbContext context) { if (!await roleManager.RoleExistsAsync("Administrator")) { await roleManager.CreateAsync(new IdentityRole <Guid> { Name = "Administrator" }); } if (userManager.FindByEmailAsync("*****@*****.**").Result == null) { WebShopUser testUser = new WebShopUser { NickName = "Admin Admin", Id = new Guid("12345678-0000-0000-0000-120000000000"), UserName = "******", NormalizedUserName = "******", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", BillingAddress = new Address() { ZipCode = "1091", Country = "Magyarország", City = "Budapest", Street = "Random utca", HouseNumberAndDoor = "13, 3/12", }, }; IdentityResult result = userManager.CreateAsync(testUser, "Asdf1234.").Result; var addToRoleResult = userManager.AddToRoleAsync(testUser, "Administrator"); context.Ratings.Add(new Rating() { Id = new Guid("12345678-1234-0000-0000-123400000000"), UserId = new Guid("12345678-0000-0000-0000-120000000000"), ItemId = new Guid("10000000-0000-0000-0000-000000000000"), Value = 4 }); context.Comments.Add(new Comment() { Id = Guid.NewGuid(), UserId = new Guid("12345678-0000-0000-0000-120000000000"), ItemId = new Guid("10000000-0000-0000-0000-000000000000"), CommentText = "Nagyon jó termék, évek óta használom hiba nélkül.", Date = DateTime.Now }); await context.SaveChangesAsync(); } }
protected async Task UsingDbContextAsync(int?tenantId, Func <WebShopDbContext, Task> action) { using (UsingTenantId(tenantId)) { using (WebShopDbContext context = LocalIocManager.Resolve <WebShopDbContext>()) { await action(context); await context.SaveChangesAsync(); } } }
public async Task AddViewCount(int productId) { var product = await _context.Products.FindAsync(productId); product.ViewCount += 1; await _context.SaveChangesAsync(); }
public async Task <IActionResult> PutItem(UpdateItemDto item) { if (!ItemExists(item.Id)) { return(NotFound()); } Item foundItem = await _context.Items.Include(x => x.Tags).FirstOrDefaultAsync(x => x.Id == item.Id); _context.Entry(foundItem).CurrentValues.SetValues(item); foundItem.Tags = new List <Tag>(); foreach (var tagId in item.TagIds) { foundItem.Tags.Add(_context.Tags.FirstOrDefault(x => x.Id == tagId)); } await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> Create([FromForm] CreateItemDto item) { Item newItem = new Item() { Description = item.Description, Discount = item.Discount, MadeAt = item.MadeAt, Name = item.Name, Price = item.Price }; newItem.Tags = new List <Tag>(); foreach (var tagId in item.TagIds) { newItem.Tags.Add(_context.Tags.FirstOrDefault(x => x.Id == tagId)); } _context.Items.Add(newItem); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
private void AddPermissionToBuyer() { Role buyerRole = GetStaticRole(StaticRoleNames.Host.Buyer); System.Collections.Generic.List <string> grantedPermissions = _context.Permissions.IgnoreQueryFilters() .OfType <RolePermissionSetting>() .Where(p => p.TenantId == _tenantId && p.RoleId == buyerRole.Id) .Select(p => p.Name) .ToList(); if (!grantedPermissions.Contains(PermissionNames.Buyers)) { _context.Permissions.Add( new RolePermissionSetting { TenantId = _tenantId, Name = PermissionNames.Buyers, IsGranted = true, RoleId = buyerRole.Id }); } _context.SaveChangesAsync(); }
protected async Task <T> UsingDbContextAsync <T>(int?tenantId, Func <WebShopDbContext, Task <T> > func) { T result; using (UsingTenantId(tenantId)) { using (WebShopDbContext context = LocalIocManager.Resolve <WebShopDbContext>()) { result = await func(context); await context.SaveChangesAsync(); } } return(result); }