private async Task AddUserNotes() { var jsonText = File.ReadAllText("./Services/userNotes.json"); var users = JsonConvert.DeserializeObject <List <UserNotesDto> >(jsonText); foreach (var usernote in users) { var ns = new List <Note>(); foreach (var n in usernote.Notes) { ns.Add(new Note { title = n.title, date = n.date }); } var u = new User { // id = usernote.id, bio = usernote.bio, name = usernote.name, birthDate = usernote.birthDate, avatar = usernote.avatar, Notes = ns }; _context.Users.Add(u); _context.SaveChanges(); } }
public IActionResult Post([FromBody] ShoppingCartItem shoppingCartItem) { var shoppingCart = _dbContext.ShoppingCartItems.FirstOrDefault(s => s.ProductId == shoppingCartItem.ProductId && s.CustomerId == shoppingCartItem.CustomerId); if (shoppingCart != null) { shoppingCart.Qty += shoppingCartItem.Qty; shoppingCart.TotalAmount = shoppingCart.Price * shoppingCart.Qty; } else { var sCart = new ShoppingCartItem() { CustomerId = shoppingCartItem.CustomerId, ProductId = shoppingCartItem.ProductId, Price = shoppingCartItem.Price, Qty = shoppingCartItem.Qty, TotalAmount = shoppingCartItem.TotalAmount }; _dbContext.ShoppingCartItems.Add(sCart); } _dbContext.SaveChanges(); return(StatusCode(StatusCodes.Status201Created)); }
public IActionResult Post([FromBody] Order order) { order.IsOrderCompleted = false; order.OrderPlaced = DateTime.Now; _dbContext.Orders.Add(order); _dbContext.SaveChanges(); var shoppingCartItems = _dbContext.ShoppingCartItems.Where(cart => cart.CustomerId == order.userId); foreach (var item in shoppingCartItems) { var orderDetail = new OrderDetail() { Price = item.Price, TotalAmount = item.TotalAmount, Qty = item.Qty, ProductId = item.ProductId, OrderId = order.Id, }; _dbContext.OrderDetails.Add(orderDetail); } _dbContext.SaveChanges(); _dbContext.ShoppingCartItems.RemoveRange(shoppingCartItems); _dbContext.SaveChanges(); return(Ok(new { OrderId = order.Id })); }
public async Task <IActionResult> Register(AppInstallations AppInstallations) { var userWithSameEmail = _dbContext.AppInstallations.SingleOrDefault(u => u.userId == AppInstallations.userId && u.DeviceID == AppInstallations.DeviceID); if (userWithSameEmail != null) { var AppInstallation = _dbContext.AppInstallations.Where(s => s.AppInstId == userWithSameEmail.AppInstId); _dbContext.AppInstallations.RemoveRange(AppInstallations); _dbContext.SaveChanges(); } var AppInstallationsObj = new AppInstallations { AppInstId = Guid.NewGuid(), userId = AppInstallations.userId, DeviceID = AppInstallations.DeviceID, DeviceOS = AppInstallations.DeviceOS, DeviceOSVersion = AppInstallations.DeviceOSVersion, DeviceModel = AppInstallations.DeviceModel, DeviceNickName = AppInstallations.DeviceNickName, InstallDate = AppInstallations.InstallDate, InstallType = AppInstallations.InstallType, LastAuthDate = AppInstallations.LastAuthDate, AppVersion = AppInstallations.AppVersion, RegId = AppInstallations.RegId, MasterSyncReq = AppInstallations.MasterSyncReq, InstStatus = AppInstallations.InstStatus }; _dbContext.AppInstallations.Add(AppInstallationsObj); await _dbContext.SaveChangesAsync(); return(StatusCode(StatusCodes.Status201Created)); }
public IActionResult Delete(string CityID) { var City = _dbContext.City.Where(s => s.CityID.ToString() == CityID); _dbContext.City.RemoveRange(City); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string OfferID) { var Offers = _dbContext.Offers.Where(s => s.OfferID.ToString() == OfferID); _dbContext.Offers.RemoveRange(Offers); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string RegionID) { var Region = _dbContext.Region.Where(s => s.RegionID.ToString() == RegionID); _dbContext.Region.RemoveRange(Region); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string SchoolID) { var Schools = _dbContext.Schools.Where(s => s.SchoolID.ToString() == SchoolID); _dbContext.Schools.RemoveRange(Schools); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string VehicleID) { var DeliveryVehicles = _dbContext.DeliveryVehicles.Where(s => s.VehicleID.ToString() == VehicleID); _dbContext.DeliveryVehicles.RemoveRange(DeliveryVehicles); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string ItemAddOnID) { var ItemAddOns = _dbContext.ItemAddOns.Where(s => s.ItemAddOnID.ToString() == ItemAddOnID); _dbContext.ItemAddOns.RemoveRange(ItemAddOns); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string ADMINMastID) { var ADMINMast = _dbContext.ADMINMast.Where(s => s.AdminID.ToString() == ADMINMastID); _dbContext.ADMINMast.RemoveRange(ADMINMast); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string UserSubscriptionID) { var UserSubscriptions = _dbContext.UserSubscriptions.Where(s => s.UserSubscriptionID.ToString() == UserSubscriptionID); _dbContext.UserSubscriptions.RemoveRange(UserSubscriptions); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string vendorItemID) { var VendorItems = _dbContext.VendorItems.Where(s => s.vendorItemID.ToString() == vendorItemID); _dbContext.VendorItems.RemoveRange(VendorItems); _dbContext.SaveChanges(); return(Ok()); }
private static void ClearDatabase(FoodDbContext _context) { var productCategories = _context.Products.ToList(); _context.Products.RemoveRange(productCategories); var productItems = _context.ProductItems.ToList(); _context.ProductItems.RemoveRange(productItems); var orderItems = _context.OrderItems.ToList(); _context.OrderItems.RemoveRange(orderItems); var orders = _context.Orders.ToList(); _context.Orders.RemoveRange(orders); var customers = _context.Customers.ToList(); _context.Customers.RemoveRange(customers); //var users = _context.Users.ToList(); //var userRoles = _context.UserRoles.ToList(); //foreach (var user in users) //{ // if (!userRoles.Any(r => r.UserId == user.Id)) // { // _context.Users.Remove(user); // } //} _context.SaveChanges(); }
public void TestExample() { var dbOptions = new DbContextOptionsBuilder <FoodDbContext>() .UseInMemoryDatabase(databaseName: "MyTestDatabase") .Options; using (var context = new FoodDbContext(dbOptions)) { context.FoodItems.Add(new FoodEntity() { Id = 999, Calories = 1300, Type = "Starter", Name = "Pizza", Created = DateTime.Now }); context.SaveChanges(); var host = new WebHostBuilder() .UseContentRoot(GetApplicationRoot()) .UseStartup <StartupFake>() .ConfigureServices(services => { services.AddSingleton(context); }); using (var server = new TestServer(host)) { var httpClient = server.CreateClient(); var response = httpClient.GetAsync("/api/v1/foods/999").Result; response.EnsureSuccessStatusCode(); } } }
public IActionResult Delete(string AdminRegionID) { var AdminRegions = _dbContext.AdminRegions.Where(s => s.AdminRegionID.ToString() == AdminRegionID); _dbContext.AdminRegions.RemoveRange(AdminRegions); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string DeliveryBoyID) { var DeliveryBoy = _dbContext.DeliveryBoys.Where(s => s.DeliveryBoyID.ToString() == DeliveryBoyID); _dbContext.DeliveryBoys.RemoveRange(DeliveryBoy); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string SubscriptionScheduleID) { var SubscriptionSchedule = _dbContext.SubscriptionSchedule.Where(s => s.SubSchID.ToString() == SubscriptionScheduleID); _dbContext.SubscriptionSchedule.RemoveRange(SubscriptionSchedule); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string ModeName) { var ModeTypes = _dbContext.ModeTypes.Where(s => s.ModeName.ToString() == ModeName); _dbContext.ModeTypes.RemoveRange(ModeTypes); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult Delete(string FamilyMemberDetailsID) { var FamilyMemberDetails = _dbContext.FamilyMemberDetails.Where(s => s.FamMembID.ToString() == FamilyMemberDetailsID); _dbContext.FamilyMemberDetails.RemoveRange(FamilyMemberDetails); _dbContext.SaveChanges(); return(Ok()); }
public async Task EnsureSeedData() { _context.Database.EnsureCreated(); _context.Users.RemoveRange(_context.Users); _context.SaveChanges(); if (_context.Users.Any()) { return; // DB is seeded. } // Creates Roles. await _roleManager.CreateAsync(new IdentityRole("administrator")); await _roleManager.CreateAsync(new IdentityRole("user")); // Adds Roles to Role Claims. var adminRole = await _roleManager.FindByNameAsync("administrator"); var userRole = await _roleManager.FindByNameAsync("user"); await _roleManager.AddClaimAsync(adminRole, new Claim(JwtClaimTypes.Role, "administrator")); await _roleManager.AddClaimAsync(userRole, new Claim(JwtClaimTypes.Role, "user")); // Seeds an admin user. var user = new IdentityUser { AccessFailedCount = 0, Email = "*****@*****.**", EmailConfirmed = false, LockoutEnabled = true, NormalizedEmail = "*****@*****.**", NormalizedUserName = "******", TwoFactorEnabled = false, UserName = "******" }; var result = await _userManager.CreateAsync(user, "admin"); if (result.Succeeded) { var adminUser = await _userManager.FindByNameAsync(user.UserName); // Assigns the 'administrator' role. await _userManager.AddToRoleAsync(adminUser, "administrator"); // Assigns claims. var claims = new List <Claim> { new Claim(type: JwtClaimTypes.Name, value: user.UserName), new Claim(type: JwtClaimTypes.Email, value: user.Email), }; await _userManager.AddClaimsAsync(adminUser, claims); } }
public ActionResult Create(Food food) { try { if (ModelState.IsValid) { db.Foods.Add(food); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log.) ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } return(View(food)); }
public IActionResult Post([FromBody] Category category) { var stream = new MemoryStream(category.ImageArray); var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; var folder = "wwwroot"; var response = FilesHelper.UploadImage(stream, folder, file); if (!response) { return(BadRequest()); } else { category.ImageUrl = file; _dbContext.Categories.Add(category); _dbContext.SaveChanges(); return(StatusCode(StatusCodes.Status201Created)); } }
public IActionResult Delete(int id) { var category = _dbContext.Categories.Find(id); if (category == null) { return(NotFound("No category found against this id...")); } else { _dbContext.Categories.Remove(category); _dbContext.SaveChanges(); return(Ok("Category deleted...")); } }
public IActionResult Delete(int id) { var product = _dbContext.Products.Find(id); if (product == null) { return(NotFound("No product found against this id...")); } else { _dbContext.Products.Remove(product); _dbContext.SaveChanges(); return(Ok("Product deleted...")); } }
private static void CreateAdminRole(FoodDbContext context, RoleManager <IdentityRole> _roleManager, UserManager <IdentityUser> _userManager) { bool roleExists = _roleManager.RoleExistsAsync("Admin").Result; if (roleExists) { return; } var role = new IdentityRole() { Name = "Admin" }; _roleManager.CreateAsync(role).Wait(); var userRole = new IdentityRole() { Name = "User" }; _roleManager.CreateAsync(userRole).Wait(); var user = new IdentityUser() { UserName = "******", Email = "*****@*****.**" }; string adminPassword = "******"; var userResult = _userManager.CreateAsync(user, adminPassword).Result; if (userResult.Succeeded) { _userManager.AddToRoleAsync(user, "Admin").Wait(); } context.SaveChanges(); }
public int Commit() { return(db.SaveChanges()); }
public int Add(Restaurant restaurant) { _dbContext.Restaurants.Add(restaurant); _dbContext.SaveChanges(); return(restaurant.RestaurantId); }
public bool Save() { return(_foodDbContext.SaveChanges() >= 0); }
public async Task EnsureSeedData(UserManager <IdentityUser> userManager, RoleManager <IdentityRole> roleManager, ILoggerFactory loggerFactory, FoodDbContext context) { context.Database.EnsureCreated(); context.Users.RemoveRange(context.Users); context.SaveChanges(); if (context.Users.Any()) { return; // Db has been seeded. } // Creates Roles. await roleManager.CreateAsync(new IdentityRole("administrator")); await roleManager.CreateAsync(new IdentityRole("user")); // Adds Roles to Role Claims. var adminRole = await roleManager.FindByNameAsync("administrator"); var userRole = await roleManager.FindByNameAsync("user"); await roleManager.AddClaimAsync(adminRole, new Claim(JwtClaimTypes.Role, "administrator")); await roleManager.AddClaimAsync(userRole, new Claim(JwtClaimTypes.Role, "user")); // Seeds an admin user. var user = new IdentityUser { AccessFailedCount = 0, Email = "*****@*****.**", EmailConfirmed = false, LockoutEnabled = true, NormalizedEmail = "*****@*****.**", NormalizedUserName = "******", TwoFactorEnabled = false, UserName = "******" }; var result = await userManager.CreateAsync(user, "admin"); if (result.Succeeded) { var adminUser = await userManager.FindByNameAsync(user.UserName); // Assigns the 'administrator' role. await userManager.AddToRoleAsync(adminUser, "administrator"); // Assigns claims. var claims = new List <Claim> { new Claim(type: JwtClaimTypes.Name, value: user.UserName), new Claim(type: JwtClaimTypes.Email, value: user.Email), }; await userManager.AddClaimsAsync(adminUser, claims); } context.FoodItems.Add(new FoodItem() { Calories = 1000, Name = "Lasagne", Created = DateTime.Now }); context.FoodItems.Add(new FoodItem() { Calories = 1100, Name = "Hamburger", Created = DateTime.Now }); context.FoodItems.Add(new FoodItem() { Calories = 1200, Name = "Spaghetti", Created = DateTime.Now }); context.FoodItems.Add(new FoodItem() { Calories = 1300, Name = "Pizza", Created = DateTime.Now }); }