public static bool Where(this Entities.Author author, string namePattern) { if (author == null) { throw new System.ArgumentNullException(nameof(author)); } if (string.IsNullOrWhiteSpace(namePattern)) { return(true); } bool has = false; if (!string.IsNullOrWhiteSpace(author.Name)) { has |= author.Name.Contains(namePattern, System.StringComparison.OrdinalIgnoreCase); } if (!string.IsNullOrWhiteSpace(author.Surname)) { has |= author.Surname.Contains(namePattern, System.StringComparison.OrdinalIgnoreCase); } if (!string.IsNullOrWhiteSpace(author.Nickname)) { has |= author.Nickname.Contains(namePattern, System.StringComparison.OrdinalIgnoreCase); } return(has); }
public void Delete(int?id) { if (id == null) { throw new AuthorException("You must select an Author to delete."); } using (SentimentAnalysisContext context = new SentimentAnalysisContext()) { Entities.Author toRemove = context.Authors.FirstOrDefault(a => a.Id == id); if (toRemove == null) { throw new AuthorException("Author not found."); } if (context.Phrases.Any(p => p.Author.Id == id)) { throw new AuthorException("Cant remove an Author with Phrases."); } context.Authors.Remove(toRemove); context.SaveChanges(); } }
public BusinessLogic.DTO.Author ToAuthorBL(Entities.Author author) { if (author == null) { return(null); } return(new BusinessLogic.DTO.Author(author.Id, author.Username, author.Name, author.LastName, author.Birthdate)); }
/// <summary> /// CreateAsync /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task CreateAsync(CreateAuthorViewModel model) { var author = new Entities.Author { Name = model.Name }; await _context.Authors.AddAsync(author); await _context.SaveChangesAsync(); }
public static IEnumerable <string> GetCoursesIdsAsStrings(this Entities.Author author) { var ids = new List <string>(); foreach (var course in author.Courses) { ids.Add(course.Id.ToString()); } return(ids); }
public Author Get(int id) { using (SentimentAnalysisContext context = new SentimentAnalysisContext()) { Entities.Author author = context.Authors.FirstOrDefault(a => a.Id == id); if (author == null) { throw new AuthorException("Author not found."); } return(Helper.Instance.ToAuthorBL(author)); } }
public void Add(Author author) { using (SentimentAnalysisContext context = new SentimentAnalysisContext()) { if (context.Authors.Any(a => a.Id == author.Id)) { throw new AuthorException("An Author with the same ID already exists."); } Exists(author); Entities.Author toAdd = Helper.Instance.ToAuthorEF(author); context.Authors.Add(toAdd); context.SaveChanges(); author.Id = toAdd.Id; } }
public static bool Has(this Entities.Author author, int?fromBookAmount, int?toBookAmount) { if (author == null) { throw new System.ArgumentNullException(nameof(author)); } bool has = true; int bookAmount = author.Books.Count; if (fromBookAmount != null) { has &= bookAmount >= fromBookAmount.Value; } if (toBookAmount != null) { has &= bookAmount <= toBookAmount.Value; } return(has); }
//we need to create a separate model for this endpoint, as it doesnt need an //id field in its input. So if there are other fields that wont be sent then // create a model that has only the properties that will be sent to this endpoint public async Task <IActionResult> CreateAuthor([FromBody] CreateAuthorDTO model) { //convert to entity var newAuthor = new Entities.Author { DateOfBirth = model.DOB, FirstName = model.FirstName, LastName = model.LastName, MainCategory = model.MainCategory }; // also contains method to add collection of courses for the author await _courseLibraryRepository.AddAuthorAsync(newAuthor); _courseLibraryRepository.Save(); // Map to return type DTO var authorDto = new AuthorDto { Id = newAuthor.Id, Name = $"{newAuthor.FirstName} {newAuthor.LastName}", Age = DateTime.Now.Year - newAuthor.DateOfBirth.Year, MainCategory = newAuthor.MainCategory }; // route name is the name of the route that will return details for the //newly created author () //routeValues: we need to create an object with the same arg names as the // route args for GetAuthor // value: will be the newly created author object // this will return the author.. to get its corresponding courses // append /Courses to the route return(CreatedAtRoute(routeName: "GetAuthor", routeValues: new { authorId = authorDto.Id }, value: authorDto)); }
// METHODS protected override void Seed(DataBaseContext context) { // COUNTRIES int countriesAmount = countriesList.Length; Entities.Country[] countries = new Entities.Country[countriesAmount]; for (int i = 0; i < countriesAmount; ++i) { countries[i] = new Entities.Country() { Name = countriesList[i] }; } context.Countries.AddRange(countries); // CATEGORIES int categoriesAmount = random.Next(10, 25); Entities.Category[] categories = new Entities.Category[categoriesAmount]; for (int i = 0; i < categoriesAmount; ++i) { categories[i] = new Entities.Category() { Name = words[random.Next(words.Length)] }; } context.Categories.AddRange(categories); // GENRES int genresAmount = random.Next(10, 25); Entities.Genre[] genres = new Entities.Genre[genresAmount]; for (int i = 0; i < genresAmount; ++i) { genres[i] = new Entities.Genre() { Name = words[random.Next(words.Length)] }; } context.Genres.AddRange(genres); // AUTHORS int authorsAmount = random.Next(50, 150); Entities.Author[] authors = new Entities.Author[authorsAmount]; for (int i = 0; i < authorsAmount; ++i) { authors[i] = new Entities.Author() { Name = words[random.Next(words.Length)], Surname = words[random.Next(words.Length)], Nickname = words[random.Next(words.Length)] + words[random.Next(words.Length)] }; } context.Authors.AddRange(authors); // PUBLISHING HOUSES int publishingHousesAmount = random.Next(20, 50); Entities.PublishingHouse[] publishingHouses = new Entities.PublishingHouse[publishingHousesAmount]; for (int i = 0; i < publishingHousesAmount; ++i) { publishingHouses[i] = new Entities.PublishingHouse() { Name = words[random.Next(words.Length)], Country = countries[random.Next(countriesAmount)], }; } context.PublishingHouses.AddRange(publishingHouses); // BOOK int booksAmount = random.Next(100, 500); Entities.Book[] books = new Entities.Book[booksAmount]; for (int i = 0; i < booksAmount; ++i) { books[i] = new Entities.Book() { Name = string.Join(" ", GenerateList(names, 1, 5)), Amount = random.Next(25, 50), Year = random.Next(1975, System.DateTime.Now.Year), Authors = GenerateList(authors, 1, 5), PublishingHouses = GenerateList(publishingHouses, 1, 3), Categories = GenerateList(categories, 1, 5), Genres = GenerateList(genres, 1, 5) }; } context.Books.AddRange(books); // READERS int readersAmount = random.Next(100, 500); Entities.Reader[] readers = new Entities.Reader[readersAmount]; for (int i = 0; i < readersAmount; ++i) { readers[i] = new Entities.Reader() { Name = names[random.Next(names.Length)], Surname = names[random.Next(names.Length)], Address = string.Join(" ", GenerateList(words, 1, 5)), Phone = GeneratePhoneNumber() }; } context.Readers.AddRange(readers); // ABONNEMENTS int abonnementsAmount = random.Next(1500, 5000); Entities.Abonnement[] abonnements = new Entities.Abonnement[abonnementsAmount]; for (int i = 0; i < abonnementsAmount; ++i) { System.DateTime takeDate = GenerateDate(); bool isReturned = random.Next(2) == 1; abonnements[i] = new Entities.Abonnement() { Reader = readers[random.Next(readers.Length)], Book = books[random.Next(books.Length)], TakeTime = takeDate, TakenPeriod = takeDate.AddDays(random.Next(7, 28)), ReturnTime = isReturned ? (System.DateTime?)takeDate.AddDays(random.Next(1, 20)) : null }; } context.Abonnements.AddRange(abonnements); base.Seed(context); }
protected override void Seed(DataBaseContext context) { // COUNTRIES Entities.Country[] countries = new Entities.Country[] { new Entities.Country() { Name = "Albania" }, new Entities.Country() { Name = "Austria" }, new Entities.Country() { Name = "Canada" }, new Entities.Country() { Name = "Germany" }, new Entities.Country() { Name = "Italy" }, new Entities.Country() { Name = "Ukraine" } }; context.Countries.AddRange(countries); // CATEGORIES Entities.Category[] categories = new Entities.Category[] { new Entities.Category() { Name = "Novel" }, new Entities.Category() { Name = "Poems" } }; context.Categories.AddRange(categories); // GENRES Entities.Genre[] genres = new Entities.Genre[] { new Entities.Genre() { Name = "Fantasy" }, new Entities.Genre() { Name = "Mystery" }, new Entities.Genre() { Name = "Detective story" }, }; context.Genres.AddRange(genres); // AUTHORS Entities.Author[] authors = new Entities.Author[] { new Entities.Author() { Name = "Howard", Surname = "Lovecraft", Nickname = "Howard Phillips Lovecraft" }, new Entities.Author() { Name = "Stephen", Surname = "King", Nickname = "Stephen Edwin King" }, new Entities.Author() { Name = "Edgar", Surname = "Poe", Nickname = "Edgar Allan Poe" }, }; context.Authors.AddRange(authors); // PUBLISHING HOUSES Entities.PublishingHouse[] publishingHouses = new Entities.PublishingHouse[] { new Entities.PublishingHouse() { Name = "Wiley", Country = countries[0] }, new Entities.PublishingHouse() { Name = "Bertelsmann", Country = countries[3] }, new Entities.PublishingHouse() { Name = "Thomson Reuters", Country = countries[2] }, }; context.PublishingHouses.AddRange(publishingHouses); // BOOK Entities.Book[] books = new Entities.Book[] { new Entities.Book() { Name = "The Call of Cthulhu", Amount = 23, Year = 1995, Authors = new [] { authors[0] }, PublishingHouses = new Entities.PublishingHouse[] { publishingHouses[0] }, Categories = new Entities.Category[] { categories[0] }, Genres = new Entities.Genre[] { genres[0], genres[1] } }, new Entities.Book() { Name = "The Shining", Amount = 23, Year = 1993, Authors = new [] { authors[1] }, PublishingHouses = new Entities.PublishingHouse[] { publishingHouses[1] }, Categories = new Entities.Category[] { categories[0] }, Genres = new Entities.Genre[] { genres[1] } }, new Entities.Book() { Name = "It", Amount = 23, Year = 1978, Authors = new [] { authors[1] }, PublishingHouses = new Entities.PublishingHouse[] { publishingHouses[2] }, Categories = new Entities.Category[] { categories[0] }, Genres = new Entities.Genre[] { genres[1] } }, new Entities.Book() { Name = "The Raven", Amount = 23, Year = 1990, Authors = new [] { authors[2] }, PublishingHouses = new Entities.PublishingHouse[] { publishingHouses[0] }, Categories = new Entities.Category[] { categories[1] }, Genres = new Entities.Genre[] { genres[1] } }, }; context.Books.AddRange(books); // READERS Entities.Reader[] readers = new Entities.Reader[] { new Entities.Reader() { Name = "John", Surname = "Doe", Address = "Lorem Ipsum", Phone = "3809912345" }, new Entities.Reader() { Name = "Jane", Surname = "Doe", Address = "Dolor sit amet", Phone = "3809812345" }, }; context.Readers.AddRange(readers); // ABONNEMENTS Entities.Abonnement[] abonnements = new Entities.Abonnement[] { new Entities.Abonnement() { Reader = readers[0], Book = books[0], TakeTime = new System.DateTime(year: 2019, month: 2, day: 23), TakenPeriod = new System.DateTime(year: 2019, month: 2, day: 27) }, new Entities.Abonnement() { Reader = readers[1], Book = books[1], TakeTime = new System.DateTime(year: 2019, month: 2, day: 23), TakenPeriod = new System.DateTime(year: 2019, month: 2, day: 27), ReturnTime = new System.DateTime(year: 2019, month: 2, day: 25) }, new Entities.Abonnement() { Reader = readers[0], Book = books[2], TakeTime = new System.DateTime(year: 2019, month: 2, day: 23), TakenPeriod = new System.DateTime(year: 2019, month: 2, day: 27), ReturnTime = new System.DateTime(year: 2019, month: 2, day: 28) }, new Entities.Abonnement() { Reader = readers[0], Book = books[3], TakeTime = new System.DateTime(year: 2019, month: 2, day: 23), TakenPeriod = new System.DateTime(year: 2019, month: 2, day: 27) }, }; context.Abonnements.AddRange(abonnements); base.Seed(context); }