/// <summary> /// Adds all checkboxes to Add_Movie_To_Playlist_Panel for each playlist /// </summary> public void AddAllPlaylistCheckBoxes(Movies movie) { MovieDatabaseEntities db = new MovieDatabaseEntities(); var UserID = MainApplicationWindow.getCurrentUserID(); currentMovie = movie; var numberOfPlaylists = (from user in db.Users where user.UserID == UserID select user.Number_Of_Playlists).FirstOrDefault(); for (int i = 0; i < numberOfPlaylists; i++) { createPlaylistCheckbox(i, movie); } foreach (MonoFlat_CheckBox checkBox in Add_Movie_To_Playlist_Panel.Controls) { foreach (Playlist playlist in db.Playlists) { if (playlist.FilmID == movie.MovieID && playlist.UserID == UserID && playlist.PlaylistID == (int)checkBox.Tag) { checkBox.Checked = true; } } } }
private void Accept_Button_Click(object sender, EventArgs e) { MovieDatabaseEntities db = new MovieDatabaseEntities(); var userID = MainApplicationWindow.getCurrentUserID(); var usr = db.Users.SingleOrDefault(o => o.UserID == userID); foreach (MonoFlat_CheckBox checkbox in Add_Movie_To_Playlist_Panel.Controls) { if (checkbox.Checked) { var playlist = db.Playlists.SingleOrDefault(o => o.UserID == userID && o.FilmID == currentMovie.MovieID && o.PlaylistID == (int)checkbox.Tag); if (playlist == null) { Methods.AddToPlaylist((int)checkbox.Tag, usr.Username, currentMovie.File_Name); } } else { Methods.RemoveFromPlaylist(currentMovie, (int)checkbox.Tag); } } this.Close(); }
/// <summary> /// removes playlist and button connected with it /// </summary> /// <param name="i">tg of removed button</param> /// <param name="usrID">id of current user</param> /// <param name="usr">Current user</param> private void Remove_Playlist(int i, int usrID, User usr) { MovieDatabaseEntities db = new MovieDatabaseEntities(); foreach (Playlist pl in db.Playlists) { if (pl.PlaylistID == i && pl.UserID == usrID) { db.Playlists.Remove(pl); } } foreach (List_Of_Playlists lop in db.List_Of_Playlists) { if (lop.ID == i && lop.UserID == usrID) { db.List_Of_Playlists.Remove(lop); usr.Number_Of_Playlists--; } } db.SaveChanges(); deletePlaylistButton((int)buttonToBeRemoved.Tag); //Playlists_Panel.Controls.Remove(buttonToBeRemoved); buttonToBeRemoved = null; foreach (MonoFlat_Button but in Playlists_Panel.Controls) { if ((int)but.Tag > i) { but.Location = new Point(but.Location.X - 95, but.Location.Y); } } }
/// <summary> /// Allows to change name of film if we click on proper column in dataGridView1 /// </summary> /// <param name="moviename">Movie name we want to check for.</param> /// <param name="filename">File name of the file we want to change</param> public static void MovieNameChange(string moviename, string filename) { try{ MovieDatabaseEntities db = new MovieDatabaseEntities(); var CurrentUserID = MainApplicationWindow.getCurrentUserID(); var result = db.Favourite_Hated.SingleOrDefault(o => o.MainMovieList.File_Name == filename && o.UserID == CurrentUserID); if (result.Movie_Name != moviename && result.UserID == CurrentUserID) { result.Movie_Name = moviename; db.SaveChanges(); } } catch (DbEntityValidationException e) { //In case of error while adding stuff to database foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
/// <summary> /// Add new path to the file whose directory was changed, to database /// </summary> /// <param name="newPath">a path that is inserted instead of previous one</param> /// <param name="file_name">a name of file that will be updated</param> public void AddNewPathToDatabase(string newPath, string file_name) { try { MovieDatabaseEntities db = new MovieDatabaseEntities(); var result = db.MainMovieLists.SingleOrDefault(o => o.File_Name == file_name); result.File_Path = newPath; db.SaveChanges(); } catch (DbEntityValidationException e) { //In case of error while adding stuff to database foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
/// <summary> /// Retruns current user's ID /// </summary> /// <returns>Current User ID</returns> public static int getCurrentUserID() { MovieDatabaseEntities db = new MovieDatabaseEntities(); var usr = db.Users.SingleOrDefault(o => o.Username == UserName); return usr.UserID; }
/// <summary> /// Retruns current user's ID /// </summary> /// <returns>Current User ID</returns> public static int getCurrentUserID() { MovieDatabaseEntities db = new MovieDatabaseEntities(); var usr = db.Users.SingleOrDefault(o => o.Username == UserName); return(usr.UserID); }
/// <summary> /// Exit Button - exits application if the database don't exist or contninue to the program /// </summary> private void monoFlat_Button2_Click(object sender, EventArgs e) { MovieDatabaseEntities db = new MovieDatabaseEntities(); if (!db.Database.Exists()) { Application.Exit(); } this.Dispose(); }
/// <summary> /// Add Playlist button in Playlist_Panel /// </summary> /// <param name="i">number of user's playlists</param> public void createPlaylistButton(int i) { MovieDatabaseEntities db = new MovieDatabaseEntities(); var userID = getCurrentUserID(); var pl = (from playlist in db.List_Of_Playlists where playlist.UserID == userID && playlist.ID == i + 1 select playlist).FirstOrDefault(); if (pl != null) { if (Playlists_Panel.Controls.Count == 0) { MonoFlat_Button button = new MonoFlat_Button(); button.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0); button.Width = 89; button.Height = 19; button.Text = pl.Playlist_Name; button.Location = new Point(0, 0); button.MouseClick += new MouseEventHandler(Playlist_Button_Click); int tag = i + 1; button.Tag = tag; Playlists_Panel.Controls.Add(button); } else { int maxi = 0; foreach (MonoFlat_Button but in Playlists_Panel.Controls) { if (Int32.Parse(but.Tag.ToString()) > maxi) { maxi = Int32.Parse(but.Tag.ToString()); } } foreach (MonoFlat_Button item in Playlists_Panel.Controls) { if (item.Tag.Equals(maxi)) { MonoFlat_Button prevButton = item; MonoFlat_Button button = new MonoFlat_Button(); button.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0); button.Width = 89; button.Height = 19; button.Text = pl.Playlist_Name; button.Location = new Point(prevButton.Bounds.Right + 6, 0); button.MouseClick += new MouseEventHandler(Playlist_Button_Click); int tag = i + 1; button.Tag = tag; Playlists_Panel.Controls.Add(button); } } } } }
public static void changeNameOfPlaylist(string newName) { MovieDatabaseEntities db = new MovieDatabaseEntities(); int usrID = getCurrentUserID(); var playlist = db.List_Of_Playlists.SingleOrDefault(o => o.ID == (int)buttonToChangeName.Tag && o.UserID == usrID); playlist.Playlist_Name = newName; db.SaveChanges(); buttonToChangeName.Text = playlist.Playlist_Name; buttonToChangeName = null; }
/// <summary> /// Create checkbox in PlaylistsManagement form /// </summary> /// <param name="i">Number of user's playlists</param> /// <param name="movie">Movie that we want to add to playlists</param> public void createPlaylistCheckbox(int i, Movies movie) { MovieDatabaseEntities db = new MovieDatabaseEntities(); var userID = MainApplicationWindow.getCurrentUserID(); var pl = (from playlist in db.List_Of_Playlists where playlist.UserID == userID && playlist.ID == i + 1 select playlist).FirstOrDefault(); if (pl != null) { if (Add_Movie_To_Playlist_Panel.Controls.Count == 0) { MonoFlat_CheckBox checkBox = new MonoFlat_CheckBox(); checkBox.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0); checkBox.Width = Add_Movie_To_Playlist_Panel.Width - 30; checkBox.Height = 20; checkBox.Text = pl.Playlist_Name; checkBox.Location = new Point(0, 0); int tag = i + 1; checkBox.Tag = tag; Add_Movie_To_Playlist_Panel.Controls.Add(checkBox); } else { int maxi = 0; foreach (MonoFlat_CheckBox chb in Add_Movie_To_Playlist_Panel.Controls) { if (Int32.Parse(chb.Tag.ToString()) > maxi) { maxi = Int32.Parse(chb.Tag.ToString()); } } foreach (MonoFlat_CheckBox item in Add_Movie_To_Playlist_Panel.Controls) { if (item.Tag.Equals(maxi)) { MonoFlat_CheckBox prevCheckBox = item; MonoFlat_CheckBox checkBox = new MonoFlat_CheckBox(); checkBox.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0); checkBox.Width = Add_Movie_To_Playlist_Panel.Width - 30; checkBox.Height = 20; checkBox.Text = pl.Playlist_Name; checkBox.Location = new Point(0, prevCheckBox.Bounds.Bottom + 6); int tag = i + 1; checkBox.Tag = tag; Add_Movie_To_Playlist_Panel.Controls.Add(checkBox); } } } } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MovieDatabaseEntities db = new MovieDatabaseEntities(); if (!db.Database.Exists()) { //if (!Methods.CheckIfTableExist()) Application.Run(new AddDatabaseSplashScreen()); } Application.Run(new MainApplicationWindow()); }
/// <summary> /// Adds a movie to database taking into consideration all possible exceptions /// </summary> /// <param name="FileName">Name of the movie filename</param> public static void AddMovie(string FilePath) { //Adds a movie id, movie name and movie path to database try { string FileName = System.IO.Path.GetFileNameWithoutExtension(FilePath); MovieDatabaseEntities db = new MovieDatabaseEntities(); MainMovieList objMovie = new MainMovieList(); if (db.MainMovieLists.Any(o => o.File_Name == FileName)) { Console.WriteLine(FileName + " is already in the MainMovieList table"); } else { objMovie.File_Name = FileName; objMovie.File_Path = FilePath; db.MainMovieLists.Add(objMovie); db.SaveChanges(); } int usrId = MainApplicationWindow.getCurrentUserID(); Console.WriteLine("Current username: "******"\n"); if (db.Favourite_Hated.Any(o => o.FilmID == objMovie.ID && o.UserID == usrId)) { Console.WriteLine(FileName + " is already in the Favourite_Hated table"); } else { Console.WriteLine("Adding " + FileName + " to the Favourite_Hated table for user with id: " + usrId); User usr = new User(); var searchs = db.Users.SingleOrDefault(o => o.UserID == usrId); AddFavouriteOrHated(-1, searchs.Username, FileName); } //Jezeli nie dasz sobie rady ze stworzeniem query: stworz funckje foreach i sprawdz czy dany film ma konkretny usrname i filmid } catch (DbEntityValidationException e) { //In case of error while adding stuff to database foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
/// <summary> /// Opens Context Menu Strip while clicked in the dataGridView in column third and handles the clicks in it /// </summary> private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { int currentMouseOverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex; if (e.Button == MouseButtons.Right && currentMouseOverRow >= 0) { int currentMouseOverColumn = dataGridView1.HitTest(e.X, e.Y).ColumnIndex; var film = ((List <Movies>)dataGridView1.DataSource).ElementAt(currentMouseOverRow); var userID = getCurrentUserID(); if (currentMouseOverRow >= 0 && currentMouseOverColumn >= 0) { dataGridView1.CurrentCell = dataGridView1[currentMouseOverColumn, currentMouseOverRow]; } if (ContextMenuAvailable) { MovieDatabaseEntities db = new MovieDatabaseEntities(); ContextMenuStrip m = new ContextMenuStrip(); if (film.LikeOrDislike != "Favourite") { m.Items.Add("Add to Favourite").Name = "Add to Favourite"; } if (film.LikeOrDislike != "Hated") { m.Items.Add("Add to Hated").Name = "Add to Hated"; } if (film.LikeOrDislike == "Favourite" || film.LikeOrDislike == "Hated") { m.Items.Add("Reset to Normal Status").Name = "Reset to Normal Status"; } var usr = db.Users.SingleOrDefault(o => o.Username == UserName); if (usr.Number_Of_Playlists > 0) { m.Items.Add("Add to/Remove from playlist").Name = "Add to/Remove from playlist"; } Point pt1 = new Point(currentMouseOverColumn, currentMouseOverRow); pt = pt1; if (currentMouseOverRow >= 0 && currentMouseOverColumn == 2) { //m.Items.Add(string.Format("Coordinates: {0}, {1}", currentMouseOverRow.ToString(), currentMouseOverColumn.ToString())); m.Show(dataGridView1, new Point(e.X, e.Y)); } m.ItemClicked += new ToolStripItemClickedEventHandler(m_Item_Clicked); } } }
/// <summary> /// Removes all playlist buttons from Playlist_Panel /// </summary> public void removeAllPlaylistButtons() { MovieDatabaseEntities db = new MovieDatabaseEntities(); var UserID = getCurrentUserID(); var numberOfPlaylists = (from user in db.Users where user.UserID == UserID select user.Number_Of_Playlists).FirstOrDefault(); for (int i = 0; i < numberOfPlaylists; i++) { deletePlaylistButton(i + 1); } }
/// <summary> /// Removes the movie from database if its directory was changed and user does not want to give a new one /// </summary> /// <param name="file_name">The name of file to be deleted</param> public void DeleteOldRowFromDatabase(string file_name) { MovieDatabaseEntities db = new MovieDatabaseEntities(); var FilmID = (from mov in db.MainMovieLists where mov.File_Name == file_name select mov.ID).FirstOrDefault(); var DeletedFromFavourite_Hated = from fav in db.Favourite_Hated where fav.FilmID == FilmID select fav; var DeletedFromPlaylist = from pl in db.Playlists where pl.FilmID == FilmID select pl; var DeletedFromMainMovieLists = from mov in db.MainMovieLists where mov.ID == FilmID select mov; foreach (var pair in DeletedFromFavourite_Hated) { db.Favourite_Hated.Remove(pair); } foreach (var playlist in DeletedFromPlaylist) { db.Playlists.Remove(playlist); } foreach (var mov in DeletedFromMainMovieLists) { db.MainMovieLists.Remove(mov); } try { db.SaveChanges(); } catch (Exception e) { Console.WriteLine(e); // Provide for exceptions. } }
/// <summary> /// Validate if a user exists in the database and if the password is correct /// </summary> /// <param name="login">User name given in the textbox</param> /// <param name="pass">Password of user given in the textbox</param> /// <returns>True if succeded, false if failed</returns> public static bool LoginFunction(String login, String pass) { bool validation = false; // Code for validation from database MovieDatabaseEntities db = new MovieDatabaseEntities(); foreach (var user in db.Users) { if (user.Username == login && user.Password == pass) { validation = true; } } return(validation); }
/// <summary> /// Removes record from Playlist in db with specified userID and filmID /// </summary> /// <param name="film">film that has to be removed from Playlist for current user</param> public static void RemoveFromPlaylist(Movies film, int playlistID) { var userID = MainApplicationWindow.getCurrentUserID(); MovieDatabaseEntities db = new MovieDatabaseEntities(); var queryPlaylist = from Playlist in db.Playlists where Playlist.UserID == userID && Playlist.FilmID == film.MovieID && Playlist.PlaylistID == playlistID select Playlist; foreach (var del in queryPlaylist) { db.Playlists.Remove(del); } db.SaveChanges(); }
/// <summary> /// Adds information if movie is added to playlist /// </summary> /// <param name="idOfPlaylist">Determines if movie is added to playlist</param> /// <param name="UserName">The name of the user that is log in</param> /// <param name="film">The name of file that we want to set</param> public static void AddToPlaylist(int idOfPlaylist, string UserName, string film) { try { MovieDatabaseEntities db = new MovieDatabaseEntities(); var movie = db.MainMovieLists.SingleOrDefault(o => o.File_Name == film); var usr = db.Users.SingleOrDefault(o => o.Username == UserName); int usrID = usr.UserID; int maxID = -10; foreach (var pl in db.Playlists) { if (pl.ID > maxID) { maxID = pl.ID; } } Playlist playlist = new Playlist(); playlist.ID = maxID + 1; playlist.FilmID = movie.ID; playlist.UserID = usr.UserID; playlist.PlaylistID = idOfPlaylist; db.Playlists.Add(playlist); db.SaveChanges(); } catch (DbEntityValidationException e) { //In case of error while adding stuff to database foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
/// <summary> /// Adds information if movie is added to favourites, hated or none of them /// </summary> /// <param name="whichcase">Determines if movie is added to favourites or hated</param> /// <param name="UserName">The name of the user that is log in</param> /// <param name="film">The name of file that we want to set</param> public static void AddFavouriteOrHated(int whichcase, string UserName, string film) { try { MovieDatabaseEntities db = new MovieDatabaseEntities(); var result = db.MainMovieLists.SingleOrDefault(o => o.File_Name == film); var usr = db.Users.SingleOrDefault(o => o.Username == UserName); int ID = usr.UserID; var search = db.Favourite_Hated.SingleOrDefault(o => o.FilmID == result.ID && o.UserID == ID); if (search == null) { Favourite_Hated favourite = new Favourite_Hated(); favourite.FilmID = result.ID; favourite.UserID = usr.UserID; favourite.LikeOrDislike = whichcase; db.Favourite_Hated.Add(favourite); db.SaveChanges(); } else if (search.LikeOrDislike != whichcase) { search.LikeOrDislike = whichcase; db.SaveChanges(); } } catch (DbEntityValidationException e) { //In case of error while adding stuff to database foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
/// <summary> /// Checks wheter all tables exists /// </summary> /// <returns></returns> public static bool CheckIfTableExist() { MovieDatabaseEntities entity = new MovieDatabaseEntities(); bool exists = entity.Database .SqlQuery <int?>(@" IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'TheSchema' AND TABLE_NAME = 'MainMovieList')) BEGIN") .SingleOrDefault() != null; if (exists) { exists = entity.Database .SqlQuery <int>(@" IF EXISTS (SELECT * FROM sys.tables WHERE name = 'Favourite_Hated') SELECT 1 ELSE SELECT 0 ").SingleOrDefault() != null; } else if (exists) { exists = entity.Database .SqlQuery <int>(@" IF EXISTS (SELECT * FROM sys.tables WHERE name = 'Playlist') SELECT 1 ELSE SELECT 0 ").SingleOrDefault() != null; } { exists = entity.Database .SqlQuery <int>(@" IF EXISTS (SELECT * FROM sys.tables WHERE name = 'User') SELECT 1 ELSE SELECT 0 ").SingleOrDefault() != null; } return(exists); }
/// <summary> /// Delete playlist from database or changes name of playlist /// </summary> private void Playlist_Context_Menu_Button_Click(object sender, ToolStripItemClickedEventArgs e) { MovieDatabaseEntities db = new MovieDatabaseEntities(); int i = (int)buttonToBeRemoved.Tag; int usrID = getCurrentUserID(); var usr = db.Users.SingleOrDefault(o => o.UserID == usrID); switch (e.ClickedItem.ToString()) { case "Remove Playlist": Remove_Playlist(i, usrID, usr); break; case "Change name": ChangePlaylistNameMessageBox myMessageBox = new ChangePlaylistNameMessageBox(); myMessageBox.ShowDialog(); break; } }
/// <summary> /// Set information to be displayed in dataGridView1 after clicking "Playlist" button /// </summary> /// <returns>Return list of movies to add to dataGridView1</returns> public static List <Movies> AddItemToPlaylist(int tag) { int UserID = MainApplicationWindow.getCurrentUserID(); MovieDatabaseEntities db = new MovieDatabaseEntities(); var movieQuery = from fh in db.Favourite_Hated join p in db.Playlists on new { ID = fh.FilmID } equals new { ID = p.FilmID } where (p.UserID == fh.UserID && p.UserID == UserID && p.PlaylistID == tag) select new Movies { MovieID = (int)fh.MainMovieList.ID, Movie_Name = fh.Movie_Name, File_Name = fh.MainMovieList.File_Name, LikeOrDislike = fh.LikeOrDislike == 1 ? "Favourite" : (fh.LikeOrDislike == 0 ? "Hated" : ""), Path = fh.MainMovieList.File_Path }; List <Movies> movieList = movieQuery.ToList(); return(movieList); }
/// <summary> /// Event Handler for MenuStripItems in the dataGridView1_MouseClick() function /// </summary> public void m_Item_Clicked(object sender, ToolStripItemClickedEventArgs e) { MovieDatabaseEntities db = new MovieDatabaseEntities(); var film = ((List <Movies>)dataGridView1.DataSource).ElementAt(pt.Y); var userID = getCurrentUserID(); Console.Write("\n\nThe film is: " + film.File_Name); switch (e.ClickedItem.Name.ToString()) { case "Add to Favourite": Methods.AddFavouriteOrHated(1, UserName, film.File_Name); break; case "Add to Hated": Methods.AddFavouriteOrHated(0, UserName, film.File_Name); break; case "Reset to Normal Status": Methods.AddFavouriteOrHated(-1, UserName, film.File_Name); break; case "Add to/Remove from playlist": PlaylistsManagement myMessageBox = new PlaylistsManagement(); myMessageBox.AddAllPlaylistCheckBoxes(film); myMessageBox.ShowDialog(); break; } if (playlistButtonPressed) { RefreshPlaylistGrid(actualButtonPressed); } else if (homelistButtonPressed) { RefreshGrid(); } }
/// <summary> /// Add playlist to List_Of_Playlists in database /// </summary> public void AddPlaylistToDataBase() { if (PlaylistButtonActive) { PlaylistButtonActive = false; MovieDatabaseEntities db = new MovieDatabaseEntities(); var UserID = getCurrentUserID(); var usr = db.Users.SingleOrDefault(o => o.UserID == UserID); var i = usr.Number_Of_Playlists; usr.Number_Of_Playlists++; // Increase Number_Of_Playlists for the Current user List_Of_Playlists playlist = new List_Of_Playlists(); while (true) { var pl = db.List_Of_Playlists.SingleOrDefault(o => o.ID == i + 1 && o.UserID == usr.UserID); if (pl != null) { i++; } else { break; } } playlist.ID = i + 1; playlist.UserID = UserID; playlist.Playlist_Name = "Playlist" + playlist.ID; db.List_Of_Playlists.Add(playlist); db.SaveChanges(); createPlaylistButton(i); PlaylistButtonActive = true; } }
/// <summary> /// Set information to be displayed in dataGridView1 /// </summary> /// <returns>Return list of movies to add to dataGridView1</returns> public static List<Movies> AddItemToList() { int UserID = MainApplicationWindow.getCurrentUserID(); MovieDatabaseEntities db = new MovieDatabaseEntities(); var movieQuery = from mml in db.MainMovieLists join fh in db.Favourite_Hated on new { ID = mml.ID } equals new { ID = fh.FilmID } into fh_join from fh in fh_join.DefaultIfEmpty() where fh.UserID == UserID select new Movies { MovieID = mml.ID, Movie_Name = fh.Movie_Name, File_Name = mml.File_Name, LikeOrDislike = fh.LikeOrDislike == 1 ? "Favourite" : (fh.LikeOrDislike == 0 ? "Hated" : ""), Path = mml.File_Path }; List<Movies> movieList = movieQuery.ToList(); return movieList; }
/// <summary> /// Adds all checkboxes to Add_Movie_To_Playlist_Panel for each playlist /// </summary> public void AddAllPlaylistCheckBoxes(Movies movie) { MovieDatabaseEntities db = new MovieDatabaseEntities(); var UserID = MainApplicationWindow.getCurrentUserID(); currentMovie = movie; var numberOfPlaylists = (from user in db.Users where user.UserID == UserID select user.Number_Of_Playlists).FirstOrDefault(); for (int i = 0; i < numberOfPlaylists; i++) { createPlaylistCheckbox(i, movie); } foreach (MonoFlat_CheckBox checkBox in Add_Movie_To_Playlist_Panel.Controls) { foreach (Playlist playlist in db.Playlists) if (playlist.FilmID == movie.MovieID && playlist.UserID == UserID && playlist.PlaylistID == (int)checkBox.Tag) checkBox.Checked = true; } }
/// <summary> /// Set information to be displayed in dataGridView1 /// </summary> /// <returns>Return list of movies to add to dataGridView1</returns> public static List <Movies> AddItemToList() { int UserID = MainApplicationWindow.getCurrentUserID(); MovieDatabaseEntities db = new MovieDatabaseEntities(); var movieQuery = from mml in db.MainMovieLists join fh in db.Favourite_Hated on new { ID = mml.ID } equals new { ID = fh.FilmID } into fh_join from fh in fh_join.DefaultIfEmpty() where fh.UserID == UserID select new Movies { MovieID = mml.ID, Movie_Name = fh.Movie_Name, File_Name = mml.File_Name, LikeOrDislike = fh.LikeOrDislike == 1 ? "Favourite" : (fh.LikeOrDislike == 0 ? "Hated" : ""), Path = mml.File_Path }; List <Movies> movieList = movieQuery.ToList(); return(movieList); }
/// <summary> /// Validate if a user exists in the database and if the password is correct /// </summary> /// <param name="login">User name given in the textbox</param> /// <param name="pass">Password of user given in the textbox</param> /// <returns>True if succeded, false if failed</returns> public static bool LoginFunction(String login, String pass) { bool validation = false; // Code for validation from database MovieDatabaseEntities db = new MovieDatabaseEntities(); foreach (var user in db.Users) { if (user.Username == login && user.Password == pass) { validation = true; } } return validation; }
/// <summary> /// Checks wheter all tables exists /// </summary> /// <returns></returns> public static bool CheckIfTableExist() { MovieDatabaseEntities entity = new MovieDatabaseEntities(); bool exists = entity.Database .SqlQuery<int?>(@" IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'TheSchema' AND TABLE_NAME = 'MainMovieList')) BEGIN") .SingleOrDefault() != null; if (exists) { exists = entity.Database .SqlQuery<int>(@" IF EXISTS (SELECT * FROM sys.tables WHERE name = 'Favourite_Hated') SELECT 1 ELSE SELECT 0 ").SingleOrDefault() != null; } else if (exists) { exists = entity.Database .SqlQuery<int>(@" IF EXISTS (SELECT * FROM sys.tables WHERE name = 'Playlist') SELECT 1 ELSE SELECT 0 ").SingleOrDefault() != null; } { exists = entity.Database .SqlQuery<int>(@" IF EXISTS (SELECT * FROM sys.tables WHERE name = 'User') SELECT 1 ELSE SELECT 0 ").SingleOrDefault() != null; } return exists; }
/// <summary> /// Adds information if movie is added to playlist /// </summary> /// <param name="idOfPlaylist">Determines if movie is added to playlist</param> /// <param name="UserName">The name of the user that is log in</param> /// <param name="film">The name of file that we want to set</param> public static void AddToPlaylist(int idOfPlaylist, string UserName, string film) { try { MovieDatabaseEntities db = new MovieDatabaseEntities(); var movie = db.MainMovieLists.SingleOrDefault(o => o.File_Name == film); var usr = db.Users.SingleOrDefault(o => o.Username == UserName); int usrID = usr.UserID; int maxID = -10; foreach (var pl in db.Playlists) if (pl.ID > maxID) maxID = pl.ID; Playlist playlist = new Playlist(); playlist.ID = maxID + 1; playlist.FilmID = movie.ID; playlist.UserID = usr.UserID; playlist.PlaylistID = idOfPlaylist; db.Playlists.Add(playlist); db.SaveChanges(); } catch (DbEntityValidationException e) { //In case of error while adding stuff to database foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
/// <summary> /// Opens Context Menu Strip while clicked in the dataGridView in column third and handles the clicks in it /// </summary> private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { int currentMouseOverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex; if (e.Button == MouseButtons.Right && currentMouseOverRow >= 0) { int currentMouseOverColumn = dataGridView1.HitTest(e.X, e.Y).ColumnIndex; var film = ((List<Movies>)dataGridView1.DataSource).ElementAt(currentMouseOverRow); var userID = getCurrentUserID(); if (currentMouseOverRow >= 0 && currentMouseOverColumn >= 0) dataGridView1.CurrentCell = dataGridView1[currentMouseOverColumn, currentMouseOverRow]; if (ContextMenuAvailable) { MovieDatabaseEntities db = new MovieDatabaseEntities(); ContextMenuStrip m = new ContextMenuStrip(); if (film.LikeOrDislike != "Favourite") m.Items.Add("Add to Favourite").Name = "Add to Favourite"; if (film.LikeOrDislike != "Hated") m.Items.Add("Add to Hated").Name = "Add to Hated"; if (film.LikeOrDislike == "Favourite" || film.LikeOrDislike == "Hated") m.Items.Add("Reset to Normal Status").Name = "Reset to Normal Status"; var usr = db.Users.SingleOrDefault(o => o.Username == UserName); if(usr.Number_Of_Playlists > 0) { m.Items.Add("Add to/Remove from playlist").Name = "Add to/Remove from playlist"; } Point pt1 = new Point(currentMouseOverColumn, currentMouseOverRow); pt = pt1; if (currentMouseOverRow >= 0 && currentMouseOverColumn == 2) { //m.Items.Add(string.Format("Coordinates: {0}, {1}", currentMouseOverRow.ToString(), currentMouseOverColumn.ToString())); m.Show(dataGridView1, new Point(e.X, e.Y)); } m.ItemClicked += new ToolStripItemClickedEventHandler(m_Item_Clicked); } } }
/// <summary> /// Allows to change name of film if we click on proper column in dataGridView1 /// </summary> /// <param name="moviename">Movie name we want to check for.</param> /// <param name="filename">File name of the file we want to change</param> public static void MovieNameChange(string moviename, string filename) { try{ MovieDatabaseEntities db = new MovieDatabaseEntities(); var CurrentUserID = MainApplicationWindow.getCurrentUserID(); var result = db.Favourite_Hated.SingleOrDefault(o => o.MainMovieList.File_Name == filename && o.UserID == CurrentUserID); if (result.Movie_Name != moviename && result.UserID == CurrentUserID ) { result.Movie_Name = moviename; db.SaveChanges(); } } catch (DbEntityValidationException e) { //In case of error while adding stuff to database foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
/// <summary> /// Event Handler for MenuStripItems in the dataGridView1_MouseClick() function /// </summary> public void m_Item_Clicked(object sender, ToolStripItemClickedEventArgs e) { MovieDatabaseEntities db = new MovieDatabaseEntities(); var film = ((List<Movies>)dataGridView1.DataSource).ElementAt(pt.Y); var userID = getCurrentUserID(); Console.Write("\n\nThe film is: " + film.File_Name); switch (e.ClickedItem.Name.ToString()) { case "Add to Favourite": Methods.AddFavouriteOrHated(1, UserName, film.File_Name); break; case "Add to Hated": Methods.AddFavouriteOrHated(0, UserName, film.File_Name); break; case "Reset to Normal Status": Methods.AddFavouriteOrHated(-1, UserName, film.File_Name); break; case "Add to/Remove from playlist": PlaylistsManagement myMessageBox = new PlaylistsManagement(); myMessageBox.AddAllPlaylistCheckBoxes(film); myMessageBox.ShowDialog(); break; } if (playlistButtonPressed) { RefreshPlaylistGrid(actualButtonPressed); } else if (homelistButtonPressed) RefreshGrid(); }
/// <summary> /// Set information to be displayed in dataGridView1 after clicking "Playlist" button /// </summary> /// <returns>Return list of movies to add to dataGridView1</returns> public static List<Movies> AddItemToPlaylist(int tag) { int UserID = MainApplicationWindow.getCurrentUserID(); MovieDatabaseEntities db = new MovieDatabaseEntities(); var movieQuery = from fh in db.Favourite_Hated join p in db.Playlists on new { ID = fh.FilmID } equals new { ID = p.FilmID } where (p.UserID == fh.UserID && p.UserID == UserID && p.PlaylistID == tag) select new Movies { MovieID = (int)fh.MainMovieList.ID, Movie_Name = fh.Movie_Name, File_Name = fh.MainMovieList.File_Name, LikeOrDislike = fh.LikeOrDislike == 1 ? "Favourite" : (fh.LikeOrDislike == 0 ? "Hated" : ""), Path = fh.MainMovieList.File_Path }; List<Movies> movieList = movieQuery.ToList(); return movieList; }
private void Accept_Button_Click(object sender, EventArgs e) { MovieDatabaseEntities db = new MovieDatabaseEntities(); var userID = MainApplicationWindow.getCurrentUserID(); var usr = db.Users.SingleOrDefault(o => o.UserID == userID); foreach(MonoFlat_CheckBox checkbox in Add_Movie_To_Playlist_Panel.Controls) { if(checkbox.Checked) { var playlist = db.Playlists.SingleOrDefault(o => o.UserID == userID && o.FilmID == currentMovie.MovieID && o.PlaylistID == (int)checkbox.Tag); if(playlist == null) Methods.AddToPlaylist((int)checkbox.Tag, usr.Username, currentMovie.File_Name); } else { Methods.RemoveFromPlaylist(currentMovie, (int)checkbox.Tag); } } this.Close(); }
/// <summary> /// Adds all playlist buttons to Playlists_Panel after log in /// </summary> public void AddAllPlaylistButtons() { MovieDatabaseEntities db = new MovieDatabaseEntities(); var UserID = getCurrentUserID(); var numberOfPlaylists = (from user in db.Users where user.UserID == UserID select user.Number_Of_Playlists).FirstOrDefault(); for (int i = 0; i < numberOfPlaylists; i++) { createPlaylistButton(i); } }
/// <summary> /// removes playlist and button connected with it /// </summary> /// <param name="i">tg of removed button</param> /// <param name="usrID">id of current user</param> /// <param name="usr">Current user</param> private void Remove_Playlist(int i, int usrID, User usr) { MovieDatabaseEntities db = new MovieDatabaseEntities(); foreach (Playlist pl in db.Playlists) { if (pl.PlaylistID == i && pl.UserID == usrID) db.Playlists.Remove(pl); } foreach (List_Of_Playlists lop in db.List_Of_Playlists) { if (lop.ID == i && lop.UserID == usrID) { db.List_Of_Playlists.Remove(lop); usr.Number_Of_Playlists--; } } db.SaveChanges(); deletePlaylistButton((int)buttonToBeRemoved.Tag); //Playlists_Panel.Controls.Remove(buttonToBeRemoved); buttonToBeRemoved = null; foreach (MonoFlat_Button but in Playlists_Panel.Controls) { if ((int)but.Tag > i) { but.Location = new Point(but.Location.X - 95, but.Location.Y); } } }
/// <summary> /// Add playlist to List_Of_Playlists in database /// </summary> public void AddPlaylistToDataBase() { if (PlaylistButtonActive) { PlaylistButtonActive = false; MovieDatabaseEntities db = new MovieDatabaseEntities(); var UserID = getCurrentUserID(); var usr = db.Users.SingleOrDefault(o => o.UserID == UserID); var i = usr.Number_Of_Playlists; usr.Number_Of_Playlists++; // Increase Number_Of_Playlists for the Current user List_Of_Playlists playlist = new List_Of_Playlists(); while(true) { var pl = db.List_Of_Playlists.SingleOrDefault(o => o.ID == i + 1 && o.UserID == usr.UserID); if(pl != null) { i++; } else { break; } } playlist.ID = i + 1; playlist.UserID = UserID; playlist.Playlist_Name = "Playlist" + playlist.ID; db.List_Of_Playlists.Add(playlist); db.SaveChanges(); createPlaylistButton(i); PlaylistButtonActive = true; } }
private void monoFlat_Button1_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; User user = new User(); MovieDatabaseEntities db = new MovieDatabaseEntities(); validation = true; loginError[i] = new MonoFlat.MonoFlat_NotificationBox(); loginError[i].BorderCurve = 8; loginError[i].Font = new System.Drawing.Font("Tahoma", 9F); loginError[i].Image = null; loginError[i].Location = new System.Drawing.Point(this.Width / 2 - 100, this.Height - 180); loginError[i].MinimumSize = new System.Drawing.Size(100, 40); loginError[i].Name = "monoFlat_NotificationBox1"; loginError[i].NotificationType = MonoFlat.MonoFlat_NotificationBox.Type.Error; loginError[i].RoundCorners = true; loginError[i].ShowCloseButton = false; loginError[i].Size = new System.Drawing.Size(200, 85); loginError[i].TabIndex = 6; loginError[i].MouseHover += new EventHandler(LoginError_MouseHover); if (monoFlat_TextBox2.Text == monoFlat_TextBox3.Text) { foreach (var username in db.Users) { if (username.Username==monoFlat_TextBox1.Text) { loginError[i].Text = "Username Error:\nThis user name is already occupied."; monoFlat_ThemeContainer1.Controls.Add(loginError[i]); loginError[i].BringToFront(); validation = false; monoFlat_TextBox1.Text = null; break; } if (monoFlat_TextBox1.Text.Length < 3) { loginError[i].Text = "Username Error:\nUsername must contatin at least 3 characters."; monoFlat_ThemeContainer1.Controls.Add(loginError[i]); loginError[i].BringToFront(); validation = false; monoFlat_TextBox1.Text = null; break; } if (monoFlat_TextBox2.Text.Length < 5) { loginError[i].Text = "Password Error:\nPassword must be of at least 5 characters."; monoFlat_ThemeContainer1.Controls.Add(loginError[i]); loginError[i].BringToFront(); validation = false; break; } } } else { loginError[i].Text = "Password Error:\nPasswords don't match!"; monoFlat_ThemeContainer1.Controls.Add(loginError[i]); loginError[i].BringToFront(); validation = false; } if (validation) { user.Username = monoFlat_TextBox1.Text; user.Password = monoFlat_TextBox2.Text; user.Number_Of_Playlists = 0; db.Users.Add(user); db.SaveChanges(); validation = false; var successPrompt = new MonoFlat.MonoFlat_NotificationBox(); successPrompt.BorderCurve = 8; successPrompt.Font = new System.Drawing.Font("Tahoma", 9F); successPrompt.Image = null; successPrompt.Location = new System.Drawing.Point(this.Width / 2 - 150, this.Height/2 -50); successPrompt.Name = "monoFlat_NotificationBox1"; successPrompt.NotificationType = MonoFlat.MonoFlat_NotificationBox.Type.Success; successPrompt.RoundCorners = true; successPrompt.ShowCloseButton = false; successPrompt.Size = new System.Drawing.Size(300, 105); successPrompt.Text = ("You have succesfully created your account " + monoFlat_TextBox1.Text + ".\nGo now and log in.\nThis window will shutdown in a moment"); monoFlat_ThemeContainer1.Controls.Add(successPrompt); successPrompt.BringToFront(); foreach (Control c in this.Controls) c.Enabled = false; System.Threading.Thread.Sleep(5000); this.Dispose(); } i++; Cursor.Current = Cursors.Default; }
private void monoFlat_Button1_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; User user = new User(); MovieDatabaseEntities db = new MovieDatabaseEntities(); validation = true; loginError[i] = new MonoFlat.MonoFlat_NotificationBox(); loginError[i].BorderCurve = 8; loginError[i].Font = new System.Drawing.Font("Tahoma", 9F); loginError[i].Image = null; loginError[i].Location = new System.Drawing.Point(this.Width / 2 - 100, this.Height - 180); loginError[i].MinimumSize = new System.Drawing.Size(100, 40); loginError[i].Name = "monoFlat_NotificationBox1"; loginError[i].NotificationType = MonoFlat.MonoFlat_NotificationBox.Type.Error; loginError[i].RoundCorners = true; loginError[i].ShowCloseButton = false; loginError[i].Size = new System.Drawing.Size(200, 85); loginError[i].TabIndex = 6; loginError[i].MouseHover += new EventHandler(LoginError_MouseHover); if (monoFlat_TextBox2.Text == monoFlat_TextBox3.Text) { foreach (var username in db.Users) { if (username.Username == monoFlat_TextBox1.Text) { loginError[i].Text = "Username Error:\nThis user name is already occupied."; monoFlat_ThemeContainer1.Controls.Add(loginError[i]); loginError[i].BringToFront(); validation = false; monoFlat_TextBox1.Text = null; break; } if (monoFlat_TextBox1.Text.Length < 3) { loginError[i].Text = "Username Error:\nUsername must contatin at least 3 characters."; monoFlat_ThemeContainer1.Controls.Add(loginError[i]); loginError[i].BringToFront(); validation = false; monoFlat_TextBox1.Text = null; break; } if (monoFlat_TextBox2.Text.Length < 5) { loginError[i].Text = "Password Error:\nPassword must be of at least 5 characters."; monoFlat_ThemeContainer1.Controls.Add(loginError[i]); loginError[i].BringToFront(); validation = false; break; } } } else { loginError[i].Text = "Password Error:\nPasswords don't match!"; monoFlat_ThemeContainer1.Controls.Add(loginError[i]); loginError[i].BringToFront(); validation = false; } if (validation) { user.Username = monoFlat_TextBox1.Text; user.Password = monoFlat_TextBox2.Text; user.Number_Of_Playlists = 0; db.Users.Add(user); db.SaveChanges(); validation = false; var successPrompt = new MonoFlat.MonoFlat_NotificationBox(); successPrompt.BorderCurve = 8; successPrompt.Font = new System.Drawing.Font("Tahoma", 9F); successPrompt.Image = null; successPrompt.Location = new System.Drawing.Point(this.Width / 2 - 150, this.Height / 2 - 50); successPrompt.Name = "monoFlat_NotificationBox1"; successPrompt.NotificationType = MonoFlat.MonoFlat_NotificationBox.Type.Success; successPrompt.RoundCorners = true; successPrompt.ShowCloseButton = false; successPrompt.Size = new System.Drawing.Size(300, 105); successPrompt.Text = ("You have succesfully created your account " + monoFlat_TextBox1.Text + ".\nGo now and log in.\nThis window will shutdown in a moment"); monoFlat_ThemeContainer1.Controls.Add(successPrompt); successPrompt.BringToFront(); foreach (Control c in this.Controls) { c.Enabled = false; } System.Threading.Thread.Sleep(5000); this.Dispose(); } i++; Cursor.Current = Cursors.Default; }