Esempio n. 1
0
        static async Task Main(string[] args)
        {
            using var log = new LoggerConfiguration()
                            .WriteTo.File("log.txt")
                            .AddMsSqlServerSink("Server=(localDB)\\MSSQLLocalDB;Initial Catalog=DumpLibrary;Integrated Security=True;")
                            .CreateLogger();

            log.Information($"Application Started {Environment.UserName}");

            try
            {
                var dbCtx         = new SampleDesignTimeDbContextFactory().CreateDbContext(args);
                var dbService     = new DbService(dbCtx);
                var consoleEngine = new ConsoleEngine(log);

                var authors = await dbService.GetAllAuthors();

                consoleEngine.Display(authors);

                ConsoleActions optionCode = ConsoleActions.Exit;
                do
                {
                    optionCode = consoleEngine.ShowMenuAndGetOption();

                    switch (optionCode)
                    {
                    case ConsoleActions.List:
                        var books = await dbService.GetBooksWithAuthors();

                        consoleEngine.DisplayBooks(books);
                        break;

                    case ConsoleActions.Insert:
                        var book = consoleEngine.GetBook();
                        await dbService.Insert(book);

                        break;

                    case ConsoleActions.Update:
                        var bookForUpdate = consoleEngine.GetBookForUpdateTitle();
                        await dbService.UpdateBookTitle(bookForUpdate.Id, bookForUpdate.Title);

                        break;

                    case ConsoleActions.Delete:
                        var deleteBookId = consoleEngine.GetBookForDelete();
                        await dbService.Delete(deleteBookId);

                        break;
                    }
                }while (optionCode != ConsoleActions.Exit);
            }
            catch (Exception ex)
            {
                log.Error(ex, "App error!");
            }

            log.Information("Application Ended");
        }
Esempio n. 2
0
        static async Task Main(string[] args)
        {
            using var log = new LoggerConfiguration()
                            //.WriteTo.Console()
                            .WriteTo.File("log.txt")
                            .AddMsSqlServerSink("Server=(localDB)\\MSSQLLocalDB;Initial Catalog=DumpLibrary;Integrated Security=True;")
                            .CreateLogger();

            try
            {
                using var dbContext = new SampleDesignTimeDbContextFactory().CreateDbContext(args);

                var authors = await dbContext.Authors.ToListAsync();

                foreach (var a in authors)
                {
                    Console.WriteLine("[{0}] {1}", a.Id, a.FullName);
                }


                int optionCode = 0;
                do
                {
                    Console.WriteLine("0)Exit\n1)List Books\n2)Insert new Book\n3)Update Book Title\n4)Delete Book\nOption:");
                    var option = Console.ReadKey();
                    optionCode = int.Parse(option.KeyChar.ToString());
                    Console.WriteLine();

                    switch (optionCode)
                    {
                    case 1:
                        var books = await dbContext.Books.Include(b => b.Author).ToArrayAsync();

                        foreach (var b in books)
                        {
                            Console.WriteLine("[{0}] {1}:{2}", b.Id, b.Author.FullName, b.Title);
                        }
                        break;

                    case 2:
                        Console.WriteLine("Input Book Id");
                        var bookId = int.Parse(Console.ReadLine());
                        Console.WriteLine("Input Book Title");
                        var bookTitle = Console.ReadLine();
                        Console.WriteLine("Input Author Id");
                        var authorId = int.Parse(Console.ReadLine());
                        var book     = new Book()
                        {
                            Id = bookId, AuthorId = authorId, Title = bookTitle
                        };
                        dbContext.Books.Add(book);
                        await dbContext.SaveChangesAsync();

                        break;

                    case 3:
                        Console.WriteLine("Input Book Id");
                        var updateBookId = int.Parse(Console.ReadLine());
                        Console.WriteLine("Input new Book Title");
                        var updateBookTitle = Console.ReadLine();
                        var bookForUpdate   = await dbContext.Books.SingleAsync(x => x.Id == updateBookId);

                        bookForUpdate.Title = updateBookTitle;
                        await dbContext.SaveChangesAsync();

                        break;

                    case 4:
                        Console.WriteLine("Input Book Id");
                        var deleteBookId  = int.Parse(Console.ReadLine());
                        var bookForDelete = await dbContext.Books.SingleAsync(x => x.Id == deleteBookId);

                        dbContext.Remove(bookForDelete);
                        await dbContext.SaveChangesAsync();

                        break;
                    }
                }while (optionCode > 0);
            }
            catch (Exception ex)
            {
                log.Error(ex, "App error!");
            }
        }
Esempio n. 3
0
        static async Task Main(string[] args)
        {
            using var dbContext = new SampleDesignTimeDbContextFactory().CreateDbContext(args);

            var authors = await dbContext.Authors.ToListAsync();

            foreach (var a in authors)
            {
                Console.WriteLine("[{0}] {1}", a.Id, a.FullName);
            }


            int optionCode = 0;

            do
            {
                Console.WriteLine("0)Exit\n1)List Books\n2)Insert new Book\n3)Update Book Title\n4)Delete Book\nOption:");
                var option = Console.ReadKey();
                optionCode = int.Parse(option.KeyChar.ToString());
                Console.WriteLine();

                switch (optionCode)
                {
                case 1:
                    var books = await dbContext.Books.Include(b => b.Author).ToArrayAsync();

                    foreach (var b in books)
                    {
                        Console.WriteLine("[{0}] {1}:{2}", b.Id, b.Author.FullName, b.Title);
                    }
                    break;

                case 2:
                    Console.WriteLine("Input Book Id");
                    var bookId = int.Parse(Console.ReadLine());
                    Console.WriteLine("Input Book Title");
                    var bookTitle = Console.ReadLine();
                    Console.WriteLine("Input Author Id");
                    var authorId = int.Parse(Console.ReadLine());
                    var book     = new Book()
                    {
                        Id = bookId, AuthorId = authorId, Title = bookTitle
                    };
                    dbContext.Books.Add(book);
                    await dbContext.SaveChangesAsync();

                    break;

                case 3:
                    Console.WriteLine("Input Book Id");
                    var updateBookId = int.Parse(Console.ReadLine());
                    Console.WriteLine("Input new Book Title");
                    var updateBookTitle = Console.ReadLine();
                    var bookForUpdate   = await dbContext.Books.SingleAsync(x => x.Id == updateBookId);

                    bookForUpdate.Title = updateBookTitle;
                    await dbContext.SaveChangesAsync();

                    break;

                case 4:
                    Console.WriteLine("Input Book Id");
                    var deleteBookId  = int.Parse(Console.ReadLine());
                    var bookForDelete = await dbContext.Books.SingleAsync(x => x.Id == deleteBookId);

                    dbContext.Remove(bookForDelete);
                    await dbContext.SaveChangesAsync();

                    break;
                }
            }while (optionCode > 0);
        }