public async Task <IActionResult> PutUsers(int id, Users users) { if (id != users.Id) { return(BadRequest()); } _context.Entry(users).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UsersExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
/// <summary> /// Deletes selected worker. /// </summary> /// <param name="obj"></param> private async void ExecDeleteWorkerAsync(object obj) { Worker worker = (Worker)WorkerListView.WorkerListUserControl.WorkerList.SelectedItem; Workers.Remove(worker); switch (App.DataSource) { case ConnectionResource.LOCALAPI: await new WebServiceManager <Worker>().DeleteAsync(worker); break; case ConnectionResource.LOCALMYSQL: using (var ctx = new MysqlDbContext(ConnectionResource.LOCALMYSQL)) { ctx.DbSetWorkers.Attach(worker); ctx.DbSetWorkers.Remove(worker); await ctx.SaveChangesAsync(); } break; default: break; } }
/// <summary> /// Deletes selected case. /// </summary> /// <param name="obj"></param> private async void ExecDeleteCaseAsync(object obj) { Case pcCase = (Case)ProductListView.ProductListUserControl.CaseList.SelectedItem; Cases.Remove(pcCase); switch (App.DataSource) { case ConnectionResource.LOCALAPI: await new WebServiceManager <Case>().DeleteAsync(pcCase); break; case ConnectionResource.LOCALMYSQL: using (var ctx = new MysqlDbContext(ConnectionResource.LOCALMYSQL)) { ctx.DbSetCases.Attach(pcCase); ctx.DbSetCases.Remove(pcCase); await ctx.SaveChangesAsync(); } break; default: break; } }
/// <summary> /// Deletes selected storage component. /// </summary> /// <param name="obj"></param> private async void ExecDeleteStorageAsync(object obj) { Storage storageComponent = (Storage)ProductListView.ProductListUserControl.StorageList.SelectedItem; StorageComponents.Remove(storageComponent); switch (App.DataSource) { case ConnectionResource.LOCALAPI: await new WebServiceManager <Storage>().DeleteAsync(storageComponent); break; case ConnectionResource.LOCALMYSQL: using (var ctx = new MysqlDbContext(ConnectionResource.LOCALMYSQL)) { ctx.DbSetStorages.Attach(storageComponent); ctx.DbSetStorages.Remove(storageComponent); await ctx.SaveChangesAsync(); } break; default: break; } }
/// <summary> /// Deletes selected memory. /// </summary> /// <param name="obj"></param> private async void ExecDeleteMemoryAsync(object obj) { Memory memory = (Memory)ProductListView.ProductListUserControl.MemoryList.SelectedItem; Rams.Remove(memory); switch (App.DataSource) { case ConnectionResource.LOCALAPI: await new WebServiceManager <Memory>().DeleteAsync(memory); break; case ConnectionResource.LOCALMYSQL: using (var ctx = new MysqlDbContext(ConnectionResource.LOCALMYSQL)) { ctx.DbSetMemories.Attach(memory); ctx.DbSetMemories.Remove(memory); await ctx.SaveChangesAsync(); } break; default: break; } }
/// <summary> /// Deletes selected GPU. /// </summary> /// <param name="obj"></param> private async void ExecDeleteGPUAsync(object obj) { GPU gpu = (GPU)ProductListView.ProductListUserControl.GPUList.SelectedItem; GPUs.Remove(gpu); switch (App.DataSource) { case ConnectionResource.LOCALAPI: await new WebServiceManager <GPU>().DeleteAsync(gpu); break; case ConnectionResource.LOCALMYSQL: using (var ctx = new MysqlDbContext(ConnectionResource.LOCALMYSQL)) { ctx.DbSetGPUs.Attach(gpu); ctx.DbSetGPUs.Remove(gpu); await ctx.SaveChangesAsync(); } break; default: break; } }
/// <summary> /// Adds or updates a customer. /// </summary> /// <param name="obj"></param> private async void ExecSaveCustomerAsync(object obj) { switch (App.DataSource) { case ConnectionResource.LOCALAPI: WebServiceManager <Customer> webServiceManager = new WebServiceManager <Customer>(); if (Customer.Id == 0) // Saving new customer { await webServiceManager.PostAsync(Customer); } else // Saving updated customer { await webServiceManager.PutAsync(Customer); } break; case ConnectionResource.LOCALMYSQL: using (var ctx = new MysqlDbContext(ConnectionResource.LOCALMYSQL)) { if (Customer.Id == 0) // Saving new customer { ctx.DbSetCustomers.Add(Customer); await ctx.SaveChangesAsync(); } else // Saving updated customer { ctx.Entry(Customer).State = EntityState.Modified; ctx.Entry(Customer.Address).State = EntityState.Modified; await ctx.SaveChangesAsync(); } } break; default: break; } CustomerView.NavigationService.Navigate(new CustomerListView()); }
public async Task <IActionResult> CreatePost(BlogItem blogItem) { if (!ModelState.IsValid) { // Fehlerbehandlung } using (var db = new MysqlDbContext(this.ConnectionString)) { var addedBlogPost = await db.Posts.AddAsync(blogItem.Post); var addedTitleImage = await db.Media.AddAsync(blogItem.TitleImage); // redirectto home if no entities were saved (because whatever) // TODO: return to specific error page if (await db.SaveChangesAsync() == 0) { return(this.RedirectToAction("index", "home")); } // redirect directly to new blogpost return(this.RedirectToAction("index", "blog", new { id = addedBlogPost.Entity.Id })); } }
/// <summary> /// Processes the payment. /// Stock of products decreased, customer gets charged. /// </summary> /// <param name="obj"></param> private async void ExecProceedToPaymentAsync(object obj) { Cart.Customer.Money -= Cart.Total; switch (App.DataSource) { case ConnectionResource.LOCALAPI: await new WebServiceManager <Cart>().PostAsync(Cart); break; case ConnectionResource.LOCALMYSQL: using (var ctx = new MysqlDbContext(ConnectionResource.LOCALMYSQL)) { ctx.Entry(Cart.Customer).State = EntityState.Modified; foreach (var item in Cart.Items) { ctx.Entry(item.Product).State = EntityState.Modified; } ctx.DbSetCarts.Add(Cart); await ctx.SaveChangesAsync(); } break; default: break; } foreach (var item in Cart.Items) { if (item.Product.Stock > 0) { if (item.Product is CPU) { CPUs.Add((CPU)item.Product); } else if (item.Product is GPU) { GPUs.Add((GPU)item.Product); } else if (item.Product is Motherboard) { Motherboards.Add((Motherboard)item.Product); } else if (item.Product is Memory) { Rams.Add((Memory)item.Product); } else if (item.Product is Storage) { StorageComponents.Add((Storage)item.Product); } else if (item.Product is PSU) { PSUs.Add((PSU)item.Product); } else // Case { Cases.Add((Case)item.Product); } } } Cart.Items.Clear(); Cart.Total = 0; }
/// <summary> /// Adds or updates a product. /// </summary> /// <param name="obj"></param> private async void ExecSaveProductAsync(object obj) { switch (App.DataSource) { case ConnectionResource.LOCALAPI: if (Product.Id == 0) // Saving new product { if (Product is CPU) { await new WebServiceManager <CPU>().PostAsync((CPU)Product); } else if (Product is GPU) { await new WebServiceManager <GPU>().PostAsync((GPU)Product); } else if (Product is Motherboard) { await new WebServiceManager <Motherboard>().PostAsync((Motherboard)Product); } else if (Product is Memory) { await new WebServiceManager <Memory>().PostAsync((Memory)Product); } else if (Product is Storage) { await new WebServiceManager <Storage>().PostAsync((Storage)Product); } else if (Product is PSU) { await new WebServiceManager <PSU>().PostAsync((PSU)Product); } else // Case { await new WebServiceManager <Case>().PostAsync((Case)Product); } } else // Saving updated product { if (Product is CPU) { await new WebServiceManager <CPU>().PutAsync((CPU)Product); } else if (Product is GPU) { await new WebServiceManager <GPU>().PutAsync((GPU)Product); } else if (Product is Motherboard) { await new WebServiceManager <Motherboard>().PutAsync((Motherboard)Product); } else if (Product is Memory) { await new WebServiceManager <Memory>().PutAsync((Memory)Product); } else if (Product is Storage) { await new WebServiceManager <Storage>().PutAsync((Storage)Product); } else if (Product is PSU) { await new WebServiceManager <PSU>().PutAsync((PSU)Product); } else // Case { await new WebServiceManager <Case>().PutAsync((Case)Product); } } break; case ConnectionResource.LOCALMYSQL: using (var ctx = new MysqlDbContext(App.DataSource)) { if (Product.Id == 0) // Saving new product { if (Product is CPU) { ctx.DbSetCPUs.Add((CPU)Product); } else if (Product is GPU) { ctx.DbSetGPUs.Add((GPU)Product); } else if (Product is Motherboard) { ctx.DbSetMotherboards.Add((Motherboard)Product); } else if (Product is Memory) { ctx.DbSetMemories.Add((Memory)Product); } else if (Product is Storage) { ctx.DbSetStorages.Add((Storage)Product); } else if (Product is PSU) { ctx.DbSetPSUs.Add((PSU)Product); } else // Case { ctx.DbSetCases.Add((Case)Product); } await ctx.SaveChangesAsync(); } else // Saving updated product { ctx.Entry(Product).State = EntityState.Modified; await ctx.SaveChangesAsync(); } } break; default: break; } string activeTabName = (string)Utility.FindParent <StackPanel>((Button)ProductView.ProductUserControl.SaveProductBtn).Tag; ProductView.NavigationService.Navigate(new ProductListView(activeTabName)); }
/// <inheritdoc /> public async Task CommitChanges() { await AccessDataSource(async() => await _mysqlDbContext.SaveChangesAsync()); }