public IQueryable<MovieModel> GetAll() { return this.ExecuteOperationHandleExceptions(() => { var context = new CinemaReserveDbContext(); return context.Movies.Select(Parser.FromMovie); }); }
public IQueryable<MovieModel> GetMovies(int cinemaId) { return this.ExecuteOperationHandleExceptions(() => { var context = new CinemaReserveDbContext(); var theCinema = context.Cinemas.FirstOrDefault(c => c.Id == cinemaId); return theCinema.Movies.AsQueryable().Select(Parser.FromMovie); }); }
public IQueryable<MovieModel> GetByKeyword(string keyword) { return this.ExecuteOperationHandleExceptions(() => { var keywordToLower = keyword.ToLower(); var context = new CinemaReserveDbContext(); return context.Movies .Where(m => m.Title.ToLower().Contains(keywordToLower) || m.Description.ToLower().Contains(keywordToLower)) .Select(Parser.FromMovie); }); }
public IEnumerable<ProjectionModel> GetMovieProjections(int cinemaId, int movieId) { return this.ExecuteOperationHandleExceptions(() => { var context = new CinemaReserveDbContext(); var projections = context.Projections .Where(pr => pr.Cinema.Id == cinemaId && pr.Movie.Id == movieId && pr.Status.Status == "upcoming"); return projections.Select(Parser.FromProjection).ToList(); }); }
public ReservationModel MakeReservation(int projectionId, CreateReservationModel model) { return this.ExecuteOperationHandleExceptions(() => { this.ValidateEmail(model.Email); var context = new CinemaReserveDbContext(); var theProjection = context.Projections.FirstOrDefault(pr => pr.Id == projectionId); if (theProjection == null) { throw new ArgumentNullException("projection is either non-existant or is not available"); } var reservedSeatStatus = context.SeatStatus.FirstOrDefault(st => st.Status == "reserved") ?? new SeatStatus() { Status = "reserved" }; List<Seat> reservedSeats = new List<Seat>(); using (TransactionScope tran = new TransactionScope()) { foreach (var seat in model.Seats) { var dbSeat = theProjection.Seats.FirstOrDefault(s => s.Row == seat.Row && s.Column == seat.Column); if (dbSeat == null || dbSeat.Status.Status == "reserved") { throw new InvalidOperationException("Seat is not available"); } dbSeat.Status = reservedSeatStatus; reservedSeats.Add(dbSeat); } tran.Complete(); } var reservation = new Reservation() { ReservedSeats = reservedSeats, UserEmail = model.Email.ToLower(), UserCode = this.GenerateUserCode() }; //context.Reservations.Add(reservation); theProjection.Reservations.Add(reservation); context.SaveChanges(); var reservationModel = Parser.ToReservationModel(reservation); return reservationModel; }); }
public ProjectionDetailsModel GetProjectionDetails(int projectionId) { return this.ExecuteOperationHandleExceptions(() => { var context = new CinemaReserveDbContext(); var theProjection = context.Projections.FirstOrDefault(pr => pr.Id == projectionId); if (theProjection == null) { throw new ArgumentNullException("projection is either non-existant or is not available"); } return Parser.ToProjectionDetails(theProjection); }); }
public MovieDetailsModel GetMovieInfo(int movieId) { return this.ExecuteOperationHandleExceptions(() => { var context = new CinemaReserveDbContext(); var theMovie = context.Movies.FirstOrDefault(m => m.Id == movieId); if (theMovie == null) { throw new ArgumentNullException("No such movie"); } return Parser.ToMovieDetails(theMovie); }); }
public HttpResponseMessage RejectReservation(int projectionId, RejectReservationModel model) { return this.ExecuteOperationHandleExceptions(() => { var context = new CinemaReserveDbContext(); var theProjection = context.Projections.FirstOrDefault(pr => pr.Id == projectionId); if (theProjection == null) { throw new ArgumentNullException("projection is either non-existant or is not available"); } var emailToLower = model.Email.ToLower(); var userCodeToLower = model.UserCode.ToLower(); var theReservation = theProjection .Reservations .FirstOrDefault(r => r.UserCode == userCodeToLower && r.UserEmail == emailToLower); if (theReservation == null) { throw new InvalidOperationException("Unauthorized operation"); } var freeSeatStatus = context.SeatStatus.FirstOrDefault(st => st.Status == "free"); foreach (var seat in theReservation.ReservedSeats) { seat.Status = freeSeatStatus; theReservation.ReservedSeats.Remove(seat); } theProjection.Reservations.Remove(theReservation); //context.Reservations.Remove(theReservation); context.SaveChanges(); return this.Request.CreateResponse(HttpStatusCode.NoContent); }); }