Exemplo n.º 1
0
        public ActionResult AddAuthor(AuthorModel author)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    AuthorRepository AuthorRepo = new AuthorRepository();
                    AuthorRepo.AddAuthor(author);

                    var profileData = Session["UserProfile"] as UserSession;
                    var logModel    = new LogModel
                    {
                        UserId    = profileData.UserID,
                        TableName = "Author",
                        Activity  = "Added Author",
                        LogDate   = DateTime.Now
                    };
                    var logRepository = new logRepository();
                    logRepository.AddLog(logModel);
                }
                return(RedirectToAction("GetAllAuthorDetails"));
            }
            catch
            {
                return(RedirectToAction("GetAllAuthorDetails"));
            }
        }
Exemplo n.º 2
0
        public void AddAuthor_AuthorWithoutCountryId_AuthorHasBEAsCountryId()
        {
            // Arrange

            // var logs = new List<string>();

            var connectionStringBuilder = new SqliteConnectionStringBuilder {
                DataSource = ":memory:"
            };
            var connection = new SqliteConnection(connectionStringBuilder.ToString());
            var options    = new DbContextOptionsBuilder <CourseContext>()
                             .UseLoggerFactory(new LoggerFactory(new[] {
                new LogToActionLoggerProvider((log) => {
                    //todo
                    _output.WriteLine(log);
                })
            }))
                             .UseSqlite(connection)
                             .Options;

            using (var context = new CourseContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                context.Countries.Add(new Entities.Country()
                {
                    Id          = "BE",
                    Description = "Belgium"
                });

                context.SaveChanges();
            }

            using (var context = new CourseContext(options))
            {
                var authorRepository = new AuthorRepository(context);
                var authorToAdd      = new Author()
                {
                    FirstName = "Kevin",
                    LastName  = "Dockx",
                    Id        = Guid.Parse("d84d3d7e-3fbc-4956-84a5-5c57c2d86d7b")
                };

                // Act
                authorRepository.AddAuthor(authorToAdd);
                authorRepository.SaveChanges();
            }

            using (var context = new CourseContext(options))
            {
                // Assert
                var authorRepository = new AuthorRepository(context);
                var addedAuthor      = authorRepository.GetAuthor(
                    Guid.Parse("d84d3d7e-3fbc-4956-84a5-5c57c2d86d7b"));
                Assert.Equal("BE", addedAuthor.CountryId);
            }
        }
Exemplo n.º 3
0
        public void AddAuthor_AuthorWithoutCountryId_AuthorHasBEAsCountryId()
        {
            // ARRANGE
            var logger        = new LogToActionLoggerProvider((log) => _ouput.WriteLine(log));
            var loggerFactory = new LoggerFactory(new[] { logger });

            var connectionStringBuilder = new SqliteConnectionStringBuilder {
                DataSource = ":memory:"
            };
            var connection = new SqliteConnection(connectionStringBuilder.ToString());
            var options    = new DbContextOptionsBuilder <CourseContext>()
                             .UseLoggerFactory(loggerFactory)
                             .UseSqlite(connection)
                             .Options;

            using (var ctx = new CourseContext(options))
            {
                ctx.Database.OpenConnection();
                ctx.Database.EnsureCreated();

                ICollection <Country> countriesLst = new List <Country>
                {
                    new Country {
                        Id = "BE", Description = "Belgium"
                    }
                };

                ctx.Countries.AddRange(countriesLst);

                ctx.SaveChanges();

                var authorRepository = new AuthorRepository(ctx);

                var authorToAdd = new Author
                {
                    FirstName = "adonis",
                    LastName  = "cruz v",
                    Id        = Guid.Parse("ec622423-d92b-430b-a4b9-b14caafdda6c")
                };

                // ACT
                authorRepository.AddAuthor(authorToAdd);
                authorRepository.SaveChanges();
            }

            using (var ctx = new CourseContext(options))
            {
                ctx.Database.OpenConnection();
                ctx.Database.EnsureCreated();

                // ASSERT
                var authorRepository = new AuthorRepository(ctx);
                var addedAuthor      = authorRepository.GetAuthor(Guid.Parse("ec622423-d92b-430b-a4b9-b14caafdda6c"));
                Assert.Equal("BE", addedAuthor.CountryId);
            }
        }
Exemplo n.º 4
0
        public async Task AddNewAuthorTest_1()
        {
            string conString = "Server=DESKTOP-RF07Q5U;Database=AspCoreBookApp;Trusted_Connection=True;MultipleActiveResultSets=true";

            IRepositoryContextFactory repositoryContextFactory = new RepositoryContextFactory();
            IAuthorRepository         authorRepository         = new AuthorRepository(conString, repositoryContextFactory);
            await authorRepository.AddAuthor(new Author { Id = 0, Name = "Author4" });

            var authorList = await authorRepository.GetAuthors(0, 10);

            var ext = authorList.Records.ToList();
        }
Exemplo n.º 5
0
        public ActionResult Create([Bind(Include = "ID,DeathDate,Age,Sex,FirstName,LastName,BornDate")] Author author)
        {
            if (ModelState.IsValid)
            {
                db1.AddAuthor(author);
                //db.Authors.Add(author);
                //db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(author));
        }
Exemplo n.º 6
0
 public IActionResult AddAuthor([FromBody] Author author)
 {
     if (author == null)
     {
         return(BadRequest());
     }
     else
     {
         AuthorRepository.AddAuthor(author);
         //return Ok(author);
         return(Json(author));
     }
 }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            var authorRepository = new AuthorRepository();
            var songRepository   = new SongRepository();

            while (true)
            {
                Console.WriteLine("Options:\n1) View all songs\n2) View all songs by an author\n3) Add an author\n5) Remove an author");

                int optionChosen = int.Parse(Console.ReadLine());

                switch (optionChosen)
                {
                case 1:
                    songRepository.GetAllSongs().ToList().ForEach(x => Console.WriteLine(x.NameAndDuration));
                    break;

                case 2:
                    Console.WriteLine("Name: ");
                    string name = Console.ReadLine();
                    authorRepository.GetAllSongsFromAuthor(name).ToList().ForEach(x => Console.WriteLine(x.NameAndDuration));
                    break;

                case 3:
                    string name1, password1;
                    do
                    {
                        Console.WriteLine("Name: ");
                        name1 = Console.ReadLine();
                        Console.WriteLine("Password: "******"Name: ");
                        name2 = Console.ReadLine();
                        Console.WriteLine("Password: ");
                        password2 = Console.ReadLine();
                    }while (authorRepository.GetAuthorByNameAndPassword(name2, password2) == null);

                    authorRepository.RemoveAuthor(name2, password2);
                    break;
                }
            }
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            var authorRepository = new AuthorRepository();

            while (true)
            {
                Console.WriteLine("Pick an option\n 1) View songs by an author\n 2) Add an author\n 3) Remove an author");

                int optionChosen = int.Parse(Console.ReadLine());

                if (optionChosen == 1)
                {
                    Console.WriteLine("Name: ");
                    string name = Console.ReadLine();
                    authorRepository.GetAuthorByName(name).Songs.ToList().ForEach(x => Console.WriteLine(x.NameAndDuration));
                }

                else if (optionChosen == 2)
                {
                    string name;     //does not work if name is defined inside 'do' block
                    do
                    {
                        Console.WriteLine("Name: ");
                        name = Console.ReadLine();
                    }while (authorRepository.GetAuthorByName(name) != null);

                    Console.WriteLine("Password: "******"Name: ");
                        name = Console.ReadLine();
                    }while (authorRepository.GetAuthorByName(name) == null);    //please note: != changed to ==

                    Console.WriteLine("Password: ");
                    string password = Console.ReadLine();

                    authorRepository.DeleteAuthor(name, password);
                }
            }
        }
        public void test_can_create_an_author_via_AddAuthor()
        {
            // Arrange
            var id     = Guid.NewGuid();
            var name   = "Bill Jobs";
            var author = new Author()
            {
                Id = id, Name = name
            };

            // Act
            _authorRepository.AddAuthor(author);
            _authorRepository.Save();

            // Assert
            var data = _dbContext.Authors.Where(a => a.Id == id && a.Name == name).ToList();

            data.Should().HaveCount(1);
        }
Exemplo n.º 10
0
        private void SaveButton_Click(object sender, EventArgs e)
        {
            NameTextBox.Text     = NameTextBox.Text.TrimAndRemoveWhiteSpaces().AllFirstLettersToUpper();
            LastNameTextBox.Text = LastNameTextBox.Text.TrimAndRemoveWhiteSpaces().AllFirstLettersToUpper();

            if (!CheckForErrors())
            {
                return;
            }

            var toAdd = new Author
            {
                Name     = NameTextBox.Text,
                LastName = LastNameTextBox.Text
            };

            _authorRepository.AddAuthor(toAdd);
            Close();
        }
        public void AddAuthor_AuthorWithoutCountryId_AuthorHasBEAsCountryId()
        {
            //Arrange
            var contextOptions = GetContext(Guid.NewGuid());

            using (var context = new CourseContext(contextOptions))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                context.Countries.Add(new Country
                {
                    Id          = "BE",
                    Description = "Belgium"
                });

                context.SaveChanges();
            }

            using (var context = new CourseContext(contextOptions))
            {
                var authorRepository = new AuthorRepository(context);
                var authorToAdd      = new Author
                {
                    FirstName = "Sunkanmi",
                    LastName  = "Ijatuyi",
                    Id        = Guid.Parse("d84d3d7e-3fbc-4956-84a5-5c57c2d86d7b")
                };
                //Act

                authorRepository.AddAuthor(authorToAdd);
                authorRepository.SaveChanges();
            }

            using (var context = new CourseContext(contextOptions))
            {
                //Assert
                var authorRepository = new AuthorRepository(context);
                var addedAuthor      = authorRepository.GetAuthor(Guid.Parse("d84d3d7e-3fbc-4956-84a5-5c57c2d86d7b"));
                Assert.Equal("BE", addedAuthor.CountryId);
            }
        }
        public void AddAuthor_AuthorWithoutCountry_DefaultsToBE()
        {
            List <string> logs = new List <string>();
            // Arrange
            var dbBuilder = new SQLiteDbBuilder(_testOutput, logs);
            var newAuthor = new Author
            {
                Id        = new Guid("ae9118fd-4ffd-4895-a756-c371f656eeed"),
                FirstName = "New First Name",
                LastName  = "New Last Name"
            };

            using (var context = dbBuilder.BuildContext())
            {
                context.Countries.Add(new Country()
                {
                    Id          = "BE",
                    Description = "Belgium"
                });
                context.SaveChanges();
            }

            // Act
            using (var context = dbBuilder.BuildContext())
            {
                var target = new AuthorRepository(context);
                target.AddAuthor(newAuthor);
                target.SaveChanges();
            }

            // Assert
            using (var context = dbBuilder.BuildContext())
            {
                var target      = new AuthorRepository(context);
                var addedAuthor = target.GetAuthor(
                    new Guid("ae9118fd-4ffd-4895-a756-c371f656eeed"));
                Assert.Equal("BE", context.Authors.Single().CountryId);
            }
        }
Exemplo n.º 13
0
        public void AddAuthor_AuthorWithoutCountryId_AuthorHasBeAsCountryId()
        {
            var options = CreateSQLliteInMemoryDatabase();
            var newGuid = Guid.NewGuid();

            //Open Connection and Add Belgim
            using (var context = new BusinessDBContext(options))
            {
                OpenConnectionAndEnsureCreated(context);
                AddBelgium(context);
                context.SaveChanges();
            }

            //Add Author
            using (var context = new BusinessDBContext(options))
            {
                var authorRepository = new AuthorRepository(context);
                var authorToAdd      = new Author()
                {
                    FirstName = "Deborah",
                    LastName  = "Kurata",
                    Id        = newGuid
                };

                //Act
                authorRepository.AddAuthor(authorToAdd);
                authorRepository.SaveChanges();
            }

            //Querry Database and Verify Test Results
            using (var context = new BusinessDBContext(options))
            {
                //assert
                var authorRepository = new AuthorRepository(context);
                var addedAuthor      = authorRepository.GetAuthor(newGuid);
                Assert.Equal("BE", addedAuthor.CountryId);
            }
        }
Exemplo n.º 14
0
        public Author AddAuthor(Author author)
        {
            try
            {
                foreach (Author aux in authorRepository.GetAll())
                {
                    if (author.Name == aux.Name)
                    {
                        return(null);
                    }
                }

                if (authorRepository.AddAuthor(author) == null)
                {
                    return(null);
                }

                return(author);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 15
0
 public ActionResult Post(Author author)
 {
     AuthorRepository.AddAuthor(author);
     return(Ok());
 }
Exemplo n.º 16
0
        public void AddAuthor_AuthorWithoutCountryId_AuthorHasBEAsCountryId()
        {
            // Arrange
            //var logs = new List<string>();

            // we commented this line because we are going to use Sqlite instead of ImMemory Provider
            // var options = new DbContextOptionsBuilder<CourseContext>().UseInMemoryDatabase($"CourseDatabaseForTesting {Guid.NewGuid()}").Options;
            var connectionStringBuilder = new SqliteConnectionStringBuilder {
                DataSource = ":memory:"
            };
            var connection = new SqliteConnection(connectionStringBuilder.ToString());
            var options    = new DbContextOptionsBuilder <CourseContext>().UseLoggerFactory(new LoggerFactory(
                                                                                                new[] { new LogToActionLoggerProvider((log) => {
                    //logs.Add(log);
                    //Debug.WriteLine(log);
                    // this will makes you see the actual SQL Server Queries that has been executed
                    _output.WriteLine(log);
                }) }
                                                                                                )).UseSqlite(connection).Options;

            using (var context = new CourseContext(options))
            {
                // note that we opened only the connection for one time, and we did not open it again in the other opened context.
                // this is because the connection is reused across our contexts, so it remains open untill it goes out of the scope, which means untill the test is done
                context.Database.OpenConnection();
                context.Database.EnsureCreated();

                // note that below add is not required when you are using "InMemory Provider" because in this provider it doesnot depend on relational features,
                // but if you are using Sql Lite, below line should exist because it acts as foreign key for Author Table, so it will throws exception if below line is missing.
                context.Countries.Add(new Entities.Country()
                {
                    Id          = "BE",
                    Description = "Belgium"
                });

                context.SaveChanges();
            }

            using (var context = new CourseContext(options))
            {
                var authorRepo = new AuthorRepository(context);

                var authorToAdd = new Author()
                {
                    FirstName = "Kevin",
                    LastName  = "Dockx",
                    Id        = Guid.Parse("d84d3d7e-3fbc-4956-84a5-5c57c2d86d7b")
                };

                authorRepo.AddAuthor(authorToAdd);
                authorRepo.SaveChanges();
            }

            using (var context = new CourseContext(options))
            {
                var authorRepo = new AuthorRepository(context);

                var addedAuthor = authorRepo.GetAuthor(Guid.Parse("d84d3d7e-3fbc-4956-84a5-5c57c2d86d7b"));

                Assert.Equal("BE", addedAuthor.CountryId);
            }
        }
Exemplo n.º 17
0
        public ActionResult <Author> Post([FromBody] Author value)
        {
            Author result = _repo.AddAuthor(value);

            return(Created("/api/author/" + result.Id, result));
        }
Exemplo n.º 18
0
        public void SaveAuthor(AuthorRequest authorRequest)
        {
            Author author = new Author(authorRequest.Name);

            authorRepository.AddAuthor(author);
        }