public static void SeedDishesTest(UlloContext context, ApplicationUser firstUser) { context.Database.ExecuteSqlCommand("DELETE FROM Categories DBCC CHECKIDENT ('dbo.Categories', RESEED, 1)"); context.Database.ExecuteSqlCommand("DELETE FROM Dishes DBCC CHECKIDENT ('dbo.Dishes', RESEED, 1)"); context.Database.ExecuteSqlCommand("DELETE FROM Posts DBCC CHECKIDENT ('dbo.Posts', RESEED, 1)"); context.Database.ExecuteSqlCommand("DELETE FROM Pictures DBCC CHECKIDENT ('dbo.Pictures', RESEED, 1)"); context.Database.ExecuteSqlCommand("DELETE FROM Votes"); string[] categoryNames = { "Piadine", "Primi piatti in promo", "Primi piatti", "Secondi piatti caldi", "Secondi piatti di pesce caldi e freddi", "Secondi piatti freddi", "Insalatoni", "Insalatoni fino a 5 ingredienti", "Contorni freddi", "Contorni caldi ordinabili entro le 11:00", "Desserts" }; List <Category> categories = new List <Category>(); foreach (string category in categoryNames) { categories.Add(new Category { Name = category, Route = IdentityModels.getNameAsRoute(category) }); } context.Categories.AddOrUpdate(categories.ToArray()); context.SaveChanges(); List <Dish> dishes = new List <Dish>(); string[] dishNames = { "Penne con Sugo di Salsiccia e Panna", "Spaghetti con Pesto alla Genovese", "Spaghetti con Aglio, Olio e Peperoncino" }; foreach (string dish in dishNames) { Dish d = new Dish { Name = dish, Route = IdentityModels.getNameAsRoute(dish), Price = 3.20M, Created = DateTime.Now }; d.User = firstUser; d.Categories.Add(categories[1]); d.Pictures.Add(new Picture { Name = dish, Route = String.Format("/Media/{0}.jpg", IdentityModels.getNameAsRoute(dish)), Guid = Guid.NewGuid(), Created = DateTime.Now, AssetType = Picture.assetTypeEnum.Picture }); dishes.Add(d); } context.Dishes.AddOrUpdate(dishes.ToArray()); context.SaveChanges(); for (int i = 1; i <= 100; i++) { // int r = randomizer.Next(dishes.Count); Dish randomDish = dishes.ElementAt(i % dishes.Count); Post p = new Post { Created = DateTime.Now }; p.User = firstUser; p.Dish = randomDish; p.Picture = randomDish.Pictures.ElementAt(0); context.Posts.AddOrUpdate(p); } context.SaveChanges(); }
public async Task <HttpResponseMessage> Signup(UserSignUp model) { if (model == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No Data")); } if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, FacebookId = model.FacebookId, FacebookPicture = model.FacebookPicture, FacebookToken = model.FacebookToken, Route = IdentityModels.getNameAsRoute(model.UserName), }; var result = await users.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInHelper.SignInAsync(user, true, false); var account = new UserDetail(); account.UserName = user.UserName; account.FirstName = user.FirstName; account.LastName = user.LastName; account.FacebookId = user.FacebookId; account.isAuthenticated = true; account.Route = user.Route; var rolesForUser = users.GetRoles(user.Id); account.isAdmin = rolesForUser.Contains("Admin"); return(Request.CreateResponse(account)); // var code = await users.GenerateEmailConfirmationTokenAsync(user.Id); // send email confirmation token } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, result.Errors.FirstOrDefault())); } } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); }
public static void SeedDishes(UlloContext context, ApplicationUser firstUser) { context.Database.ExecuteSqlCommand("DELETE FROM Categories DBCC CHECKIDENT ('dbo.Categories', RESEED, 1)"); context.Database.ExecuteSqlCommand("DELETE FROM Dishes DBCC CHECKIDENT ('dbo.Dishes', RESEED, 1)"); context.Database.ExecuteSqlCommand("DELETE FROM Posts DBCC CHECKIDENT ('dbo.Posts', RESEED, 1)"); context.Database.ExecuteSqlCommand("DELETE FROM Pictures DBCC CHECKIDENT ('dbo.Pictures', RESEED, 1)"); context.Database.ExecuteSqlCommand("DELETE FROM Votes"); // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. /** CATEGORIES **/ List <Category> categories = new List <Category>(); /* * Category[] categories = { * new Category { Name = "Piadine", Route = Identity.getNameAsRoute("Piadine") }, * new Category { Name = "Primi piatti in promo" }, * new Category { Name = "Primi piatti" }, * new Category { Name = "Secondi piatti caldi" }, * new Category { Name = "Secondi piatti di pesce caldi e freddi" }, * new Category { Name = "Secondi piatti freddi" }, * new Category { Name = "Insalatoni" }, * new Category { Name = "Insalatoni fino a 5 ingredienti" }, * new Category { Name = "Contorni freddi" }, * new Category { Name = "Contorni caldi" }, * new Category { Name = "Contorni caldi ordinabili entro le 11:00" }, * new Category { Name = "Desserts" } * }; * context.Categories.AddOrUpdate(categories); */ string[] categoryNames = { "Piadine", "Primi piatti in promo", "Primi piatti", "Secondi piatti caldi", "Secondi piatti di pesce caldi e freddi", "Secondi piatti freddi", "Insalatoni", "Insalatoni fino a 5 ingredienti", "Contorni freddi", "Contorni caldi ordinabili entro le 11:00", "Desserts" }; foreach (string category in categoryNames) { categories.Add(new Category { Name = category, Route = IdentityModels.getNameAsRoute(category) }); } context.Categories.AddOrUpdate(categories.ToArray()); context.SaveChanges(); /** DISHES **/ /* * Dish[] dishes = { * new Dish { Name = "Penne con Sugo di Salsiccia e Panna", Price = 3.20M }, * new Dish { Name = "Spaghetti con Pesto alla Genovese", Price = 3.20M }, * new Dish { Name = "Spaghetti con Aglio, Olio e Peperoncino", Price = 3.20M } * }; * dishes[0].Categories.Add(categories[1]); * dishes[1].Categories.Add(categories[1]); * dishes[2].Categories.Add(categories[1]); * context.Dishes.AddOrUpdate(dishes); */ string[] dishNames = { "Penne con Sugo di Salsiccia e Panna", "Spaghetti con Pesto alla Genovese", "Spaghetti con Aglio, Olio e Peperoncino" }; foreach (string dish in dishNames) { Dish d = new Dish { Name = dish, Route = IdentityModels.getNameAsRoute(dish), Price = 3.20M, Created = DateTime.Now }; d.User = firstUser; d.Categories.Add(categories[1]); d.Pictures.Add(new Picture { Name = dish, Route = String.Format("/Media/{0}.jpg", IdentityModels.getNameAsRoute(dish)), Guid = Guid.NewGuid(), Created = DateTime.Now, AssetType = Picture.assetTypeEnum.Picture }); context.Dishes.AddOrUpdate(d); } context.SaveChanges(); }
public async Task <HttpResponseMessage> AddDish([FromBody] DishAddView model) { if (model == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No Data")); } var user = db.Users.SingleOrDefault(u => u.UserName == User.Identity.Name); if (user == null) { return(Request.CreateErrorResponse(HttpStatusCode.MethodNotAllowed, "Unknown User")); // throw new HttpResponseException(HttpStatusCode.MethodNotAllowed); } if (ModelState.IsValid) { Picture picture = Picture.getPictureFromBase64(model.PictureBase64); if (picture == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No Picture")); } try { foreach (Category category in model.Categories) { db.Categories.Attach(category); } decimal Price = 0.0M; Decimal.TryParse(model.Price.Replace(",", "."), NumberStyles.Currency, CultureInfo.InvariantCulture, out Price); Dish dish = new Dish { Categories = model.Categories, Name = model.Name, Route = IdentityModels.getNameAsRoute(model.Name), Price = Price, isVeganFriendly = model.isVeganFriendly, Created = DateTime.Now }; dish.Pictures.Add(picture); dish.User = user; db.Dishes.Add(dish); await db.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, new DishView { Id = dish.Id, Name = dish.Name, Price = dish.Price, isVeganFriendly = dish.isVeganFriendly, Yes = dish.Yes, No = dish.No, Categories = dish.Categories.Select(c => new CategoryView { Id = c.Id, Name = c.Name, }).ToList(), Pictures = dish.Pictures.Select(c => new PictureView { Id = c.Id, Name = c.Name, Route = c.Route, Created = c.Created, }).ToList(), })); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); }