Reputation: 1303
I am trying to create my own CMS Blog for education purposes, and nearly every Blog has a reference in each article that generally says something like:
Updated: April 1, 1999 | Published: April 1, 1999 | Created: April 1, 1999
I have my model set up to allow manual entry of this through the view form, but I am assuming there is a way to do this via the model when the form is created.
I know I should be using DateTime.UTCNow
to establish it, but I'm not sure how I should be triggering it.
using System;
using System.Data.Entity;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;
namespace JosephMCasey.Areas.Article.Models
{
public class Articles
{
[Key]
public int PostId { get; set; }
[Required]
[StringLength(256, MinimumLength = 1, ErrorMessage = "Title cannot be longer than 256 characters.")]
public string Title { get; set;}
[Display(Name = "Create Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? CreateDate { get; set; }
[Display(Name = "Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? PublishDate { get; set; }
[Display(Name = "Modify Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? ModifyDate { get; set; }
public string Author { get; set; }
[Required]
[NotMapped]
public MvcHtmlString PageBody { get; set; }
public string Body
{
get { return PageBody.ToHtmlString(); }
set { PageBody = new MvcHtmlString(value); }
}
// Feature is the featured image of each blog
public string Feature { get; set; }
public bool Published { get; set; }
public string Excerpt { get; set;}
[Display(Name = "GMT of Create Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? CreateDateUTC { get; set; }
[Display(Name = "UTC of Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? PublishDateUTC { get; set; }
[Display(Name = "GMT of Modify Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? ModifyDateUTC { get; set; }
public string CanonicalUrl { get; set; }
public string UrlSlug { get; set; }
public string Category { get; set; }
public string Tag { get; set; }
}
public class ArticleDBContext : DbContext
{
public DbSet<Articles> Articles { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
}
}
//Where should the extension method be coded?
internal static void SyncObjectsStatePreCommit(this DbContext dbContext)
{
foreach (var dbEntityEntry in dbContext.ChangeTracker.Entries())
{
var trackableObject = dbEntityEntry.Entity as ITrackable;
// we need to set/update trackable properties
if (trackableObject == null)
{
continue;
}
var dateTime = DateTime.Now;
var dateTimeUTC = DateTime.UtcNow;
// set createddate only for added entities
if (entityState.ObjectState == ObjectState.Added)
{
trackableObject.CreateDate = dateTime;
trackableObject.CreateDateUTC = dateTimeUTC;
}
// set LastUpdatedDate for any case other than Unchanged
if (entityState.ObjectState != ObjectState.Unchanged)
{
trackableObject.ModifyDate = dateTime;
trackableObject.ModifyDateUTC = dateTimeUTC;
}
}
}
Handling Created and Modified Date in MVC
DateCreated and DateModified in ASP.NET MVC 5
Upvotes: 0
Views: 3712
Reputation: 12805
I would set all of those when you save your blog entries to your database. Either have a trigger on the table, or set them in C# code as you're making each of those actions.
You should also set up Published
and Modified
as DateTime?
as you don't have data for them when you initially create the blog entry. One could argue that creation and publishing can be the same task, so those could be coupled, but I would still leave the possibility that you create a blog before publishing it.
Upvotes: 3