public async Task <IActionResult> CreateLocation([FromBody] PostedLocation location)
        {
            Int32 key;

            try
            {
                if (location == null || !ModelState.IsValid)
                {
                    return(BadRequest("Location body supplied is invalid!"));
                }

                if (string.IsNullOrWhiteSpace(location.Name) || string.IsNullOrWhiteSpace(location.Description))
                {
                    return(BadRequest("One or more required paramaters were empty."));
                }

                key = await _locationRepo.Add(location);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(BadRequest("Error while creating location!"));
            }

            return(Ok(key));
        }
        public async Task <Int32> AddLocation(PostedLocation location)
        {
            Int32 retval = -1;

            using (var c = new NpgsqlConnection(_connectionString))
            {
                c.Open();

                var cmd = new NpgsqlCommand
                {
                    Connection  = c,
                    CommandType = System.Data.CommandType.Text,
                    CommandText = "INSERT INTO public.locations(name, location_data, description) VALUES(@name, ST_SetSRID(ST_MakePoint(@latitude, @longitude), 4326), @description) RETURNING location_key;"
                };

                cmd.Parameters.AddWithValue("name", NpgsqlDbType.Varchar, location.Name);
                cmd.Parameters.AddWithValue("latitude", NpgsqlDbType.Double, location.Latitude);
                cmd.Parameters.AddWithValue("longitude", NpgsqlDbType.Double, location.Longitude);
                cmd.Parameters.AddWithValue("description", NpgsqlDbType.Text, location.Description);

                cmd.Prepare();

                retval = (Int32)await cmd.ExecuteScalarAsync();

                c.Close();
            }

            return(retval);
        }
Beispiel #3
0
 public async Task <Int32> Add(PostedLocation location)
 {
     return(await _dal.AddLocation(location));
 }