/* * Get the list containing the tickets of a user */ public List <Ticket> GetTicketsList(string username) { // Database connection using (SqlConnection connection = DatabaseHandler.GetConnection()) { // Define a new list of Users List <Ticket> ticketsList = new List <Ticket>(); connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try{ // Insert the Ticket in the list command.Parameters.Clear(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.VisualizzaPrenotazione"; command.Parameters.Add("@Username", SqlDbType.VarChar).Value = username; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Prenotation p = new Prenotation(); p.PrenotationCode = reader.GetInt32(0); p.DateTime = reader.GetDateTime(1); p.UsernameUser = reader.GetString(2); p.EventCode = reader.GetInt32(3); Event e = new Event(); e.EventCode = reader.GetInt32(3); e.DateTime = reader.GetDateTime(5); e.HallCode = reader.GetInt32(6); e.Price = reader.GetDecimal(7); Film f = new Film(); f.Title = reader.GetString(4); Reservation r = new Reservation(); r.PlaceNumber = reader.GetInt32(8); ticketsList.Add(new Ticket(p, e, f, r)); } } // Attempt to commit the transaction. transaction.Commit(); return(ticketsList); } catch (Exception ex) { Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(new List <Ticket>() { }); } } }
/* * Delete a Subscription from the database * * @return true if the operation success, false if not */ public bool DeleteSubscription(string username) { using (SqlConnection connection = DatabaseHandler.GetConnection()) { connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.DeleteAbbonamento"; command.Parameters.Add("@Username", SqlDbType.VarChar).Value = username; command.ExecuteNonQuery(); // Initialize a int value to check if the Stored Procedure success var returnParameter = command.Parameters.Add("@ReturnVal", SqlDbType.Int); returnParameter.Direction = ParameterDirection.ReturnValue; // Commit the transaction. transaction.Commit(); // If the int value > 0 the Stored Procedure success if (returnParameter.Direction > 0) { return(true); } else { command.Parameters.Clear(); return(false); } } catch (SqlException ex) { Console.WriteLine("\nCommit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(false); } } }
/* * Get the list containing the Prenotations of the database */ public List <Prenotation> GetPrenotationsList() { // Database connection using (SqlConnection connection = DatabaseHandler.GetConnection()) { // Define a new list of Users List <Prenotation> prenotationsList = new List <Prenotation>(); connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { command.CommandText = "SELECT * FROM Cinema.Prenotazione;"; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var prenotationCode = reader.GetInt32(0); var dateTime = reader.GetDateTime(1); var username = reader.GetString(2); var eventCode = reader.GetInt32(3); prenotationsList.Add(new Prenotation(prenotationCode, dateTime, username, eventCode)); } } // Attempt to commit the transaction. transaction.Commit(); return(prenotationsList); } catch (Exception ex) { Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(new List <Prenotation>() { }); } } }
/* * Get the list containing the Events of the database */ public List <Show> GetShowsList() { // Database connection using (SqlConnection connection = DatabaseHandler.GetConnection()) { // Define a new list of Users List <Show> showsList = new List <Show>(); connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { command.CommandText = "SELECT * FROM Show;"; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Event e = new Event(); e.EventCode = reader.GetInt32(0); e.DateTime = reader.GetDateTime(1); e.HallCode = reader.GetInt32(3); e.Price = reader.GetDecimal(4); Film f = new Film(); f.Title = reader.GetString(2); showsList.Add(new Show(e, f)); } } // Attempt to commit the transaction. transaction.Commit(); return(showsList); } catch (Exception ex) { Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(new List <Show>() { }); } } }
/* * Add a Prenotation into the database. * * @return true if the operation success, false if not */ public bool AddPrenotation(DateTime dateTime, string usernameUser, int eventCode, int placeNumber) { using (SqlConnection connection = DatabaseHandler.GetConnection()) { // Bool value for user subscription // true if the user is subscribed, false otherwise bool IsSubscribed = GetSubscription(usernameUser); decimal price = 6.40M; connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { // If the User is not subscribed take the price from the event, if he is // insert the price of the ticket at 6,40€ if (!IsSubscribed) { command.CommandText = "SELECT Cinema.Evento.Prezzo FROM Cinema.Evento WHERE Cinema.Evento.CodiceEvento = @CodiceEvento"; command.Parameters.Add("@CodiceEvento", SqlDbType.Int).Value = eventCode; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { price = reader.GetDecimal(0); } } } // Reset the Parameters command.Parameters.Clear(); // Insert the Prenotation into the database command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.AddNewPrenotazione"; command.Parameters.Add("@DataOra", SqlDbType.DateTime).Value = dateTime; command.Parameters.Add("@Username_UtenteFree", SqlDbType.VarChar).Value = usernameUser; command.Parameters.Add("@Codice_Evento", SqlDbType.Int).Value = eventCode; command.Parameters.Add("@Prezzo", SqlDbType.Decimal).Value = price; command.Parameters.Add("@CodicePrenotazione", SqlDbType.Int).Direction = ParameterDirection.Output; command.ExecuteNonQuery(); // Create a Prenotation Object Prenotation prenotation = new Prenotation(Convert.ToInt32(command.Parameters["@CodicePrenotazione"].Value), dateTime, usernameUser, eventCode); // Initialize a int value to check if the Stored Procedure success var returnParameter1 = command.Parameters.Add("@ReturnVal", SqlDbType.Int); returnParameter1.Direction = ParameterDirection.ReturnValue; //Reset the parameters command.Parameters.Clear(); // Insert data in Reserve table in the database command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.AddNewRiserva"; command.Parameters.Add("@Numero_Posto", SqlDbType.Int).Value = placeNumber; command.Parameters.Add("@Codice_Prenotazione", SqlDbType.Int).Value = prenotation.PrenotationCode; command.ExecuteNonQuery(); // Initialize a int value to check if the Stored Procedure success var returnParameter2 = command.Parameters.Add("@ReturnVal", SqlDbType.Int); returnParameter2.Direction = ParameterDirection.ReturnValue; // Commit the transaction transaction.Commit(); // If both the int value > 0 the Stored Procedure both success if (returnParameter1.Direction > 0 && returnParameter2.Direction > 0) { return(true); } else { command.Parameters.Clear(); return(false); } } catch (SqlException ex) { Console.WriteLine("\nCommit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(false); } } }
/* * Get the list containing the User who has signed up for a subscription in the Cinema */ public List <User> GetSubscribersList() { // Database connection using (SqlConnection connection = DatabaseHandler.GetConnection()) { // Define a new list of Users List <User> subscribersList = new List <User>(); connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { command.CommandText = "SELECT Cinema.Abbonamento.Username, Cinema.UtenteFree.Nome, Cinema.UtenteFree.Cognome " + "FROM Cinema.Abbonamento, Cinema.UtenteFree WHERE Cinema.Abbonamento.Username = Cinema.UtenteFree.UsernameUtenteFree;"; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var username = reader.GetString(0); var name = reader.GetString(1); var surname = reader.GetString(2); subscribersList.Add(new User(username, name, surname)); } } // Attempt to commit the transaction. transaction.Commit(); return(subscribersList); } catch (Exception ex) { Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(new List <User>() { }); } } }
/* * Add an event into the database * * @return true if the operation success, false if not */ public bool AddEvent(string usernameAdmin, DateTime dateTime, int filmCode, int hallCode, decimal price) { using (SqlConnection connection = DatabaseHandler.GetConnection()) { connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { // Check if in the hall is already present a film proiection in the same dateTime. // If so, return false, add the event otherwise. command.CommandText = "SELECT * FROM Cinema.Evento;"; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { if (dateTime == reader.GetDateTime(1) && hallCode == reader.GetInt32(3)) { return(false); } } } // Reset the parameters command.Parameters.Clear(); // Insert the event in the database command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.AddNewEvento"; command.Parameters.Add("@DataOra", SqlDbType.DateTime).Value = dateTime; command.Parameters.Add("@Codice_Film", SqlDbType.Int).Value = filmCode; command.Parameters.Add("@Codice_Sala", SqlDbType.Int).Value = hallCode; command.Parameters.Add("@Username_Admin", SqlDbType.VarChar).Value = usernameAdmin; command.Parameters.Add("@Prezzo", SqlDbType.Decimal).Value = price; command.Parameters.Add("@CodiceEvento", SqlDbType.Int).Direction = ParameterDirection.Output; command.ExecuteNonQuery(); // Initialize a int value to check if the Stored Procedure success var returnParameter = command.Parameters.Add("@ReturnVal", SqlDbType.Int); returnParameter.Direction = ParameterDirection.ReturnValue; // Commit the transaction transaction.Commit(); // If the int value > 0 the Stored Procedure success if (returnParameter.Direction > 0) { return(true); } else { command.Parameters.Clear(); return(false); } } catch (SqlException ex) { Console.WriteLine("\nCommit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(false); } } }
/* * Register an Admin or Free User in the database. * * @return true if the operation success, false if not */ public bool Registration(bool isAdmin, string username, string password, string name, string surname) { using (SqlConnection connection = DatabaseHandler.GetConnection()) { connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { switch (isAdmin) { case true: command.CommandText = "INSERT Cinema.Admin VALUES ( @username, @password, @name, @surname)"; break; case false: command.CommandText = "INSERT Cinema.UtenteFree VALUES ( @username, @password, @name, @surname)"; break; } command.Parameters.Add("@username", SqlDbType.VarChar).Value = username; command.Parameters.Add("@password", SqlDbType.VarChar).Value = password; command.Parameters.Add("@name", SqlDbType.VarChar).Value = name; command.Parameters.Add("@surname", SqlDbType.VarChar).Value = surname; // Commit the transaction. transaction.Commit(); int result = command.ExecuteNonQuery(); if (result > 0) { return(true); } else { command.Parameters.Clear(); return(false); } } catch (SqlException ex) { Console.WriteLine("\nCommit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(false); } } }
/* * Check Foreign Key bond * * @return true if the foreign key exists, false if not */ public bool CheckIntFK(string value, string valueType) { //Control if the string insered is an Integer if (!int.TryParse(value, out int number)) { return(false); } int intValue = Convert.ToInt32(value); // Datbase Connection using (SqlConnection connection = DatabaseHandler.GetConnection()) { connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { command.CommandText = "SELECT * FROM Cinema." + valueType + ";"; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { if (intValue == reader.GetInt32(0)) { return(true); } } return(false); } } catch (Exception ex) { Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(false); } } }
/* * Get the list containing the avilable Places for an Event of the database */ public List <Place> GetAvailablePlacesList(int eventCode) { // Define two list: one for available places, // the second for the booked ones List <Place> availablePlacesList = new List <Place>(); List <Reservation> reservationList = new List <Reservation>(); // Connessione al DB Cinema using (SqlConnection connection = DatabaseHandler.GetConnection()) { connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.VisualizzaPostiSala"; command.Parameters.Add("@CodiceEvento", SqlDbType.Int).Value = eventCode; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Place place = new Place(); place.PlaceNumber = reader.GetInt32(0); place.HallCode = reader.GetInt32(1); availablePlacesList.Add(place); } } // Reset the parameters command.Parameters.Clear(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.VisualizzaPostiRiservati"; command.Parameters.Add("@CodiceEvento", SqlDbType.Int).Value = eventCode; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Reservation reserve = new Reservation(); reserve.PlaceNumber = reader.GetInt32(0); reserve.PrenotationCode = reader.GetInt32(1); reservationList.Add(reserve); } } // Attempt to commit the transaction. transaction.Commit(); for (int x = availablePlacesList.Count - 1; x >= 0; x--) { foreach (Reservation r in reservationList) { if (availablePlacesList[x].PlaceNumber.Equals(r.PlaceNumber)) { // If an element is in both the lists remove it from the availablePlaceList availablePlacesList.Remove(availablePlacesList[x]); break; // Stop the cycle } } } return(availablePlacesList); } catch (Exception ex) { Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(new List <Place>() { }); } } }
/* * Check if the place the User wants to buy is a valid one. * The place must exist in that hall and must not already be reserved * * @return true if the place is a valid one, false otherwise */ public bool CheckPlace(int eventCode, int placeNumber) { // Define two list, the first containing the existing place // the second containing the reserved ones List <Reservation> reservationsList = new List <Reservation>(); List <Place> availablePlacesList = new List <Place>(); // Define two bool value, he first check if the place exists, // the second whether it is already reserved bool placeExists = false; bool placeReserved = false; // Connessione al DB Cinema using (SqlConnection connection = DatabaseHandler.GetConnection()) { connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.VisualizzaPostiSala"; command.Parameters.Add("@CodiceEvento", SqlDbType.Int).Value = eventCode; // Fill the availablePlaceList with the places of the hall of the event using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Place p = new Place(); p.PlaceNumber = reader.GetInt32(0); p.HallCode = reader.GetInt32(1); availablePlacesList.Add(p); } } // Check if the place exists foreach (Place p in availablePlacesList) { if (p.PlaceNumber == placeNumber) { placeExists = true; } } // Reset the parameters command.Parameters.Clear(); // Takes the Reservations and fill the list reservationsList with bought places command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.VisualizzaPostiRiservati"; command.Parameters.Add("@CodiceEvento", SqlDbType.Int).Value = eventCode; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Reservation r = new Reservation(); r.PlaceNumber = reader.GetInt32(0); r.PrenotationCode = reader.GetInt32(1); reservationsList.Add(r); } } // Attempt to commit the transaction. transaction.Commit(); // Check if the place is already in Reservation table foreach (Reservation r in reservationsList) { if (r.PlaceNumber == placeNumber) { placeReserved = true; } } // Return false if the place doesn't exist or it's been bought if (placeExists == false || placeReserved == true) { return(false); } else { return(true); } } catch (Exception ex) { Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(false); } } }
/* * Show the places in a hall. * Simple console rappresentation of the places disposition in a hall */ public string DrawHall(int eventCode) { string drawHall = string.Empty; Hall h = new Hall(); // Database connection using (SqlConnection connection = DatabaseHandler.GetConnection()) { connection.Open(); // Start a local transaction. SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); // Assign both transaction object and connection to Command object command.Connection = connection; command.Transaction = transaction; try { command.CommandType = CommandType.StoredProcedure; command.CommandText = "Cinema.RichiestaCodiceSala"; command.Parameters.Add("@CodiceEvento", SqlDbType.Int).Value = eventCode; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { h.HallCode = reader.GetInt32(0); } } // Attempt to commit the transaction. transaction.Commit(); switch (h.HallCode) { case 1: drawHall = "\n ________________________________\n \\______________________________/\n\n | 1 | 2 | 3 | 4 | 5 | 6 |\n" + " | 7 | 8 | 9 | 10| 11| 12|\n | 13| 14| 15| 16| 17| 18|\n | 19| 20| 21| 22| 23| 24|\n"; break; case 2: drawHall = " \n________________________\n\\______________________/\n\n | 1 | 2 | 3 | 4 |\n" + " | 5 | 6 | 7 | 8 |\n | 9 | 10| 11| 12|\n | 13| 14| 15| 16|\n"; break; case 3: drawHall = " \n________________________________\n\\______________________________/\n\n | 1 | 2 | 3 | 4 | 5 | 6 |\n" + " | 7 | 8 | 9 | 10| 11| 12|\n | 13| 14| 15| 16| 17| 18|\n | 19| 20| 21| 22| 23| 24|\n | 25| 26| 27| 28| 29| 30|\n"; break; default: drawHall = "Hall not found."; break; } return(drawHall); } catch (Exception ex) { Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } return(drawHall); } } }