Example #1
0
        /// <summary>
        /// Update rating of the location asynchronously.
        /// </summary>
        /// <param name="id">Identifier of the location to have it's rating updated</param>
        /// <param name="rating">Rating from the user input(1-5)</param>
        /// <param name="currentRating">Current rating of the location</param>
        /// <param name="rateCount">Current rate count of the location. will be increased by 1</param>
        /// <returns>
        /// The updated location.
        /// </returns>
        public async Task <ILocation> UpdateLocationRatingAsync(Guid id, double rating, double currenRating, int rateCount)
        {
            log4net.GlobalContext.Properties["AppName"] = Assembly.GetExecutingAssembly().FullName;
            try
            {
                LocationEntity location = new LocationEntity();
                using (var connection = Connection.CreateConnection())
                    using (NpgsqlCommand command = new NpgsqlCommand(LocationQueryHelper.GetUpdateLocationRatingQueryString(), connection))
                    {
                        await connection.OpenAsync();

                        double NewRating    = 0;
                        int    NewRateCount = rateCount + 1;

                        NewRating = (currenRating * rateCount + rating) / NewRateCount;

                        command.Parameters.AddWithValue(QueryConstants.ParId, NpgsqlDbType.Uuid, id);
                        command.Parameters.AddWithValue(QueryConstants.ParRateCount, NpgsqlDbType.Integer, NewRateCount);
                        command.Parameters.AddWithValue(QueryConstants.ParRating, NpgsqlDbType.Double, NewRating);

                        await command.ExecuteNonQueryAsync();

                        return(await GetLocationByIdAsync(id));
                    }
            }
            catch (Exception ex)
            {
                _log.Error(ex.StackTrace, ex);
                throw new Exception(ex.StackTrace);
            }
        }
Example #2
0
        /// <summary>
        /// Get Location by Id asynchronously.
        /// </summary>
        /// <param name="id">Identifier of the location to be retrieved.</param>
        /// <returns>
        /// The location.
        /// </returns>
        public async Task <ILocation> GetLocationByIdAsync(Guid id)
        {
            log4net.GlobalContext.Properties["AppName"] = Assembly.GetExecutingAssembly().FullName;
            try
            {
                LocationEntity location = new LocationEntity();
                using (var connection = Connection.CreateConnection())
                    using (NpgsqlCommand command = new NpgsqlCommand(LocationQueryHelper.GetSelectLocationByIdQueryString(), connection))
                    {
                        command.Parameters.AddWithValue(QueryConstants.ParId, NpgsqlDbType.Uuid, id);
                        await connection.OpenAsync();

                        DbDataReader dr = await command.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])
                            };
                        }
                    }
                return(Mapper.Map <ILocation>(location));
            }
            catch (Exception ex)
            {
                _log.Error(ex.StackTrace, ex);
                throw new Exception(ex.StackTrace);
            }
        }
Example #3
0
        /// <summary>
        /// Creates Location asynchronously.
        /// </summary>
        /// <param name="location">Location that will be created.</param>
        /// <returns>
        /// The created location.
        /// </returns>
        public async Task <ILocation> CreateLocationAsync(ILocation location)
        {
            log4net.GlobalContext.Properties["AppName"] = Assembly.GetExecutingAssembly().FullName;
            try
            {
                using (var connection = Connection.CreateConnection())
                    using (NpgsqlCommand command = new NpgsqlCommand(LocationQueryHelper.GetInsertCreateLocationQueryString(), connection))
                    {
                        await connection.OpenAsync();

                        command.Parameters.AddWithValue(QueryConstants.ParId, NpgsqlDbType.Uuid, location.Id);
                        command.Parameters.AddWithValue(QueryConstants.ParAddress, NpgsqlDbType.Text, location.Address);
                        command.Parameters.AddWithValue(QueryConstants.ParRating, NpgsqlDbType.Double, location.Rating);
                        command.Parameters.AddWithValue(QueryConstants.ParRateCount, NpgsqlDbType.Double, location.RateCount);

                        await command.ExecuteNonQueryAsync();

                        return(await GetLocationByIdAsync(location.Id));
                    }
            }
            catch (Exception ex)
            {
                _log.Error(ex.StackTrace, ex);
                throw new Exception(ex.StackTrace);
            }
        }