public async Task <ActionResult <Station> > Post([FromBody] StationRequest request)
        {
            if (request == null)
            {
                return(BadRequest("Request Body can not be null"));
            }
            if (request.Height == null || request.Height.Value <= 0)
            {
                return(BadRequest("塔高必须设置并且大于0"));
            }
            if (await _context.Stations.AnyAsync(
                    st => st.Name.Equals(request.Name, StringComparison.OrdinalIgnoreCase)))
            {
                return(BadRequest($"{request.Name} 已经存在"));
            }

            Station station = new Station();

            station.Name     = request.Name;
            station.City     = request.City;
            station.Lat      = request.Lat;
            station.Lng      = request.Lng;
            station.Province = request.Province;
            station.Tag      = request.Tag;
            station.Height   = request.Height.Value;

            station.Disabled  = false;
            station.CreatedAt = DateTime.Now;

            _context.Stations.Add(station);
            await _context.SaveChangesAsync();

            return(station);
        }
        public async Task <ActionResult <Station> > Put(long id, [FromBody] StationRequest updated)
        {
            var station = await _context.Stations.FindAsync(id);

            if (station == null)
            {
                return(NotFound());
            }

            if (!string.IsNullOrWhiteSpace(updated.Name))
            {
                if (await _context.Stations.AnyAsync(st =>
                                                     st.Name.Equals(updated.Name, StringComparison.OrdinalIgnoreCase) &&
                                                     st.Id != id))
                {
                    return(BadRequest($"Name '{updated.Name}' already exist"));
                }
                station.Name = updated.Name;
            }

            if (updated.Tag != null)
            {
                station.Tag = updated.Tag;
            }
            if (updated.Lat.HasValue)
            {
                station.Lat = updated.Lat;
            }
            if (updated.Lng.HasValue)
            {
                station.Lng = updated.Lng;
            }
            if (updated.City != null)
            {
                station.City = updated.City;
            }
            if (updated.Province != null)
            {
                station.Province = updated.Province;
            }
            if (updated.Height != null)
            {
                station.Height = updated.Height.Value;
            }
            if (updated.Disabled.HasValue)
            {
                station.Disabled = updated.Disabled.Value;
            }


            await _context.SaveChangesAsync();

            return(station);
        }
 public override Task <SolarStation> GetStation(StationRequest request, ServerCallContext context)
 {
     try
     {
         SolarStation station = db.SolarStations.Where(x => x.Id == request.StationId).FirstOrDefault();
         return(Task.FromResult(station));
     }
     catch (NullReferenceException)
     {
         return(Task.FromResult(new SolarStation()));
     }
 }
 public override Task <MeteoStation> GetStationForecast(StationRequest request, ServerCallContext context)
 {
     try
     {
         var forecast = db.MeteoStations.Where(x => x.StationId == request.StationId).FirstOrDefault();
         return(Task.FromResult(forecast));
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     return(Task.FromResult(new MeteoStation()));
 }