public async Task <bool> Like(int userID, int reviewID) { try { var like = new Like() { User = await this._context.Users.FindAsync(userID), Review = await this._context.Reviews.FindAsync(reviewID) }; await _context.Likes.AddAsync(like); await _context.SaveChangesAsync(); var review = await this._context.Reviews.FindAsync(reviewID); review.LikesCount = await this._context.Likes.Where(l => l.ReviewID == reviewID).CountAsync(); _context.Update(review); await _context.SaveChangesAsync(); return(true); } catch (Exception) { return(false); } }
public async Task DeleteAsync_ShouldDeleteRecordBeersOfStyleIfExist() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldDeleteRecordBeersOfStyleIfExist"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", Breweries = new List <Brewery>() { new Brewery() { Name = "Brewery", } } }; context.Countries.Add(country); await context.SaveChangesAsync(); var beer = new Beer() { Name = "Carlsberg", ABV = 5, Country = await context.Countries.FirstOrDefaultAsync(c => c.Name == "Bulgaria"), Brewery = await context.Breweries.FirstOrDefaultAsync(b => b.Name == "Brewery"), Style = new BeerStyle() { Name = "Ale", Description = "Some description", }, Rating = 5 }; context.Beers.Add(beer); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BeerStylesService(context); await sut.DeleteAsync(1); var dbresult = await context.BeerStyles.FirstOrDefaultAsync(c => c.Name == "Ale"); var dbBeerResult = await context.Beers.FirstOrDefaultAsync(b => b.StyleID == dbresult.ID); //Assert Assert.AreEqual(dbresult.Name, "Ale"); Assert.AreEqual(dbresult.DeletedOn, dbresult.ModifiedOn); Assert.AreEqual(dbresult.IsDeleted, true); Assert.AreEqual(dbBeerResult.Name, "Carlsberg"); Assert.AreEqual(dbBeerResult.DeletedOn, dbBeerResult.ModifiedOn); Assert.AreEqual(dbBeerResult.IsDeleted, true); } }
public async Task GetAllAsync_ShouldReturnNullBreweryIfModelHasNoNameAsync() { //Arrange var options = InMemory.GetOptions("GetAllAsync_ShouldReturnNullBreweryIfModelHasNoNameAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", Breweries = new List <Brewery>() { new Brewery() } }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BreweryServices(context); var result = await sut.GetAllAsync(); //Assert Assert.AreEqual(result, null); } }
public async Task DeleteAsync_ShouldReturnTrueStyleIfSucceded() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldReturnTrueStyleIfSucceded"); using (var context = new BOContext(options)) { var style = new BeerStyle() { Name = "Ale", Description = "This description" }; context.BeerStyles.Add(style); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BeerStylesService(context); var result = await sut.DeleteAsync(1); //Assert Assert.AreEqual(result, true); } }
public async Task GetAsync_ShouldReturnBreweryDTOAsync() { //Arrange var options = InMemory.GetOptions("GetAsync_ShouldReturnBreweryDTOAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", Breweries = new List <Brewery>() { new Brewery() { Name = "Brewery", Beers = new List <Beer>() } } }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BreweryServices(context); var result = await sut.GetAsync(1); //Assert Assert.IsInstanceOfType(result, typeof(BreweryDTO));; } }
public async Task DeleteAsync_ShouldDeleteStyleIfExist() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldDeleteStyleIfExist"); using (var context = new BOContext(options)) { var style = new BeerStyle() { Name = "Ale", Description = "This description" }; context.BeerStyles.Add(style); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BeerStylesService(context); await sut.DeleteAsync(1); var dbresult = await context.BeerStyles.FirstOrDefaultAsync(c => c.Name == "Ale"); //Assert Assert.AreEqual(dbresult.Name, "Ale"); Assert.AreEqual(dbresult.DeletedOn, dbresult.ModifiedOn); Assert.AreEqual(dbresult.IsDeleted, true); } }
public async Task DeleteAsync_ShouldReturnTrueIfSucceded() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldReturnTrueIfSucceded"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria" }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new CountriesService(context); var result = await sut.DeleteAsync(1); //Assert Assert.AreEqual(result, true); } }
public async Task UpdateAsync_ShouldChangeNameOfCountryAsync() { //Arrange var options = InMemory.GetOptions("UpdateAsync_ShouldChangeNameOfCountryAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria" }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { var countryDTO = new CountryDTO() { Name = "Belgium" }; //Act var sut = new CountriesService(context); var result = await sut.UpdateAsync(1, countryDTO); var dbresult = await context.Countries.FindAsync(1); //Assert Assert.AreEqual(dbresult.Name, "Belgium"); } }
public async Task GetAllAsync_ShouldReturnIEnumerableCountryDTOAsync() { //Arrange var options = InMemory.GetOptions("GetAllAsync_ShouldReturnIEnumerableCountryDTOAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria" }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new CountriesService(context); var result = await sut.GetAllAsync(); //Assert Assert.IsInstanceOfType(result, typeof(IEnumerable <CountryDTO>)); } }
public async Task DeleteAsync_ShouldDeleteCountryIfExist() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldDeleteCountryIfExist"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria" }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new CountriesService(context); await sut.DeleteAsync(1); var dbresult = await context.Countries.FirstOrDefaultAsync(c => c.Name == "Bulgaria"); //Assert Assert.AreEqual(dbresult.Name, "Bulgaria"); Assert.AreEqual(dbresult.DeletedOn, dbresult.ModifiedOn); Assert.AreEqual(dbresult.IsDeleted, true); } }
public async Task CreateAsync_ShouldUndeleteRecordIfExist() { //Arrange var options = InMemory.GetOptions("CreateAsync_ShouldUndeleteRecordIfExist"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", DeletedOn = DateTime.UtcNow, IsDeleted = true }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { var countryDTO = new CountryDTO() { Name = "Bulgaria" }; //Act var sut = new CountriesService(context); await sut.CreateAsync(countryDTO); var dbresult = await context.Countries.FirstOrDefaultAsync(c => c.Name == "Bulgaria"); //Assert Assert.AreEqual(dbresult.Name, "Bulgaria"); Assert.AreEqual(dbresult.DeletedOn, null); Assert.AreEqual(dbresult.IsDeleted, false); } }
private static async Task SeedBeersAsync(BOContext context) { if (context.Beers.Any()) { return; } var beerNames = new[] { "Carlsberg", "Shumensko", "Pirinsko" }; foreach (var beer in beerNames) { var b = new Beer() { Name = beer, Country = await context.Countries.FirstOrDefaultAsync(c => c.Name == "Bulgaria"), Brewery = await context.Breweries.FirstOrDefaultAsync(b => b.Name == "Carlsberg"), ABV = 4, Style = await context.BeerStyles.FindAsync(1), CreatedOn = DateTime.UtcNow }; b.CountryID = b.Country.ID; b.BreweryID = b.Brewery.ID; b.StyleID = b.Style.ID; await context.Beers.AddAsync(b); } await context.SaveChangesAsync(); }
public async Task DeleteAsync_ShouldReturnTrueIfReviewSucceded() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldReturnTrueIfSucceded"); using (var context = new BOContext(options)) { var review = new Review { Description = "Great", Beer = new Beer() { Name = "Carlsberg" }, User = new User() { Name = "SuperMan" }, }; context.Reviews.Add(review); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new ReviewsService(context); var result = await sut.DeleteAsync(1); //Assert Assert.AreEqual(result, true); } }
public async Task CreateAsync_ShouldRecordBreweryIfNotOnRecordAsync() { //Arrange var options = InMemory.GetOptions("CreateAsync_ShouldRecordBreweryIfNotOnRecordAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria" }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { var breweryDTO = new BreweryDTO() { Name = "Brewery", Country = "Bulgaria" }; //Act var sut = new BreweryServices(context); await sut.CreateAsync(breweryDTO); var dbresult = await context.Breweries.Include(b => b.Country).FirstOrDefaultAsync(c => c.Name == "Brewery"); //Assert Assert.AreEqual(dbresult.Name, "Brewery"); } }
public async Task UpdateAsync_ShouldChangeNameOfStyleAndDescriptionAsync() { //Arrange var options = InMemory.GetOptions("UpdateAsync_ShouldChangeNameOfStyleAndDescriptionAsync"); using (var context = new BOContext(options)) { var style = new BeerStyle() { Name = "Ale", Description = "This description" }; context.BeerStyles.Add(style); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { var styleDTO = new BeerStyleDTO() { Name = "Ale2", Description = "This description2" }; //Act var sut = new BeerStylesService(context); await sut.UpdateAsync(1, styleDTO); var dbresult = await context.BeerStyles.FindAsync(1); //Assert Assert.AreEqual(dbresult.Name, "Ale2"); Assert.AreEqual(dbresult.Description, "This description2"); } }
public async Task GetAllAsync_ShouldReturnIEnumerableReviewDTOAsync() { //Arrange var options = InMemory.GetOptions("GetAllAsync_ShouldReturnIEnumerableReviewDTOAsync"); using (var context = new BOContext(options)) { var review = new Review { Description = "Great", Beer = new Beer() { Name = "Carlsberg" }, User = new User() { Name = "SuperMan" } }; context.Reviews.Add(review); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new ReviewsService(context); var result = await sut.GetAllAsync(); //Assert Assert.IsInstanceOfType(result, typeof(IEnumerable <ReviewDTO>)); } }
public async Task GetAllAsync_ShouldReturnIEnumerableBeerStyleDTOAsync() { //Arrange var options = InMemory.GetOptions("GetAllAsync_ShouldReturnIEnumerableBeerStyleDTOAsync"); using (var context = new BOContext(options)) { var style = new BeerStyle() { Name = "Ale", Description = "This description" }; context.BeerStyles.Add(style); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BeerStylesService(context); var result = await sut.GetAllAsync(); //Assert Assert.IsInstanceOfType(result, typeof(IEnumerable <BeerStyleDTO>)); } }
public async Task DeleteAsync_ShouldReturnTrueIfCommentSucceded() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldReturnTrueIfCommentSucceded"); using (var context = new BOContext(options)) { var comment = new Comment { Description = "Gotham", User = new User() { Name = "Batman" }, Review = new Review() { Description = "Description" } }; context.Comments.Add(comment); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new CommentService(context); var result = await sut.DeleteAsync(1); //Assert Assert.AreEqual(result, true); } }
public async Task DeleteAsync_ShouldDeleteRecordCommentsIfExist() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldDeleteRecordCommentsIfExist"); using (var context = new BOContext(options)) { var review = new Review { Description = "Great", Beer = new Beer() { Name = "Carlsberg" }, User = new User() { Name = "SuperMan" }, Comments = new List <Comment>() { new Comment() { Description = "Some description", User = new User() { Name = "Batman" }, } } }; context.Reviews.Add(review); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new ReviewsService(context); await sut.DeleteAsync(1); var dbresult = await context.Reviews.FindAsync(1); var dbCommentResult = await context.Comments.FindAsync(1); //Assert Assert.AreEqual(dbresult.Description, "Great"); Assert.AreEqual(dbresult.DeletedOn, dbresult.ModifiedOn); Assert.AreEqual(dbresult.IsDeleted, true); Assert.AreEqual(dbCommentResult.Description, "Some description"); Assert.AreEqual(dbCommentResult.DeletedOn, dbCommentResult.ModifiedOn); Assert.AreEqual(dbCommentResult.IsDeleted, true); } }
public async Task DeleteAsync_ShouldDeleteBeerIfExist() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldDeleteBeerIfExist"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", Breweries = new List <Brewery>() { new Brewery() { Name = "Brewery", Beers = new List <Beer>() { new Beer() { ABV = 4.5f, Rating = 2, Name = "Carlsberg", Country = new Country() { Name = "Germany" }, Style = new BeerStyle() { Name = "Ale" } } } } } };; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BeerService(context); await sut.DeleteAsync(1); var dbresult = await context.Beers.FirstOrDefaultAsync(c => c.Name == "Carlsberg"); //Assert Assert.AreEqual(dbresult.Name, "Carlsberg"); Assert.AreEqual(dbresult.DeletedOn, dbresult.ModifiedOn); Assert.AreEqual(dbresult.IsDeleted, true); } }
public async Task DeleteAsync_ShouldReturnTrueIfBeerSucceded() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldReturnTrueIfBeerSucceded"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", Breweries = new List <Brewery>() { new Brewery() { Name = "Brewery", Beers = new List <Beer>() { new Beer() { ABV = 4.5f, Rating = 2, Name = "Carlsberg", Country = new Country() { Name = "Germany" }, Style = new BeerStyle() { Name = "Ale" } } } } } }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BeerService(context); var result = await sut.DeleteAsync(1); //Assert Assert.AreEqual(result, true); } }
public async Task GetAllAsync_ShouldReturnIEnumerableBeerDTOAsync() { //Arrange var options = InMemory.GetOptions("GetAllAsync_ShouldReturnIEnumerableBeerDTOAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", Breweries = new List <Brewery>() { new Brewery() { Name = "Brewery", Beers = new List <Beer>() { new Beer() { ABV = 4.5f, Rating = 2, Name = "Carlsberg", Country = new Country() { Name = "Germany" }, Style = new BeerStyle() { Name = "Ale" } } } } } }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BeerService(context); var result = await sut.GetAllAsync(); //Assert Assert.IsInstanceOfType(result, typeof(IEnumerable <BeerDTO>)); } }
public async Task GetAllAsync_ShouldReturnNullBeerIfModelHasNoNameAsync() { //Arrange var options = InMemory.GetOptions("GetAllAsync_ShouldReturnNullBeerIfModelHasNoNameAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", Breweries = new List <Brewery>() { new Brewery() { Name = "Brewery", Beers = new List <Beer>() { new Beer() { ABV = 4.5f, Rating = 2, Country = new Country() { Name = "Germany" }, Style = new BeerStyle() { Name = "Ale" } } } } } }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new BeerService(context); var result = await sut.GetAllAsync(); //Assert Assert.AreEqual(result, null); } }
private static async Task SeedStylesAsync(BOContext context) { if (context.BeerStyles.Any()) { return; } await context.BeerStyles.AddAsync(new BeerStyle() { Name = "Lager", Description = "Pale lagers are the standard international beer style, as personified by products from Miller to Heineken. This style is the generic spin-off of the pilsner style. Pale lagers are generally light- to medium-bodied with a light-to-medium hop impression and a clean, crisp malt character.", CreatedOn = DateTime.UtcNow, }); await context.SaveChangesAsync(); }
public async Task UpdateAsync_ShouldChangeDescriptionOfReviewAsync() { //Arrange var options = InMemory.GetOptions("UpdateAsync_ShouldChangeDescriptionOfReviewAsync"); using (var context = new BOContext(options)) { var review = new Review { Description = "Great", Beer = new Beer() { Name = "Carlsberg" }, User = new User() { IDOld = 1, Name = "SuperMan" }, }; context.Reviews.Add(review); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { var reviewDTO = new ReviewDTO { Description = "Great2", Beer = new BeerDTO() { Name = "Carlsberg" }, User = new UserDTO() { Name = "SuperMan" } }; //Act var sut = new ReviewsService(context); await sut.UpdateAsync(1, reviewDTO); var dbresult = await context.Reviews.FindAsync(1); //Assert Assert.AreEqual(dbresult.Description, "Great2"); } }
public async Task UpdateAsync_ShouldChangeNameAndCountryOfBreweryAsync() { //Arrange var options = InMemory.GetOptions("UpdateAsync_ShouldChangeNameOfBreweryAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", Breweries = new List <Brewery>() { new Brewery() { Name = "Brewery" } } }; var country2 = new Country() { Name = "Belgium" }; context.Countries.Add(country); context.Countries.Add(country2); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { var breweryDTO = new BreweryDTO() { Name = "Brewery2", Country = "Belgium" }; //Act var sut = new BreweryServices(context); await sut.UpdateAsync(1, breweryDTO); var dbresult = await context.Breweries.Include(b => b.Country).FirstOrDefaultAsync(b => b.ID == 1); //Assert Assert.AreEqual(dbresult.Name, "Brewery2"); Assert.AreEqual(dbresult.Country.Name, "Belgium"); } }
//private static async Task SeedFirstAdmin(BOContext context) //{ // if (context.Users.Any()) // { // return; // } // var user = new User() // { // Name = "Carlsberg", // UserName = "******", // NormalizedUserName = "******", // Email = "*****@*****.**", // NormalizedEmail = "*****@*****.**", // Password = "******", // Role = await context.Roles.FindAsync(2) // }; // await context.Users.AddAsync(user); // await context.SaveChangesAsync(); //} private static async Task SeedBreweriesAsync(BOContext context) { if (context.Breweries.Any()) { return; } await context.Breweries.AddAsync( new Brewery() { Name = "Carlsberg", CountryID = context.Countries.FirstOrDefault(c => c.Name == "Bulgaria").ID, Country = context.Countries.FirstOrDefault(c => c.Name == "Bulgaria"), CreatedOn = DateTime.UtcNow }); await context.SaveChangesAsync(); }
private static async Task SeedRolesAsync(BOContext context) { if (context.Roles.Any()) { return; } var roleNames = new[] { "member", "admin" }; await context.Roles.AddRangeAsync( roleNames.Select(name => new Role() { Name = name, NormalizedName = name.ToUpper(), }) ); await context.SaveChangesAsync(); }
private static async Task SeedCountriesAsync(BOContext context) { if (context.Countries.Any()) { return; } var countryNames = new[] { "Bulgaria", "Germany", "Chech Republic" }; await context.Countries.AddRangeAsync( countryNames.Select(name => new Country() { Name = name, CreatedOn = DateTime.UtcNow, }) ); await context.SaveChangesAsync(); }
public async Task DeleteAsync_ShouldDeleteRecordBreweriesIfExist() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldDeleteRecordBreweriesIfExist"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", Breweries = new List <Brewery>() { new Brewery() { Name = "Brewery", Beers = new List <Beer>() } } }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new CountriesService(context); await sut.DeleteAsync(1); var dbresult = await context.Countries.FirstOrDefaultAsync(c => c.Name == "Bulgaria"); var dbBreweryResult = await context.Breweries.Include(b => b.Beers).FirstOrDefaultAsync(b => b.Name == "Brewery"); //Assert Assert.AreEqual(dbresult.Name, "Bulgaria"); Assert.AreEqual(dbresult.DeletedOn, dbresult.ModifiedOn); Assert.AreEqual(dbresult.IsDeleted, true); Assert.AreEqual(dbBreweryResult.Name, "Brewery"); Assert.AreEqual(dbBreweryResult.DeletedOn, dbBreweryResult.ModifiedOn); Assert.AreEqual(dbBreweryResult.IsDeleted, true); } }