mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-19 21:03:17 -07:00
Made a start on the VoteEngine !wip
This commit is contained in:
parent
b8739142ca
commit
8b9c375628
5 changed files with 125 additions and 0 deletions
96
src/Ombi.Core/Engine/VoteEngine.cs
Normal file
96
src/Ombi.Core/Engine/VoteEngine.cs
Normal file
|
@ -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<TEntity> Set<TEntity>() where TEntity : class;
|
||||||
DbSet<NotificationTemplates> NotificationTemplates { get; set; }
|
DbSet<NotificationTemplates> NotificationTemplates { get; set; }
|
||||||
DbSet<ApplicationConfiguration> ApplicationConfigurations { get; set; }
|
DbSet<ApplicationConfiguration> ApplicationConfigurations { get; set; }
|
||||||
|
DbSet<Votes> Votes { get; set; }
|
||||||
void Seed();
|
void Seed();
|
||||||
DbSet<Audit> Audit { get; set; }
|
DbSet<Audit> Audit { get; set; }
|
||||||
DbSet<MovieRequests> MovieRequests { get; set; }
|
DbSet<MovieRequests> MovieRequests { get; set; }
|
||||||
|
|
|
@ -39,6 +39,7 @@ namespace Ombi.Store.Context
|
||||||
public DbSet<IssueComments> IssueComments { get; set; }
|
public DbSet<IssueComments> IssueComments { get; set; }
|
||||||
public DbSet<RequestLog> RequestLogs { get; set; }
|
public DbSet<RequestLog> RequestLogs { get; set; }
|
||||||
public DbSet<RecentlyAddedLog> RecentlyAddedLogs { get; set; }
|
public DbSet<RecentlyAddedLog> RecentlyAddedLogs { get; set; }
|
||||||
|
public DbSet<Votes> Votes { get; set; }
|
||||||
|
|
||||||
|
|
||||||
public DbSet<Audit> Audit { get; set; }
|
public DbSet<Audit> Audit { get; set; }
|
||||||
|
|
25
src/Ombi.Store/Entities/Votes.cs
Normal file
25
src/Ombi.Store/Entities/Votes.cs
Normal file
|
@ -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
|
// Generate a API Key
|
||||||
settings.ApiKey = Guid.NewGuid().ToString("N");
|
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);
|
ombiService.SaveSettings(settings);
|
||||||
}
|
}
|
||||||
if (settings.BaseUrl.HasValue())
|
if (settings.BaseUrl.HasValue())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue