module Api module V1 class ReportsController < BaseController def monthly rows = current_user.entries.group(Arel.sql("DATE_TRUNC('month', date)"), :praktikums_typ, :entry_art) .order(Arel.sql("DATE_TRUNC('month', date) DESC")) .pluck(Arel.sql("DATE_TRUNC('month', date)"), :praktikums_typ, :entry_art, Arel.sql("SUM(COALESCE(hours, 0) * 60 + COALESCE(minutes, 0))")) render json: rows.map { |month, typ, art, minutes| { month: month.to_date, typ:, art:, total_minutes: minutes.to_i } } end end end end