summaryrefslogtreecommitdiff
path: root/app/services/snippets/update_statistics_service.rb
blob: 295cb963ccccb9581484756d7ef94a28cb0467dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

module Snippets
  class UpdateStatisticsService
    attr_reader :snippet

    def initialize(snippet)
      @snippet = snippet
    end

    def execute
      unless snippet.repository_exists?
        return ServiceResponse.error(message: 'Invalid snippet repository', http_status: 400)
      end

      snippet.repository.expire_statistics_caches
      statistics.refresh!

      ServiceResponse.success(message: 'Snippet statistics successfully updated.')
    end

    private

    def statistics
      @statistics ||= snippet.statistics || snippet.build_statistics
    end
  end
end