Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("Title,Content,CreatedOn,Subtitle")] CreatePostViewModel createPostViewModel)
        {
            if (ModelState.IsValid)
            {
                var post = _mapper.Map <Post>(createPostViewModel);

                // Create meta if only required
                if (createPostViewModel.CreatedOn != null)
                {
                    var createdOnMeta = new PostMeta()
                    {
                        Key   = nameof(CreatePostViewModel.CreatedOn),
                        Value = createPostViewModel.CreatedOn.ToString()
                    };
                    post.Metas.Add(createdOnMeta);
                }

                if (createPostViewModel.Subtitle != null)
                {
                    var subtitleMeta = new PostMeta()
                    {
                        Key   = nameof(CreatePostViewModel.Subtitle),
                        Value = createPostViewModel.Subtitle
                    };
                    post.Metas.Add(subtitleMeta);
                }

                _context.Add(post);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(createPostViewModel));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PostMetaModel"/> class.
 /// </summary>
 /// <param name="meta">The meta.</param>
 public PostMetaModel(PostMeta meta)
 {
     this.Id            = meta.Id;
     this.Slug          = meta.Slug;
     this.Title         = meta.Title;
     this.Description   = meta.Description;
     this.WhenPublished = meta.GoesLive;
     this.Tags          = meta.Tags;
 }
        public ActionResult Create([Bind(Include = "PostTitle,PostContent,PostFormat,PostStatus,CommentStatus,UserID")] Post post)
        {
            PostMeta postMeta  = new PostMeta();
            var      thumbnail = Request.Form["upload"];

            try
            {
                if (ModelState.IsValid)
                {
                    // Default datas - which are automatically defined
                    post.PostRelease  = DateTime.Now;
                    post.PostModified = DateTime.Now;
                    post.CommentCount = 0;
                    post.PostContent.Replace("\n", "<br />");
                    post.User = db.Users.Where(u => u.UserID == post.UserID).Single();

                    // Store post to DB
                    db.Posts.Add(post);
                    db.SaveChanges();

                    // Prepare PostMeta's values
                    postMeta.PostID    = post.PostID;
                    postMeta.MetaKey   = "img_thumbnail";
                    postMeta.MetaValue = thumbnail;

                    // Store PostMeta to DB
                    db.PostMetas.Add(postMeta);
                    db.SaveChanges();

                    // Prepare Post's terms


                    // Store Terms to DB


                    // Direct to Edit page follow this Post's ID
                    return(RedirectToAction("Edit", new { id = post.PostID }));
                }
            }
            catch (DataException /*de*/)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            ViewBag.Thumbnail = thumbnail;
            ViewBag.UserID    = new SelectList(db.Users, "UserID", "UserName", post.UserID);

            return(View(post));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AdminMetaData"/> class.
 /// </summary>
 /// <param name="meta">The meta.</param>
 public AdminMetaData(PostMeta meta)
 {
     this.Id            = meta.Id;
     this.Title         = meta.Title;
     this.WhenCreated   = meta.WhenCreated;
     this.WhenPublished = meta.GoesLive;
     this.Slug          = meta.Slug;
     this.Description   = meta.Description;
     this.IsReserved    = meta.IsReserved;
     this.IsPublished   = meta.IsPublished;
     this.IsDeleted     = meta.IsDeleted;
     this.Revision      = meta.Revision;
     this.Tags          = meta.Tags;
     this.History       = meta.History.Select(h => new AdminAuditHistory(h)).ToList();
 }
Ejemplo n.º 5
0
        public static Item GetBannerImage(this Item post, IDictionary <int, Item> attachments)
        {
            Item image = null;

            PostMeta thumbnail = post.PostMetas?.FirstOrDefault(meta => meta.Key == "_thumbnail_id");

            if (thumbnail != null)
            {
                if (int.TryParse(thumbnail.Value, out int thumbnailId))
                {
                    attachments.TryGetValue(thumbnailId, out image);
                }
            }

            return(image);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Create a post model.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 /// <param name="content">The content.</param>
 /// <returns>The <see cref="PostModel"/>.</returns>
 public static PostModel Create(PostMeta metadata, string content)
 {
     return(new PostModel(metadata, content));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PostModel"/> class.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 /// <param name="html">The html.</param>
 private PostModel(PostMeta metadata, string html)
 {
     this.Metadata          = new PostMetaModel(metadata);
     this.RawHtmlThenIGuess = html;
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AdminPost"/> class.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 /// <param name="postContent">The content.</param>
 public AdminPost(PostMeta metadata, string postContent)
 {
     this.Metadata        = new AdminMetaData(metadata);
     this.MarkdownContent = postContent;
 }