public static PleyContext Create() { // Create a fresh service provider, and therefore a fresh // InMemory database instance. var serviceProvider = new ServiceCollection() .AddEntityFrameworkInMemoryDatabase() .BuildServiceProvider(); var options = new DbContextOptionsBuilder <PleyContext>() .UseInMemoryDatabase("Test Database") .UseInternalServiceProvider(serviceProvider) .EnableSensitiveDataLogging() .Options; var context = new PleyContext(options); // Seeding the test context if (!context.Users.Any()) { context.Users.Add(Fixtures.reviewer1); context.Users.Add(Fixtures.reviewer2); context.Users.Add(Fixtures.owner1); context.Users.Add(Fixtures.owner2); } if (!context.Restaurants.Any()) { context.Restaurants.Add(Fixtures.restaurant1); context.Restaurants.Add(Fixtures.restaurant1); } if (!context.Reviews.Any()) { context.Reviews.Add(Fixtures.review1); context.Reviews.Add(Fixtures.review2); } context.SaveChanges(); return(context); }
public Restaurant Create(Restaurant restaurant) { if (restaurant.Owner != null) { var owner = _dbContext.Users.Find(restaurant.Owner.Id); if (owner == null) { throw new PleyNotFoundException("Owner not found"); } restaurant.Owner = owner; } restaurant.CreatedOn = DateTime.Now; restaurant.ModifiedOn = DateTime.Now; _dbContext.Restaurants.Add(restaurant); _dbContext.SaveChanges(); return(restaurant); }
public User CreateUser(User user, string password) { if (String.IsNullOrWhiteSpace(password)) { throw new PleyException("Password cannot be blank"); } if (_dbContext.Users.Any(u => u.Email == user.Email)) { throw new PleyException("User already exists."); } (user.PasswordSalt, user.PasswordHash) = CreatePasswordHash(password); user.CreatedOn = DateTime.Now; user.ModifiedOn = DateTime.Now; _dbContext.Users.Add(user); _dbContext.SaveChanges(); return(user); }
public Review Create(Review review) { if (review.Restaurant == null) { throw new ArgumentException("Need to provide restaurant id"); } if (_dbContext.Restaurants.Find(review.Restaurant.Id) == null) { throw new PleyNotFoundException("Restaurant for review not found"); } if (_dbContext.Users.Find(review.Reviewer.Id) == null) { throw new PleyNotFoundException("User for review not found"); } review.CreatedOn = DateTime.Now; review.ModifiedOn = DateTime.Now; _dbContext.Reviews.Add(review); _dbContext.SaveChanges(); _restaurantSvc.UpdateAverageRating(review.Restaurant.Id); return(review); }
public static void Initialize(PleyContext context) { context.Database.EnsureCreated(); if (!context.Users.Any()) { var hmac = new System.Security.Cryptography.HMACSHA512(); context.Users.Add(new User { FirstName = "Olivier", LastName = "Example", Email = "*****@*****.**", CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now, Type = UserType.ADMIN, PasswordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes("password")), PasswordSalt = hmac.Key }); hmac.Dispose(); context.SaveChanges(); } }