Calibre_EntityFrameworkCore

Details

diff --git a/src/Calibre.Model.Domain/Dal/SearchProvider.cs b/src/Calibre.Model.Domain/Dal/SearchProvider.cs
index 1822bbf..555bb81 100644
--- a/src/Calibre.Model.Domain/Dal/SearchProvider.cs
+++ b/src/Calibre.Model.Domain/Dal/SearchProvider.cs
@@ -124,7 +124,9 @@ namespace Calibre.Model.Domain.Dal
                 var library = elem.Key;
                 var libraryProxy = new ImmutableProxy<Library>(library);
 
-                var books = elem.Value.Result
+                var libBooks = elem.Value.Result;
+
+                var books = libBooks
                     .Select(
                         e2 => BookWithLibraryContainer.Create(e2, libraryProxy)
                         )
diff --git a/src/Calibre.Model.Domain/Entities/Library.cs b/src/Calibre.Model.Domain/Entities/Library.cs
index 7999c51..9f59976 100644
--- a/src/Calibre.Model.Domain/Entities/Library.cs
+++ b/src/Calibre.Model.Domain/Entities/Library.cs
@@ -97,18 +97,25 @@ namespace Calibre.Model.Domain.Entities
             BookWithLibraryContainer bookContainer
             )
         {
-            var result = new Library() 
+            try
             {
-                Name = library.Name,
-                FullName = library.FullName
-            };
-
-            result.Books = library.Books.Add(
-                bookContainer.Book.Title, 
-                bookContainer
-                );
-
-            return result;
+                var result = new Library()
+                {
+                    Name = library.Name,
+                    FullName = library.FullName
+                };
+
+                result.Books = library.Books.Add(
+                    bookContainer.Book.Title,
+                    bookContainer
+                    );
+
+                return result;
+            }
+            catch (Exception ex)
+            {
+                throw;
+            }
         }
 
         public static string GetPdfFilePath(

src/Calibre.sln 11(+10 -1)

diff --git a/src/Calibre.sln b/src/Calibre.sln
index c5a1d79..235211e 100644
--- a/src/Calibre.sln
+++ b/src/Calibre.sln
@@ -15,7 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tools.PdfProvider", "Tools.
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibraryText", "LibraryText\LibraryText.csproj", "{EA856697-8295-45DF-A730-D9A082AEEFCE}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calibre.Model.Domain", "Calibre.Model.Domain\Calibre.Model.Domain.csproj", "{81BA693F-7887-4155-B877-B65966EB9B76}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Calibre.Model.Domain", "Calibre.Model.Domain\Calibre.Model.Domain.csproj", "{81BA693F-7887-4155-B877-B65966EB9B76}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "App", "App", "{11D5A3F3-C060-475C-8D93-AB4D3412CB58}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Calibre.WebApi", "Calibre.WebApi\Calibre.WebApi.csproj", "{7566FD47-94ED-48FC-9256-A35E3B29E880}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -43,6 +47,10 @@ Global
 		{81BA693F-7887-4155-B877-B65966EB9B76}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{81BA693F-7887-4155-B877-B65966EB9B76}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{81BA693F-7887-4155-B877-B65966EB9B76}.Release|Any CPU.Build.0 = Release|Any CPU
+		{7566FD47-94ED-48FC-9256-A35E3B29E880}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{7566FD47-94ED-48FC-9256-A35E3B29E880}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{7566FD47-94ED-48FC-9256-A35E3B29E880}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{7566FD47-94ED-48FC-9256-A35E3B29E880}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -53,6 +61,7 @@ Global
 		{057A6C6C-DC6B-4BA2-9D25-C263C6F04B68} = {951387AC-1771-4B72-B885-B5DFAFB55D4D}
 		{EA856697-8295-45DF-A730-D9A082AEEFCE} = {951387AC-1771-4B72-B885-B5DFAFB55D4D}
 		{81BA693F-7887-4155-B877-B65966EB9B76} = {951387AC-1771-4B72-B885-B5DFAFB55D4D}
+		{7566FD47-94ED-48FC-9256-A35E3B29E880} = {11D5A3F3-C060-475C-8D93-AB4D3412CB58}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {362F0FEB-FB13-46F0-825D-15E6F10100D4}
diff --git a/src/Calibre.WebApi/appsettings.Development.json b/src/Calibre.WebApi/appsettings.Development.json
new file mode 100644
index 0000000..8983e0f
--- /dev/null
+++ b/src/Calibre.WebApi/appsettings.Development.json
@@ -0,0 +1,9 @@
+{
+  "Logging": {
+    "LogLevel": {
+      "Default": "Information",
+      "Microsoft": "Warning",
+      "Microsoft.Hosting.Lifetime": "Information"
+    }
+  }
+}
diff --git a/src/Calibre.WebApi/appsettings.json b/src/Calibre.WebApi/appsettings.json
new file mode 100644
index 0000000..d9d9a9b
--- /dev/null
+++ b/src/Calibre.WebApi/appsettings.json
@@ -0,0 +1,10 @@
+{
+  "Logging": {
+    "LogLevel": {
+      "Default": "Information",
+      "Microsoft": "Warning",
+      "Microsoft.Hosting.Lifetime": "Information"
+    }
+  },
+  "AllowedHosts": "*"
+}
diff --git a/src/Calibre.WebApi/Calibre.WebApi.csproj b/src/Calibre.WebApi/Calibre.WebApi.csproj
new file mode 100644
index 0000000..68df2ce
--- /dev/null
+++ b/src/Calibre.WebApi/Calibre.WebApi.csproj
@@ -0,0 +1,17 @@
+<Project Sdk="Microsoft.NET.Sdk.Web">
+
+  <PropertyGroup>
+    <TargetFramework>net5.0</TargetFramework>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.12" />
+    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\Calibre.Model.Database\Calibre.Model.Database.csproj" />
+    <ProjectReference Include="..\Calibre.Model.Domain\Calibre.Model.Domain.csproj" />
+  </ItemGroup>
+
+</Project>
diff --git a/src/Calibre.WebApi/Controllers/BookController.cs b/src/Calibre.WebApi/Controllers/BookController.cs
new file mode 100644
index 0000000..3c74070
--- /dev/null
+++ b/src/Calibre.WebApi/Controllers/BookController.cs
@@ -0,0 +1,67 @@
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+using System.IO;
+
+using Tools.PdfProvider;
+
+using LibraryText;
+using LibraryText.Entities;
+
+using Calibre.Model.Database;
+using Calibre.Model.Database.Entities;
+using Calibre.Model.Domain.Entities;
+using Calibre.Model.Domain.Dal;
+
+namespace Calibre.WebApi.Controllers
+{
+    [ApiController]
+    [Route("[controller]")]
+    public class BookController 
+        : ControllerBase
+    {
+        private readonly SearchProvider SearchProvider;
+
+        private static DirectoryInfo LibDirectory { set; get; }
+            = new DirectoryInfo(@"S:\BooksText\Calibre");
+
+
+        public BookController(
+            SearchProvider searchProvider
+            )
+        {
+            SearchProvider = searchProvider;
+        }
+
+
+        [HttpGet]
+        public async Task<BookWithLibraryContainer[]> Get(
+            int top = -1
+            ) 
+        {
+            var libraries = Library.SearchAll(LibDirectory);
+
+            var searchResult = await SearchProvider.GetBooksByLibrariesAsync(
+                libraries: libraries
+                )
+                .ConfigureAwait(false);
+
+            var allBooks = searchResult
+                .SelectMany(e => e.Value)
+                .ToArray();
+
+            if (top > 0)
+            {
+                allBooks = allBooks
+                    .Take(top)
+                    .ToArray();
+            }
+
+            return allBooks;
+        }
+    }
+}
diff --git a/src/Calibre.WebApi/Program.cs b/src/Calibre.WebApi/Program.cs
new file mode 100644
index 0000000..e692f9a
--- /dev/null
+++ b/src/Calibre.WebApi/Program.cs
@@ -0,0 +1,26 @@
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Calibre.WebApi
+{
+    public class Program
+    {
+        public static void Main(string[] args)
+        {
+            CreateHostBuilder(args).Build().Run();
+        }
+
+        public static IHostBuilder CreateHostBuilder(string[] args) =>
+            Host.CreateDefaultBuilder(args)
+                .ConfigureWebHostDefaults(webBuilder =>
+                {
+                    webBuilder.UseStartup<Startup>();
+                });
+    }
+}
diff --git a/src/Calibre.WebApi/Properties/launchSettings.json b/src/Calibre.WebApi/Properties/launchSettings.json
new file mode 100644
index 0000000..35d3e28
--- /dev/null
+++ b/src/Calibre.WebApi/Properties/launchSettings.json
@@ -0,0 +1,31 @@
+{
+  "$schema": "http://json.schemastore.org/launchsettings.json",
+  "iisSettings": {
+    "windowsAuthentication": false,
+    "anonymousAuthentication": true,
+    "iisExpress": {
+      "applicationUrl": "http://localhost:35446",
+      "sslPort": 0
+    }
+  },
+  "profiles": {
+    "IIS Express": {
+      "commandName": "IISExpress",
+      "launchBrowser": true,
+      "launchUrl": "swagger",
+      "environmentVariables": {
+        "ASPNETCORE_ENVIRONMENT": "Development"
+      }
+    },
+    "Calibre.WebApi": {
+      "commandName": "Project",
+      "dotnetRunMessages": "true",
+      "launchBrowser": true,
+      "launchUrl": "swagger",
+      "applicationUrl": "http://localhost:5000",
+      "environmentVariables": {
+        "ASPNETCORE_ENVIRONMENT": "Development"
+      }
+    }
+  }
+}
diff --git a/src/Calibre.WebApi/Startup.cs b/src/Calibre.WebApi/Startup.cs
new file mode 100644
index 0000000..cdd3865
--- /dev/null
+++ b/src/Calibre.WebApi/Startup.cs
@@ -0,0 +1,84 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using Microsoft.OpenApi.Models;
+using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
+
+using Newtonsoft.Json;
+
+using Tools.PdfProvider;
+
+using LibraryText;
+using LibraryText.Entities;
+
+using Calibre.Model.Database;
+using Calibre.Model.Database.Entities;
+using Calibre.Model.Domain.Entities;
+using Calibre.Model.Domain.Dal;
+
+namespace Calibre.WebApi
+{
+    public class Startup
+    {
+        public Startup(IConfiguration configuration)
+        {
+            Configuration = configuration;
+        }
+
+        public IConfiguration Configuration { get; }
+
+        // This method gets called by the runtime. Use this method to add services to the container.
+        public void ConfigureServices(IServiceCollection services)
+        {
+            //mvc controller
+            services
+                .AddControllers()
+                .AddNewtonsoftJson(
+                    e => 
+                    {
+                        e.SerializerSettings.ReferenceLoopHandling 
+                            = ReferenceLoopHandling.Ignore;
+                    }
+                );
+
+            //swaggerp
+            services.AddSwaggerGen(c =>
+            {
+                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Calibre.WebApi", Version = "v1" });
+            });
+
+
+            services.AddSingleton<SearchProvider>();
+            services.AddSingleton<SimplePdfReader>();
+        }
+
+        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
+        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
+        {
+            if (env.IsDevelopment())
+            {
+                app.UseDeveloperExceptionPage();
+                app.UseSwagger();
+                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Calibre.WebApi v1"));
+            }
+
+            app.UseRouting();
+
+            app.UseAuthorization();
+
+            app.UseEndpoints(endpoints =>
+            {
+                endpoints.MapControllers();
+            });
+        }
+    }
+}
diff --git a/src/Calibre/Program.cs b/src/Calibre/Program.cs
index ed5ec33..e8529f3 100644
--- a/src/Calibre/Program.cs
+++ b/src/Calibre/Program.cs
@@ -34,7 +34,7 @@ namespace Calibre
 
             //await SearchByTitleInDirectory(
             //    new DirectoryInfo(@"S:\BooksText\Calibre"),
-            //    "Python Guide"
+            //    "iot"
             //    );
 
 
@@ -103,6 +103,15 @@ namespace Calibre
                 }
             }
 
+            var a = allBooks
+                .Where(
+                    e => e.Book.Autors
+                        .Any(
+                            e2 => e2.AuthorItem.Name.Contains("Тарик")
+                            )
+                    )
+                .ToArray();
+
             return result;
         }