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

385 lines
13 KiB
C#

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;
return CreateMovie(userId,movie,properName,description);
}
public Show CreateShow(string user, string show, string properName, string description)
{
var userId=GetUserAccount(user).Id;
return CreateShow(userId,show,properName,description);
}
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");
private ILiteCollection<Album> Albums => db.GetCollection<Album>("albums");
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)
{
movie.LastUpdated = DateTime.Now;
Movies.Update(movie);
}
public void UpdateShow(Show show)
{
show.LastUpdated = DateTime.Now;
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;
return SeasonCount(userId,showId);
}
public Season GetSeason(string user, string show, int season)
{
var myShow = GetShow(user,show);
var userId = myShow.UserId;
var showId = myShow.Id;
return GetSeason(userId,showId,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;
return CreateSeason(userId,showId,season,properName,description);
}
public int EpisodeCount(string user, string show, int season)
{
var myShow = GetShow(user,show);
var userId = myShow.UserId;
var showId = myShow.Id;
return EpisodeCount(userId,showId,season);
}
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 GetEpisode(userId,showId,season,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;
return CreateEpisode(userId,showId,season,episode,episodename,properName,description);
}
public void UpdateEpisode(Episode episode)
{
episode.LastUpdated = DateTime.Now;
Episodes.Update(episode);
}
public void UpdateSeason(Season season)
{
season.LastUpdated = DateTime.Now;
Seasons.Update(season);
}
public Album CreateAlbum(string user, string album, string properName, string description)
{
var userId=GetUserAccount(user).Id;
return CreateAlbum(userId,album,properName,description);
}
public Album GetAlbum(string user, string album)
{
var userId=GetUserAccount(user).Id;
return GetAlbum(userId,album);
}
public void UpdateAlbum(Album album)
{
album.LastUpdated = DateTime.Now;
Albums.Update(album);
}
public IEnumerable<Album> GetAlbums(string user)
{
return GetAlbums(GetUserAccount(user).Id);
}
public IEnumerable<Album> GetAlbums(long user)
{
return Albums.Find(e=>e.UserId == user);
}
public Movie CreateMovie(long user, string movie, string properName, string description)
{
Movie _movie = new Movie(){UserId = user,Name = movie,ProperName=properName,Description = description};
_movie.CreationTime = DateTime.Now;
_movie.LastUpdated = DateTime.Now;
_movie.Id=Movies.Insert(_movie);
return _movie;
}
public Album CreateAlbum(long user, string album, string properName, string description)
{
Album _album = new Album(){UserId = user,Name = album,ProperName=properName,Description = description};
_album.CreationTime = DateTime.Now;
_album.LastUpdated = DateTime.Now;
_album.Id=Albums.Insert(_album);
return _album;
}
public Show CreateShow(long user, string show, string properName, string description)
{
Show _show = new Show(){UserId = user,Name = show,ProperName=properName,Description = description};
_show.CreationTime = DateTime.Now;
_show.LastUpdated = DateTime.Now;
_show.Id=Shows.Insert(_show);
return _show;
}
public Show GetShow(long user, long show)
{
return Shows.FindOne(e=>e.Id == show && e.UserId == user);
}
public int SeasonCount(long user, long show)
{
int seasonLargest=0;
foreach(var item in Seasons.Find(e=>e.ShowId==show && e.UserId == user))
{
if(item.SeasonNumber > seasonLargest)
seasonLargest = item.SeasonNumber;
}
return seasonLargest;
}
public Season GetSeason(long user, long show, int season)
{
return Seasons.FindOne(e=>e.ShowId == show && e.UserId == user && e.SeasonNumber == season);
}
public Season CreateSeason(long user, long show, int season, string properName, string description)
{
Season _season = new Season(){UserId = user,ShowId = show,ProperName=properName,Description = description, SeasonNumber=season};
_season.CreationTime = DateTime.Now;
_season.LastUpdated = DateTime.Now;
_season.Id=Seasons.Insert(_season);
return _season;
}
public int EpisodeCount(long user, long show, int season)
{
int episodeLargest=0;
foreach(var item in Episodes.Find(e=>e.ShowId==show && e.UserId == user && e.SeasonNumber == season))
{
if(item.EpisodeNumber > episodeLargest)
episodeLargest = item.EpisodeNumber;
}
return episodeLargest;
}
public Episode GetEpisode(long user, long show, int season, int episode)
{
return Episodes.FindOne(e=>e.ShowId == show && e.UserId == user && e.SeasonNumber == season && e.EpisodeNumber == episode);
}
public Episode CreateEpisode(long user, long show, int season, int episode, string episodename, string properName, string description)
{
Episode _episode = new Episode(){UserId = user,ShowId = show,ProperName=properName,Description = description, SeasonNumber=season, EpisodeNumber = episode,EpisodeName=episodename};
_episode.CreationTime = DateTime.Now;
_episode.LastUpdated = DateTime.Now;
_episode.Id=Episodes.Insert(_episode);
return _episode;
}
public Album GetAlbum(long user, string album)
{
return Albums.FindOne(e=>e.Name == album && e.UserId == user);
}
}
}