using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace MyTunes.Library
{
    public partial class CreatePlaylist1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                using (MyTunesDataContext db = new MyTunesDataContext())
                {
                    int matches = (from Playlists in db.Playlists
                                   where Playlists.UserName == User.Identity.Name && Playlists.Name == txtPlaylistName.Text
                                   select Playlists.PlaylistID).Count();

                    if (matches == 0)
                    {
                        Playlist list = new Playlist();
                        list.UserName = User.Identity.Name;
                        list.Name = txtPlaylistName.Text;
                        db.Playlists.InsertOnSubmit(list);
                        try
                        {
                            db.SubmitChanges();
                            Response.Redirect("~/Library/Home.aspx");
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex);
                        }
                    }
                }
            }
        }
    }
}