Skip to content

Commit 7705d4c

Browse files
committed
add initial api controller
1 parent 8a54678 commit 7705d4c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

app/models/user.rb

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def referrer
3333
has_many :content_change_events, dependent: :destroy
3434
has_many :user_content_type_activators, dependent: :destroy
3535

36+
has_many :api_keys, dependent: :destroy
37+
3638
def contributable_universes
3739
@user_contributable_universes ||= begin
3840
# todo email confirmation needs to happy for data safety / privacy (only verified emails)
@@ -174,6 +176,13 @@ def forum_username
174176
username
175177
end
176178

179+
def self.from_api_key(key)
180+
found_key = ApiKey.includes(:user).find_by(key: key)
181+
return nil unless found_key.present?
182+
183+
found_key.user
184+
end
185+
177186
private
178187

179188
# Attributes that are non-public, and should be blacklisted from any public
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Api::V1::ApiContentService < Service
2+
# e.g. content(api_key: 'test-key', content_type: 'characters')
3+
def self.content(api_key:, content_type:)
4+
user = User.from_api_key(api_key)
5+
6+
return "Error: Invalid API Key" if user.nil?
7+
return "Error: Invalid content type" if valid_content_type?(content_type)
8+
9+
# todo we need to serialize attributes instead of natural model columns
10+
user.send(content_type.downcase.pluralize).as_json
11+
end
12+
13+
# todo create
14+
def self.create(api_key:, content_type:, attributes_hash:)
15+
end
16+
17+
# todo update
18+
def self.update(api_key:, content_type:, content_id:, attributes_hash:)
19+
end
20+
21+
# todo delete
22+
def self.delete(api_key:, content_type:, content_id:)
23+
end
24+
25+
# todo list: permission to create
26+
# todo list: page turned on
27+
# todo link to anonymous account from app?
28+
29+
private
30+
31+
def self.valid_content_type?(content_type)
32+
Rails.application.config.content_types[:all].map(&:name).include?(content_type.titleize)
33+
end
34+
35+
end

0 commit comments

Comments
 (0)