public void AddCity([FromBody] CityModel model)
 {
     if (ModelState.IsValid)
     {
         if (model.Id == 0)
         {
             City city = new City()
             {
                 Id        = model.Id,
                 Name      = model.Name,
                 CountryId = model.CountryId,
                 IsDeleted = false
             };
             db.Cities.Add(city);
         }
         if (model.Id != 0)
         {
             City city = db.Cities.FirstOrDefault(x => x.Id == model.Id);
             city.Name            = model.Name;
             city.CountryId       = model.CountryId;
             db.Entry(city).State = EntityState.Modified;
         }
         db.SaveChanges();
     }
 }
Esempio n. 2
0
 public void AddAirport([FromBody] AirportModel model)
 {
     if (ModelState.IsValid)
     {
         if (model.Id == 0)
         {
             Airport airport = new Airport()
             {
                 Id        = model.Id,
                 Name      = model.Name,
                 Code      = model.Code,
                 CityId    = model.CityId,
                 IsDeleted = false
             };
             db.Airports.Add(airport);
         }
         if (model.Id != 0)
         {
             Airport airport = db.Airports.FirstOrDefault(x => x.Id == model.Id);
             airport.Name            = model.Name;
             airport.Code            = model.Code;
             airport.CityId          = model.CityId;
             db.Entry(airport).State = EntityState.Modified;
         }
         db.SaveChanges();
     }
 }
        public IHttpActionResult EditCustomer(Customer editCustomer)
        {
            if (ModelState.IsValid)
            {
                var  customer = db.Customers.Find(GetCustomer(User.Identity.Name).Id);
                User user     = db.Users.Include(u => u.Role).FirstOrDefault(u => u.Email == customer.Email);

                if (user != null && User.Identity.Name != editCustomer.Email)
                {
                    ModelState.AddModelError("", "user with such email already exists");
                    return(BadRequest(ModelState));
                }
                customer.FirstName       = editCustomer.FirstName;
                customer.LastName        = editCustomer.LastName;
                customer.PhoneNumber     = editCustomer.PhoneNumber;
                customer.DateOfBirth     = editCustomer.DateOfBirth;
                customer.Email           = editCustomer.Email;
                db.Entry(customer).State = EntityState.Modified;
                user.Email           = editCustomer.Email;
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();

                Utils utils = new Utils();
                utils.AddUserInCookies(user, AuthenticationManager);
                return(Ok());
            }

            return(BadRequest(ModelState));
        }
        public void DeleteCity(int id)
        {
            City city = GetCityById(id);

            city.IsDeleted = true;

            context.Entry(city).State = EntityState.Modified;
            Save();
        }
Esempio n. 5
0
        public IHttpActionResult DeleteTour([FromBody] int id)
        {
            Tour tour = db.Tours.Find(id);

            tour.IsDeleted = true;

            db.Entry(tour).State = EntityState.Modified;
            db.SaveChanges();
            return(Ok());
        }
        public void EditFlight([FromBody] EditFlightModel model)
        {
            Flight flight = db.Flights.FirstOrDefault(x => x.Id == model.id);

            flight.Number             = model.number;
            flight.Price              = model.price;
            flight.DepartureAirportId = model.departureAirportId;
            flight.ArrivalAirportId   = model.arrivalAirportId;

            flight.Airplane.Model       = model.airplaneModel;
            flight.Airplane.Company     = model.airplaneCompany;
            flight.Airplane.SeatsAmount = model.seatsAmount;

            db.Entry(flight).State          = EntityState.Modified;
            db.Entry(flight.Airplane).State = EntityState.Modified;
            db.SaveChanges();
        }
 public void AddCountry(CountryModel model)
 {
     if (model.Id == 0)
     {
         Country country = new Country()
         {
             Id        = model.Id,
             Name      = model.Name,
             IsDeleted = false
         };
         db.Coutries.Add(country);
     }
     if (model.Id != 0)
     {
         Country country = db.Coutries.FirstOrDefault(x => x.Id == model.Id);
         country.Name            = model.Name;
         db.Entry(country).State = EntityState.Modified;
     }
     db.SaveChanges();
 }
 private void AddCountriesForManager(SevenWondersContext db, Manager manager, int[] countries)
 {
     if (manager.Countries != null)
     {
         foreach (var country in manager.Countries.ToList())
         {
             country.ManagerId       = null;
             db.Entry(country).State = EntityState.Modified;
         }
     }
     db.SaveChanges();
     if (countries != null)
     {
         var length = countries.Length;
         for (int i = 0; i < length; i++)
         {
             var country = db.Coutries.Find(countries[i]);
             country.ManagerId       = manager.Id;
             db.Entry(country).State = EntityState.Modified;
         }
     }
     db.SaveChanges();
 }
        public virtual bool ChangePersonStatus(SevenWondersContext db, int id)
        {
            DbSet <T> dbSet    = db.Set <T>();
            var       result   = false;
            var       customer = dbSet.Find(id);

            if (customer != null)
            {
                customer.IsDeleted       = !customer.IsDeleted;
                db.Entry(customer).State = EntityState.Modified;
                result = true;
                db.SaveChanges();
            }
            return(result);
        }
        public void EditFullManager(SevenWondersContext db, FullManagerViewModel user, int[] countries)
        {
            Manager manager    = db.Managers.Find(user.Id);
            var     userFromDb = db.Users.FirstOrDefault(x => x.Email == manager.Email);

            manager.LastName        = user.LastName;
            manager.FirstName       = user.FirstName;
            manager.Email           = user.Email;
            manager.PhoneNumber     = user.PhoneNumber;
            manager.DateOfBirth     = user.DateOfBirth;
            db.Entry(manager).State = EntityState.Modified;
            db.SaveChanges();

            Utils utils = new Utils();

            if (user.Password != userFromDb.Password)
            {
                userFromDb.Password = utils.GetEncodedHash(user.Password, Security.solt);
            }
            userFromDb.Email = user.Email;
            AddCountriesForManager(db, manager, countries);
            db.Entry(userFromDb).State = EntityState.Modified;
            db.SaveChanges();
        }
        public IHttpActionResult AddHotel([FromBody] HotelShortInfoModel model)
        {
            Hotel hotel = new Hotel();

            if (model.Id == -1)
            {
                hotel.Name        = model.Name;
                hotel.Address     = model.Address;
                hotel.Description = model.Description;
                hotel.CityId      = model.CityId;
                hotel.FoodPrice   = model.FoodPrice;
                hotel.FoodTypeId  = model.FoodTypeId;
                hotel.RatingId    = model.Rating;

                db.Hotels.Add(hotel);

                if (model.Facilities != null)
                {
                    foreach (var f in model.Facilities)
                    {
                        Facility facility = db.Facilities.FirstOrDefault(e => e.Id.ToString() == f);
                        facility.Hotels.Add(hotel);
                    }
                }
            }
            else
            {
                hotel             = db.Hotels.FirstOrDefault(x => x.Id == model.Id);
                hotel.Name        = model.Name;
                hotel.Address     = model.Address;
                hotel.Description = model.Description;
                hotel.CityId      = model.CityId;
                hotel.FoodPrice   = model.FoodPrice;
                hotel.FoodTypeId  = model.FoodTypeId;
                hotel.RatingId    = model.Rating;
                var photosForDeleting = hotel.HotelsPhotos.Where(x => !model.HotelPhotos.Any(y => y.Id == x.Id));
                foreach (var photo in photosForDeleting)
                {
                    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, photo.PhotoLink.Remove(0, 6).Replace("/", "\\"));
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }

                    photo.IsDeleted       = true;
                    db.Entry(photo).State = EntityState.Modified;
                }

                hotel.Facilities.RemoveRange(0, hotel.Facilities.Count);
                if (model.Facilities != null)
                {
                    foreach (var eq in model.Facilities)
                    {
                        Facility facility = db.Facilities.FirstOrDefault(e => e.Id.ToString() == eq);
                        hotel.Facilities.Add(facility);
                    }
                }
                db.Entry(hotel).State = EntityState.Modified;
            }
            db.SaveChanges();
            return(Ok(hotel.Id));
        }