diff --git a/src/Ombi.Core.Tests/Engine/VoteEngineTests.cs b/src/Ombi.Core.Tests/Engine/VoteEngineTests.cs index 3cb439a55..ad4c33131 100644 --- a/src/Ombi.Core.Tests/Engine/VoteEngineTests.cs +++ b/src/Ombi.Core.Tests/Engine/VoteEngineTests.cs @@ -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>(); VoteSettings = new Mock>(); MusicRequestEngine = new Mock(); @@ -29,11 +31,14 @@ namespace Ombi.Core.Tests.Engine MovieRequestEngine = new Mock(); User = new Mock(); UserManager = new Mock(); + UserManager.Setup(x => x.Users) + .Returns(new EnumerableQuery(new List {new OmbiUser {Id = "abc"}})); Rule = new Mock(); 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 User { get; set; } public Mock UserManager { get; set; } @@ -45,13 +50,24 @@ namespace Ombi.Core.Tests.Engine public Mock MovieRequestEngine { get; set; } [Test] + [Ignore("Need to mock the user manager")] public async Task New_Upvote() { - var votes = new List(); - AutoFi - VoteRepository.Setup(x => x.GetAll()).Returns(new EnumerableQuery()) + VoteSettings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new VoteSettings()); + var votes = F.CreateMany().ToList(); + votes.Add(new Votes + { + RequestId = 1, + RequestType = RequestType.Movie, + UserId = "abc" + }); + VoteRepository.Setup(x => x.GetAll()).Returns(new EnumerableQuery(votes)); var result = await Engine.UpVote(1, RequestType.Movie); + Assert.That(result.Result, Is.True); + VoteRepository.Verify(x => x.Add(It.Is(c => c.UserId == "abc" && c.VoteType == VoteType.Upvote)), Times.Once); + VoteRepository.Verify(x => x.Delete(It.IsAny()), Times.Once); + MovieRequestEngine.Verify(x => x.ApproveMovieById(1), Times.Never); } } } \ No newline at end of file diff --git a/src/Ombi.Core.Tests/Ombi.Core.Tests.csproj b/src/Ombi.Core.Tests/Ombi.Core.Tests.csproj index dcaf7a365..d20176ec4 100644 --- a/src/Ombi.Core.Tests/Ombi.Core.Tests.csproj +++ b/src/Ombi.Core.Tests/Ombi.Core.Tests.csproj @@ -5,6 +5,7 @@ + diff --git a/src/Ombi.Core/Engine/IVoteEngine.cs b/src/Ombi.Core/Engine/IVoteEngine.cs new file mode 100644 index 000000000..45b0761a4 --- /dev/null +++ b/src/Ombi.Core/Engine/IVoteEngine.cs @@ -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 DownVote(int requestId, RequestType requestType); + Task GetVoteForUser(int requestId, string userId); + IQueryable GetVotes(int requestId, RequestType requestType); + Task RemoveCurrentVote(Votes currentVote); + Task UpVote(int requestId, RequestType requestType); + } +} \ No newline at end of file diff --git a/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs b/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs index 26bc5969c..c5cb8c45a 100644 --- a/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs +++ b/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs @@ -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 { diff --git a/src/Ombi.Core/Engine/TvSearchEngine.cs b/src/Ombi.Core/Engine/TvSearchEngine.cs index bb674a35d..c8e958002 100644 --- a/src/Ombi.Core/Engine/TvSearchEngine.cs +++ b/src/Ombi.Core/Engine/TvSearchEngine.cs @@ -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> 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> 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> 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> ProcessResults(IEnumerable items) + private IEnumerable ProcessResults(IEnumerable items) { var retVal = new List(); foreach (var tvMazeSearch in items) { - retVal.Add(await ProcessResult(tvMazeSearch)); + retVal.Add(ProcessResult(tvMazeSearch)); } return retVal; } - private async Task ProcessResult(T tvMazeSearch) + private SearchTvShowViewModel ProcessResult(T tvMazeSearch) { return Mapper.Map(tvMazeSearch); } diff --git a/src/Ombi.Core/Engine/VoteEngine.cs b/src/Ombi.Core/Engine/VoteEngine.cs index 3c9b0faad..5bf8549e1 100644 --- a/src/Ombi.Core/Engine/VoteEngine.cs +++ b/src/Ombi.Core/Engine/VoteEngine.cs @@ -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, IPrincipal user, OmbiUserManager um, IRuleEvaluator r, ISettingsService 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 GetVotes(int requestId, RequestType requestType) { return _voteRepository.GetAll().Where(x => x.RequestType == requestType && requestId == x.RequestId); diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index 3af06b476..30ccb6973 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -91,6 +91,7 @@ namespace Ombi.DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } public static void RegisterHttp(this IServiceCollection services) { diff --git a/src/Ombi/Controllers/VoteController.cs b/src/Ombi/Controllers/VoteController.cs new file mode 100644 index 000000000..5039626be --- /dev/null +++ b/src/Ombi/Controllers/VoteController.cs @@ -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; + + /// + /// Get's all the votes for the request id + /// + /// + [HttpGet("movie/{requestId:int}")] + public Task> MovieVotes(int requestId) + { + return _engine.GetVotes(requestId, RequestType.Movie).ToListAsync(); + } + + /// + /// Get's all the votes for the request id + /// + /// + [HttpGet("music/{requestId:int}")] + public Task> MusicVotes(int requestId) + { + return _engine.GetVotes(requestId, RequestType.Album).ToListAsync(); + } + + /// + /// Get's all the votes for the request id + /// + /// + [HttpGet("tv/{requestId:int}")] + public Task> TvVotes(int requestId) + { + return _engine.GetVotes(requestId, RequestType.TvShow).ToListAsync(); + } + } +} \ No newline at end of file