示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
 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()));
     }
 }
示例#4
0
 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()));
 }