develop #10
28
Serwer/Serwer.Api/Controllers/HistoryController.cs
Normal file
28
Serwer/Serwer.Api/Controllers/HistoryController.cs
Normal file
@ -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<IActionResult> Get()
|
||||||
|
{
|
||||||
|
var id = User.Identity.Name;
|
||||||
|
var history = await _historyService.GetAsync(Guid.Parse(id));
|
||||||
|
return Ok(history);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,7 +23,8 @@ namespace Serwer.Api.Controllers
|
|||||||
[HttpGet("{query}")]
|
[HttpGet("{query}")]
|
||||||
public async Task<IActionResult> Search(string query)
|
public async Task<IActionResult> 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);
|
return Ok(results);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,8 +89,10 @@ namespace Serwer.Api
|
|||||||
services.AddSingleton<IMapper>(AutoMapperConfig.Initialize());
|
services.AddSingleton<IMapper>(AutoMapperConfig.Initialize());
|
||||||
services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings));
|
services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings));
|
||||||
services.AddScoped<IUserRepository, UserRepository>();
|
services.AddScoped<IUserRepository, UserRepository>();
|
||||||
|
services.AddScoped<IHistoryRepository, HistoryRepository>();
|
||||||
services.AddScoped<IImageHandler, ImageHandler>();
|
services.AddScoped<IImageHandler, ImageHandler>();
|
||||||
services.AddScoped<IUserService, UserService>();
|
services.AddScoped<IUserService, UserService>();
|
||||||
|
services.AddScoped<IHistoryService, HistoryService>();
|
||||||
services.AddScoped<ISearchService, SearchService>();
|
services.AddScoped<ISearchService, SearchService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
32
Serwer/Serwer.Core/Domain/UserHistory.cs
Normal file
32
Serwer/Serwer.Core/Domain/UserHistory.cs
Normal file
@ -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<string> _history;
|
||||||
|
public User User { get; protected set; }
|
||||||
|
public List<string> History
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _history.Select(x => x).ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserHistory(User user)
|
||||||
|
{
|
||||||
|
User = user;
|
||||||
|
_history = new HashSet<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(string query)
|
||||||
|
{
|
||||||
|
_history.Add(query);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
Serwer/Serwer.Core/Repositories/IHistoryRepository.cs
Normal file
15
Serwer/Serwer.Core/Repositories/IHistoryRepository.cs
Normal file
@ -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<UserHistory> GetAsync(User user);
|
||||||
|
}
|
||||||
|
}
|
@ -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<UserHistory> _history = new HashSet<UserHistory>();
|
||||||
|
|
||||||
|
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<UserHistory> GetAsync(User user)
|
||||||
|
{
|
||||||
|
var history = _history.FirstOrDefault(x => x.User.Id == user.Id);
|
||||||
|
return await Task.FromResult(history);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
32
Serwer/Serwer.Infrastructure/Services/HistoryService.cs
Normal file
32
Serwer/Serwer.Infrastructure/Services/HistoryService.cs
Normal file
@ -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<UserHistory> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
Serwer/Serwer.Infrastructure/Services/IHistoryService.cs
Normal file
14
Serwer/Serwer.Infrastructure/Services/IHistoryService.cs
Normal file
@ -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<UserHistory> GetAsync(Guid userId);
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,6 @@ namespace Serwer.Infrastructure.Services
|
|||||||
{
|
{
|
||||||
public interface ISearchService
|
public interface ISearchService
|
||||||
{
|
{
|
||||||
Task<IList<SearchResultDTO>> Search(string query);
|
Task<IList<SearchResultDTO>> Search(Guid userId, string query);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using Google.Apis.Customsearch.v1;
|
using Google.Apis.Customsearch.v1;
|
||||||
using Google.Apis.Services;
|
using Google.Apis.Services;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Serwer.Core.Repositories;
|
||||||
using Serwer.Infrastructure.DTO;
|
using Serwer.Infrastructure.DTO;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -15,14 +16,25 @@ namespace Serwer.Infrastructure.Services
|
|||||||
public class SearchService : ISearchService
|
public class SearchService : ISearchService
|
||||||
{
|
{
|
||||||
private readonly IImageHandler _imageHandler;
|
private readonly IImageHandler _imageHandler;
|
||||||
|
private readonly IHistoryRepository _historyRepository;
|
||||||
|
private readonly IUserRepository _userRepository;
|
||||||
private const string apiKey = "AIzaSyCagG6QyQyBuJNb1YDuK25qSzoC0Rrjo0c";
|
private const string apiKey = "AIzaSyCagG6QyQyBuJNb1YDuK25qSzoC0Rrjo0c";
|
||||||
private const string searchEngineId = "17b946686537c46e3";
|
private const string searchEngineId = "17b946686537c46e3";
|
||||||
public SearchService(IImageHandler imageHandler)
|
public SearchService(IImageHandler imageHandler, IHistoryRepository historyRepository, IUserRepository userRepository)
|
||||||
{
|
{
|
||||||
_imageHandler = imageHandler;
|
_imageHandler = imageHandler;
|
||||||
|
_historyRepository = historyRepository;
|
||||||
|
_userRepository = userRepository;
|
||||||
}
|
}
|
||||||
public async Task<IList<SearchResultDTO>> Search(string query)
|
public async Task<IList<SearchResultDTO>> 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}");
|
WebRequest webRequest = WebRequest.Create($"https://www.googleapis.com/customsearch/v1?key={apiKey}&cx={searchEngineId}&q={query}");
|
||||||
using (var stream = new StreamReader(webRequest.GetResponse().GetResponseStream()))
|
using (var stream = new StreamReader(webRequest.GetResponse().GetResponseStream()))
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user