public void AddWorker(string id, string role, int productId) { var user = this.userManager.FindByIdAsync(id).Result; if (!(userManager.IsInRoleAsync(user, "Worker").Result)) { this.userManager.AddToRoleAsync(user, role).Wait(); } var workerFromDb = this.db.Worker.FirstOrDefault(w => w.userId == id); WorkerProduct workerProduct = new WorkerProduct(); if (workerFromDb == null) { Worker worker = new Worker(); worker.userId = id; worker.Email = user.Email; this.db.Worker.Add(worker); workerProduct.Worker = worker; } else { workerProduct.Worker = workerFromDb; } //TO DO fix bug with dublicate .... var product = this.db.Products.FirstOrDefault(p => p.Id == productId); workerProduct.Product = product; this.db.WorkerProduct.Add(workerProduct); db.SaveChanges(); }
public void AddWorker(string id, string role, int productId) { var user = this.userManager.FindByIdAsync(id).Result; this.userManager.AddToRoleAsync(user, role).Wait(); Worker worker = new Worker(); worker.userId = id; this.db.Worker.Add(worker); var product = this.db.Products.FirstOrDefault(p => p.Id == productId); WorkerProduct workerProduct = new WorkerProduct(); workerProduct.Worker = worker; workerProduct.Product = product; this.db.WorkerProduct.Add(workerProduct); db.SaveChanges(); }