/// <summary> /// For the given show, all the tickets with corresponding seatnumbers are removed /// </summary> /// <param name="show">The show where tickets are to be removed</param> /// <param name="seatsToRemove">a list of integers, containing the seats to remove</param> /// <returns>a boolean representing if it was a success or not</returns> public bool RemoveTickets(Show show, List <int> seatsToRemove) { bool success = true; using (FestivalContext context = new FestivalContext(ConnectionString)) { List <Ticket> tickets = (from ticket in context.Tickets where ticket.ShowId == show.ShowId select ticket).ToList(); //Foreach through the tickets found, and get the ones to remove. Then use those to RemoveRange in the context List <Ticket> ticketsToRemove = new List <Ticket>(); foreach (int seat in seatsToRemove) { ticketsToRemove.Add(tickets.Find(x => x.Seat == seat)); } try { context.Tickets.RemoveRange(ticketsToRemove); context.SaveChanges(); } catch (SqlException) { success = false; } } return(success); }
/// <summary> /// Creates and saves tickets for given customer,show and seats. The seats also holds a boolean representation of if customer uses wheelchair /// </summary> /// <param name="customer">The customer who booked the tickets</param> /// <param name="show">The show that was booked</param> /// <param name="seats">The seats that are booked</param> /// <returns>A boolean representation if it succeeded or not</returns> public bool BookTickets(Customer customer, Show show, Dictionary <int, bool> seats) { bool success = true; customer = SaveCustomerInDB(customer); using (FestivalContext context = new FestivalContext(ConnectionString)) { try { foreach (var seat in seats) { context.Tickets.Add(new Ticket() { CustomerId = customer.CustomerId, ShowId = show.ShowId, Seat = seat.Key, Wheelchair = seat.Value }); } context.SaveChanges(); } catch (SqlException) { success = false; } } return(success); }
/// <summary> /// Called on when a given customer wants to drop all tickets for a given show /// </summary> /// <param name="show">the show to drop tickets from</param> /// <param name="activeCustomer">The customer who wants to drop their tickets</param> /// <returns>a boolean representation of if it succeeded or not</returns> public bool DropTicketsForSpecificShowAndCustomer(Show show, Customer activeCustomer) { try { using (FestivalContext context = new FestivalContext(ConnectionString)) { List <Ticket> toRemove = (from ticket in context.Tickets where ticket.CustomerId == activeCustomer.CustomerId && ticket.ShowId == show.ShowId select ticket).ToList(); context.Tickets.RemoveRange(toRemove); context.SaveChanges(); } } catch (SqlException) { return(false); } return(true); }
/// <summary> /// If the customer is NOT found in the database, saves it (To be called on when customer wants to book tickets /// to ensure that the customer is added). /// </summary> /// <param name="customer">The customer to check, and add if not found</param> /// <returns>a Customer, if the input customer didn't have a customerId, the returned one defenitivly does</returns> private Customer SaveCustomerInDB(Customer customer) { Customer toReturn = customer; using (FestivalContext context = new FestivalContext(ConnectionString)) { Customer existingCustomer = (from cust in context.Customers where cust.Ssn == customer.Ssn select cust).FirstOrDefault(); if (existingCustomer == null) { context.Customers.Add(customer); context.SaveChanges(); //We do this as to put the customer through the database and get it's customerId toReturn = (from cust in context.Customers where cust.Ssn == customer.Ssn select cust).FirstOrDefault(); } } return(toReturn); }