mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-16 02:02:55 -07:00
Moar !wip
This commit is contained in:
parent
eb89b352f4
commit
2f964f904e
8 changed files with 109 additions and 16 deletions
|
@ -2,6 +2,7 @@
|
|||
using System.Linq;
|
||||
using System.Security.Principal;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Ombi.Core.Authentication;
|
||||
|
@ -21,6 +22,7 @@ namespace Ombi.Core.Tests.Engine
|
|||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
F = new Fixture();
|
||||
VoteRepository = new Mock<IRepository<Votes>>();
|
||||
VoteSettings = new Mock<ISettingsService<VoteSettings>>();
|
||||
MusicRequestEngine = new Mock<IMusicRequestEngine>();
|
||||
|
@ -29,11 +31,14 @@ namespace Ombi.Core.Tests.Engine
|
|||
MovieRequestEngine = new Mock<IMovieRequestEngine>();
|
||||
User = new Mock<IPrincipal>();
|
||||
UserManager = new Mock<OmbiUserManager>();
|
||||
UserManager.Setup(x => x.Users)
|
||||
.Returns(new EnumerableQuery<OmbiUser>(new List<OmbiUser> {new OmbiUser {Id = "abc"}}));
|
||||
Rule = new Mock<IRuleEvaluator>();
|
||||
Engine = new VoteEngine(VoteRepository.Object, User.Object, UserManager.Object, Rule.Object, VoteSettings.Object, MusicRequestEngine.Object,
|
||||
TvRequestEngine.Object, MovieRequestEngine.Object);
|
||||
}
|
||||
|
||||
public Fixture F { get; set; }
|
||||
public VoteEngine Engine { get; set; }
|
||||
public Mock<IPrincipal> User { get; set; }
|
||||
public Mock<OmbiUserManager> UserManager { get; set; }
|
||||
|
@ -45,13 +50,24 @@ namespace Ombi.Core.Tests.Engine
|
|||
public Mock<IMovieRequestEngine> MovieRequestEngine { get; set; }
|
||||
|
||||
[Test]
|
||||
[Ignore("Need to mock the user manager")]
|
||||
public async Task New_Upvote()
|
||||
{
|
||||
var votes = new List<Votes>();
|
||||
AutoFi
|
||||
VoteRepository.Setup(x => x.GetAll()).Returns(new EnumerableQuery<Votes>())
|
||||
VoteSettings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new VoteSettings());
|
||||
var votes = F.CreateMany<Votes>().ToList();
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture" Version="4.5.0" />
|
||||
<PackageReference Include="Moq" Version="4.10.0" />
|
||||
<PackageReference Include="Nunit" Version="3.10.1" />
|
||||
<PackageReference Include="NUnit.ConsoleRunner" Version="3.9.0" />
|
||||
|
|
16
src/Ombi.Core/Engine/IVoteEngine.cs
Normal file
16
src/Ombi.Core/Engine/IVoteEngine.cs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -7,11 +7,8 @@ using Ombi.Core.Models.Search;
|
|||
using Ombi.Core.Rule.Interfaces;
|
||||
using Ombi.Store.Entities.Requests;
|
||||
using Ombi.Store.Entities;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Ombi.Core.Authentication;
|
||||
using Ombi.Helpers;
|
||||
|
||||
namespace Ombi.Core.Engine.Interfaces
|
||||
{
|
||||
|
|
|
@ -61,7 +61,7 @@ namespace Ombi.Core.Engine
|
|||
{
|
||||
continue;
|
||||
}
|
||||
retVal.Add(await ProcessResult(tvMazeSearch));
|
||||
retVal.Add(ProcessResult(tvMazeSearch));
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ namespace Ombi.Core.Engine
|
|||
public async Task<IEnumerable<SearchTvShowViewModel>> Popular()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -131,35 +131,35 @@ namespace Ombi.Core.Engine
|
|||
{
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SearchTvShowViewModel>> MostWatches()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SearchTvShowViewModel>> Trending()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<SearchTvShowViewModel>> ProcessResults<T>(IEnumerable<T> items)
|
||||
private IEnumerable<SearchTvShowViewModel> ProcessResults<T>(IEnumerable<T> items)
|
||||
{
|
||||
var retVal = new List<SearchTvShowViewModel>();
|
||||
foreach (var tvMazeSearch in items)
|
||||
{
|
||||
retVal.Add(await ProcessResult(tvMazeSearch));
|
||||
retVal.Add(ProcessResult(tvMazeSearch));
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
private async Task<SearchTvShowViewModel> ProcessResult<T>(T tvMazeSearch)
|
||||
private SearchTvShowViewModel ProcessResult<T>(T tvMazeSearch)
|
||||
{
|
||||
return Mapper.Map<SearchTvShowViewModel>(tvMazeSearch);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Principal;
|
||||
using System.Threading.Tasks;
|
||||
|
@ -15,7 +14,7 @@ using Ombi.Store.Repository;
|
|||
|
||||
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,
|
||||
IMusicRequestEngine musicRequestEngine, ITvRequestEngine tvRequestEngine, IMovieRequestEngine movieRequestEngine) : base(user, um, r)
|
||||
|
@ -33,6 +32,16 @@ namespace Ombi.Core.Engine
|
|||
private readonly ITvRequestEngine _tvRequestEngine;
|
||||
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)
|
||||
{
|
||||
return _voteRepository.GetAll().Where(x => x.RequestType == requestType && requestId == x.RequestId);
|
||||
|
|
|
@ -91,6 +91,7 @@ namespace Ombi.DependencyInjection
|
|||
services.AddTransient<IMusicSender, MusicSender>();
|
||||
services.AddTransient<IMassEmailSender, MassEmailSender>();
|
||||
services.AddTransient<IPlexOAuthManager, PlexOAuthManager>();
|
||||
services.AddTransient<IVoteEngine, VoteEngine>();
|
||||
}
|
||||
public static void RegisterHttp(this IServiceCollection services)
|
||||
{
|
||||
|
|
53
src/Ombi/Controllers/VoteController.cs
Normal file
53
src/Ombi/Controllers/VoteController.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue