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);
            }
        }