public void DeleteRestaraunt(int restarauntID)
        {
            Restaraunt     dbEntry = context.Restaraunts.Find(restarauntID);
            RestarauntLang dbEntryRestarauntLangs = context.RestarauntLangs.Find(restarauntID);
            Image          dbEntryImages          = context.Images.Find(restarauntID);
            Comment        dbEntryComments        = context.Comments.Find(restarauntID);

            if (dbEntry != null)
            {
                context.Restaraunts.Remove(dbEntry);
            }
            if (dbEntryRestarauntLangs != null)
            {
                context.RestarauntLangs.Remove(dbEntryRestarauntLangs);
            }
            if (dbEntryImages != null)
            {
                context.Images.Remove(dbEntryImages);
            }
            if (dbEntryComments != null)
            {
                context.Comments.Remove(dbEntryComments);
            }
            context.SaveChanges();
        }
Beispiel #2
0
        public void Delete(int id)
        {
            Restaraunt restarauntToDelete = _db.Restraunts.FirstOrDefault(r => r.Id == id);

            _db.Restraunts.Remove(restarauntToDelete);
            _db.SaveChanges();
        }
        public async Task <IActionResult> Create(Restaraunt restaraunt)
        {
            if (ModelState.IsValid)
            {
                RestarauntService restarauntService = new RestarauntService(_configuration);
                restaraunt.ImageUrl = restarauntService.GetImage(restaraunt.Name);
                _context.Add(restaraunt);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(restaraunt));
        }
Beispiel #4
0
    //This method should respond to a POST request and save the restaurant information
    public IActionResult Create(RestarauntEditModel model)
    {
        var restaraunt = new Restaraunt();

        restaraunt.Name    = model.Name;
        restaraunt.Cuisine = model.Cuisine;

        var newRestaurant = _restaurantData.Add(restaraunt);

        //Here instead of returning an HTML as a result of a post we do a redirect
        // If we dont do redirect then when we refresh a page then the post action will be repeated and data may become duplicated or inconsistent
        //This is the POST-REDIRECT-GET pattern
        return(RedirectToAction(nameof(Details), new { id = newRestaurant.Id }));
    }
        /// <summary>
        /// Creates the restaurant if it doesn't already exist otherwise it updates the restaurant in the database
        /// </summary>
        /// <param name="restaraunt">Restaurant to be updated</param>
        /// <returns>the restaurant after being updated</returns>
        public async Task <Restaraunt> CreateRestaraunt(Restaraunt restaraunt)
        {
            if (restaraunt.Id != 0)
            {
                await _database.UpdateAsync(restaraunt);

                return(restaraunt);
            }
            else
            {
                int id = await _database.InsertAsync(restaraunt);

                restaraunt.Id = id;
                return(restaraunt);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Gets restaurants from the cache or Yelp, updates cached data with Yelp data, and returns List of that data normalized into Restaurant objects.
        /// </summary>
        /// <param name="searchTerm">
        /// string: a search term
        /// </param>
        /// <returns>
        /// List<Restaurant>: the collection of normalized restaurants for the given search term.
        /// </returns>
        private async Task <List <Restaraunt> > GetRestaurantsByLocation(string searchTerm)
        {
            Xamarin.Essentials.Location location = await ScorcherLocation.GetDeviceLocation();

            List <Restaraunt> restarauntsWithinRadius = await App.database.GetAllRestarauntsByLocation(location.Latitude, location.Longitude, searchTerm);

            IList <BusinessResponse> yelpRestaurants = await SearchYelp(location, searchTerm);

            foreach (BusinessResponse oneBiz in yelpRestaurants)
            {
                Restaraunt restaraunt = new Restaraunt()
                {
                    Date             = DateTime.Now,
                    Lat              = oneBiz.Coordinates.Latitude,
                    Lon              = oneBiz.Coordinates.Longitude,
                    Name             = oneBiz.Name,
                    Address          = oneBiz.Location.Address1,
                    City             = oneBiz.Location.City,
                    State            = oneBiz.Location.State,
                    Zip              = oneBiz.Location.ZipCode,
                    YelpId           = oneBiz.Id,
                    YelpCategory     = searchTerm,
                    ImageUrl         = oneBiz.ImageUrl,
                    LevelMax         = 5,
                    LevelMin         = 1,
                    RestarauntOffset = 0.0m
                };
                Restaraunt dbRestaurant = await App.database.FindRestarauntYelpId(restaraunt.YelpId);

                if (dbRestaurant == null)
                {
                    await App.database.CreateRestaraunt(restaraunt);

                    restarauntsWithinRadius.Add(restaraunt);
                }
                else
                {
                    if (DateTime.Now.Subtract(dbRestaurant.Date).TotalDays > 3)
                    {
                        restaraunt.RestarauntOffset = dbRestaurant.RestarauntOffset;
                        await App.database.UpdateRestaraunt(restaraunt);
                    }
                }
            }
            return(restarauntsWithinRadius);
        }
 public ActionResult GetRestarauntInfo(int restarauntID)
 {
     try
     {
         if (!restarauntID.Equals(null))
         {
             Restaraunt        Restaraunt     = restRepository.GetRestarauntByID(restarauntID);
             RestarauntLang    RestarauntLang = restLangRepository.GetRestarauntLangByID(restarauntID);
             RestarauntAllData result         = workWithDBProvider.CreateRestarauntAllData(Restaraunt, RestarauntLang);
             return(Json(new { result = result }));
         }
         return(Json(new { result = "JSON is null" }));
     }
     catch
     {
         return(Json(new { result = "NaN" }));
     }
 }
        public ActionResult Index(IEnumerable <HttpPostedFileBase> files)
        {
            var        id         = Convert.ToInt32(Request.Cookies["id"].Value);
            Restaraunt Restaraunt = restRepository.GetRestarauntByID(id);
            var        res        = new List <HttpPostedFileBase>();
            // set path and foldername
            string FolderName = "Restaraunt" + id;
            string Folder     = Server.MapPath("~/Content/Images/RestaurantImages/");
            string pathString = System.IO.Path.Combine(Folder, FolderName);

            System.IO.Directory.CreateDirectory(pathString);
            string ForSaving = Server.MapPath("~/Content/Images/RestaurantImages/" + FolderName + "/");
            //
            int counter = 0;

            foreach (var file in files)
            {
                System.Drawing.Image imageObject = new Bitmap(file.InputStream);

                string FileName          = counter + ".jpg";
                string ThumbnailFileName = "small_" + counter + ".jpg";

                string Url          = System.IO.Path.Combine(ForSaving, FileName);
                string ThumbnailUrl = System.IO.Path.Combine(ForSaving, ThumbnailFileName);
                counter++;

                imageObject.Save(Url, System.Drawing.Imaging.ImageFormat.Jpeg);
                GetThumbnail(imageObject, 200, 200).Save(ThumbnailUrl, System.Drawing.Imaging.ImageFormat.Jpeg);

                // adding url to DB
                // костыль. не универсальный способ для сохранения URL
                Url = System.IO.Path.Combine("http://rest-rate.azurewebsites.net/Content/Images/RestaurantImages/" + FolderName + "/", FileName);
                //
                Domain.Entities.Image New = new Domain.Entities.Image()
                {
                    Url = Url, Name = FileName
                };
                New.RestarauntID = id;
                imageRepository.SaveImage(New);
                //
            }
            return(null);
        }
 public ActionResult EditRestaurant(RestaurantData data)
 {
     if (!data.Equals(null))
     {
         try
         {
             Restaraunt     EditRestaraunt = data.RestarauntData;
             RestarauntLang EditRestLang   = data.RestaurantLangData;
             restRepository.SaveRestaraunt(EditRestaraunt);
             restLangRepository.SaveRestarauntLang(EditRestLang);
             return(Json(new { result = "success" }));
         }
         catch
         {
             return(Json(new { result = "error", message = "Ooooooops! Some troubles was happened with DB." }));
         }
     }
     return(Json(new { result = "error", message = "JSON IS NULL" }));
 }
        public RestarauntAllData CreateRestarauntAllData(Restaraunt restaraunt, RestarauntLang restLang)
        {
            RestarauntAllData result = new RestarauntAllData();

            result.Address         = restLang.Address;
            result.Country         = restLang.Country;
            result.Locality        = restLang.Locality;
            result.Region          = restLang.Region;
            result.Review          = restLang.Review;
            result.Name            = restLang.Name;
            result.Longitude       = restaraunt.Longitude;
            result.Latitude        = restaraunt.Latitude;
            result.KitchenRate     = restaraunt.KitchenRate;
            result.InteriorRate    = restaraunt.InteriorRate;
            result.MaintenanceRate = restaraunt.MaintenanceRate;
            result.RestarauntType  = restaraunt.RestarauntType;
            result.AddedDate       = restaraunt.AddedDate;
            result.Images          = imageRepository.GetRestarauntImages(restaraunt.RestarauntID);
            return(result);
        }
 public void SaveRestaraunt(Restaraunt restaraunt)
 {
     if (restaraunt.RestarauntID == 0)
     {
         context.Restaraunts.Add(restaraunt);
     }
     else
     {
         Restaraunt dbEntry = context.Restaraunts.Find(restaraunt.RestarauntID);
         if (dbEntry != null)
         {
             dbEntry.InteriorRate    = restaraunt.InteriorRate;
             dbEntry.KitchenRate     = restaraunt.KitchenRate;
             dbEntry.MaintenanceRate = restaraunt.MaintenanceRate;
             dbEntry.RestarauntType  = restaraunt.RestarauntType;
             dbEntry.Longitude       = restaraunt.Longitude;
             dbEntry.Latitude        = restaraunt.Latitude;
         }
     }
     context.SaveChanges();
 }
        /// <summary>
        /// Updates the offset for therestaurant associated with the YelpId
        /// </summary>
        /// <param name="yelpId">Yelp Id associated with a particular restaurant</param>
        /// <returns></returns>
        public async Task UpdateRestarauntOffSet(string yelpId)
        {
            List <Dish> restaurantDishes = await GetAllDishesByYelpId(yelpId);

            int     count       = 0;
            decimal totalOffset = 0;

            foreach (var item in restaurantDishes)
            {
                if (item.RestaurantDishOffset != 0)
                {
                    totalOffset += item.RestaurantDishOffset;
                    count++;
                }
            }
            decimal    offSet     = totalOffset / count;
            Restaraunt restaraunt = await FindRestarauntYelpId(yelpId);

            restaraunt.RestarauntOffset = Math.Round(offSet, 1);
            await CreateRestaraunt(restaraunt);
        }
        public async Task <IActionResult> Edit(int id, Restaraunt restaraunt, IFormFile image)
        {
            if (id != restaraunt.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    RestarauntService restarauntService = new RestarauntService(_configuration);
                    restaraunt.ImageUrl = restarauntService.GetImage(restaraunt.Name);
                    if (image != null)
                    {
                        using (var stream = new MemoryStream())
                        {
                            image.CopyTo(stream);
                            restaraunt.Image = stream.ToArray();
                        }
                    }
                    _context.Update(restaraunt);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RestarauntExists(restaraunt.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Details), new { id = restaraunt.Id }));
            }
            return(View(restaraunt));
        }
 public ActionResult AddRestaurant(RestaurantData data)
 {
     if (!data.Equals(null))
     {
         try
         {
             Restaraunt     NewRestaraunt = data.RestarauntData;
             RestarauntLang NewRestLang   = data.RestaurantLangData;
             NewRestaraunt.AddedDate      = DateTime.Now;
             NewRestaraunt.RestarauntType = Restaraunt.RestType.Bar;
             restRepository.SaveRestaraunt(NewRestaraunt);
             NewRestLang.LanguageID   = 1;
             NewRestLang.RestarauntID = NewRestaraunt.RestarauntID;
             restLangRepository.SaveRestarauntLang(NewRestLang);
             return(Json(new { result = "success", id = NewRestaraunt.RestarauntID }));
         }
         catch
         {
             return(Json(new { result = "error", message = "Ooooooops! Some troubles was happened with DB." }));
         }
     }
     return(Json(new { result = "error", message = "JSON IS NULL" }));
 }
Beispiel #15
0
 public ActionResult GetRestaurantInfo(int restarauntID)
 {
     try
     {
         if (!restarauntID.Equals(null))
         {
             Restaraunt        Restaraunt     = restRepository.GetRestarauntByID(restarauntID);
             RestarauntLang    RestarauntLang = restLangRepository.GetRestarauntLangByID(restarauntID);
             RestarauntAllData result         = workWithDBProvider.CreateRestarauntAllData(Restaraunt, RestarauntLang);
             string            tmp            = String.Empty;
             foreach (var image in result.Images) // need debug. подумать как упростить
             {
                 for (int i = image.Url.Length - 1; i >= 0; i--)
                 {
                     if (image.Url[i].Equals('\\'))
                     {
                         string NameAndExtension = new string(tmp.ToCharArray().Reverse().ToArray()); // gpj.<image_name> to <image_name>.jpg
                         image.Url = image.Url.Remove(i + 1) + "small_" + NameAndExtension;
                         tmp       = string.Empty;
                         break;
                     }
                     else
                     {
                         tmp += image.Url[i];
                     }
                 }
             }
             return(Json(new { result = result }));
         }
         return(Json(new { result = "JSON is null" }));
     }
     catch
     {
         return(Json(new { result = "NaN" }));
     }
 }
Beispiel #16
0
        public Restaraunt Add(Restaraunt restaraunt)
        {
            _restaraunts.Add(restaraunt);

            return(restaraunt);
        }
Beispiel #17
0
        public static void Initialize(RestarauntDBContext context)
        {
            context.Database.EnsureCreated();
            if (!context.Countries.Any())
            {
                var Countries = new Country[]
                {
                    new Country {
                        Name = "England", CallCode = "+44", Currency = "Pounds"
                    },
                    new Country {
                        Name = "United States", CallCode = "+1", Currency = "Dollars"
                    },
                    new Country {
                        Name = "Germany", CallCode = "+49", Currency = "Euro"
                    },
                    new Country {
                        Name = "France", CallCode = "+33", Currency = "Euro"
                    }
                };
                context.Countries.AddRange(Countries);
                context.SaveChanges();
            }

            if (!context.Cities.Any())
            {
                var CountEN = context.Countries.FirstOrDefault(x => x.Name == "England");
                var CountUS = context.Countries.FirstOrDefault(x => x.Name == "United States");
                var CountDE = context.Countries.FirstOrDefault(x => x.Name == "Germany");
                var CountFR = context.Countries.FirstOrDefault(x => x.Name == "France");

                var Cities = new City[]
                {
                    new City {
                        CountryID = CountEN.ID, Name = "London", Longitude = 51.5074, Latitude = 0.127
                    },
                    new City {
                        CountryID = CountUS.ID, Name = "New York", Longitude = 40.7128, Latitude = 74.0060
                    },
                    new City {
                        CountryID = CountDE.ID, Name = "Berlin", Longitude = 52.5200, Latitude = 13.4050
                    },
                    new City {
                        CountryID = CountFR.ID, Name = "Paris", Longitude = 48.8566, Latitude = 2.3522
                    }
                };
                context.Cities.AddRange(Cities);
                context.SaveChanges();
            }

            if (!context.Restaurants.Any())
            {
                var CityLN = context.Cities.FirstOrDefault(x => x.Name == "London");
                var CityNY = context.Cities.FirstOrDefault(x => x.Name == "New York");
                var CityBL = context.Cities.FirstOrDefault(x => x.Name == "Berlin");
                var CityPR = context.Cities.FirstOrDefault(x => x.Name == "Paris");

                var Restaraunts = new Restaraunt[]
                {
                    new Restaraunt {
                        CityID = CityLN.ID, Name = "Bleecker Burger Victoria", Address = "205 Victoria St, Westminster, London SW1E 5NE", Type = Helper.RestarauntType.Burger
                    },
                    new Restaraunt {
                        CityID = CityNY.ID, Name = "The Modern", Address = "9 W 53rd St, New York, NY 10019, United States", Type = Helper.RestarauntType.FineDining
                    },
                    new Restaraunt {
                        CityID = CityBL.ID, Name = "Asia Mami Berlin Marzahn", Address = "Allee der Kosmonauten 151, 12685 Berlin, Germany", Type = Helper.RestarauntType.Indian
                    },
                    new Restaraunt {
                        CityID = CityPR.ID, Name = "Tour Maine Montparnasse", Address = "56ème, Avenue du Maine, 75015 Paris, France", Type = Helper.RestarauntType.FineDining
                    },
                };
                context.Restaurants.AddRange(Restaraunts);
                context.SaveChanges();
            }
        }
        public async Task <Restaraunt> UpdateRestaraunt(Restaraunt restaraunt)
        {
            await _database.UpdateAsync(restaraunt);

            return(await GetRestaraunt(restaraunt.Id));
        }
 public Restaraunt Add(Restaraunt restaraunt)
 {
     _context.Restaurants.Add(restaraunt);
     _context.SaveChanges();
     return(restaraunt);
 }
        /// <summary>
        /// Deletes a restaurant from the database
        /// </summary>
        /// <param name="restaraunt">The restaurant to be deleted</param>
        /// <returns>The restaurant that was deleted</returns>
        public async Task <Restaraunt> DeleteRestaraunt(Restaraunt restaraunt)
        {
            await _database.DeleteAsync(restaraunt);

            return(restaraunt);
        }
Beispiel #21
0
 public void Put(int id, [FromBody] Restaraunt updatedRestaraunt)
 {
     updatedRestaraunt.Id = id;
     _db.Entry(updatedRestaraunt).State = EntityState.Modified;
     _db.SaveChanges();
 }
Beispiel #22
0
 public void Post([FromBody] Restaraunt newRestaraunt)
 {
     _db.Restraunts.Add(newRestaraunt);
     _db.SaveChanges();
 }