Browse Source

Fixed deleting of dependencies when deleting user.

tags/2.0.3
Teknikode 4 years ago
parent
commit
c3774bd811

+ 1
- 1
Teknik/Areas/Blog/Views/Blog/Comment.cshtml View File

@@ -7,7 +7,7 @@
<div class="col-sm-8 col-sm-offset-2">
<div class="post-comment">
<p class="post-comment-meta text-muted">
<a href="@Url.SubRouteUrl("profile", "Profile.Index", new { username = Model.Post.Blog.User.Username })">@Model.Post.Blog.User.Username</a> replied at <time datetime="@Model.DatePosted.ToString("o")">@Model.DatePosted.ToString("HH:mm:ss tt") on @Model.DatePosted.ToString("MMMM dd, yyyy")</time>
<a href="@Url.SubRouteUrl("profile", "Profile.Index", new { username = Model.Post.Blog.User.Username })">@Model.Post.Blog.User.Username</a> replied at <time datetime="@Model.DatePosted.ToString("o")">@Model.DatePosted.ToString("hh:mm:ss tt") on @Model.DatePosted.ToString("MMMM dd, yyyy")</time>
@if (Model.Post.Blog.User.Username == User.Identity.Name || User.IsInRole("Admin"))
{
<br />

+ 1
- 1
Teknik/Areas/Podcast/Views/Podcast/Comment.cshtml View File

@@ -7,7 +7,7 @@
<div class="col-sm-8 col-sm-offset-2">
<div class="post-comment">
<p class="post-comment-meta text-muted">
<a href="@Url.SubRouteUrl("profile", "Profile.Index", new { username = Model.User.Username })">@Model.User.Username</a> replied at <time datetime="@Model.DatePosted.ToString("o")">@Model.DatePosted.ToString("HH:mm:ss tt") on @Model.DatePosted.ToString("MMMM dd, yyyy")</time>
<a href="@Url.SubRouteUrl("profile", "Profile.Index", new { username = Model.User.Username })">@Model.User.Username</a> replied at <time datetime="@Model.DatePosted.ToString("o")">@Model.DatePosted.ToString("hh:mm:ss tt") on @Model.DatePosted.ToString("MMMM dd, yyyy")</time>
@if (Model.User.Username == User.Identity.Name || User.IsInRole("Admin"))
{
<br />

+ 44
- 2
Teknik/Areas/Profile/Controllers/ProfileController.cs View File

@@ -242,14 +242,56 @@ namespace Teknik.Areas.Profile.Controllers
{
if (ModelState.IsValid)
{
// Update uploads
List<Upload.Models.Upload> uploads = db.Uploads.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
if (uploads != null)
{
foreach (Upload.Models.Upload upload in uploads)
{
upload.UserId = null;
db.Entry(upload).State = EntityState.Modified;
}
}

// Update pastes
List<Paste.Models.Paste> pastes = db.Pastes.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
if (pastes != null)
{
foreach (Paste.Models.Paste paste in pastes)
{
paste.UserId = null;
db.Entry(paste).State = EntityState.Modified;
}
}

// Delete Blogs
Blog.Models.Blog blog = db.Blogs.Include("BlogPosts").Include("User").Where(u => u.User.Username == User.Identity.Name).FirstOrDefault();
Blog.Models.Blog blog = db.Blogs.Include("BlogPosts").Include("BlogPosts.Comments").Include("User").Where(u => u.User.Username == User.Identity.Name).FirstOrDefault();
if (blog != null)
{
db.Blogs.Remove(blog);
db.SaveChanges();
}

// Delete post comments
List<BlogPostComment> postComments = db.BlogComments.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
if (postComments != null)
{
foreach (BlogPostComment postComment in postComments)
{
db.BlogComments.Remove(postComment);
}
}

// Delete post comments
List<Podcast.Models.PodcastComment> podComments = db.PodcastComments.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
if (podComments != null)
{
foreach (Podcast.Models.PodcastComment podComment in podComments)
{
db.PodcastComments.Remove(podComment);
}
}

// Delete User
User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
if (user != null)
{

+ 4
- 0
Teknik/Areas/Profile/Models/User.cs View File

@@ -26,6 +26,10 @@ namespace Teknik.Areas.Profile.Models
public virtual UploadSettings UploadSettings { get; set; }

public List<Upload.Models.Upload> Uploads { get; set; }

public List<Paste.Models.Paste> Pastes { get; set; }

public User()
{
Username = String.Empty;

+ 20
- 0
Teknik/Models/TeknikEntities.cs View File

@@ -42,6 +42,7 @@ namespace Teknik.Models

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// User Settings Mappings
modelBuilder.Entity<User>()
.HasRequired(a => a.UserSettings)
.WithRequiredPrincipal(a => a.User);
@@ -63,6 +64,25 @@ namespace Teknik.Models
.HasRequired(a => a.UploadSettings)
.WithRequiredPrincipal(a => a.BlogSettings);

// User Mappings
modelBuilder.Entity<User>()
.HasMany<Upload>(u => u.Uploads)
.WithOptional(u => u.User)
.WillCascadeOnDelete(false);

modelBuilder.Entity<User>()
.HasMany<Paste>(u => u.Pastes)
.WithOptional(u => u.User)
.WillCascadeOnDelete(false);

// Upload Mappings
modelBuilder.Entity<Upload>()
.HasOptional(u => u.User);

// Paste Mappings
modelBuilder.Entity<Paste>()
.HasOptional(u => u.User);

// Users
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Group>().ToTable("Groups");

Loading…
Cancel
Save