1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Updated models to use virtual for all entity types. This allows Lazy Loading of all entities.

This commit is contained in:
Uncled1023 2016-06-16 10:52:59 -07:00
parent a3a1823dc1
commit 0bfe971504
26 changed files with 70 additions and 68 deletions

View File

@ -113,7 +113,7 @@ namespace Teknik.Areas.API.Controllers
}
// Save the file data
Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize);
Upload.Models.Upload upload = Uploader.SaveFile(db, Config, (encrypt) ? data : fileData, contentType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize);
if (upload != null)
{

View File

@ -32,7 +32,7 @@ namespace Teknik.Areas.Blog.Controllers
ViewBag.Title = Config.BlogConfig.Title + " - " + Config.Title;
ViewBag.Description = Config.BlogConfig.Description;
bool isAuth = User.IsInRole("Admin");
var foundPosts = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => ((p.System || isAuth) && p.Published));
var foundPosts = db.BlogPosts.Where(p => ((p.System || isAuth) && p.Published));
model = new BlogViewModel();
model.BlogId = Config.BlogConfig.ServerBlogId;
@ -47,7 +47,7 @@ namespace Teknik.Areas.Blog.Controllers
}
else // A user specific blog
{
Models.Blog blog = db.Blogs.Include("User").Where(p => p.User.Username == username && p.BlogId != Config.BlogConfig.ServerBlogId).FirstOrDefault();
Models.Blog blog = db.Blogs.Where(p => p.User.Username == username && p.BlogId != Config.BlogConfig.ServerBlogId).FirstOrDefault();
// find the blog specified
if (blog != null)
{
@ -58,7 +58,7 @@ namespace Teknik.Areas.Blog.Controllers
}
ViewBag.Description = blog.User.BlogSettings.Description;
bool isAuth = User.IsInRole("Admin");
var foundPosts = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blog.BlogId && !p.System) &&
var foundPosts = db.BlogPosts.Where(p => (p.BlogId == blog.BlogId && !p.System) &&
(p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
model = new BlogViewModel();
model.BlogId = blog.BlogId;
@ -87,7 +87,7 @@ namespace Teknik.Areas.Blog.Controllers
PostViewModel model = new PostViewModel();
// find the post specified
bool isAuth = User.IsInRole("Admin");
var post = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => (p.Blog.User.Username == username && p.BlogPostId == id) &&
var post = db.BlogPosts.Where(p => (p.Blog.User.Username == username && p.BlogPostId == id) &&
(p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
if (post != null)
{
@ -120,7 +120,7 @@ namespace Teknik.Areas.Blog.Controllers
public ActionResult GetPosts(int blogID, int startPostID, int count)
{
bool isAuth = User.IsInRole("Admin");
var posts = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => ((p.BlogId == blogID && !p.System) || (p.System && blogID == Config.BlogConfig.ServerBlogId)) &&
var posts = db.BlogPosts.Where(p => ((p.BlogId == blogID && !p.System) || (p.System && blogID == Config.BlogConfig.ServerBlogId)) &&
(p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList();
List<PostViewModel> postViews = new List<PostViewModel>();
if (posts != null)
@ -138,7 +138,7 @@ namespace Teknik.Areas.Blog.Controllers
public ActionResult GetPostTitle(int postID)
{
bool isAuth = User.IsInRole("Admin");
BlogPost post = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => (p.BlogPostId == postID) && (p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
BlogPost post = db.BlogPosts.Where(p => (p.BlogPostId == postID) && (p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
if (post != null)
{
return Json(new { result = post.Title });
@ -151,7 +151,7 @@ namespace Teknik.Areas.Blog.Controllers
public ActionResult GetPostArticle(int postID)
{
bool isAuth = User.IsInRole("Admin");
BlogPost post = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => (p.BlogPostId == postID) && (p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
BlogPost post = db.BlogPosts.Where(p => (p.BlogPostId == postID) && (p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
if (post != null)
{
return Json(new { result = post.Article });
@ -169,7 +169,7 @@ namespace Teknik.Areas.Blog.Controllers
bool system = (blogID == Config.BlogConfig.ServerBlogId);
if (system)
{
var user = db.Blogs.Include("User").Where(b => b.User.Username == User.Identity.Name);
var user = db.Blogs.Where(b => b.User.Username == User.Identity.Name);
if (user != null)
{
blogID = user.First().BlogId;
@ -198,7 +198,7 @@ namespace Teknik.Areas.Blog.Controllers
{
if (ModelState.IsValid)
{
BlogPost post = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => p.BlogPostId == postID).FirstOrDefault();
BlogPost post = db.BlogPosts.Where(p => p.BlogPostId == postID).FirstOrDefault();
if (post != null)
{
if (User.IsInRole("Admin") || post.Blog.User.Username == User.Identity.Name)
@ -222,7 +222,7 @@ namespace Teknik.Areas.Blog.Controllers
{
if (ModelState.IsValid)
{
BlogPost post = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => p.BlogPostId == postID).FirstOrDefault();
BlogPost post = db.BlogPosts.Where(p => p.BlogPostId == postID).FirstOrDefault();
if (post != null)
{
if (User.IsInRole("Admin") || post.Blog.User.Username == User.Identity.Name)
@ -246,7 +246,7 @@ namespace Teknik.Areas.Blog.Controllers
{
if (ModelState.IsValid)
{
BlogPost post = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => p.BlogPostId == postID).FirstOrDefault();
BlogPost post = db.BlogPosts.Where(p => p.BlogPostId == postID).FirstOrDefault();
if (post != null)
{
if (User.IsInRole("Admin") || post.Blog.User.Username == User.Identity.Name)
@ -268,7 +268,7 @@ namespace Teknik.Areas.Blog.Controllers
[AllowAnonymous]
public ActionResult GetComments(int postID, int startCommentID, int count)
{
var comments = db.BlogComments.Include("BlogPost").Include("BlogPost.Blog").Include("BlogPost.Blog.User").Include("User").Where(p => (p.BlogPostId == postID)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
var comments = db.BlogComments.Where(p => (p.BlogPostId == postID)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
List<CommentViewModel> commentViews = new List<CommentViewModel>();
if (comments != null)
{
@ -320,7 +320,7 @@ namespace Teknik.Areas.Blog.Controllers
{
if (ModelState.IsValid)
{
BlogPostComment comment = db.BlogComments.Include("User").Where(c => c.BlogPostCommentId == commentID).FirstOrDefault();
BlogPostComment comment = db.BlogComments.Where(c => c.BlogPostCommentId == commentID).FirstOrDefault();
if (comment != null)
{
if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
@ -343,7 +343,7 @@ namespace Teknik.Areas.Blog.Controllers
{
if (ModelState.IsValid)
{
BlogPostComment comment = db.BlogComments.Include("User").Where(c => c.BlogPostCommentId == commentID).FirstOrDefault();
BlogPostComment comment = db.BlogComments.Where(c => c.BlogPostCommentId == commentID).FirstOrDefault();
if (comment != null)
{
if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))

View File

@ -10,8 +10,8 @@ namespace Teknik.Areas.Blog.Models
public int UserId { get; set; }
public User User { get; set; }
public virtual User User { get; set; }
public List<BlogPost> BlogPosts { get; set; }
public virtual ICollection<BlogPost> BlogPosts { get; set; }
}
}

View File

@ -10,7 +10,7 @@ namespace Teknik.Areas.Blog.Models
public int BlogId { get; set; }
public Blog Blog { get; set; }
public virtual Blog Blog { get; set; }
public bool System { get; set; }
@ -28,6 +28,6 @@ namespace Teknik.Areas.Blog.Models
public List<string> Tags { get; set; }
public List<BlogPostComment> Comments { get; set; }
public virtual ICollection<BlogPostComment> Comments { get; set; }
}
}

View File

@ -12,11 +12,11 @@ namespace Teknik.Areas.Blog.Models
public int BlogPostId { get; set; }
public BlogPost BlogPost { get; set; }
public virtual BlogPost BlogPost { get; set; }
public int? UserId { get; set; }
public User User { get; set; }
public virtual User User { get; set; }
public DateTime DatePosted { get; set; }
public DateTime DateEdited { get; set; }

View File

@ -52,7 +52,7 @@ namespace Teknik.Areas.Blog.ViewModels
Title = post.Title;
Tags = post.Tags;
Article = post.Article;
Comments = post.Comments;
Comments = post.Comments.ToList();
}
}
}

View File

@ -13,7 +13,7 @@ namespace Teknik.Areas.Paste.Models
public int? UserId { get; set; }
public User User { get; set; }
public virtual User User { get; set; }
public DateTime DatePosted { get; set; }

View File

@ -59,7 +59,7 @@ namespace Teknik.Areas.Podcast.Controllers
PodcastViewModel model = new PodcastViewModel();
// find the podcast specified
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Include("Files").Where(p => ((p.Published || editor) && p.Episode == episode)).FirstOrDefault();
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.Episode == episode)).FirstOrDefault();
if (foundPodcast != null)
{
model = new PodcastViewModel(foundPodcast);
@ -76,7 +76,7 @@ namespace Teknik.Areas.Podcast.Controllers
public ActionResult Download(int episode, string fileName)
{
// find the podcast specified
var foundPodcast = db.Podcasts.Include("Files").Where(p => (p.Published && p.Episode == episode)).FirstOrDefault();
var foundPodcast = db.Podcasts.Where(p => (p.Published && p.Episode == episode)).FirstOrDefault();
if (foundPodcast != null)
{
PodcastFile file = foundPodcast.Files.Where(f => f.FileName == fileName).FirstOrDefault();
@ -108,7 +108,7 @@ namespace Teknik.Areas.Podcast.Controllers
public ActionResult GetPodcasts(int startPodcastID, int count)
{
bool editor = User.IsInRole("Podcast");
var podcasts = db.Podcasts.Include("Files").Where(p => p.Published || editor).OrderByDescending(p => p.DatePosted).Skip(startPodcastID).Take(count).ToList();
var podcasts = db.Podcasts.Where(p => p.Published || editor).OrderByDescending(p => p.DatePosted).Skip(startPodcastID).Take(count).ToList();
List<PodcastViewModel> podcastViews = new List<PodcastViewModel>();
if (podcasts != null)
{
@ -164,7 +164,7 @@ namespace Teknik.Areas.Podcast.Controllers
public ActionResult GetPodcastFiles(int podcastId)
{
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Include("Files").Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
if (foundPodcast != null)
{
List<object> files = new List<object>();
@ -221,7 +221,7 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (User.IsInRole("Podcast"))
{
Models.Podcast podcast = db.Podcasts.Include("Files").Where(p => p.PodcastId == podcastId).FirstOrDefault();
Models.Podcast podcast = db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault();
if (podcast != null)
{
if (db.Podcasts.Where(p => p.Episode != episode).FirstOrDefault() == null || podcast.Episode == episode)
@ -238,7 +238,7 @@ namespace Teknik.Areas.Podcast.Controllers
}
for (int i = 0; i < podcast.Files.Count; i++)
{
PodcastFile curFile = podcast.Files[i];
PodcastFile curFile = podcast.Files.ElementAt(i);
if (!fileIdList.Exists(id => id == curFile.PodcastFileId.ToString()))
{
if (System.IO.File.Exists(curFile.Path))
@ -251,7 +251,10 @@ namespace Teknik.Areas.Podcast.Controllers
}
// Add any new files
List<PodcastFile> newFiles = SaveFiles(Request.Files, episode);
podcast.Files.AddRange(newFiles);
foreach (PodcastFile file in newFiles)
{
podcast.Files.Add(file);
}
// Save podcast
db.Entry(podcast).State = EntityState.Modified;
@ -298,7 +301,7 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (User.IsInRole("Podcast"))
{
Models.Podcast podcast = db.Podcasts.Include("Files").Where(p => p.PodcastId == podcastId).FirstOrDefault();
Models.Podcast podcast = db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault();
if (podcast != null)
{
foreach (PodcastFile file in podcast.Files)
@ -322,7 +325,7 @@ namespace Teknik.Areas.Podcast.Controllers
[AllowAnonymous]
public ActionResult GetComments(int podcastId, int startCommentID, int count)
{
var comments = db.PodcastComments.Include("BlogPost").Include("BlogPost.Blog").Include("BlogPost.Blog.User").Include("User").Where(p => (p.PodcastId == podcastId)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
var comments = db.PodcastComments.Where(p => (p.PodcastId == podcastId)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
List<CommentViewModel> commentViews = new List<CommentViewModel>();
if (comments != null)
{
@ -374,7 +377,7 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (ModelState.IsValid)
{
PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
if (comment != null)
{
if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
@ -397,7 +400,7 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (ModelState.IsValid)
{
PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
if (comment != null)
{
if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))

View File

@ -15,7 +15,7 @@ namespace Teknik.Areas.Podcast.Models
public string Description { get; set; }
public List<PodcastFile> Files { get; set; }
public virtual ICollection<PodcastFile> Files { get; set; }
public List<string> Tags { get; set; }
@ -27,6 +27,6 @@ namespace Teknik.Areas.Podcast.Models
public DateTime DateEdited { get; set; }
public List<PodcastComment> Comments { get; set; }
public virtual ICollection<PodcastComment> Comments { get; set; }
}
}

View File

@ -12,11 +12,11 @@ namespace Teknik.Areas.Podcast.Models
public int PodcastId { get; set; }
public Podcast Podcast { get; set; }
public virtual Podcast Podcast { get; set; }
public int UserId { get; set; }
public User User { get; set; }
public virtual User User { get; set; }
public DateTime DatePosted { get; set; }

View File

@ -11,7 +11,7 @@ namespace Teknik.Areas.Podcast.Models
public int PodcastId { get; set; }
public Podcast Podcast { get; set; }
public virtual Podcast Podcast { get; set; }
public string FileName { get; set; }

View File

@ -37,7 +37,7 @@ namespace Teknik.Areas.Podcast.ViewModels
Episode = podcast.Episode;
Title = podcast.Title;
Description = podcast.Description;
Files = podcast.Files;
Files = podcast.Files.ToList();
Tags = podcast.Tags;
DatePosted = podcast.DatePosted;
Published = podcast.Published;

View File

@ -12,7 +12,7 @@ namespace Teknik.Areas.Shortener.Models
public int? UserId { get; set; }
public User User { get; set; }
public virtual User User { get; set; }
public string ShortUrl { get; set; }

View File

@ -22,6 +22,6 @@ namespace Teknik.Areas.Transparency.Models
public DateTime DateActionTaken { get; set; }
public List<Upload.Models.Upload> Attachments { get; set; }
public virtual ICollection<Upload.Models.Upload> Attachments { get; set; }
}
}

View File

@ -110,7 +110,7 @@ namespace Teknik.Areas.Upload.Controllers
return Json(new { error = new { message = "Unable to encrypt file" } });
}
}
Models.Upload upload = Uploader.SaveFile(fileData, fileType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize);
Models.Upload upload = Uploader.SaveFile(db, Config, fileData, fileType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize);
if (upload != null)
{
if (User.Identity.IsAuthenticated)

View File

@ -12,7 +12,7 @@ namespace Teknik.Areas.Upload.Models
public int? UserId { get; set; }
public User User { get; set; }
public virtual User User { get; set; }
public DateTime DateUploaded { get; set; }

View File

@ -10,30 +10,27 @@ namespace Teknik.Areas.Upload
{
public static class Uploader
{
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength)
public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength)
{
return SaveFile(file, contentType, contentLength, string.Empty, null, null, 256, 128);
return SaveFile(db, config, file, contentType, contentLength, string.Empty, null, null, 256, 128);
}
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string defaultExtension)
public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension)
{
return SaveFile(file, contentType, contentLength, defaultExtension, null, null, 256, 128);
return SaveFile(db, config, file, contentType, contentLength, defaultExtension, null, null, 256, 128);
}
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string defaultExtension, string iv)
public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension, string iv)
{
return SaveFile(file, contentType, contentLength, defaultExtension, iv, null, 256, 128);
return SaveFile(db, config, file, contentType, contentLength, defaultExtension, iv, null, 256, 128);
}
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key)
public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key)
{
return SaveFile(file, contentType, contentLength, defaultExtension, iv, key, 256, 128);
return SaveFile(db, config, file, contentType, contentLength, defaultExtension, iv, key, 256, 128);
}
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key, int keySize, int blockSize)
public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key, int keySize, int blockSize)
{
Config config = Config.Load();
TeknikEntities db = new TeknikEntities();
if (!Directory.Exists(config.UploadConfig.UploadDirectory))
{
Directory.CreateDirectory(config.UploadConfig.UploadDirectory);

View File

@ -14,8 +14,8 @@ namespace Teknik.Areas.Users.Models
public string Description { get; set; }
public List<User> Users { get; set; }
public virtual ICollection<User> Users { get; set; }
public List<Role> Roles { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
}

View File

@ -11,7 +11,7 @@ namespace Teknik.Areas.Users.Models
public int UserId { get; set; }
public User User { get; set; }
public virtual User User { get; set; }
public string Code { get; set; }

View File

@ -11,7 +11,7 @@ namespace Teknik.Areas.Users.Models
public int UserId { get; set; }
public User User { get; set; }
public virtual User User { get; set; }
public string Code { get; set; }

View File

@ -14,6 +14,6 @@ namespace Teknik.Areas.Users.Models
public string Description { get; set; }
public List<Group> Groups { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
}

View File

@ -24,7 +24,7 @@ namespace Teknik.Areas.Users.Models
public DateTime LastSeen { get; set; }
public List<Group> Groups { get; set; }
public virtual ICollection<Group> Groups { get; set; }
public virtual UserSettings UserSettings { get; set; }
@ -32,9 +32,9 @@ namespace Teknik.Areas.Users.Models
public virtual UploadSettings UploadSettings { get; set; }
public List<Upload.Models.Upload> Uploads { get; set; }
public virtual ICollection<Upload.Models.Upload> Uploads { get; set; }
public List<Paste.Models.Paste> Pastes { get; set; }
public virtual ICollection<Paste.Models.Paste> Pastes { get; set; }
public User()
{

View File

@ -8,6 +8,6 @@ namespace Teknik.Areas.Vault.Models
public class PasteItem : VaultItem
{
public int PasteId { get; set; }
public Paste.Models.Paste Paste { get; set; }
public virtual Paste.Models.Paste Paste { get; set; }
}
}

View File

@ -8,6 +8,6 @@ namespace Teknik.Areas.Vault.Models
public class UploadItem : VaultItem
{
public int UploadId { get; set; }
public Upload.Models.Upload Upload { get; set; }
public virtual Upload.Models.Upload Upload { get; set; }
}
}

View File

@ -9,12 +9,12 @@ namespace Teknik.Areas.Vault.Models
{
public int VaultId { get; set; }
public int? UserId { get; set; }
public Users.Models.User User { get; set; }
public virtual Users.Models.User User { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateEdited { get; set; }
public List<VaultItem> Items { get; set; }
public virtual ICollection<VaultItem> Items { get; set; }
public Vault()
{

View File

@ -8,6 +8,8 @@ namespace Teknik.Areas.Vault.Models
public class VaultItem
{
public int VaultItemId { get; set; }
public int VaultId { get; set; }
public virtual Vault Vault { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime DateAdded { get; set; }