Made a start on the VoteEngine !wip

pull/2556/head
Jamie Rees 6 years ago
parent b8739142ca
commit 8b9c375628

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Core.Authentication;
using Ombi.Core.Engine.Interfaces;
using Ombi.Core.Rule.Interfaces;
using Ombi.Store.Entities;
using Ombi.Store.Repository;
using Ombi.Store.Repository.Requests;
namespace Ombi.Core.Engine
{
public class VoteEngine : BaseEngine
{
public VoteEngine(IRepository<Votes> votes, IPrincipal user, OmbiUserManager um, IRuleEvaluator r) : base(user, um, r)
{
_voteRepository = votes;
}
private readonly IRepository<Votes> _voteRepository;
public async Task<Votes> GetVotesForMovie(int requestId)
{
return await _voteRepository.GetAll().FirstOrDefaultAsync(x => x.RequestType == RequestType.Movie && x.RequestId == requestId);
}
public IQueryable<Votes> GetVotesForMovie(IEnumerable<int> requestIds)
{
return _voteRepository.GetAll().Where(x => x.RequestType == RequestType.Movie && requestIds.Contains(x.RequestId));
}
public async Task<Votes> GetVotesForTv(int requestId)
{
return await _voteRepository.GetAll().FirstOrDefaultAsync(x => x.RequestType == RequestType.TvShow && x.RequestId == requestId);
}
public IQueryable<Votes> GetVotesForTv(IEnumerable<int> requestIds)
{
return _voteRepository.GetAll().Where(x => x.RequestType == RequestType.TvShow && requestIds.Contains(x.RequestId));
}
public async Task UpvoteMovie(int requestId)
{
var user = await GetUser();
await _voteRepository.Add(new Votes
{
Date = DateTime.UtcNow,
RequestId = requestId,
RequestType = RequestType.Movie,
UserId = user.Id,
VoteType = VoteType.Upvote
});
}
public async Task DownvoteMovie(int requestId)
{
var user = await GetUser();
await _voteRepository.Add(new Votes
{
Date = DateTime.UtcNow,
RequestId = requestId,
RequestType = RequestType.Movie,
UserId = user.Id,
VoteType = VoteType.Downvote
});
}
public async Task UpvoteTv(int requestId)
{
var user = await GetUser();
await _voteRepository.Add(new Votes
{
Date = DateTime.UtcNow,
RequestId = requestId,
RequestType = RequestType.TvShow,
UserId = user.Id,
VoteType = VoteType.Upvote
});
}
public async Task DownvoteTv(int requestId)
{
var user = await GetUser();
await _voteRepository.Add(new Votes
{
Date = DateTime.UtcNow,
RequestId = requestId,
RequestType = RequestType.TvShow,
UserId = user.Id,
VoteType = VoteType.Downvote
});
}
}
}

@ -25,6 +25,7 @@ namespace Ombi.Store.Context
DbSet<TEntity> Set<TEntity>() where TEntity : class;
DbSet<NotificationTemplates> NotificationTemplates { get; set; }
DbSet<ApplicationConfiguration> ApplicationConfigurations { get; set; }
DbSet<Votes> Votes { get; set; }
void Seed();
DbSet<Audit> Audit { get; set; }
DbSet<MovieRequests> MovieRequests { get; set; }

@ -39,6 +39,7 @@ namespace Ombi.Store.Context
public DbSet<IssueComments> IssueComments { get; set; }
public DbSet<RequestLog> RequestLogs { get; set; }
public DbSet<RecentlyAddedLog> RecentlyAddedLogs { get; set; }
public DbSet<Votes> Votes { get; set; }
public DbSet<Audit> Audit { get; set; }

@ -0,0 +1,25 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace Ombi.Store.Entities
{
[Table("Votes")]
public class Votes : Entity
{
public int RequestId { get; set; }
public VoteType VoteType { get; set; }
public RequestType RequestType { get; set; }
public string UserId { get; set; }
public DateTime Date { get; set; }
public bool Deleted { get; set; }
[ForeignKey(nameof(UserId))]
public OmbiUser User { get; set; }
}
public enum VoteType
{
Upvote = 0,
Downvote = 1
}
}

@ -169,6 +169,8 @@ namespace Ombi
{
// Generate a API Key
settings.ApiKey = Guid.NewGuid().ToString("N");
var userManager = app.ApplicationServices.GetService<OmbiUserManager>();
userManager.CreateAsync(new OmbiUser {UserName = "API User", UserType = UserType.LocalUser}).Wait();
ombiService.SaveSettings(settings);
}
if (settings.BaseUrl.HasValue())

Loading…
Cancel
Save