Beispiel #1
0
        /// <summary>
        /// This function is called when the client navigates to *hostname*/CompanyListings/DisplayCompany/*info*
        /// </summary>
        /// <param name="id">The name of the company whos info is to be displayed</param>
        /// <returns>A view to be sent to the client</returns>
        public ActionResult DisplayCompany(string id)
        {
            if (Globals.isLoggedIn() == false)
            {
                return(RedirectToAction("Index", "Authentication"));
            }
            if ("".Equals(id))
            {
                return(View("Index"));
            }

            ServiceBusConnection connection = ConnectionManager.getConnectionObject(Globals.getUser());

            if (connection == null)
            {
                return(RedirectToAction("Index", "Authentication"));
            }

            ViewBag.CompanyName = id;

            GetCompanyInfoRequest infoRequest  = new GetCompanyInfoRequest(new CompanyInstance(id));
            ServiceBusResponse    infoResponse = connection.getCompanyInfo(infoRequest);



            String[] responseToArray = infoResponse.response.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            String[] locations       = new String[responseToArray.Length - 3];

            Array.Copy(responseToArray, 3, locations, 0, responseToArray.Length - 3);
            CompanyInstance value = new CompanyInstance(responseToArray[0], responseToArray[1], responseToArray[2], locations);

            ViewBag.CompanyInfo = value;
            ViewBag.username    = Globals.getUser();
            ViewBag.time        = DateTime.Now.ToString();


            //Harjee format the string into an array or something, then display it nicely on the view
            string reviews = GetReview(value.companyName);

            if (!reviews.Contains(":]"))
            {
                JObject json = JObject.Parse(reviews);

                JProperty allReviews = json.Property("reviews");

                string   totalReviews       = allReviews.Value.ToString();
                string[] unformattedResults = totalReviews.Split(',');

                for (int i = 4; i < unformattedResults.Length; i += 5)
                {
                    int position = unformattedResults[i].IndexOf('}');

                    if (position < 0)
                    {
                        position = unformattedResults[i].IndexOf(']');
                    }

                    if (position >= 0)
                    {
                        unformattedResults[i] = unformattedResults[i].Substring(0, position);
                    }
                }
                if (unformattedResults.Length > 2)
                {
                    for (int i = 0; i < unformattedResults.Length; i += 5)
                    {
                        int      reviewNumber = (i / 5) + 1;
                        string[] temp         = unformattedResults[i + 1].Split(':');
                        ViewBag.reviews += "Review #" + reviewNumber + ": ";
                        ViewBag.reviews += temp[1];
                        ViewBag.reviews += " <br/> ";

                        temp             = unformattedResults[i + 2].Split(':');
                        ViewBag.reviews += "Stars: ";
                        ViewBag.reviews += temp[1];
                        ViewBag.reviews += " <br/> ";

                        temp             = unformattedResults[i + 3].Split(':');
                        ViewBag.reviews += "Timestamp: ";
                        ViewBag.reviews += temp[1];
                        ViewBag.reviews += " <br/> ";

                        temp             = unformattedResults[i + 4].Split(':');
                        ViewBag.reviews += "Username: "******" <br/> <br/> ";
                    }

                    ViewBag.reviews += " <br/> ";
                }
            }

            else
            {
                ViewBag.reviews = " No reviews found <br/> ";
            }

            //Call Weather Service

            WeatherServiceRequest  info     = new WeatherServiceRequest(value.locations[0]);
            WeatherServiceResponse response = (WeatherServiceResponse)connection.getWeather(info);

            ViewBag.weatherText         = response.returnData.weatherText;
            ViewBag.temperature         = response.returnData.temperature;
            ViewBag.realFeelTemperature = response.returnData.realFeelTemperature;
            if (response.returnData.weatherIcon < 10)
            {
                ViewBag.weatherIcon = "https://apidev.accuweather.com/developers/Media/Default/WeatherIcons/0" + response.returnData.weatherIcon + "-s.png";
            }
            else
            {
                ViewBag.weatherIcon = "https://apidev.accuweather.com/developers/Media/Default/WeatherIcons/" + response.returnData.weatherIcon + "-s.png";
            }


            return(View("DisplayCompany"));
        }
        public async Task <HttpResponseMessage> ReadAndStoreWeatherDataToFile(string cityFilePath, string outputFolderPath)
        {
            using (StreamReader sr = new StreamReader(cityFilePath))
            {
                //Csv reader reads the stream
                CsvReader csvread = new CsvReader(sr);

                //csvread will fetch all record in one go to the IEnumerable object record
                IEnumerable <City> cities = csvread.GetRecords <City>();

                foreach (var city in cities) // Each record will be fetched and printed on the screen
                {
                    HttpResponseMessage response = await ReturnCityWeatherBasedOnCityId(city.CityId);

                    string json;
                    using (var content = response.Content)
                    {
                        json = await content.ReadAsStringAsync();
                    }
                    if (response.IsSuccessStatusCode)
                    {
                        WeatherServiceResponse weatherServiceResponse = JsonConvert.DeserializeObject <WeatherServiceResponse>(json);
                        var path = System.IO.Path.Combine(outputFolderPath, weatherServiceResponse.Date.ToString("ddMMyyyy") + "" + weatherServiceResponse.Name + ".txt");
                        using (StreamWriter writer = new StreamWriter(path, false))
                        {
                            writer.WriteLine("City Id: " + weatherServiceResponse.Id);
                            writer.WriteLine("City Name: " + weatherServiceResponse.Name);
                            writer.WriteLine("Coordinates: ");
                            writer.WriteLine("   Latitude: " + weatherServiceResponse.Coordinates.Latitude);
                            writer.WriteLine("   Longitude: " + weatherServiceResponse.Coordinates.Longitude);
                            writer.WriteLine("Weather:");
                            foreach (var items in weatherServiceResponse.Weather.Select((value, index) => new { value, index }))
                            {
                                writer.WriteLine("  Weather " + items.index + 1);
                                writer.WriteLine("    Id:" + items.value.Id);
                                writer.WriteLine("    Main:" + items.value.Main);
                                writer.WriteLine("    Description:" + items.value.Description);
                                writer.WriteLine("    Icon:" + items.value.Icon);
                            }
                            writer.WriteLine("Base:" + weatherServiceResponse.Base);
                            writer.WriteLine("Main:");
                            writer.WriteLine("  Temp:" + weatherServiceResponse.Main.Temp);
                            writer.WriteLine("  Pressure:" + weatherServiceResponse.Main.Pressure);
                            writer.WriteLine("  Humidity:" + weatherServiceResponse.Main.Humidity);
                            writer.WriteLine("  Minimum Temp:" + weatherServiceResponse.Main.MinTemp);
                            writer.WriteLine("  Maximum Temp:" + weatherServiceResponse.Main.MaxTemp);
                            writer.WriteLine("Visibility:" + weatherServiceResponse.Visibility);
                            writer.WriteLine("Wind:");
                            writer.WriteLine("  Speed:" + weatherServiceResponse.Wind.Speed);
                            writer.WriteLine("  Degree:" + weatherServiceResponse.Wind.Degree);
                            writer.WriteLine("Clouds:");
                            writer.WriteLine("  All:" + weatherServiceResponse.Clouds.All);
                            writer.WriteLine("Sys:");
                            writer.WriteLine("  Type:" + weatherServiceResponse.Sys.Type);
                            writer.WriteLine("  Id:" + weatherServiceResponse.Sys.Id);
                            writer.WriteLine("  Country:" + weatherServiceResponse.Sys.Country);
                            writer.WriteLine("  Sunrise:" + weatherServiceResponse.Sys.SunRise);
                            writer.WriteLine("  Sunset:" + weatherServiceResponse.Sys.SunSet);
                        }
                    }
                    else
                    {
                        throw new Exception("Error Response from Open Weather service");
                    }
                }
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }