Exemplo n.º 1
0
        public void Consume(Reservation reservationDto)
        {
            Console.WriteLine("Reservation request received.");
            writeToLog("Reservation request received.");

            var success = false;

            using (var db = new DatabaseContext())
            {
                var reservedSpots = db.Reservations
                                    .Where(r => r.SeanceId == reservationDto.SeanceId)
                                    .SelectMany(r => r.Spots)
                                    .ToList();

                var reservation = reservedSpots.Any(s => reservationDto.Spots.Any(sDto => s.Id == sDto.Id));

                if (!reservation)
                {
                    var spotsId = reservationDto.Spots.Select(s => s.Id).ToArray();
                    reservationDto.Spots           = db.Spots.Where(s => spotsId.Contains(s.Id)).ToList();
                    reservationDto.ReservationDate = DateTime.Now;
                    db.Reservations.Add(reservationDto);
                    success = true;
                }
                else
                {
                    emailService.SendRejection(reservationDto);
                    Console.WriteLine("At least one spot is already engaged.");
                    writeToLog("At least one spot is already engaged.", LogMessage.LogType.WARNING);
                }

                try {
                    db.SaveChanges();
                }
                catch (DbUpdateException e)
                {
                    Console.WriteLine(e.InnerException.InnerException.Message);
                    writeToLog(e.InnerException.InnerException.Message, LogMessage.LogType.ERROR);
                    return;
                }

                Console.WriteLine("Reservation succeeded.");
                writeToLog("Reservation succeeded.");
            }

            if (success)
            {
                transactionService.Send(reservationDto);
                emailService.Send(reservationDto);
                messageService.Send(reservationDto.UserEmail, "Reservation performed successfully");
                Console.WriteLine("Reservation performed correctly");
                writeToLog("Reservation performed correctly");
            }
            else
            {
                messageService.Send(reservationDto.UserEmail, "At least one spot was already engaged");
            }
        }
        public void Consume(ReservationDto reservationDto)
        {
            Reservation res;
            var         success = false;

            using (var db = new Model1Container())
            {
                var reservation =
                    db.ReservationSet.Where(r => r.SeanceId == reservationDto.SeanceId)
                    .SelectMany(r => r.Spots)
                    .Any(s => reservationDto.Spots.Contains(s.Id));

                if (!reservation)
                {
                    var spots = db.SpotSet.Where(spot => reservationDto.Spots.Contains(spot.Id)).ToList();

                    res = new Reservation()
                    {
                        SeanceId  = reservationDto.SeanceId,
                        Spots     = spots,
                        UserEmail = reservationDto.Email
                    };
                    db.ReservationSet.Add(res);
                    success = true;
                    Console.WriteLine("Reservation succeeded.");
                }
                else
                {
                    Console.WriteLine("At least one spot is already engaged.");
                }

                db.SaveChanges();
            }

            if (success)
            {
                transactionService.Send(reservationDto);
            }
        }