public static void Initialize(OnlineShopContext context) { context.Database.EnsureCreated(); var customer = new Customer() { FirstName = "Jok", LastName = "Garcia", Email = "*****@*****.**", ContactNumber = "888777", IsActive = true }; context.Customers.Add(customer); context.SaveChanges(); var customer2 = new Customer() { FirstName = "Lebron", LastName = "James", Email = "*****@*****.**", ContactNumber = "31231231", }; context.Customers.Add(customer2); context.SaveChanges(); }
public IActionResult SaveProduct(AddOrUpdateProductVM model) { Product neki; if (model.ProductID == 0) { neki = new Product(); _database.product.Add(neki); } else { neki = _database.product.Find(model.ProductID); } neki.ProductNumber = model.ProductNumber; neki.SubCategoryID = model.SubCategoryID; neki.ManufacturerID = model.ManufacturerID; neki.ProductName = model.ProductName; neki.ImageUrl = model.ImageURL; neki.Description = model.Description; neki.UnitPrice = model.UnitPrice; _database.SaveChanges(); return(Redirect("/Product/Show")); }
public IHttpActionResult PutOrder(int id, Order order) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != order.id) { return(BadRequest()); } db.Entry(order).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!OrderExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public Customer AddCustomer(Customer customer) { context.Customers.Add(customer); context.SaveChanges(); return(customer); }
public Product AddProduct(Product prod) { _patternDbContext.Products.Add(prod); _patternDbContext.SaveChanges(); return(prod); //return _patternDbContext.Products.ToList(); }
public IHttpActionResult PutPolicy(int id, PolicyModel policyModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var policy = db.Predicates.Include(x => x.Groups).FirstOrDefault(x => x.id == policyModel.id); if (id != policyModel.id || policy == null) { return(BadRequest()); } var oldGroupIds = policy.Groups.Select(x => x.id); var newGroupIds = policyModel.GroupIds.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(x => int.Parse(x)); var groupsIdsToAdd = newGroupIds.Except(oldGroupIds); var groupsIdsToRemove = oldGroupIds.Except(newGroupIds); var groupsToAdd = db.Groups.Where(x => groupsIdsToAdd.Contains(x.id)); var groupsToRemove = db.Groups.Where(x => groupsIdsToRemove.Contains(x.id)); foreach (var group in groupsToAdd) { policy.Groups.Add(group); } foreach (var group in groupsToRemove) { policy.Groups.Remove(group); } policy.TableName = policyModel.TableName; policy.Value = policyModel.PredicateValue; db.Entry(policy).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!PolicyExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "ProductID,Price,Descrption,Name,Stock")] Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }
public ActionResult Create(Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }
public ActionResult Create([Bind(Include = "Id,Username,Password")] User user) { if (ModelState.IsValid) { db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); }
public ActionResult Create([Bind(Include = "ID,Name,MetaTitle,ParentID,DisplayOrder,SeoTitle,CreatedDate,CreatedBy,ModifiedDate,ModefiedBy,MetaKeywords,MetakeyDescriptions,Status,ShowOnHome")] Category category) { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(category)); }
public IHttpActionResult PutGroup(int id, GroupModel groupModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var group = db.Groups.Include(x => x.Employees).FirstOrDefault(x => x.id == groupModel.id); if (id != groupModel.id || group == null) { return(BadRequest()); } var oldEmployeeIds = group.Employees.Select(x => x.id); var newEmployeeIds = groupModel.EmployeeIds.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(x => int.Parse(x)); var employeeIdsToAdd = newEmployeeIds.Except(oldEmployeeIds); var employeeIdsToRemove = oldEmployeeIds.Except(newEmployeeIds); var employeesToAdd = db.Employees.Where(x => employeeIdsToAdd.Contains(x.id)); var employeesToRemove = db.Employees.Where(x => employeeIdsToRemove.Contains(x.id)); foreach (var employee in employeesToAdd) { group.Employees.Add(employee); } foreach (var employee in employeesToRemove) { group.Employees.Remove(employee); } db.Entry(group).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!GroupExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "Id,Name,ImageData,CategoryId")] Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name", product.CategoryId); return(View(product)); }
public IActionResult SaveUserInfo(ChangeUserInfoVM model) { int userid = Int32.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)); var user = _database.Users.Find(userid); user.Name = model.name; user.Surname = model.surname; user.Adress = model.adress; user.CityID = model.choosencity; user.PhoneNumber = model.phonenumber; _database.SaveChanges(); return(Redirect("/Customer/Panel")); }
public async Task <IActionResult> Registration(AuthDTO authDTO) { var users = shopContext.Users.ToList(); foreach (var user in users) { if (user.PhoneNumber == authDTO.PhoneNumber) { return(BadRequest()); } } var responseDTO = await twilioSmsService.SendVerificationCode(authDTO.PhoneNumber); if (responseDTO == null) { return(BadRequest("Сообщение не отправлено")); } if (authDTO.Code != responseDTO.VerificationCode) { return(BadRequest("Код введен не верно")); } shopContext.Users.AddRange(new User { PhoneNumber = authDTO.PhoneNumber, VerificationCode = authDTO.Code }); shopContext.SaveChanges(); return(Ok("Пользователь создан")); }
private static List <AdType> SeedAdTypes(OnlineShopContext context) { var adTypes = new List <AdType> { new AdType() { Name = "Normal", PricePerDay = 3.99m, Index = 100 }, new AdType() { Name = "Premium", PricePerDay = 5.99m, Index = 200 }, new AdType() { Name = "Diamond", PricePerDay = 9.99m, Index = 300 } }; foreach (var adType in adTypes) { context.AdTypes.Add(adType); } context.SaveChanges(); return(adTypes); }
public void DeleteAllIvanovs() { using (var ctx = new OnlineShopContext()) { ctx.Customers .Where(customer => customer.Lastname == "Ivanov") .ToList() .ForEach(ivanov => { ctx.Orders.Where(ivanovOrder => ivanovOrder.CustomerId == ivanov.Id) .ToList() .ForEach(order => { ctx.OrderItems .Where(ivanovOrderItem => ivanovOrderItem.OrderId == order.Id) .ToList() .ForEach(orderItem => ctx.OrderItems.Remove(orderItem)); ctx.Orders.Remove(order); }); ctx.Customers.Remove(ivanov); }); ctx.SaveChanges(); } }
public WorkItemServiceTests() { // Arrange var iteration1 = new Iteration { Id = 1, Name = "Iteration1" }; var iteration2 = new Iteration { Id = 2, Name = "Iteration2" }; var iteration3 = new Iteration { Id = 3, Name = "Iteration3" }; var workItem1 = new WorkItem { Id = 1, Name = "Iteration1", ImageUrl = "Iteration1_ImageUrl", Iteration = iteration1 }; var workItem2 = new WorkItem { Id = 2, Name = "Iteration2", ImageUrl = "Iteration2_ImageUrl", Iteration = iteration2 }; var workItem3 = new WorkItem { Id = 3, Name = "Iteration3", ImageUrl = "Iteration3_ImageUrl", Iteration = iteration3 }; using (var context = new OnlineShopContext(options)) { context.Iteration.AddRange(iteration1, iteration2, iteration3); context.WorkItem.AddRange(workItem1, workItem2, workItem3); context.SaveChanges(); } }
public void UpdateFirstTwoUsers() { using (var ctx = new OnlineShopContext()) { ctx.Customers.Take(2).ToList().ForEach(c => c.Lastname = "Ivanov"); ctx.SaveChanges(); } }
public int Add(Product aType) { int key = -1; try { _db.Entry(aType).State = EntityState.Added; _db.SaveChanges(); key = aType.ProductId; } catch (Exception) { throw; } return(key); }
public IActionResult SaveOrderChanges(EditOrderVM model) { var order = _database.order.Find(model.OrderID); foreach (var x in model.items) { var a = 0; foreach (var y in x.branches) { a += y.Input; _database.branchproduct.FirstOrDefault(a => a.BranchID == y.BranchID && x.ProductID == a.ProductID).UnitsInBranch -= y.Input; } if (a != x.RequiredQuantity) { return(Redirect("/Administration/EditOrder?id=" + model.OrderID + "&again=" + true)); } } order.OrderStatusID = 2; order.OrderStatus = _database.orderstatus.Find(2); order.ShipDate = DateTime.Now; Notification nova = new Notification { UserID = order.UserID, Text = "Vaša narudžba (" + model.OrderID + ") je isporučena." }; Parallel.Invoke(() => Sms("Narudzba <" + model.OrderID + "> je isporucena.")); _database.Add(nova); _database.Add(new AdminActivity { ActivityID = 10, AdminID = Int32.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)), DateOfActivity = DateTime.Now }); _database.SaveChanges(); foreach (var x in model.items) { if (_database.product.Find(x.ProductID).UnitsInStock == 0) { Sms("Proizvod " + _database.product.Find(x.ProductID).ProductName + " (" + x.ProductID + ") vise nije dostupan."); } } return(PartialView("SuccessMessage")); }
public void EditProduct([Bind(Include = "Id,Name,ImageData,ImageFile,CategoryId")] Product product) { var file = product.ImageFile; if (file != null && file.ContentLength > 0) { if (product.ImageData != "default.png") { System.IO.File.Delete(Server.MapPath("~/Content/Images/" + product.ImageData)); } file.SaveAs(Server.MapPath("~/Content/Images/" + file.FileName)); product.ImageData = file.FileName; } db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return; }
private static void ClearDatabase() { var context = new OnlineShopContext(); context.Ads.Delete(); context.AdTypes.Delete(); context.Categories.Delete(); context.Users.Delete(); context.SaveChanges(); }
private static void SeedCategories(OnlineShopContext context) { var category = new Category() { Ads = new List <Ad>(), Name = "cat1" }; context.Categories.Add(category); context.SaveChanges(); }
public IHttpActionResult PostCustomer(Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Customers.Add(customer); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = customer.id }, customer)); }
public bool Insert(OrderDetail detail) { try { db.OrderDetails.Add(detail); db.SaveChanges(); return(true); } catch (Exception) { return(false); } }
private static void SeedAdTypes(OnlineShopContext context) { context.AdTypes.Add(new AdType { Name = "Normal", Index = 100 }); context.AdTypes.Add(new AdType { Name = "Premium", Index = 200 }); context.AdTypes.Add(new AdType { Name = "Gold", Index = 300 }); context.SaveChanges(); }
private static void SeedAdTypes(OnlineShopContext context) { var adType = new AdType() { Ads = new List <Ad>(), Index = 1, Name = "type1", PricePerDay = 1 }; context.AdTypes.Add(adType); context.SaveChanges(); }
private static void SeedCategories(OnlineShopContext context) { context.Categories.Add(new Category { Name = "Cars" }); context.Categories.Add(new Category { Name = "Phones" }); context.Categories.Add(new Category { Name = "Cameras" }); context.SaveChanges(); }
public IHttpActionResult DeleteEmployee(int id) { Employee employee = db.Employees.Find(id); if (employee == null) { return(NotFound()); } db.Employees.Remove(employee); db.SaveChanges(); return(Ok(employee)); }
public void AddToCart(int productid, int userid, int q) { Cart singlerecord = _database.cart.SingleOrDefault(u => u.UserID == userid && u.ProductID == productid); Product product = _database.product.Find(productid); if (singlerecord != null) { singlerecord.Quantity += q; singlerecord.TotalPrice = (singlerecord.Quantity + q) * product.UnitPrice; } else { Cart newrecord = new Cart { UserID = userid, ProductID = productid, Quantity = q, TotalPrice = product.UnitPrice * q }; _database.Add(newrecord); } _database.SaveChanges(); }
private static List<AdType> SeedAdTypes(OnlineShopContext context) { var adTypes = new List<AdType> { new AdType() { Name = "Normal", PricePerDay = 3.99m, Index = 100 }, new AdType() { Name = "Premium", PricePerDay = 5.99m, Index = 200 }, new AdType() { Name = "Diamond", PricePerDay = 9.99m, Index = 300 } }; foreach (var adType in adTypes) { context.AdTypes.Add(adType); } context.SaveChanges(); return adTypes; }
private static List<Category> SeedCategories(OnlineShopContext context) { var categories = new List<Category>() { new Category() {Name = "Business"}, new Category() {Name = "Garden"}, new Category() {Name = "Toys"}, new Category() {Name = "Pleasure"}, new Category() {Name = "Electronics"}, new Category() {Name = "Clothes"} }; foreach (var category in categories) { context.Categories.Add(category); } context.SaveChanges(); return categories; }