private static void SeedSubCategory(AuctionSystemDbContext context) { context.SubCategories.AddAsync(new SubCategory { Id = DataConstants.SampleSubCategoryId }); context.SaveChangesAsync(); }
private static async Task SeedItems(AuctionSystemDbContext dbContext) { if (!dbContext.Items.Any()) { var random = new Random(); var allItems = new List <Item>(); foreach (var category in dbContext.Categories.Include(c => c.SubCategories)) { int i = 1; foreach (var subCategory in category.SubCategories) { var startTime = DateTime.UtcNow.AddDays(random.Next(0, 5)); var item = new Item { Description = $"Test Description_{i}", Title = $"Test Title_{i}", StartTime = startTime, EndTime = startTime.AddHours(random.Next(1, 10)), StartingPrice = random.Next(10, 500), MinIncrease = random.Next(1, 100), SubCategoryId = subCategory.Id, UserId = dbContext.Users.First().Id }; i++; allItems.Add(item); } } await dbContext.Items.AddRangeAsync(allItems); await dbContext.SaveChangesAsync(); } }
private static void SeedCategory(AuctionSystemDbContext context) { context.Categories.Add(new Category { Id = DataConstants.SampleCategoryId }); context.SaveChanges(); }
private static void SeedPictures(AuctionSystemDbContext context) { context.Pictures.AddAsync(new Picture { Id = DataConstants.SamplePictureId, ItemId = DataConstants.SampleItemId }); context.SaveChangesAsync(CancellationToken.None); }
private static async Task SeedRequiredData(AuctionSystemDbContext dbContext, UserManager <AuctionUser> userManager, RoleManager <IdentityRole> roleManager) { await SeedCategories(dbContext); await SeedDefaultRoles(roleManager); await SeedUsers(userManager, dbContext); await SeedItems(dbContext); }
public PictureService(AuctionSystemDbContext context, IOptions <CloudinaryOptions> options) : base(context) { this.options = options.Value; var account = new Account( this.options.CloudName, this.options.ApiKey, this.options.ApiSecret); this.cloudinary = new Cloudinary(account); }
public AuctionSystemDbContextTests() { this.dateTime = new DateTime(3001, 1, 1); this.dateTimeMock = new Mock <IDateTime>(); this.dateTimeMock.Setup(m => m.UtcNow).Returns(this.dateTime); this.currentUserServiceMock = new Mock <ICurrentUserService>(); this.currentUserServiceMock.Setup(m => m.UserId).Returns(SampleUserId); var options = new DbContextOptionsBuilder <AuctionSystemDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; this.context = new AuctionSystemDbContext(options, this.dateTimeMock.Object, this.currentUserServiceMock.Object); }
private static void SeedRefreshTokens(AuctionSystemDbContext context) { context.Users.ForEachAsync(user => { context.RefreshTokens.Add(new RefreshToken() { Token = Guid.NewGuid(), Used = false, UserId = user.Id, JwtId = Guid.NewGuid().ToString() }); }); context.SaveChanges(); }
private static void SeedItems(AuctionSystemDbContext context) { var item = new Item { Id = DataConstants.SampleItemId, Title = DataConstants.SampleItemTitle, Description = DataConstants.SampleItemDescription, StartingPrice = DataConstants.SampleItemStartingPrice, MinIncrease = DataConstants.SampleItemMinIncrease, StartTime = DateTime.UtcNow.AddDays(10), EndTime = DataConstants.SampleItemEndTime, UserId = context.Users.FirstOrDefault()?.Id, SubCategoryId = context.SubCategories.FirstOrDefault().Id }; context.Items.Add(item); context.SaveChanges(); }
public static AuctionSystemDbContext Create() { var currentUserServiceMock = new Mock <ICurrentUserService>(); currentUserServiceMock.Setup(m => m.UserId).Returns(DataConstants.SampleUserId); var options = new DbContextOptionsBuilder <AuctionSystemDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var context = new AuctionSystemDbContext(options, DateTime, currentUserServiceMock.Object); context.Database.EnsureCreated(); SeedUsers(context); SeedCategory(context); SeedSubCategory(context); SeedItems(context); SeedPictures(context); SeedRefreshTokens(context); return(context); }
private static void SeedUsers(AuctionSystemDbContext context) { context.Users.AddRange( new AuctionUser { Id = DataConstants.SampleUserId, Email = "*****@*****.**", FullName = "Test Testov", UserName = "******", EmailConfirmed = true }, new AuctionUser { Id = DataConstants.SampleAdminUserId, Email = "*****@*****.**", FullName = "Admin admin", UserName = "******", EmailConfirmed = true }); context.Roles.Add(new IdentityRole(AppConstants.AdministratorRole)); context.SaveChanges(); }
private static async Task SeedUsers(UserManager <AuctionUser> userManager, AuctionSystemDbContext dbContext) { if (!dbContext.Users.Any()) { var allUsers = new List <AuctionUser>(); for (int i = 1; i <= 2; i++) { var user = new AuctionUser { Email = $"test{i}@test.com", FullName = $"Test Testov{i}", UserName = $"test{i}@test.com", EmailConfirmed = true }; allUsers.Add(user); } foreach (var user in allUsers) { await userManager.CreateAsync(user, "test123"); } var admin = new AuctionUser { Email = "*****@*****.**", FullName = "Admin Adminski", UserName = "******", EmailConfirmed = true }; await userManager.CreateAsync(admin, "admin123"); await userManager.AddToRoleAsync(admin, WebConstants.AdministratorRole); } }
private static async Task SeedCategories(AuctionSystemDbContext dbContext) { if (!dbContext.Categories.Any()) { var categories = File.ReadAllText(WebConstants.CategoriesPath); var deserializedCategoriesWithSubCategories = JsonConvert.DeserializeObject <CategoryDto[]>(categories); var allCategories = deserializedCategoriesWithSubCategories.Select(deserializedCategory => new Category { Name = deserializedCategory.Name, SubCategories = deserializedCategory.SubCategoryNames.Select(deserializedSubCategory => new SubCategory { Name = deserializedSubCategory.Name }).ToList() }).ToList(); await dbContext.AddRangeAsync(allCategories); await dbContext.SaveChangesAsync(); } }
public JobManager(AuctionSystemDbContext dbContext, IEmailSender emailSender, ILogger <JobManager> logger) { this.dbContext = dbContext; this.emailSender = emailSender; this.logger = logger; }
public BidServiceTests() { this.dbContext = base.DatabaseInstance; this.bidService = new BidService(this.dbContext); }
public UserService(AuctionSystemDbContext context) : base(context) { }
public ItemsService(AuctionSystemDbContext context, IPictureService pictureService) : base(context) { this.pictureService = pictureService; }
public CategoriesService(AuctionSystemDbContext context) : base(context) { }
public static void Destroy(AuctionSystemDbContext context) { context.Database.EnsureDeleted(); context.Dispose(); }
protected BaseService(AuctionSystemDbContext context) { this.Context = context; }
public static void Initialize() { Database.SetInitializer(new MigrateDatabaseToLatestVersion <AuctionSystemDbContext, Configuration>()); AuctionSystemDbContext.Create().Database.Initialize(true); }
public UserServiceTests() { this.dbContext = base.DatabaseInstance; this.userService = new UserService(this.dbContext); }
public CategoriesServiceTests() { this.dbContext = base.DatabaseInstance; this.categoriesService = new CategoriesService(this.dbContext); }
public ItemsServiceTests() { this.dbContext = base.DatabaseInstance; this.itemsService = new ItemsService(this.dbContext, Mock.Of <IPictureService>()); }