public IActionResult Edit(int?id)
        {
            restaurant_review restaurantList = null;

            string xmlFilePath = Path.GetFullPath("Data/restaurant_review .xml");

            FileStream xs = new FileStream(xmlFilePath, FileMode.Open);

            XmlSerializer serializor = new XmlSerializer(typeof(restaurant_review));

            restaurantList = (restaurant_review)serializor.Deserialize(xs);

            xs.Close();
            //id = 0;
            int i = (int)id - 1;

            RestaurantEditViewModel restaurantEditView = new RestaurantEditViewModel()
            {
                Id            = i,
                Name          = restaurantList.restaurant[i].name,
                StreetAddress = restaurantList.restaurant[i].address.StreetAddress,
                City          = restaurantList.restaurant[i].address.city,
                ProvinceState = restaurantList.restaurant[i].address.ProvinceState,
                PostalZipCode = restaurantList.restaurant[i].address.PostalZipCode,
                Rating        = decimal.Parse(restaurantList.restaurant[i].reviews.review.rating.Value),
                Summary       = restaurantList.restaurant[i].reviews.review.summary
            };

            return(View(restaurantEditView));
        }
        public IActionResult Index()
        {
            restaurant_review restaurantList = null;
            List <RestaurantOverviewViewModel> restaurantOverviewViewModels = new List <RestaurantOverviewViewModel>();

            string xmlFilePath = Path.GetFullPath("Data/restaurant_review .xml");

            XmlSerializer serializor = new XmlSerializer(typeof(restaurant_review));

            FileStream xs = new FileStream(xmlFilePath, FileMode.Open);

            restaurantList = (restaurant_review)serializor.Deserialize(xs);

            int count = 0;

            foreach (restaurant_reviewRestaurant restaurant in restaurantList.restaurant)
            {
                count++;
                restaurantOverviewViewModels.Add(new RestaurantOverviewViewModel()
                {
                    Id            = count,
                    Name          = restaurant.name,
                    FoodType      = restaurant.type,
                    Rating        = decimal.Parse(restaurant.reviews.review.rating.Value.ToString()),
                    Cost          = restaurant.pricecredit.Value,
                    City          = restaurant.address.city,
                    ProvinceState = restaurant.address.ProvinceState.ToString()
                });
            }
            xs.Close();
            return(View(restaurantOverviewViewModels));
        }
Beispiel #3
0
        public List <RestaurantInfo> GetAllRestaurants()
        {
            List <RestaurantInfo> restaurantList = new List <RestaurantInfo>();
            restaurant_review     allRestaurants = GetRestaurantsFromXml();
            int count = 0;

            if (allRestaurants != null)
            {
                foreach (restaurant_reviewRestaurant rest in allRestaurants.restaurant)
                {
                    restaurantList.Add(new RestaurantInfo()
                    {
                        Name     = rest.name,
                        Id       = count,
                        Rating   = (int)double.Parse(rest.reviews.review.rating.Value),
                        FoodType = rest.type,
                        Cost     = rest.pricecredit.Value,
                        Summary  = rest.reviews.review.summary,
                        Location = new Address
                        {
                            Street     = rest.address.StreetAddress,
                            City       = rest.address.city,
                            PostalCode = rest.address.PostalZipCode,
                            Province   = rest.address.ProvinceState.ToString()
                        }
                    });
                    count++;
                }
                return(restaurantList);
            }
            return(null);
        }
Beispiel #4
0
        public List <string> GetRestaurantNames()
        {
            List <string>     names          = new List <string>();
            restaurant_review allRestaurants = GetRestaurantsFromXml();

            if (allRestaurants != null)
            {
                foreach (restaurant_reviewRestaurant rest in allRestaurants.restaurant)
                {
                    names.Add(rest.name);
                }
            }
            return(names);
        }
Beispiel #5
0
        public restaurant_review GetRestaurantsFromXml()  //this method makes the xml readable as an object
        {
            restaurant_review listOfRestaurants = null;

            string xmlFile = HttpContext.Current.Server.MapPath("~/App_Data/restaurant_review.xml");
            string rs      = System.Reflection.Assembly.GetExecutingAssembly().Location;

            //opening the xml file and deserializing it (converting xml file into object)
            using (FileStream xs = new FileStream(xmlFile, FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(restaurant_review));
                listOfRestaurants = (restaurant_review)serializer.Deserialize(xs);
            }
            return(listOfRestaurants);
        }
        public IActionResult Edit(RestaurantEditViewModel rsVM)
        {
            restaurant_review restaurantList = null;
            string            xmlFilePath    = Path.GetFullPath("Data/restaurant_review .xml");

            XmlSerializer serializor = new XmlSerializer(typeof(restaurant_review));

            FileStream xs = new FileStream(xmlFilePath, FileMode.Open);

            restaurantList = (restaurant_review)serializor.Deserialize(xs);

            xs.Close();
            string xmlFile = Path.GetFullPath("Data/restaurant_review .xml");

            XmlWriterSettings settings = new XmlWriterSettings();

            settings.CheckCharacters     = true;
            settings.Encoding            = Encoding.Unicode;
            settings.Indent              = true;
            settings.NewLineOnAttributes = true;


            XmlWriter xw = XmlWriter.Create(xmlFile, settings);

            XmlSerializer serializer = new XmlSerializer(typeof(restaurant_review));
            int           i          = (int)rsVM.Id - 1;
            restaurant_reviewRestaurant restaurant = restaurantList.restaurant[i];

            restaurant.name                        = rsVM.Name;
            restaurant.address.city                = rsVM.City;
            restaurant.address.PostalZipCode       = rsVM.PostalZipCode;
            restaurant.address.ProvinceState       = rsVM.ProvinceState;
            restaurant.address.StreetAddress       = rsVM.StreetAddress;
            restaurant.reviews.review.summary      = rsVM.Summary;
            restaurant.reviews.review.rating.Value = rsVM.Rating.ToString();


            serializer.Serialize(xw, restaurantList);
            xw.Close();

            return(RedirectToAction("Index"));
        }
Beispiel #7
0
        public void SaveRestaurant(RestaurantInfo restInfo)
        {
            string            xmlFile        = HttpContext.Current.Server.MapPath("~/App_Data/restaurant_review.xml");
            restaurant_review allRestaurants = GetRestaurantsFromXml();
            //Get restaurant by id and save it on xml file
            restaurant_reviewRestaurant restToUpdate = allRestaurants.restaurant[restInfo.Id];

            if (restToUpdate != null)
            {
                restToUpdate.name = restInfo.Name;
                restToUpdate.reviews.review.summary      = restInfo.Summary;
                restToUpdate.reviews.review.rating.Value = restInfo.Rating.ToString();
                restToUpdate.address.StreetAddress       = restInfo.Location.Street;
                restToUpdate.address.city          = restInfo.Location.City;
                restToUpdate.address.ProvinceState = (ProvinceType)Enum.Parse(typeof(ProvinceType), restInfo.Location.Province, true);
                restToUpdate.address.PostalZipCode = restInfo.Location.PostalCode;
                using (FileStream xs = new FileStream(xmlFile, FileMode.Create))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(restaurant_review));
                    serializer.Serialize(xs, allRestaurants);
                }
            }
        }