Beispiel #1
0
 public static List <User> GetUsers()
 {
     using (var _context = new DiemServiceDB())
     {
         return(_context.UserDbSet.Where(u => u.Role != Role.Admin).ToList());
     }
 }
        public static void RegisterSendEmail(TempUser user)
        {
            if (user == null)
            {
                return;
            }
            using (var _context = new DiemServiceDB())
            {
                _context.TempUserDbSet.Add(user);
                _context.SaveChanges();
            }

            MailMessage mail       = new MailMessage();
            SmtpClient  SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("*****@*****.**");
            mail.To.Add(user.Email);
            mail.Subject = "User activation email";
            mail.Body    = "Click this link to activate email: " + "localhost:4200/ActivateUser/" + user.ActivationLink;

            SmtpServer.Port        = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("*****@*****.**", "web2projekat2020");
            SmtpServer.EnableSsl   = true;

            SmtpServer.Send(mail);
        }
Beispiel #3
0
        public static List <Flight> SearchFlights(SearchFlightForm form)
        {
            using (var _context = new DiemServiceDB())
            {
                List <Flight> retVal = new List <Flight>();
                if (form.Flight_Arrival_Time == null || form.Flight_Departure_Time == null || form.From_Location == null || form.To_Location == null)
                {
                    throw new Exception("BAD QUERY VERY BAD QUERY");
                }
                retVal = _context.FlightDbSet.Include(x => x.From_Location).Include(x => x.To_Location).Include(x => x.Transits).Include(x => x.Provider)
                         .Where(x => x.To_Location.State == form.To_Location &&
                                x.From_Location.State == form.From_Location &&
                                DbFunctions.TruncateTime(x.Flight_Arrival_Time) == form.Flight_Arrival_Time.Date &&
                                DbFunctions.TruncateTime(x.Flight_Departure_Time) == form.Flight_Departure_Time.Date)
                         .ToList();
                if (form.Free_seats != 0)
                {
                    retVal = retVal.Where(u => u.Seats.Count(x => x == '0') > form.Free_seats).ToList();
                }
                if (form.Flight_class != 0)
                {
                    retVal = retVal.Where(u => u.FlightClass == form.Flight_class).ToList();
                }

                return(retVal);
            }
        }
Beispiel #4
0
 public static void EditAvio(AvioCompanyEditForm form, int id)
 {
     if (form != null)
     {
         using (var _context = new DiemServiceDB())
         {
             AvioCompany retVal = _context.AvioCompanyDbSet.Where(u => u.Id == id)
                                  .Include(x => x.Owner)
                                  .Include(y => y.Flights)
                                  .Include(z => z.Destinations)
                                  .Include(i => i.Address)
                                  .FirstOrDefault();
             string caller = ((ClaimsPrincipal)HttpContext.Current.User).FindFirst("username").Value;
             User   found  = _context.UserDbSet.Where(u => u.Username == caller).FirstOrDefault();
             if (found.Role != Role.Admin && found.Username != retVal.Owner.Username)
             {
                 return;
             }
             if (form.Name != null)
             {
                 retVal.Name = form.Name;
             }
             if (form.Promo_description != null)
             {
                 retVal.Promo_description = form.Promo_description;
             }
             if (form.Address != null)
             {
                 retVal.Address.State = form.Address;
             }
             _context.SaveChanges();
         }
     }
 }
Beispiel #5
0
        public static void PromoteUser(string username, Role role)
        {
            using (var _context = new DiemServiceDB())
            {
                User found = _context.UserDbSet.Where(user => user.Username == username).FirstOrDefault();
                found.Role = role;
                switch (role)
                {
                case Role.AdminAvio:
                    AdminAvio toAdd = _context.AdminAvioDbSet.Add(new AdminAvio());
                    _context.SaveChanges();
                    found.UlogaID = toAdd.Id;
                    break;

                case Role.AdminRentACar:
                    AdminRent adminRent = _context.AdminRentDbSet.Add(new AdminRent());
                    _context.SaveChanges();
                    found.UlogaID = adminRent.Id;
                    break;

                default:
                    break;
                }
                _context.SaveChanges();
            }
        }
Beispiel #6
0
        public static void AddFastFlightReservation(ReservationForm form)
        {
            using (var _context = new DiemServiceDB())
            {
                string caller = ((ClaimsPrincipal)HttpContext.Current.User).FindFirst("username").Value;
                User   found  = _context.UserDbSet.Where(u => u.Username == caller).FirstOrDefault();
                if (found.Role != Role.RegisteredUser || found == null || form.FlightId == 0 /*|| form.Seat == 0*/ || form.Passport == 0)
                {
                    throw new Exception("BAD QUERY");
                }

                Flight        wanted = _context.FlightDbSet.Where(u => u.Id == form.FlightId).FirstOrDefault();
                StringBuilder sb     = new StringBuilder(wanted.Seats);
                if (sb[form.Seat] != '5')
                {
                    throw new Exception("NOT AN OFFER");
                }
                sb[form.Seat] = '1';
                wanted.Seats  = sb.ToString();
                FlightReservation fr = _context.FlightReservationDbSet.Add(new FlightReservation(found.Name, found.LastName, form.Seat, form.Passport, found, wanted));
                _context.RegisteredUserDbSet.Where(u => u.Id == found.UlogaID).First().FlightReservations.Add(fr);
                MailServiceManager.SendReservationEmail(fr);
                wanted.Reservations.Add(fr);
                _context.SaveChanges();
            }
        }
Beispiel #7
0
        public static void AddCompany(AddCompanyForm rent)
        {
            using (var _context = new DiemServiceDB())
            {
                User found = _context.UserDbSet.Where(user => user.Username == rent.OwnerUsername).FirstOrDefault();
                switch (found.Role)
                {
                case Role.AdminAvio:
                    AvioCompany toAdd   = rent.getAvio();
                    string      imgName = "avioCompany" + toAdd.Id;
                    File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + "/" + imgName, Convert.FromBase64String(toAdd.Logo));
                    toAdd.Logo  = imgName;
                    toAdd.Owner = found;
                    _context.AdminAvioDbSet.Include(x => x.OwnedAvioCompanies)
                    .Where(x => x.Id == found.UlogaID)
                    .FirstOrDefault().OwnedAvioCompanies
                    .Add(_context.AvioCompanyDbSet.Add(toAdd));
                    break;

                case Role.AdminRentACar:
                    RentACar toAdd2 = rent.getRent();
                    toAdd2.Owner = found;
                    _context.AdminRentDbSet.Include(x => x.OwnedRentServices)
                    .Where(x => x.Id == found.UlogaID)
                    .FirstOrDefault().OwnedRentServices
                    .Add(_context.RentACarDbSet.Add(toAdd2));
                    break;

                default:
                    break;
                }

                _context.SaveChanges();
            }
        }
Beispiel #8
0
 public static User GetUser(int userid)
 {
     using (var _context = new DiemServiceDB())
     {
         return(_context.UserDbSet.Find(userid));
     }
 }
Beispiel #9
0
        public static void AddSpecialOffer(AddOfferForm form)
        {
            if (form == null || form.seatsToDiscount == null || form.slashedPrice == 0 || form.flightId == 0)
            {
                throw new Exception("BAD QUERY");
            }
            using (var _context = new DiemServiceDB())
            {
                string caller = ((ClaimsPrincipal)HttpContext.Current.User).FindFirst("username").Value;
                User   found  = _context.UserDbSet.Where(u => u.Username == caller).FirstOrDefault();
                Flight flight = _context.FlightDbSet.Where(u => u.Id == form.flightId).Include(u => u.Provider).Include(u => u.Provider.Owner).FirstOrDefault();
                if (flight == null)
                {
                    throw new Exception("BAD QUERY");
                }
                if (found.Username != flight.Provider.Owner.Username || flight.Price.Value < form.slashedPrice)
                {
                    throw new Exception("BAD QUERY");
                }

                StringBuilder sb = new StringBuilder(flight.Seats);
                foreach (int item in form.seatsToDiscount)
                {
                    if (sb[item] == '1')
                    {
                        throw new Exception("SEAT ALREADY TAKEN");
                    }
                    sb[item] = '5';
                }

                flight.Seats           = sb.ToString();
                flight.DiscountedPrice = form.slashedPrice;
                _context.SaveChanges();
            }
        }
Beispiel #10
0
        public static List <object> GetHardcoreUsers()
        {
            List <object> retVal = new List <object>();

            using (var _context = new DiemServiceDB())
            {
                List <User> s = _context.UserDbSet.Where(u => u.Role != Role.Admin).ToList();
                foreach (User basic in s)
                {
                    switch (basic.Role)
                    {
                    case Role.RegisteredUser:
                        retVal.Add(basic);
                        break;

                    case Role.AdminAvio:
                        retVal.Add(new AdminAvioDTO(basic, _context));
                        break;

                    case Role.AdminRentACar:
                        retVal.Add(new AdminRentDTO(basic, _context));
                        break;

                    default:
                        break;
                    }
                }
                return(retVal);
            }
        }
Beispiel #11
0
 public static void AddFlight(FlightForm flight, int avioId)
 {
     using (var _context = new DiemServiceDB())
     {
         if (flight.FlightClass == 0 || flight.seats == 0 || flight.Flight_Arrival_Time == null || flight.Flight_Departure_Time == null || flight.fromLocation == null || flight.toLocation == null || flight.price == null ||
             flight.Flight_Departure_Time < DateTime.Now ||
             flight.Flight_Departure_Time.Date > flight.Flight_Arrival_Time.Date
             )
         {
             throw new Exception("BAD QEURY");
         }
         string      caller     = ((ClaimsPrincipal)HttpContext.Current.User).FindFirst("username").Value;
         User        loggedUser = _context.UserDbSet.Where(u => u.Username == caller).FirstOrDefault();
         AvioCompany found      = _context.AvioCompanyDbSet.Where(u => u.Id == avioId).Include(x => x.Owner).FirstOrDefault();
         if (loggedUser.Role != Role.Admin && loggedUser.Username != found.Owner.Username)
         {
             return;
         }
         Flight toAdd = flight.toFlight();
         toAdd.To_Location   = _context.LocationDbSet.Add(toAdd.To_Location);
         toAdd.From_Location = _context.LocationDbSet.Add(toAdd.From_Location);
         toAdd.Provider      = found;
         found.Flights.Add(_context.FlightDbSet.Add(toAdd));
         _context.SaveChanges();
     }
 }
Beispiel #12
0
        public static string LogIn(string username, string password)
        {
            if (username == null || password == null)
            {
                throw new Exception("Username or password N U L L !");
            }

            using (var _context = new DiemServiceDB())
            {
                User   logIn          = _context.UserDbSet.AsNoTracking().Where(s => s.Username == username).FirstOrDefault();
                object optionalReturn = logIn;

                if (logIn == null)
                {
                    throw new Exception("No specified username exists in the database.");
                }

                if (logIn.Hash != password)
                {
                    throw new Exception("Wrong password!");
                }

                return(TokenManager.GetToken(logIn));
            }
        }
Beispiel #13
0
        public static void ModifyFlight(FlightFormUpdate modifyValues)
        {
            using (var _context = new DiemServiceDB())
            {
                Flight Modify = _context.FlightDbSet.Find(Int32.Parse(modifyValues.Id));

                if (Modify == null)
                {
                    throw new Exception("Asked flight ID is not present in the database");
                }
                if (!string.IsNullOrEmpty(modifyValues.Price))
                {
                    Modify.Price = new Price(Double.Parse(modifyValues.Price));
                }
                if (modifyValues.Flight_Arrival_Time != null)
                {
                    Modify.Flight_Arrival_Time = modifyValues.Flight_Arrival_Time;
                    Modify.Flight_Duration     = (Modify.Flight_Arrival_Time - Modify.Flight_Departure_Time).ToString();// TRIGGER NAPRAVITI
                }
                if (modifyValues.Flight_Departure_Time != null)
                {
                    Modify.Flight_Departure_Time = modifyValues.Flight_Departure_Time;
                    Modify.Flight_Duration       = (Modify.Flight_Arrival_Time - Modify.Flight_Departure_Time).ToString();// TRIGGER NAPRAVITI
                }
                if (!string.IsNullOrEmpty(modifyValues.FromLocation))
                {
                    Modify.From_Location = _context.LocationDbSet.Add(new Location(modifyValues.FromLocation));
                }
                if (!string.IsNullOrEmpty(modifyValues.ToLocation))
                {
                    Modify.To_Location = _context.LocationDbSet.Add(new Location(modifyValues.ToLocation));
                }
                _context.SaveChanges();
            }
        }
Beispiel #14
0
 public static void DeleteVehicle(int id)
 {
     using (var context = new DiemServiceDB())
     {
         context.VehicleDbSet.Remove(context.VehicleDbSet.Find(id));
         context.SaveChanges();
     }
 }
Beispiel #15
0
 public static void DeleteFlight(int ID)
 {
     using (var _context = new DiemServiceDB())
     {
         _context.FlightDbSet.Remove(_context.FlightDbSet.Find(ID));
         _context.SaveChanges();
     }
 }
Beispiel #16
0
 public static void CancelVehicleReservation(int myid, int vehicleid)
 {
     using (var _context = new DiemServiceDB())
     {
         //_context.RegisteredUserDbSet.Find(_context.UserDbSet.Find(myid).UlogaID).VehicleReservations.Remove(_context.VehicleDbSet.Find(vehicleid));
         //_context.SaveChanges();
     }
 }
Beispiel #17
0
 public static List <User> GetUsers(string loggedUsername)
 {
     using (var _context = new DiemServiceDB())
     {
         User        caller   = _context.UserDbSet.Where(u => u.Username == loggedUsername).Include(u => u.Friends).FirstOrDefault();
         List <User> allUsers = _context.UserDbSet.Where(u => u.Role != Role.Admin).Where(u => u.Username != loggedUsername).ToList();
         return(allUsers.Where(u => !caller.Friends.Contains(u)).ToList());
     }
 }
Beispiel #18
0
 public static ICollection <Vehicle> GetAllVehicles()
 {
     using (var context = new DiemServiceDB())
     {
         ICollection <Vehicle> result = context.VehicleDbSet.Include(temp => temp.Location)
                                        .Include(temp => temp.Information)
                                        .ToList();
         return(result);
     }
 }
 public static RentACar GetById(int id)
 {
     using (var _context = new DiemServiceDB())
     {
         return(_context.RentACarDbSet.Where(u => u.Id == id)
                .Include(x => x.Owner)
                .Include(y => y.Vehicles)
                .Include(z => z.Holdings)
                .FirstOrDefault());
     }
 }
Beispiel #20
0
 public static void AddVehicle(VehicleForm vehicle)
 {
     using (var context = new DiemServiceDB())
     {
         Vehicle newVehicle = vehicle.NewVehicle();
         // Is this actually necessary
         newVehicle.Location = context.LocationDbSet.Add(newVehicle.Location);
         context.VehicleDbSet.Add(newVehicle);
         context.SaveChanges();
     }
 }
 public static void RemoveExpiredLinks()
 {
     using (var _context = new DiemServiceDB())
     {
         if (_context.TempUserDbSet.Any())
         {
             _context.TempUserDbSet.RemoveRange(_context.TempUserDbSet.Where(s => s.ExpiryDate < DateTime.Now));
         }
         _context.SaveChanges();
     }
 }
Beispiel #22
0
 public static void AvioAddFlight(FlightForm flight)
 {
     using (var _context = new DiemServiceDB())
     {
         Flight toAdd = flight.toFlight();
         toAdd.To_Location   = _context.LocationDbSet.Add(toAdd.To_Location);
         toAdd.From_Location = _context.LocationDbSet.Add(toAdd.From_Location);
         _context.FlightDbSet.Add(toAdd);
         _context.SaveChanges();
     }
 }
Beispiel #23
0
        public static void UnfriendRequest(string from, string to)
        {
            using (var _context = new DiemServiceDB())
            {
                User fromUser = _context.UserDbSet.Where(s => s.Username == from).Include(x => x.Friends).FirstOrDefault();
                User toUser   = _context.UserDbSet.Where(s => s.Username == to).Include(x => x.Friends).FirstOrDefault();

                fromUser.Friends.Remove(toUser);
                toUser.Friends.Remove(fromUser);
                _context.SaveChanges();
            }
        }
Beispiel #24
0
        public static List <Flight> GetAllFlights()
        {
            using (var _context = new DiemServiceDB())
            {
                //_context.ThePurge();
                List <Flight> retVal = _context.FlightDbSet.Include(x => x.From_Location)
                                       .Include(x => x.To_Location)
                                       .Include(x => x.Transits)
                                       .Include(x => x.Provider)
                                       .ToList();

                return(retVal);
            }
        }
Beispiel #25
0
 public AdminAvioDTO(User user, DiemServiceDB _context)
 {
     Id                 = user.Id;
     Name               = user.Name;
     LastName           = user.LastName;
     Role               = user.Role;
     Username           = user.Username;
     Email              = user.Email;
     PendingFriends     = user.PendingFriends;
     Friends            = user.Friends;
     FriendRequestsSent = user.FriendRequestsSent;
     OwnedAvioCompanies = _context.AdminAvioDbSet.AsNoTracking().Where(u => u.Id == user.UlogaID)
                          .Include(x => x.OwnedAvioCompanies.Select(y => y.Address))
                          .FirstOrDefault().OwnedAvioCompanies;                         // UVEK IDE WHERE PA INCLUDE
 }
Beispiel #26
0
 public AdminRentDTO(User user, DiemServiceDB _context)
 {
     Id                 = user.Id;
     Name               = user.Name;
     LastName           = user.LastName;
     Role               = user.Role;
     Username           = user.Username;
     Email              = user.Email;
     PendingFriends     = user.PendingFriends;
     Friends            = user.Friends;
     FriendRequestsSent = user.FriendRequestsSent;
     OwnedRentServices  = _context.AdminRentDbSet.Where(u => u.Id == user.UlogaID)
                          .Include(x => x.OwnedRentServices.Select(y => y.Address))
                          .FirstOrDefault().OwnedRentServices;
 }
Beispiel #27
0
 public static void EditReview(ReviewForm form)
 {
     using (var _context = new DiemServiceDB())
     {
         string caller = ((ClaimsPrincipal)HttpContext.Current.User).FindFirst("username").Value;
         Review found  = _context.ReviewDbSet.Where(u => u.Id == form.ReviewId).Include(u => u.User).FirstOrDefault();
         if (found == null || found.User.Username != caller)
         {
             throw new Exception("");
         }
         found.Stars   = form.Rating;
         found.Comment = form.Comment;
         _context.SaveChanges();
     }
 }
Beispiel #28
0
        public static void SendRequest(string from, string to)
        {
            using (var _context = new DiemServiceDB())
            {
                User fromUser = _context.UserDbSet.Include(x => x.FriendRequestsSent).Where(s => s.Username == from).FirstOrDefault();
                User toUser   = _context.UserDbSet.Include(x => x.PendingFriends).Where(s => s.Username == to).FirstOrDefault();

                toUser.PendingFriends.Add(fromUser);

                fromUser.FriendRequestsSent.Add(toUser);
                _context.Entry(toUser).State   = EntityState.Modified;
                _context.Entry(fromUser).State = EntityState.Modified;
                _context.SaveChanges();

                List <User> all = _context.UserDbSet.Include(x => x.FriendRequestsSent).Include(x => x.Friends).Include(x => x.PendingFriends).ToList();
            }
        }
        public static void ActivateUser(string actId)
        {
            using (var _context = new DiemServiceDB())
            {
                TempUser toAdd = _context.TempUserDbSet.Where(s => s.ActivationLink == actId).FirstOrDefault();
                if (toAdd == null)
                {
                    throw new Exception("Activation link expired or wrong");
                }

                User           toInjectPoison = _context.UserDbSet.Add(new User(toAdd));
                RegisteredUser dumbshit       = _context.RegisteredUserDbSet.Add(new RegisteredUser());
                _context.SaveChanges();
                toInjectPoison.UlogaID = dumbshit.Id;
                _context.TempUserDbSet.Remove(toAdd);
                _context.SaveChanges();
            }
        }
Beispiel #30
0
        public static object GetLoggedUser(string username)
        {
            using (var _context = new DiemServiceDB())
            {
                User retVal = _context.UserDbSet.Where(user => user.Username == username).Include(x => x.FriendRequestsSent).Include(x => x.Friends).Include(x => x.PendingFriends).FirstOrDefault();
                switch (retVal.Role)
                {
                case Role.RegisteredUser:
                    return(new RegisteredUserDTO(retVal, _context));

                case Role.AdminAvio:
                    return(new AdminAvioDTO(retVal, _context));

                case Role.AdminRentACar:
                    return(new AdminRentDTO(retVal, _context));
                }
                return(retVal);
            }
        }