using DAL;
using HeyRed.MarkdownSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class editar_post : System.Web.UI.Page
{
    public int post_id;
    public int user_id;

    protected void Page_Load(object sender, EventArgs e)
    {
        post_id = Convert.ToInt32(Request.QueryString["id"]);
        user_id = Convert.ToInt32(Session["user_id"]);

        Entities entities = new Entities();

        var post = entities.Posts.FirstOrDefault(p => p.id == post_id);

        if (post == null)
        {
            // No existe el post
            Response.Redirect("/index.aspx");
            return;
        }

        if(post.user_id != user_id)
        {
            // El post no es del usuario logeado
            Response.Redirect("/ver_post.aspx?id=" + post_id);
            return;
        }

        if (IsPostBack) return;

        title_box.Text = post.title;
        markdown_box.Text = post.markdown;

    }
    protected void OnEditarClick(object sender, EventArgs e)
    {
        Markdown markdown = new Markdown();
        Entities entities = new Entities();

        var post = entities.Posts.First(p => p.id == post_id);

        post.title = title_box.Text;
        post.markdown = markdown_box.Text;
        post.html = markdown.Transform(post.markdown);

        entities.SaveChanges();

        Response.Redirect("/ver_post.aspx?id=" + post.id);
    }
}