Esempio n. 1
0
        public async Task <ActionResult <FlightDestination> > DeleteFlightDestination(MAANPP20ContextFlight _context, int id)
        {
            var flightDestinaion = await _context.FlightDestinations
                                   .Include(address1 => address1.startAddress)
                                   .Include(address2 => address2.endAddress)
                                   .FirstOrDefaultAsync(i => i.id == id);

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

            flightDestinaion.deleted = true;
            flightDestinaion.startAddress.deleted = true;
            flightDestinaion.endAddress.deleted   = true;

            _context.Entry(flightDestinaion.startAddress).State = EntityState.Modified;
            _context.Entry(flightDestinaion.endAddress).State   = EntityState.Modified;
            _context.Entry(flightDestinaion).State = EntityState.Modified;

            //_context.FlightDestinations.Remove(flightDestinaion);
            await _context.SaveChangesAsync();

            return(flightDestinaion);
        }
Esempio n. 2
0
        public async Task <FlightCompany> UpdateFlightCompany(MAANPP20ContextFlight _context, FlightCompany flightCompany)
        {
            if (flightCompany.admin != null)
            {
                flightCompany.admin.serviceId             = flightCompany.id;
                _context.Entry(flightCompany.admin).State = EntityState.Modified;
            }

            _context.Entry(flightCompany.address).State = EntityState.Modified;
            _context.Entry(flightCompany).State         = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FlightCompanyExists(_context, flightCompany.id))
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }

            return(flightCompany);
        }
        public async Task <Flight> UpdateFlight(MAANPP20ContextFlight _context, Flight flight)
        {
            foreach (var seat in flight.allSeatsForThisFlight)
            {
                _context.Entry(seat).State = EntityState.Modified;
            }
            _context.Entry(flight).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FlightExists(_context, flight.id))
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }

            return(flight);
        }
        public async Task <Friend> DeleteFriend(MAANPP20ContextFlight _context, string id)
        {
            if (id == null)
            {
                return(null);
            }

            string myId  = id.Split('|')[0];
            string hisId = id.Split('|')[1];

            var friend = await _context.Friends.Where(x => x.deleted == false)
                         .Include(messages => messages.messages)
                         .FirstOrDefaultAsync(i => i.myId == myId && i.hisId == hisId);

            if (friend == null)
            {
                return(null);
            }
            if (friend.deleted == true)
            {
                return(null);
            }

            foreach (var message in friend.messages)
            {
                message.deleted = true;
                _context.Entry(message).State = EntityState.Modified;
            }

            friend.deleted = true;
            _context.Entry(friend).State = EntityState.Modified;

            var friend1 = await _context.Friends.Where(x => x.deleted == false)
                          .Include(messages => messages.messages)
                          .FirstOrDefaultAsync(i => i.myId == hisId && i.hisId == myId);

            if (friend1 == null)
            {
                return(null);
            }
            if (friend1.deleted == true)
            {
                return(null);
            }

            foreach (var message in friend1.messages)
            {
                message.deleted = true;
                _context.Entry(message).State = EntityState.Modified;
            }

            friend1.deleted = true;
            _context.Entry(friend1).State = EntityState.Modified;

            await _context.SaveChangesAsync();

            return(friend);
        }
Esempio n. 5
0
        public async Task <FlightReservation> UpdateFlightReservation(MAANPP20ContextFlight _context, FlightReservation flightReservation)
        {
            Flight flight = await _context.Flights.Where(x => x.deleted == false)
                            .Include(ocene => ocene.ocene)
                            .FirstOrDefaultAsync(id => id.id == flightReservation.flightId);

            if (flight == null)
            {
                return(null);
            }
            if (flightReservation.ocenaLeta > 0)
            {
                DoubleForICollection doubleForICollection = new DoubleForICollection();
                doubleForICollection.DoubleValue = flightReservation.ocenaLeta;
                flight.ocene.Add(doubleForICollection);
                _context.Entry(flight).State = EntityState.Modified;
            }

            if (flightReservation.ocenaKompanije > 0)
            {
                FlightCompany flightCompany = await _context.FlightCompanies.Where(x => x.deleted == false)
                                              .Include(ocene => ocene.ocene)
                                              .FirstOrDefaultAsync(id => id.id == flight.idCompany);

                if (flightCompany == null)
                {
                    return(null);
                }
                DoubleForICollection doubleForICollection = new DoubleForICollection();
                doubleForICollection.DoubleValue = flightReservation.ocenaKompanije;
                flightCompany.ocene.Add(doubleForICollection);
                _context.Entry(flightCompany).State = EntityState.Modified;
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FlightReservationExists(_context, flightReservation.id))
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }

            return(flightReservation);
        }
Esempio n. 6
0
        public async Task <ActionResult <IEnumerable <FriendForFlight> > > GetCallsForFlight(MAANPP20ContextFlight _context, string email)
        {
            var user = await _context.Users.Where(x => x.deleted == false)
                       .FirstOrDefaultAsync(i => i.Email == email);

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

            #region Provara kod svakod da li je proslo vise od 3 dana od kreiranja rezervacije
            var friendForFlightForCheck = await _context.FriendForFlights
                                          .Where(x => x.deleted == false && x.acceptedCall == false).ToListAsync();

            foreach (var friend in friendForFlightForCheck)
            {
                if (friend == null)
                {
                    continue;
                }
                if (CheckDateIf(friend.reservationDate))
                {
                    friend.deleted = true;
                    _context.Entry(friend).State = EntityState.Modified;

                    var avioSeat = await _context.AvioSedista
                                   .Where(x => x.deleted == false)
                                   .FirstOrDefaultAsync(x => x.id == friend.seatId);

                    if (avioSeat == null)
                    {
                        continue;
                    }
                    avioSeat.reserved = false;
                    _context.Entry(avioSeat).State = EntityState.Modified;

                    _context.SaveChanges();
                }
            }
            #endregion


            var friendForFlight = await _context.FriendForFlights
                                  .Where(x => x.deleted == false && x.email == email && x.ime == user.firstName && x.prezime == user.lastName && x.acceptedCall == false)
                                  .ToListAsync();

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

            return(friendForFlight);
        }
Esempio n. 7
0
        public async Task <ActionResult <FlightCompany> > DeleteFlightCompany(MAANPP20ContextFlight _context, int id)
        {
            var flightCompany = await _context.FlightCompanies
                                .Include(address => address.address)
                                .Include(destinations => destinations.destinations)
                                .ThenInclude(startAddr => startAddr.startAddress)
                                .Include(destinations => destinations.destinations)
                                .ThenInclude(endAddr => endAddr.endAddress)
                                .Include(flights => flights.flights)
                                .ThenInclude(allSeats => allSeats.allSeatsForThisFlight)
                                .Include(admin => admin.admin)
                                .Include(ocene => ocene.ocene)
                                .FirstOrDefaultAsync(i => i.id == id);

            if (flightCompany == null)
            {
                return(null);
            }
            else if (flightCompany.deleted == true)
            {
                return(null);
            }

            flightCompany.admin.serviceId             = 0;
            _context.Entry(flightCompany.admin).State = EntityState.Modified;

            flightCompany.deleted         = true;
            flightCompany.address.deleted = true;
            foreach (var destination in flightCompany.destinations)
            {
                destination.deleted = true;
                destination.startAddress.deleted = true;
                destination.endAddress.deleted   = true;
            }
            foreach (var flight in flightCompany.flights)
            {
                flight.deleted = true;
                foreach (var seat in flight.allSeatsForThisFlight)
                {
                    seat.deleted = true;
                }
            }
            _context.Entry(flightCompany).State = EntityState.Modified;

            //_context.FlightCompanies.Remove(flightCompany);
            await _context.SaveChangesAsync();

            return(flightCompany);
        }
Esempio n. 8
0
        public async Task <FriendForFlight> DeclineFriendForFlight(MAANPP20ContextFlight _context, string invitationString)
        {
            var friendForFlight = await _context.FriendForFlights
                                  .Where(x => x.deleted == false)
                                  .FirstOrDefaultAsync(x => x.invitationString == invitationString);

            if (friendForFlight == null)
            {
                return(null);
            }
            if (friendForFlight.deleted == true)
            {
                return(null);
            }

            var seat = await _context.AvioSedista
                       .Where(x => x.reserved == true)
                       .FirstOrDefaultAsync(x => x.id == friendForFlight.seatId);

            if (seat == null)
            {
                return(null);
            }
            seat.reserved = false;
            _context.Entry(seat).State = EntityState.Modified;

            friendForFlight.deleted = true;
            _context.Entry(friendForFlight).State = EntityState.Modified;

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

            return(friendForFlight);
        }
Esempio n. 9
0
        public async Task <FlightDestination> UpdateFlightCompany(MAANPP20ContextFlight _context, FlightDestination flightDestination)
        {
            _context.Entry(flightDestination.startAddress).State = EntityState.Modified;
            _context.Entry(flightDestination.endAddress).State   = EntityState.Modified;
            _context.Entry(flightDestination).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FlightDestinationExists(_context, flightDestination.id))
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }

            return(flightDestination);
        }
Esempio n. 10
0
        public async Task <FriendForFlight> AcceptFriendForFlight(MAANPP20ContextFlight _context, StringForICollection invitationString)
        {
            var friendForFlight = await _context.FriendForFlights
                                  .Where(x => x.deleted == false)
                                  .FirstOrDefaultAsync(x => x.invitationString == invitationString.PlainString);

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

            friendForFlight.acceptedCall          = true;
            _context.Entry(friendForFlight).State = EntityState.Modified;

            _context.SaveChanges();

            return(friendForFlight);
        }
        public async Task <ActionResult <Flight> > DeleteFlight(MAANPP20ContextFlight _context, int id)
        {
            var flight = await _context.Flights
                         .Include(presedanje => presedanje.presedanje)
                         .ThenInclude(gradoviPresedanja => gradoviPresedanja.gradoviPresedanja)
                         .Include(luggage => luggage.luggage)
                         .Include(ocene => ocene.ocene)
                         .Include(allSeatsForThisFlight => allSeatsForThisFlight.allSeatsForThisFlight)
                         .FirstOrDefaultAsync(i => i.id == id);

            if (flight == null)
            {
                return(null);
            }
            else if (flight.deleted == true)
            {
                return(null);
            }

            flight.deleted = true;
            foreach (var seat in flight.allSeatsForThisFlight)
            {
                seat.deleted = true;
            }

            flight.presedanje.deleted = true;
            foreach (var gradPresedanja in flight.presedanje.gradoviPresedanja)
            {
                gradPresedanja.deleted = true;
            }

            flight.luggage.deleted = true;

            // TO DO: brisanje ocena

            _context.Entry(flight).State = EntityState.Modified;
            //_context.Flights.Remove(flight);
            await _context.SaveChangesAsync();

            return(flight);
        }
        public async Task <Aeroplane> UpdatePlane(MAANPP20ContextFlight _context, Aeroplane aeroplane)
        {
            _context.Entry(aeroplane).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PlaneExists(_context, aeroplane.id))
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }

            return(aeroplane);
        }
        public async Task <ActionResult <Aeroplane> > DeletePlane(MAANPP20ContextFlight _context, int id)
        {
            var plane = await _context.Aeroplanes
                        .Where(x => x.deleted == false)
                        .FirstOrDefaultAsync(i => i.id == id);

            if (plane == null)
            {
                return(null);
            }
            else if (plane.deleted == true)
            {
                return(null);
            }

            plane.deleted = true;

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

            //_context.Aeroplanes.Remove(plane);
            await _context.SaveChangesAsync();

            return(plane);
        }
Esempio n. 14
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);
                }
            }
        }
Esempio n. 15
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);
        }
        public async Task <ActionResult <Friend> > AddFriend(MAANPP20ContextFlight _context, string id)
        {
            if (id == null)
            {
                return(null);
            }
            string myId  = null;
            string hisId = null;

            try
            {
                myId  = id.Split('|')[0];
                hisId = id.Split('|')[1];
            }
            catch
            {
                return(null);
            }


            var user = await _context.Users.Where(x => x.deleted == false)
                       .Include(address => address.address)
                       .Include(friends => friends.friends)
                       //.ThenInclude()
                       .FirstOrDefaultAsync(i => i.Id == myId);

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

            var friendUser = await _context.Users.Where(x => x.deleted == false)
                             .Include(address => address.address)
                             .Include(friends => friends.friends)
                             .FirstOrDefaultAsync(i => i.Id == hisId);

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

            // dodajem njega kod sebe u listu prijatelja
            var friend = new Friend();

            friend.messages = new List <Message>();
            friend.myId     = user.Id;
            friend.hisId    = friendUser.Id;
            user.friends.Add(friend);

            //_context.Entry(user.friends).State = EntityState.Added;
            _context.Entry(user).State = EntityState.Modified;

            // sebe dodajem kod njega u listu prijatelja
            var friend2 = new Friend();

            friend2.messages = new List <Message>();
            friend2.myId     = friendUser.Id;
            friend2.hisId    = user.Id;
            friendUser.friends.Add(friend2);
            _context.Entry(friendUser).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                //if (!FlightExists(flight.id))
                //{
                return(null);
                //}
                //else
                //{
                //    throw;
                //}
            }

            return(friend);
        }
Esempio n. 17
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);
        }