// Personal Webpage Engine
// Copyright(c) 2008, 2009 Mihail Kochanov
// You can contact me at StilgarAD@yahoo.com
// Copyright(c) 2009 Ivelina Georgieva
// You can contact me at ivelina.georgieva@gmail.com
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
//
// Personal Web Page Engine
//
//------------------------------------------------------------------------------

using System;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using System.Web.UI.WebControls;

public partial class Main : System.Web.UI.Page
{
    private BlogNETDataContext dc;
    public BlogNETDataContext DataContext
    {
        get { return dc; }
    }

    #region PageEvents

    protected void Page_PreInit(object sender, EventArgs e)
    {
        dc = new BlogNETDataContext(ConfigurationManager.ConnectionStrings["BlogNETDB"].ConnectionString);
        DataLoadOptions dlo = new DataLoadOptions();
        dlo.LoadWith<NewsItem>(ni => ni.Tags);
        dc.LoadOptions = dlo;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string title = (string)Application[Constants.Application.WebsiteTitle] ?? "";
        if (!String.IsNullOrEmpty(title))
        {
            Title = title;
        }
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        if (dc != null)
        {
            dc.Dispose();
        }
    }

    #endregion

    #region ControlEvents

    protected void ldsNewsItems_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        string tagParam = Request.QueryString[Constants.QueryString.Tag];

        e.Result = SharedQueries.GetNewsItemsByTags(tagParam, dc);
    }

    protected void ldsNewsItems_Deleted(object sender, LinqDataSourceStatusEventArgs e)
    {
        Cache.Remove(Constants.Cache.LatestComments);
        Cache.Remove(Constants.Cache.Tags);

        Response.Redirect(Request.Url.ToString());
    }

    #endregion
}
