tesses-cms/Tesses.CMS.Providers.LiteDb/Class1.cs

284 lines
9.1 KiB
C#
Raw Normal View History

2024-01-04 02:53:13 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LiteDB;
using Tesses.CMS;
namespace Tesses.CMS.Providers {
public class LiteDBContentProvider : IContentProvider
{
string dir;
ILiteDatabase db;
public LiteDBContentProvider(string dir)
{
this.dir = dir;
db=new LiteDatabase(Path.Combine(dir,"tesses-cms.db"));
}
public Movie CreateMovie(string user, string movie, string properName, string description)
{
var userId=GetUserAccount(user).Id;
Movie _movie = new Movie(){UserId = userId,Name = movie,ProperName=properName,Description = description};
_movie.Id=Movies.Insert(_movie);
return _movie;
}
public Show CreateShow(string user, string show, string properName, string description)
{
var userId=GetUserAccount(user).Id;
Show _show = new Show(){UserId = userId,Name = show,ProperName=properName,Description = description};
_show.Id=Shows.Insert(_show);
return _show;
}
private ILiteCollection<UserAccount> UserAccounts => db.GetCollection<UserAccount>("users");
private ILiteCollection<Movie> Movies => db.GetCollection<Movie>("movies");
private ILiteCollection<Show> Shows => db.GetCollection<Show>("shows");
private ILiteCollection<Season> Seasons => db.GetCollection<Season>("seasons");
private ILiteCollection<Episode> Episodes => db.GetCollection<Episode>("episodes");
private ILiteCollection<LiteDbSession> Sessions => db.GetCollection<LiteDbSession>("sessions");
private ILiteCollection<LiteDbVerificationToken> VerificationCodes => db.GetCollection<LiteDbVerificationToken>("verificationcodes");
public UserAccount GetFirstUser()
{
return GetUsers().First();
}
public Movie GetMovie(string user, string movie)
{
var userId=GetUserAccount(user).Id;
return Movies.FindOne(e=>e.Name == movie && e.UserId == userId);
}
public Show GetShow(string user,string show)
{
var userId=GetUserAccount(user).Id;
return Shows.FindOne(e=>e.Name == show && e.UserId == userId);
}
public IEnumerable<Movie> GetMovies(string user)
{
var userId = GetUserAccount(user).Id;
return GetMovies(userId);
}
public IEnumerable<Movie> GetMovies(long user)
{
return Movies.Find(e=>e.UserId==user);
}
public IEnumerable<Show> GetShows(long user)
{
return Shows.Find(e=>e.UserId==user);
}
public IEnumerable<Show> GetShows(string user)
{
var userId = GetUserAccount(user).Id;
return GetShows(userId);
}
public UserAccount GetUserAccount(string user)
{
return UserAccounts.FindOne(e=>e.Username == user);
}
public IEnumerable<UserAccount> GetUsers()
{
return UserAccounts.FindAll();
}
public void CreateUser(CMSConfiguration configuration,string user,string properName,string email,string password)
{
bool isOwner = UserAccounts.Count() == 0;
UserAccount account=new UserAccount();
account.IsAdmin=isOwner;
account.IsVerified = isOwner;
account.Username = user;
account.ProperName= properName;
account.NewSalt();
account.PasswordHash=account.GetPasswordHash(password);
account.Email = email;
UserAccounts.Insert(account);
}
public void UpdateMovie(Movie movie)
{
Movies.Update(movie);
}
public void UpdateShow(Show show)
{
Shows.Update(show);
}
public void UpdateUser(UserAccount account)
{
UserAccounts.Update(account);
}
public UserAccount GetUserById(long account)
{
return UserAccounts.FindById(account);
}
public class LiteDbSession
{
public long Id {get;set;}
public string Session {get;set;}
public long Account {get;set;}
}
public class LiteDbVerificationToken
{
public long Id {get;set;}
public string Code {get;set;}
public long Account {get;set;}
public DateTime Expires {get;set;}
}
public void CreateSession(string session, long account)
{
LiteDbSession _session=new LiteDbSession();
_session.Account = account;
_session.Session = session;
Sessions.Insert(_session);
}
public void ChangeSession(string session, long account)
{
var s=Sessions.FindOne(e=>e.Session == session);
if(s!=null){s.Account = account; Sessions.Update(s);}
}
public void DeleteSession(string session)
{
var s=Sessions.FindOne(e=>e.Session == session);
if(s!=null){ Sessions.Delete(s.Id);}
}
public long? GetSession(string session)
{
var s=Sessions.FindOne(e=>e.Session == session);
return s != null ? s.Account : (long?)null;
}
public bool ContainsSession(string cookie)
{
return Sessions.Exists(e=>e.Session == cookie);
}
public void CreateVerificationCode(string code, long account)
{
LiteDbVerificationToken liteDbVerificationToken=new LiteDbVerificationToken();
liteDbVerificationToken.Account = account;
liteDbVerificationToken.Code = code;
liteDbVerificationToken.Expires=DateTime.Now.AddDays(1);
VerificationCodes.Insert(liteDbVerificationToken);
}
public void DeleteVerificationCode(string code)
{
var c = VerificationCodes.FindOne(e=>e.Code==code);
if(c != null){VerificationCodes.Delete(c.Id);}
}
public long? GetVerificationAccount(string code)
{
var c = VerificationCodes.FindOne(e=>e.Code==code);
if(c != null){
if(DateTime.Now > c.Expires)
VerificationCodes.Delete(c.Id);
else
return c.Account;
}
return null;
}
public bool ContainsVerificationCode(string code)
{
return VerificationCodes.Exists(e=>e.Code==code);
}
public int SeasonCount(string user, string show)
{
var myShow = GetShow(user,show);
var userId = myShow.UserId;
var showId = myShow.Id;
int seasonLargest=0;
foreach(var item in Seasons.Find(e=>e.ShowId==showId && e.UserId == userId))
{
if(item.SeasonNumber > seasonLargest)
seasonLargest = item.SeasonNumber;
}
return seasonLargest;
}
public Season GetSeason(string user, string show, int season)
{
var myShow = GetShow(user,show);
var userId = myShow.UserId;
var showId = myShow.Id;
return Seasons.FindOne(e=>e.ShowId == showId && e.UserId == userId && e.SeasonNumber == season);
}
public Season CreateSeason(string user, string show, int season, string properName, string description)
{
var myShow = GetShow(user,show);
var userId = myShow.UserId;
var showId = myShow.Id;
Season _season = new Season(){UserId = userId,ShowId = showId,ProperName=properName,Description = description, SeasonNumber=season};
_season.Id=Seasons.Insert(_season);
return _season;
}
public int EpisodeCount(string user, string show, int season)
{
var myShow = GetShow(user,show);
var userId = myShow.UserId;
var showId = myShow.Id;
int episodeLargest=0;
foreach(var item in Episodes.Find(e=>e.ShowId==showId && e.UserId == userId && e.SeasonNumber == season))
{
if(item.EpisodeNumber > episodeLargest)
episodeLargest = item.EpisodeNumber;
}
return episodeLargest;
}
public Episode GetEpisode(string user, string show, int season, int episode)
{
var myShow = GetShow(user,show);
var userId = myShow.UserId;
var showId = myShow.Id;
return Episodes.FindOne(e=>e.ShowId == showId && e.UserId == userId && e.SeasonNumber == season && e.EpisodeNumber == episode);
}
public Episode CreateEpisode(string user, string show, int season, int episode, string episodename, string properName, string description)
{
var myShow = GetShow(user,show);
var userId = myShow.UserId;
var showId = myShow.Id;
Episode _episode = new Episode(){UserId = userId,ShowId = showId,ProperName=properName,Description = description, SeasonNumber=season, EpisodeNumber = episode,EpisodeName=episodename};
_episode.Id=Episodes.Insert(_episode);
return _episode;
}
public void UpdateEpisode(Episode episode)
{
Episodes.Update(episode);
}
public void UpdateSeason(Season season)
{
Seasons.Update(season);
}
}
}