Exemplo n.º 1
0
        public async Task <ActionResult <FlightReservation> > AddFlightReservation(MAANPP20ContextFlight _context, FlightReservation flightReservation)
        {
            User user = await _context.Users.Where(x => x.deleted == false)
                        .Include(flightReservations => flightReservations.flightReservations)
                        .FirstOrDefaultAsync(id => id.Id == flightReservation.UserIdForPOST);

            if (user == null)
            {
                return(null);
            }

            AvioSediste avioSediste = await _context.AvioSedista
                                      .Where(x => x.deleted == false)
                                      .FirstOrDefaultAsync(id => id.id == flightReservation.seatId);

            if (avioSediste == null)
            {
                return(null);
            }
            if (avioSediste.isFastReservation == true)
            {
                return(null);
            }
            if (avioSediste.deleted == true)
            {
                return(null);
            }
            if (avioSediste.reserved == true)
            {
                return(null);
            }
            avioSediste.reserved = true;
            //_context.Entry(avioSediste).State = EntityState.Modified;

            foreach (var friend in flightReservation.friendForFlights)
            {
                AvioSediste avioSediste1 = await _context.AvioSedista
                                           .Where(x => x.deleted == false)
                                           .FirstOrDefaultAsync(id => id.id == friend.seatId);

                if (avioSediste1 == null)
                {
                    return(null);
                }
                if (avioSediste1.isFastReservation == true)
                {
                    return(null);
                }
                if (avioSediste1.deleted == true)
                {
                    return(null);
                }
                if (avioSediste1.reserved == true)
                {
                    return(null);
                }
                avioSediste1.reserved = true;
                //_context.Entry(avioSediste1).State = EntityState.Modified;
            }
            bool saveFailed;

            do
            {
                saveFailed = false;
                try
                {
                    _context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    saveFailed = true;
                    return(null);
                }
                catch (Exception e)
                {
                    throw;
                }
            } while (saveFailed);

            foreach (var friendForFlight in flightReservation.friendForFlights)
            {
                friendForFlight.invitationString = RandomString(_context);
                friendForFlight.reservationDate  = DateTime.Now;
            }

            user.flightReservations.Add(flightReservation);
            // ako je user iskoristion svoj bonus
            if (flightReservation.userBonus == true)
            {
                user.bonus = 0;
            }
            else
            {
                // ako nije, proverava se da li je dosao do 100%?
                if (user.bonus < 100)
                {
                    user.bonus += 1;
                }
            }

            try
            {
                _context.Entry(user).State = EntityState.Modified;
            }
            catch (Exception e)
            {
                throw;
            }

            _context.FlightReservations.Add(flightReservation);



            try
            {
                SendMail(flightReservation.friendForFlights as List <FriendForFlight>);
                await _context.SaveChangesAsync();

                //else BadRequest();
            }
            catch (Exception e)
            {
                return(null);
            }
            //await _context.SaveChangesAsync();

            return(flightReservation);
        }
Exemplo n.º 2
0
        public async Task <ActionResult <FlightReservation> > DeleteFlightReservation(MAANPP20ContextFlight _context, int id)
        {
            var flightReservation = await _context.FlightReservations
                                    .Where(x => x.deleted == false)
                                    .FirstOrDefaultAsync(i => i.id == id);

            if (flightReservation == null)
            {
                return(null);
            }
            else if (flightReservation.deleted == true)
            {
                return(null);
            }
            var flight = await _context.Flights
                         .Where(x => x.deleted == false)
                         .FirstOrDefaultAsync(x => x.id == flightReservation.flightId);

            if (flight == null)
            {
                return(null);
            }
            else
            {
                if (CanDelete(flight.datumPolaska))
                {
                    AvioSediste avioSediste = await _context.AvioSedista
                                              .Where(x => x.deleted == false)
                                              .FirstOrDefaultAsync(id => id.id == flightReservation.seatId);

                    if (avioSediste == null)
                    {
                        return(null);
                    }
                    if (avioSediste.isFastReservation == true)
                    {
                        return(null);
                    }
                    if (avioSediste.deleted == true)
                    {
                        return(null);
                    }
                    if (avioSediste.reserved == false)
                    {
                        return(null);
                    }
                    avioSediste.reserved = false;
                    _context.Entry(avioSediste).State = EntityState.Modified;

                    flightReservation.deleted = true;

                    _context.Entry(flightReservation).State = EntityState.Modified;

                    await _context.SaveChangesAsync();

                    return(flightReservation);
                }
                else
                {
                    return(null);
                }
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult <FastFlightReservation> > AddFastFlightReservations(MAANPP20ContextFlight _context, FastFlightReservation fastFlightReservation)
        {
            User user = await _context.Users.Where(x => x.deleted == false)
                        .Include(fastFlightReservations => fastFlightReservations.fastFlightReservations)
                        .FirstOrDefaultAsync(id => id.Id == fastFlightReservation.UserIdForPOST);

            if (user == null)
            {
                return(null);
            }
            AvioSediste avioSediste = await _context.AvioSedista
                                      .Where(x => x.deleted == false)
                                      .FirstOrDefaultAsync(id => id.id == fastFlightReservation.seatId);

            if (avioSediste == null)
            {
                return(null);
            }
            if (avioSediste.isFastReservation == false)
            {
                return(null);
            }
            if (avioSediste.deleted == true)
            {
                return(null);
            }
            if (avioSediste.reserved == true)
            {
                return(null);
            }
            avioSediste.reserved = true;
            //_context.Entry(avioSediste).State = EntityState.Modified;

            bool saveFailed;

            do
            {
                saveFailed = false;
                try
                {
                    _context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    saveFailed = true;
                    return(null);
                }
                catch (Exception e)
                {
                    throw;
                }
            } while (saveFailed);


            user.fastFlightReservations.Add(fastFlightReservation);
            // ako je user iskoristion svoj bonus
            if (fastFlightReservation.userBonus == true)
            {
                user.bonus = 0;
            }
            else
            {
                // ako nije, proverava se da li je dosao do 100%?
                if (user.bonus < 100)
                {
                    user.bonus += 1;
                }
            }

            try
            {
                _context.Entry(user).State = EntityState.Modified;
            }
            catch (Exception e)
            {
                throw;
            }

            _context.FastFlightReservations.Add(fastFlightReservation);



            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw;
            }
            //await _context.SaveChangesAsync();

            return(fastFlightReservation);
        }