示例#1
0
        static void Main()
        {
            var provider  = new WeatherSupplier();
            var observer1 = new WeatherMonitor("TP");
            var observer2 = new WeatherMonitor("H");

            provider.WeatherConditions(32.0, 0.05, 1.5);
            observer1.Subscribe(provider);
            provider.WeatherConditions(33.5, 0.04, 1.7);
            observer2.Subscribe(provider);
            provider.WeatherConditions(37.5, 0.07, 1.2);
        }
        public IActionResult AddLikes(string wname, string loc, string typ)
        {
            if (!_context.WeatherSuppliers.Any(x => x.Name == wname))
            {
                var w = new WeatherSupplier {
                    Name = wname
                };
                _context.WeatherSuppliers.Add(w);
                _context.Votes.Add(new Vote {
                    Likes = typ == "like" ? 1 : -1, Supplier = w, Location = loc
                });
            }
            else if (!_context.Votes.Any(x => x.Supplier.Name == wname))
            {
                var w = _context.WeatherSuppliers.First(x => x.Name == wname);
                _context.Votes.Add(new Vote {
                    Supplier = w, Likes = typ == "like" ? 1 : -1, Location = loc
                });
            }
            else
            {
                var vote = _context.Votes.First(x => x.Supplier.Name == wname);
                if (typ == "like")
                {
                    vote.Likes++;
                }
                else
                {
                    vote.Likes--;
                }
            }

            _context.SaveChanges();
            var sortedWeather = SortWeathers(allWeathers.Weathers);

            allWeathers.Weathers = sortedWeather;
            return(View("Like", allWeathers));
        }