示例#1
0
        public async Task <IActionResult> UpdateStop(int id)
        {
            string jsonString;

            using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                jsonString = await reader.ReadToEndAsync();
            }
            JObject stop;

            try
            {
                stop = JObject.Parse(jsonString);
            }
            catch (JsonReaderException) { return(BadRequest()); }

            foreach (var prop in stop)
            {
                if (!(prop.Key == "name" || prop.Key == "departureTime" || prop.Key == "arrivalTime" || prop.Key == "cityId"))
                {
                    return(BadRequest());
                }
            }

            JToken   value;
            DateTime DepartureTime = new DateTime();
            DateTime ArrivalTime   = new DateTime();

            if (stop.TryGetValue("departureTime", StringComparison.OrdinalIgnoreCase, out value) && (value.Type == JTokenType.Date))
            {
                DepartureTime = value.Value <DateTime>();
            }
            if (stop.TryGetValue("arrivalTime", StringComparison.OrdinalIgnoreCase, out value) && (value.Type == JTokenType.Date))
            {
                ArrivalTime = value.Value <DateTime>();
            }

            TimeSpan duration = ArrivalTime - DepartureTime;

            if ((int)duration.TotalSeconds < 0)
            {
                return(BadRequest());
            }

            return(Ok(_stops.UpdateStop(stop, id, (int)duration.TotalSeconds)));
        }