diff --git a/Serwer/Serwer.Api/Controllers/HistoryController.cs b/Serwer/Serwer.Api/Controllers/HistoryController.cs new file mode 100644 index 0000000..4efb348 --- /dev/null +++ b/Serwer/Serwer.Api/Controllers/HistoryController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Serwer.Infrastructure.Services; +using System; +using System.Threading.Tasks; + +namespace Serwer.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class HistoryController: ControllerBase + { + private readonly IHistoryService _historyService; + public HistoryController(IHistoryService historyService) + { + _historyService = historyService; + } + + [Authorize] + [HttpGet()] + public async Task Get() + { + var id = User.Identity.Name; + var history = await _historyService.GetAsync(Guid.Parse(id)); + return Ok(history); + } + } +} diff --git a/Serwer/Serwer.Api/Controllers/SearchController.cs b/Serwer/Serwer.Api/Controllers/SearchController.cs index b986537..91744bd 100644 --- a/Serwer/Serwer.Api/Controllers/SearchController.cs +++ b/Serwer/Serwer.Api/Controllers/SearchController.cs @@ -23,7 +23,8 @@ namespace Serwer.Api.Controllers [HttpGet("{query}")] public async Task Search(string query) { - var results = await _searchService.Search(query); + var id = User.Identity.Name; + var results = await _searchService.Search(Guid.Parse(id), query); return Ok(results); } } diff --git a/Serwer/Serwer.Api/Startup.cs b/Serwer/Serwer.Api/Startup.cs index 67ac049..8f01ce2 100644 --- a/Serwer/Serwer.Api/Startup.cs +++ b/Serwer/Serwer.Api/Startup.cs @@ -89,8 +89,10 @@ namespace Serwer.Api services.AddSingleton(AutoMapperConfig.Initialize()); services.AddSingleton(sp => new JwtHandler(jwtSettings)); services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); } diff --git a/Serwer/Serwer.Core/Domain/UserHistory.cs b/Serwer/Serwer.Core/Domain/UserHistory.cs new file mode 100644 index 0000000..f518a8c --- /dev/null +++ b/Serwer/Serwer.Core/Domain/UserHistory.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Serwer.Core.Domain +{ + public class UserHistory + { + private readonly ISet _history; + public User User { get; protected set; } + public List History + { + get + { + return _history.Select(x => x).ToList(); + } + } + + public UserHistory(User user) + { + User = user; + _history = new HashSet(); + } + + public void Add(string query) + { + _history.Add(query); + } + } +} diff --git a/Serwer/Serwer.Core/Repositories/IHistoryRepository.cs b/Serwer/Serwer.Core/Repositories/IHistoryRepository.cs new file mode 100644 index 0000000..30eab73 --- /dev/null +++ b/Serwer/Serwer.Core/Repositories/IHistoryRepository.cs @@ -0,0 +1,15 @@ +using Serwer.Core.Domain; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Serwer.Core.Repositories +{ + public interface IHistoryRepository + { + Task AddAsync(User user, string query); + Task GetAsync(User user); + } +} diff --git a/Serwer/Serwer.Infrastructure/Repositories/HistoryRepository.cs b/Serwer/Serwer.Infrastructure/Repositories/HistoryRepository.cs new file mode 100644 index 0000000..9ef8109 --- /dev/null +++ b/Serwer/Serwer.Infrastructure/Repositories/HistoryRepository.cs @@ -0,0 +1,31 @@ +using Serwer.Core.Domain; +using Serwer.Core.Repositories; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Serwer.Infrastructure.Repositories +{ + public class HistoryRepository: IHistoryRepository + { + private static ISet _history = new HashSet(); + + public async Task AddAsync(User user, string query) + { + var history = _history.FirstOrDefault(x => x.User.Id == user.Id); + if(history == null) + { + history = new UserHistory(user); + } + history.Add(query); + await Task.FromResult(_history.Add(history)); + } + + public async Task GetAsync(User user) + { + var history = _history.FirstOrDefault(x => x.User.Id == user.Id); + return await Task.FromResult(history); + } + + } +} diff --git a/Serwer/Serwer.Infrastructure/Services/HistoryService.cs b/Serwer/Serwer.Infrastructure/Services/HistoryService.cs new file mode 100644 index 0000000..e8186b7 --- /dev/null +++ b/Serwer/Serwer.Infrastructure/Services/HistoryService.cs @@ -0,0 +1,32 @@ +using Serwer.Core.Domain; +using Serwer.Core.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Serwer.Infrastructure.Services +{ + public class HistoryService: IHistoryService + { + private readonly IHistoryRepository _historyRepository; + private readonly IUserRepository _userRepository; + public HistoryService(IHistoryRepository historyRepository, IUserRepository userRepository) + { + _historyRepository = historyRepository; + _userRepository = userRepository; + } + + public async Task GetAsync(Guid userId) + { + var user = await _userRepository.GetAsync(userId); + if(user == null) + { + throw new Exception("Coś poszło nie tak."); + } + var history = await _historyRepository.GetAsync(user); + return history; + } + } +} diff --git a/Serwer/Serwer.Infrastructure/Services/IHistoryService.cs b/Serwer/Serwer.Infrastructure/Services/IHistoryService.cs new file mode 100644 index 0000000..7dfb4e5 --- /dev/null +++ b/Serwer/Serwer.Infrastructure/Services/IHistoryService.cs @@ -0,0 +1,14 @@ +using Serwer.Core.Domain; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Serwer.Infrastructure.Services +{ + public interface IHistoryService + { + Task GetAsync(Guid userId); + } +} diff --git a/Serwer/Serwer.Infrastructure/Services/ISearchService.cs b/Serwer/Serwer.Infrastructure/Services/ISearchService.cs index 8377694..c5f1117 100644 --- a/Serwer/Serwer.Infrastructure/Services/ISearchService.cs +++ b/Serwer/Serwer.Infrastructure/Services/ISearchService.cs @@ -8,6 +8,6 @@ namespace Serwer.Infrastructure.Services { public interface ISearchService { - Task> Search(string query); + Task> Search(Guid userId, string query); } } diff --git a/Serwer/Serwer.Infrastructure/Services/SearchService.cs b/Serwer/Serwer.Infrastructure/Services/SearchService.cs index 8447cd3..42cbcb3 100644 --- a/Serwer/Serwer.Infrastructure/Services/SearchService.cs +++ b/Serwer/Serwer.Infrastructure/Services/SearchService.cs @@ -1,6 +1,7 @@ using Google.Apis.Customsearch.v1; using Google.Apis.Services; using Newtonsoft.Json; +using Serwer.Core.Repositories; using Serwer.Infrastructure.DTO; using System; using System.Collections.Generic; @@ -15,14 +16,25 @@ namespace Serwer.Infrastructure.Services public class SearchService : ISearchService { private readonly IImageHandler _imageHandler; + private readonly IHistoryRepository _historyRepository; + private readonly IUserRepository _userRepository; private const string apiKey = "AIzaSyCagG6QyQyBuJNb1YDuK25qSzoC0Rrjo0c"; private const string searchEngineId = "17b946686537c46e3"; - public SearchService(IImageHandler imageHandler) + public SearchService(IImageHandler imageHandler, IHistoryRepository historyRepository, IUserRepository userRepository) { _imageHandler = imageHandler; + _historyRepository = historyRepository; + _userRepository = userRepository; } - public async Task> Search(string query) + public async Task> Search(Guid userId, string query) { + var user = await _userRepository.GetAsync(userId); + if(user == null) + { + throw new Exception("Coś poszło nie tak."); + } + await _historyRepository.AddAsync(user, 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())) {