Moar !wip

This commit is contained in:
TidusJar 2018-09-28 12:50:57 +01:00
parent eb89b352f4
commit 2f964f904e
8 changed files with 109 additions and 16 deletions

View file

@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using System.Security.Principal; using System.Security.Principal;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoFixture;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using Ombi.Core.Authentication; using Ombi.Core.Authentication;
@ -21,6 +22,7 @@ namespace Ombi.Core.Tests.Engine
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
F = new Fixture();
VoteRepository = new Mock<IRepository<Votes>>(); VoteRepository = new Mock<IRepository<Votes>>();
VoteSettings = new Mock<ISettingsService<VoteSettings>>(); VoteSettings = new Mock<ISettingsService<VoteSettings>>();
MusicRequestEngine = new Mock<IMusicRequestEngine>(); MusicRequestEngine = new Mock<IMusicRequestEngine>();
@ -29,11 +31,14 @@ namespace Ombi.Core.Tests.Engine
MovieRequestEngine = new Mock<IMovieRequestEngine>(); MovieRequestEngine = new Mock<IMovieRequestEngine>();
User = new Mock<IPrincipal>(); User = new Mock<IPrincipal>();
UserManager = new Mock<OmbiUserManager>(); UserManager = new Mock<OmbiUserManager>();
UserManager.Setup(x => x.Users)
.Returns(new EnumerableQuery<OmbiUser>(new List<OmbiUser> {new OmbiUser {Id = "abc"}}));
Rule = new Mock<IRuleEvaluator>(); Rule = new Mock<IRuleEvaluator>();
Engine = new VoteEngine(VoteRepository.Object, User.Object, UserManager.Object, Rule.Object, VoteSettings.Object, MusicRequestEngine.Object, Engine = new VoteEngine(VoteRepository.Object, User.Object, UserManager.Object, Rule.Object, VoteSettings.Object, MusicRequestEngine.Object,
TvRequestEngine.Object, MovieRequestEngine.Object); TvRequestEngine.Object, MovieRequestEngine.Object);
} }
public Fixture F { get; set; }
public VoteEngine Engine { get; set; } public VoteEngine Engine { get; set; }
public Mock<IPrincipal> User { get; set; } public Mock<IPrincipal> User { get; set; }
public Mock<OmbiUserManager> UserManager { get; set; } public Mock<OmbiUserManager> UserManager { get; set; }
@ -45,13 +50,24 @@ namespace Ombi.Core.Tests.Engine
public Mock<IMovieRequestEngine> MovieRequestEngine { get; set; } public Mock<IMovieRequestEngine> MovieRequestEngine { get; set; }
[Test] [Test]
[Ignore("Need to mock the user manager")]
public async Task New_Upvote() public async Task New_Upvote()
{ {
var votes = new List<Votes>(); VoteSettings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new VoteSettings());
AutoFi var votes = F.CreateMany<Votes>().ToList();
VoteRepository.Setup(x => x.GetAll()).Returns(new EnumerableQuery<Votes>()) votes.Add(new Votes
{
RequestId = 1,
RequestType = RequestType.Movie,
UserId = "abc"
});
VoteRepository.Setup(x => x.GetAll()).Returns(new EnumerableQuery<Votes>(votes));
var result = await Engine.UpVote(1, RequestType.Movie); var result = await Engine.UpVote(1, RequestType.Movie);
Assert.That(result.Result, Is.True);
VoteRepository.Verify(x => x.Add(It.Is<Votes>(c => c.UserId == "abc" && c.VoteType == VoteType.Upvote)), Times.Once);
VoteRepository.Verify(x => x.Delete(It.IsAny<Votes>()), Times.Once);
MovieRequestEngine.Verify(x => x.ApproveMovieById(1), Times.Never);
} }
} }
} }

View file

@ -5,6 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoFixture" Version="4.5.0" />
<PackageReference Include="Moq" Version="4.10.0" /> <PackageReference Include="Moq" Version="4.10.0" />
<PackageReference Include="Nunit" Version="3.10.1" /> <PackageReference Include="Nunit" Version="3.10.1" />
<PackageReference Include="NUnit.ConsoleRunner" Version="3.9.0" /> <PackageReference Include="NUnit.ConsoleRunner" Version="3.9.0" />

View file

@ -0,0 +1,16 @@
using System.Linq;
using System.Threading.Tasks;
using Ombi.Core.Models;
using Ombi.Store.Entities;
namespace Ombi.Core.Engine
{
public interface IVoteEngine
{
Task<VoteEngineResult> DownVote(int requestId, RequestType requestType);
Task<Votes> GetVoteForUser(int requestId, string userId);
IQueryable<Votes> GetVotes(int requestId, RequestType requestType);
Task RemoveCurrentVote(Votes currentVote);
Task<VoteEngineResult> UpVote(int requestId, RequestType requestType);
}
}

View file

@ -7,11 +7,8 @@ using Ombi.Core.Models.Search;
using Ombi.Core.Rule.Interfaces; using Ombi.Core.Rule.Interfaces;
using Ombi.Store.Entities.Requests; using Ombi.Store.Entities.Requests;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Microsoft.AspNetCore.Identity;
using System.Linq;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Ombi.Core.Authentication; using Ombi.Core.Authentication;
using Ombi.Helpers;
namespace Ombi.Core.Engine.Interfaces namespace Ombi.Core.Engine.Interfaces
{ {

View file

@ -61,7 +61,7 @@ namespace Ombi.Core.Engine
{ {
continue; continue;
} }
retVal.Add(await ProcessResult(tvMazeSearch)); retVal.Add(ProcessResult(tvMazeSearch));
} }
return retVal; return retVal;
} }
@ -123,7 +123,7 @@ namespace Ombi.Core.Engine
public async Task<IEnumerable<SearchTvShowViewModel>> Popular() public async Task<IEnumerable<SearchTvShowViewModel>> Popular()
{ {
var result = await Cache.GetOrAdd(CacheKeys.PopularTv, async () => await TraktApi.GetPopularShows(), DateTime.Now.AddHours(12)); var result = await Cache.GetOrAdd(CacheKeys.PopularTv, async () => await TraktApi.GetPopularShows(), DateTime.Now.AddHours(12));
var processed = await ProcessResults(result); var processed = ProcessResults(result);
return processed; return processed;
} }
@ -131,35 +131,35 @@ namespace Ombi.Core.Engine
{ {
var result = await Cache.GetOrAdd(CacheKeys.AnticipatedTv, async () => await TraktApi.GetAnticipatedShows(), DateTime.Now.AddHours(12)); var result = await Cache.GetOrAdd(CacheKeys.AnticipatedTv, async () => await TraktApi.GetAnticipatedShows(), DateTime.Now.AddHours(12));
var processed = await ProcessResults(result); var processed = ProcessResults(result);
return processed; return processed;
} }
public async Task<IEnumerable<SearchTvShowViewModel>> MostWatches() public async Task<IEnumerable<SearchTvShowViewModel>> MostWatches()
{ {
var result = await Cache.GetOrAdd(CacheKeys.MostWatchesTv, async () => await TraktApi.GetMostWatchesShows(), DateTime.Now.AddHours(12)); var result = await Cache.GetOrAdd(CacheKeys.MostWatchesTv, async () => await TraktApi.GetMostWatchesShows(), DateTime.Now.AddHours(12));
var processed = await ProcessResults(result); var processed = ProcessResults(result);
return processed; return processed;
} }
public async Task<IEnumerable<SearchTvShowViewModel>> Trending() public async Task<IEnumerable<SearchTvShowViewModel>> Trending()
{ {
var result = await Cache.GetOrAdd(CacheKeys.TrendingTv, async () => await TraktApi.GetTrendingShows(), DateTime.Now.AddHours(12)); var result = await Cache.GetOrAdd(CacheKeys.TrendingTv, async () => await TraktApi.GetTrendingShows(), DateTime.Now.AddHours(12));
var processed = await ProcessResults(result); var processed = ProcessResults(result);
return processed; return processed;
} }
private async Task<IEnumerable<SearchTvShowViewModel>> ProcessResults<T>(IEnumerable<T> items) private IEnumerable<SearchTvShowViewModel> ProcessResults<T>(IEnumerable<T> items)
{ {
var retVal = new List<SearchTvShowViewModel>(); var retVal = new List<SearchTvShowViewModel>();
foreach (var tvMazeSearch in items) foreach (var tvMazeSearch in items)
{ {
retVal.Add(await ProcessResult(tvMazeSearch)); retVal.Add(ProcessResult(tvMazeSearch));
} }
return retVal; return retVal;
} }
private async Task<SearchTvShowViewModel> ProcessResult<T>(T tvMazeSearch) private SearchTvShowViewModel ProcessResult<T>(T tvMazeSearch)
{ {
return Mapper.Map<SearchTvShowViewModel>(tvMazeSearch); return Mapper.Map<SearchTvShowViewModel>(tvMazeSearch);
} }

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Principal; using System.Security.Principal;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -15,7 +14,7 @@ using Ombi.Store.Repository;
namespace Ombi.Core.Engine namespace Ombi.Core.Engine
{ {
public class VoteEngine : BaseEngine public class VoteEngine : BaseEngine, IVoteEngine
{ {
public VoteEngine(IRepository<Votes> votes, IPrincipal user, OmbiUserManager um, IRuleEvaluator r, ISettingsService<VoteSettings> voteSettings, public VoteEngine(IRepository<Votes> votes, IPrincipal user, OmbiUserManager um, IRuleEvaluator r, ISettingsService<VoteSettings> voteSettings,
IMusicRequestEngine musicRequestEngine, ITvRequestEngine tvRequestEngine, IMovieRequestEngine movieRequestEngine) : base(user, um, r) IMusicRequestEngine musicRequestEngine, ITvRequestEngine tvRequestEngine, IMovieRequestEngine movieRequestEngine) : base(user, um, r)
@ -33,6 +32,16 @@ namespace Ombi.Core.Engine
private readonly ITvRequestEngine _tvRequestEngine; private readonly ITvRequestEngine _tvRequestEngine;
private readonly IMovieRequestEngine _movieRequestEngine; private readonly IMovieRequestEngine _movieRequestEngine;
public async Task GetMovieViewModel()
{
var requests = await _movieRequestEngine.GetRequests();
foreach (var r in requests)
{
// Make model
var votes = GetVotes(r.Id, RequestType.Movie);
}
}
public IQueryable<Votes> GetVotes(int requestId, RequestType requestType) public IQueryable<Votes> GetVotes(int requestId, RequestType requestType)
{ {
return _voteRepository.GetAll().Where(x => x.RequestType == requestType && requestId == x.RequestId); return _voteRepository.GetAll().Where(x => x.RequestType == requestType && requestId == x.RequestId);

View file

@ -91,6 +91,7 @@ namespace Ombi.DependencyInjection
services.AddTransient<IMusicSender, MusicSender>(); services.AddTransient<IMusicSender, MusicSender>();
services.AddTransient<IMassEmailSender, MassEmailSender>(); services.AddTransient<IMassEmailSender, MassEmailSender>();
services.AddTransient<IPlexOAuthManager, PlexOAuthManager>(); services.AddTransient<IPlexOAuthManager, PlexOAuthManager>();
services.AddTransient<IVoteEngine, VoteEngine>();
} }
public static void RegisterHttp(this IServiceCollection services) public static void RegisterHttp(this IServiceCollection services)
{ {

View file

@ -0,0 +1,53 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Ombi.Core.Engine;
using Ombi.Store.Entities;
namespace Ombi.Controllers
{
[ApiV1]
[Authorize]
[Produces("application/json")]
public class VoteController : Controller
{
public VoteController(IVoteEngine engine)
{
_engine = engine;
}
private readonly IVoteEngine _engine;
/// <summary>
/// Get's all the votes for the request id
/// </summary>
/// <returns></returns>
[HttpGet("movie/{requestId:int}")]
public Task<List<Votes>> MovieVotes(int requestId)
{
return _engine.GetVotes(requestId, RequestType.Movie).ToListAsync();
}
/// <summary>
/// Get's all the votes for the request id
/// </summary>
/// <returns></returns>
[HttpGet("music/{requestId:int}")]
public Task<List<Votes>> MusicVotes(int requestId)
{
return _engine.GetVotes(requestId, RequestType.Album).ToListAsync();
}
/// <summary>
/// Get's all the votes for the request id
/// </summary>
/// <returns></returns>
[HttpGet("tv/{requestId:int}")]
public Task<List<Votes>> TvVotes(int requestId)
{
return _engine.GetVotes(requestId, RequestType.TvShow).ToListAsync();
}
}
}