Example #1
0
        /// <summary>
        /// Gets the location by adress or craetes it if it's not found asynchronously.
        /// </summary>
        /// <param name="address">Adress of the location to be retrieved or created.</param>
        /// <returns>
        /// The location.
        /// </returns>
        public async Task <ILocation> GetLocationAsync(string address)
        {
            log4net.GlobalContext.Properties["AppName"] = Assembly.GetExecutingAssembly().FullName;
            try
            {
                LocationEntity location    = null;
                LocationEntity NewLocation = new LocationEntity(Guid.NewGuid(), 0, 0, address);

                using (var connection = Connection.CreateConnection())
                    using (NpgsqlCommand commandSelect = new NpgsqlCommand(LocationQueryHelper.GetSelectLocationQueryString(), connection))
                    {
                        commandSelect.Parameters.AddWithValue(QueryConstants.ParAddress, NpgsqlDbType.Text, address);
                        await connection.OpenAsync();

                        DbDataReader dr = await commandSelect.ExecuteReaderAsync();

                        if (dr.Read())
                        {
                            location = new LocationEntity()
                            {
                                Id        = new Guid(dr[0].ToString()),
                                Address   = dr[1].ToString(),
                                Rating    = Convert.ToDouble(dr[2]),
                                RateCount = Convert.ToInt32(dr[3])
                            };
                        }
                        dr.Close();

                        if (location == null)
                        {
                            return(await CreateLocationAsync(Mapper.Map <ILocation>(NewLocation)));
                        }
                        return(await GetLocationByIdAsync(location.Id));
                    }
            }
            catch (Exception ex)
            {
                _log.Error(ex.StackTrace, ex);
                throw new Exception(ex.StackTrace);
            }
        }