From 362cfdcee0d616929dad46b6aa93b41507825cfa Mon Sep 17 00:00:00 2001
From: s426226
Date: Sun, 20 Dec 2020 18:03:20 +0100
Subject: [PATCH] POS-34 dodanie wyszukiwania przez google api
---
.../Serwer.Api/Controllers/ImageController.cs | 4 +-
.../Controllers/SearchController.cs | 28 +++++++++++
Serwer/Serwer.Api/Startup.cs | 3 +-
Serwer/Serwer.Core/Domain/SearchResult.cs | 13 ++++++
.../DTO/SearchResultDto.cs | 15 ++++++
.../Services/IImageHandler.cs | 5 +-
.../{IImageService.cs => ISearchService.cs} | 9 ++--
.../Services/ImageHandler.cs | 22 ++++++++-
.../Services/ImageService.cs | 30 ------------
.../Services/SearchService.cs | 46 +++++++++++++++++++
.../Serwer.Infrastructure.csproj | 2 +
11 files changed, 135 insertions(+), 42 deletions(-)
create mode 100644 Serwer/Serwer.Api/Controllers/SearchController.cs
create mode 100644 Serwer/Serwer.Core/Domain/SearchResult.cs
create mode 100644 Serwer/Serwer.Infrastructure/DTO/SearchResultDto.cs
rename Serwer/Serwer.Infrastructure/Services/{IImageService.cs => ISearchService.cs} (50%)
delete mode 100644 Serwer/Serwer.Infrastructure/Services/ImageService.cs
create mode 100644 Serwer/Serwer.Infrastructure/Services/SearchService.cs
diff --git a/Serwer/Serwer.Api/Controllers/ImageController.cs b/Serwer/Serwer.Api/Controllers/ImageController.cs
index 2f62cea..11a164a 100644
--- a/Serwer/Serwer.Api/Controllers/ImageController.cs
+++ b/Serwer/Serwer.Api/Controllers/ImageController.cs
@@ -13,8 +13,8 @@ namespace Serwer.Api.Controllers
[ApiController]
public class ImageController : ControllerBase
{
- private readonly IImageService _imageService;
- public ImageController(IImageService imageService)
+ private readonly IImageHandler _imageService;
+ public ImageController(IImageHandler imageService)
{
_imageService = imageService;
}
diff --git a/Serwer/Serwer.Api/Controllers/SearchController.cs b/Serwer/Serwer.Api/Controllers/SearchController.cs
new file mode 100644
index 0000000..3300ab7
--- /dev/null
+++ b/Serwer/Serwer.Api/Controllers/SearchController.cs
@@ -0,0 +1,28 @@
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Serwer.Infrastructure.Services;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Serwer.Api.Controllers
+{
+ [Route("api/[controller]")]
+ [ApiController]
+ public class SearchController : ControllerBase
+ {
+ private readonly ISearchService _searchService;
+ public SearchController(ISearchService searchService)
+ {
+ _searchService = searchService;
+ }
+
+ [HttpGet("{query}")]
+ public async Task Search(string query)
+ {
+ var results = await _searchService.Search(query);
+ return Ok(results);
+ }
+ }
+}
diff --git a/Serwer/Serwer.Api/Startup.cs b/Serwer/Serwer.Api/Startup.cs
index 357811a..67ac049 100644
--- a/Serwer/Serwer.Api/Startup.cs
+++ b/Serwer/Serwer.Api/Startup.cs
@@ -89,8 +89,9 @@ namespace Serwer.Api
services.AddSingleton(AutoMapperConfig.Initialize());
services.AddSingleton(sp => new JwtHandler(jwtSettings));
services.AddScoped();
- services.AddScoped();
+ services.AddScoped();
services.AddScoped();
+ services.AddScoped();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
diff --git a/Serwer/Serwer.Core/Domain/SearchResult.cs b/Serwer/Serwer.Core/Domain/SearchResult.cs
new file mode 100644
index 0000000..f20d95b
--- /dev/null
+++ b/Serwer/Serwer.Core/Domain/SearchResult.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Serwer.Core.Domain
+{
+ public class SearchResult
+ {
+
+ }
+}
diff --git a/Serwer/Serwer.Infrastructure/DTO/SearchResultDto.cs b/Serwer/Serwer.Infrastructure/DTO/SearchResultDto.cs
new file mode 100644
index 0000000..646d0fa
--- /dev/null
+++ b/Serwer/Serwer.Infrastructure/DTO/SearchResultDto.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Serwer.Infrastructure.DTO
+{
+ public class SearchResultDTO
+ {
+ public string Title { get; set; }
+ public string Link { get; set; }
+ public string Snippet { get; set; }
+ }
+}
diff --git a/Serwer/Serwer.Infrastructure/Services/IImageHandler.cs b/Serwer/Serwer.Infrastructure/Services/IImageHandler.cs
index a7cc44f..2d71c93 100644
--- a/Serwer/Serwer.Infrastructure/Services/IImageHandler.cs
+++ b/Serwer/Serwer.Infrastructure/Services/IImageHandler.cs
@@ -1,4 +1,5 @@
-using System;
+using Serwer.Core.Domain;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -8,6 +9,6 @@ namespace Serwer.Infrastructure.Services
{
public interface IImageHandler
{
-
+ Task Process(string name, string contentType, byte[] bytes);
}
}
diff --git a/Serwer/Serwer.Infrastructure/Services/IImageService.cs b/Serwer/Serwer.Infrastructure/Services/ISearchService.cs
similarity index 50%
rename from Serwer/Serwer.Infrastructure/Services/IImageService.cs
rename to Serwer/Serwer.Infrastructure/Services/ISearchService.cs
index 780c155..8377694 100644
--- a/Serwer/Serwer.Infrastructure/Services/IImageService.cs
+++ b/Serwer/Serwer.Infrastructure/Services/ISearchService.cs
@@ -1,14 +1,13 @@
-using Serwer.Core.Domain;
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
-
+using Serwer.Infrastructure.DTO;
namespace Serwer.Infrastructure.Services
{
- public interface IImageService
+ public interface ISearchService
{
- Task Process(string name, string contentType, byte[] bytes);
+ Task> Search(string query);
}
}
diff --git a/Serwer/Serwer.Infrastructure/Services/ImageHandler.cs b/Serwer/Serwer.Infrastructure/Services/ImageHandler.cs
index ba262e6..4dc0bf1 100644
--- a/Serwer/Serwer.Infrastructure/Services/ImageHandler.cs
+++ b/Serwer/Serwer.Infrastructure/Services/ImageHandler.cs
@@ -1,12 +1,30 @@
-using System;
+using Serwer.Core.Domain;
+using Serwer.Infrastructure.Settings;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Tesseract;
namespace Serwer.Infrastructure.Services
{
- class ImageHandler
+ public class ImageHandler : IImageHandler
{
+ private readonly ISet _files = new HashSet();
+ private readonly string _env;
+ public ImageHandler(IHostEnviroment hostEnviroment)
+ {
+ _env = hostEnviroment.RootPath;
+ }
+ public async Task Process(string name, string contentType, byte[] bytes)
+ {
+ var engine = new TesseractEngine(System.IO.Path.Combine(_env, "tessdata"),"eng+equ", EngineMode.Default);
+ var img = Pix.LoadFromMemory(bytes);
+ var res = engine.Process(img);
+ var txt = res.GetText();
+
+ return await Task.FromResult(txt);
+ }
}
}
diff --git a/Serwer/Serwer.Infrastructure/Services/ImageService.cs b/Serwer/Serwer.Infrastructure/Services/ImageService.cs
deleted file mode 100644
index 02d062d..0000000
--- a/Serwer/Serwer.Infrastructure/Services/ImageService.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-using Serwer.Core.Domain;
-using Serwer.Infrastructure.Settings;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using Tesseract;
-
-namespace Serwer.Infrastructure.Services
-{
- public class ImageService : IImageService
- {
- private readonly ISet _files = new HashSet();
- private readonly string _env;
- public ImageService(IHostEnviroment hostEnviroment)
- {
- _env = hostEnviroment.RootPath;
- }
- public async Task Process(string name, string contentType, byte[] bytes)
- {
- var engine = new TesseractEngine(System.IO.Path.Combine(_env, "tessdata"),"eng+equ", EngineMode.Default);
- var img = Pix.LoadFromMemory(bytes);
- var res = engine.Process(img);
- var txt = res.GetText();
-
- return await Task.FromResult(txt);
- }
- }
-}
diff --git a/Serwer/Serwer.Infrastructure/Services/SearchService.cs b/Serwer/Serwer.Infrastructure/Services/SearchService.cs
new file mode 100644
index 0000000..8447cd3
--- /dev/null
+++ b/Serwer/Serwer.Infrastructure/Services/SearchService.cs
@@ -0,0 +1,46 @@
+using Google.Apis.Customsearch.v1;
+using Google.Apis.Services;
+using Newtonsoft.Json;
+using Serwer.Infrastructure.DTO;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Text.Json.Serialization;
+using System.Threading.Tasks;
+namespace Serwer.Infrastructure.Services
+{
+ public class SearchService : ISearchService
+ {
+ private readonly IImageHandler _imageHandler;
+ private const string apiKey = "AIzaSyCagG6QyQyBuJNb1YDuK25qSzoC0Rrjo0c";
+ private const string searchEngineId = "17b946686537c46e3";
+ public SearchService(IImageHandler imageHandler)
+ {
+ _imageHandler = imageHandler;
+ }
+ public async Task> Search(string query)
+ {
+ WebRequest webRequest = WebRequest.Create($"https://www.googleapis.com/customsearch/v1?key={apiKey}&cx={searchEngineId}&q={query}");
+ using (var stream = new StreamReader(webRequest.GetResponse().GetResponseStream()))
+ {
+ var response = await stream.ReadToEndAsync();
+ dynamic jsonData = JsonConvert.DeserializeObject(response);
+
+ var results = new List();
+ foreach(var item in jsonData.items)
+ {
+ results.Add(new SearchResultDTO
+ {
+ Title = item.title,
+ Link = item.link,
+ Snippet = item.snippet
+ });
+ }
+ return results;
+ }
+ }
+ }
+}
diff --git a/Serwer/Serwer.Infrastructure/Serwer.Infrastructure.csproj b/Serwer/Serwer.Infrastructure/Serwer.Infrastructure.csproj
index 5fd4559..760f634 100644
--- a/Serwer/Serwer.Infrastructure/Serwer.Infrastructure.csproj
+++ b/Serwer/Serwer.Infrastructure/Serwer.Infrastructure.csproj
@@ -6,6 +6,8 @@
+
+