diff --git a/app/controllers/link.cr b/app/controllers/link.cr index 947e392..1b53959 100644 --- a/app/controllers/link.cr +++ b/app/controllers/link.cr @@ -16,7 +16,7 @@ module App::Controllers::Link url = body["url"].to_s query = Database::Query.where(url: url, user_id: user.id.as(String)).limit(1) - existing_links = Database.all(Link, query) + existing_links = Database.all(Link, query, preload: [:clicks]) existing_link = existing_links.empty? ? nil : existing_links.first if existing_link response = {"data" => App::Serializers::Link.new(existing_link)} @@ -41,7 +41,9 @@ module App::Controllers::Link raise App::UnprocessableEntityException.new(env, map_changeset_errors(changeset.errors)) end + link.clicks = [] of App::Models::Click response = {"data" => App::Serializers::Link.new(link)} + response.to_json end end diff --git a/app/serializers/link.cr b/app/serializers/link.cr index 0fd05cc..60ec040 100644 --- a/app/serializers/link.cr +++ b/app/serializers/link.cr @@ -16,7 +16,7 @@ module App::Serializers builder.field("id", @link.id) builder.field("refer", @refer) builder.field("origin", @link.url) - builder.field("clicks", @link.clicks.try(&.map { |click| App::Serializers::Click.new(click) }) || [] of App::Serializers::Click) + builder.field("clicks", @link.clicks.map { |click| App::Serializers::Click.new(click) }) end end end