private static void SeedDatabase() { var context = new OnlineShopContext(); var userStore = new UserStore<ApplicationUser>(context); var userManager = new ApplicationUserManager(userStore); var user = new ApplicationUser() { UserName = "******", Email = "*****@*****.**" }; var result = userManager.CreateAsync(user, "Test123%").Result; if (!result.Succeeded) { Assert.Fail(string.Join(Environment.NewLine, result.Errors)); } context.Categories.Add(new Category() { Name = "Auto" }); context.Categories.Add(new Category() { Name = "Education" }); context.Categories.Add(new Category() { Name = "home and Family" }); context.AdTypes.Add(new AdType() { Name = "Economy", PricePerDay = 10 }); context.AdTypes.Add(new AdType() { Name = "Normal", PricePerDay = 20 }); context.AdTypes.Add(new AdType() { Name = "Lux", PricePerDay = 40 }); context.SaveChanges(); }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value == null) { return new ValidationResult("Missing categories."); } List<int> categories = value as List<int>; if (categories.Count < 1 || categories.Count > 3) { return new ValidationResult("Categories should be at least 1 and no more than 3."); } using (OnlineShopContext data = new OnlineShopContext()) { var categoriesInDb = data.Categories .Select(c => c.Id) .ToList(); foreach (var category in categories) { if (!categoriesInDb.Contains(category)) { return new ValidationResult("There is invalid category Id."); } } } return ValidationResult.Success; }
private static void CleanDatabase() { var context = new OnlineShopContext(); context.Ads.Delete(); context.AdTypes.Delete(); context.Users.Delete(); context.Categories.Delete(); }
static void Main() { //var migstrat = new MigrateDatabaseToLatestVersion<OnlineShopContext, Configuration>(); //Database.SetInitializer(migstrat); var context = new OnlineShopContext(); //var booksCount = context.Ads.CountAsync(); //Console.WriteLine(booksCount); var cat = context.Categories.Select(c => new { id = c.Id, name = c.Name }); foreach (var categoryId in cat) { Console.WriteLine(); } }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { int id = (int)value; if (id == 0) { return new ValidationResult("Missing ad type."); } using (OnlineShopContext data = new OnlineShopContext()) { var adType = data.AdTypes.Find(id); if (adType == null) { return new ValidationResult("No such ad type in database."); } } return ValidationResult.Success; }
public BaseApiController(OnlineShopContext data) { this.Data = data; }
public static void Main() { var context = new OnlineShopContext(); context.Users.Count(); }
private static void 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(); }
private static void SeedCategories(OnlineShopContext context) { context.Categories.Add(new Category() { Name = "cat1" }); context.Categories.Add(new Category() { Name = "cat2" }); context.Categories.Add(new Category() { Name = "cat3" }); context.Categories.Add(new Category() { Id = 4, Name = "cat4" }); }
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 SeedDatabase() { var context = new OnlineShopContext(); var userStore = new UserStore<ApplicationUser>(context); var userManager = new ApplicationUserManager(userStore); var user = new ApplicationUser { UserName = TestUserUsername, Email = "*****@*****.**" }; var result = userManager.CreateAsync(user, TestUserPassword).Result; if (!result.Succeeded) { Assert.Fail(string.Join(Environment.NewLine, result.Errors)); } SeedCategories(context); SeedAdTypes(context); }
public BaseApiController(OnlineShopContext context) { this.Context = context; }
public void CreateNewAd_InvalidNumberOfCategories_ShouldReturnBadRequest() { var context = new OnlineShopContext(); var category = context.Categories.First(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("name","OpelAstra"), new KeyValuePair<string, string>("description","...."), new KeyValuePair<string, string>("price","1565"), new KeyValuePair<string, string>("typeId","-1"), new KeyValuePair<string, string>("categories[0]",category.Id.ToString()), new KeyValuePair<string, string>("categories[1]",category.Id.ToString()), new KeyValuePair<string, string>("categories[2]",category.Id.ToString()), new KeyValuePair<string, string>("categories[3]",category.Id.ToString()) }); var response = PostNewAd(data); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void Posting_Ad_With_More_Than_3_Categories_Should_Return_Bad_Request() { var context = new OnlineShopContext(); var categories = context.Categories.ToList(); var adType = context.AdTypes.First(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("name", "Opel Atra"), new KeyValuePair<string, string>("description", "some description"), new KeyValuePair<string, string>("price", "2000"), new KeyValuePair<string, string>("typeId", adType.Id.ToString()), new KeyValuePair<string, string>("categories[0]", categories[0].Id.ToString()), new KeyValuePair<string, string>("categories[1]", categories[1].Id.ToString()), new KeyValuePair<string, string>("categories[2]", categories[2].Id.ToString()), new KeyValuePair<string, string>("categories[3]", categories[3].Id.ToString()), }); var response = this.PostNewAd(data); var ads = context.Ads.ToList(); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void Posting_Ad_Without_Categories_Should_Return_BadRequest() { var context = new OnlineShopContext(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("name","Opel Astra"), new KeyValuePair<string, string>("description","..."), new KeyValuePair<string, string>("price","2000"), new KeyValuePair<string, string>("typeId","1"), }); var response = this.PostNewAd(data); Assert.AreEqual(HttpStatusCode.BadRequest,response.StatusCode); }
public void Posting_Valid_Ad_Should_Return_200OK_And_Create_Ad() { var context = new OnlineShopContext(); var category = context.Categories.First(); var adType = context.AdTypes.First(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("name", "Opel Astra"), new KeyValuePair<string, string>("description", "some description"), new KeyValuePair<string, string>("price", "2000"), new KeyValuePair<string, string>("typeId", adType.Id.ToString()), new KeyValuePair<string, string>("categories[0]", category.Id.ToString()) }); var response = this.PostNewAd(data); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var ads = context.Ads.ToList(); var newAdd = ads[ads.Count - 1]; Assert.AreEqual(3, ads.Count); Assert.AreEqual("Opel Astra", newAdd.Name); Assert.AreEqual("some description", newAdd.Description); Assert.AreEqual(2000, newAdd.Price); }
public void Posting_Ad_With_More_Than_3_Categories_Should_Return_BadRequest() { var context = new OnlineShopContext(); var categorie1 = context.Categories.First(); var categorie2 = context.Categories.OrderBy(c=>c.Id).Skip(1).Take(1).Single(); var categorie3 = context.Categories.OrderBy(c=>c.Id).Skip(2).Take(1).Single(); var categorie4 = context.Categories.OrderBy(c => c.Id).Skip(3).Take(1).Single(); var adType = context.AdTypes.First(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("name","Opel Astra"), new KeyValuePair<string, string>("description","..."), new KeyValuePair<string, string>("price","2000"), new KeyValuePair<string, string>("typeId",adType.Id.ToString()), new KeyValuePair<string, string>("categories[0]",categorie1.Id.ToString()), new KeyValuePair<string, string>("categories[1]",categorie2.Id.ToString()), new KeyValuePair<string, string>("categories[2]",categorie3.Id.ToString()), new KeyValuePair<string, string>("categories[3]",categorie4.Id.ToString()) }); var response = this.PostNewAd(data); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
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(); }
private static void 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(); }
public void Posting_Ad_With_Invalid_AdType_Should_Return_Bad_Request() { var context = new OnlineShopContext(); var category = context.Categories.First(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("name", "Opel Astra"), new KeyValuePair<string, string>("description", "some description"), new KeyValuePair<string, string>("price", "2000"), new KeyValuePair<string, string>("typeId", "-1"), // invalid Id new KeyValuePair<string, string>("categories[0]", category.Id.ToString()) }); var response = this.PostNewAd(data); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
private static void SeedAdTypes(OnlineShopContext context) { context.AdTypes.Add(new AdType() { Name = "Premium", Index = 300, PricePerDay = 1234 }); context.AdTypes.Add(new AdType() { Name = "Diamond", Index = 400, PricePerDay = 2315 }); context.AdTypes.Add(new AdType() { Name = "Normal", Index = 100, PricePerDay = 4516 }); }