Skip to content

Commit

Permalink
yegor256#1206 extra checks
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 30, 2024
1 parent ee1f653 commit 00cae0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
15 changes: 11 additions & 4 deletions objects/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
# SOFTWARE.

require_relative 'nb'
require_relative 'bout'
require_relative 'humans'
require_relative 'flags'

# Message of a user (reader).
# Author:: Yegor Bugayenko (yegor256@gmail.com)
Expand Down Expand Up @@ -53,38 +56,42 @@ def mine?
end

def exists?
!@pgsql.exec('SELECT * FROM message WHERE id = $1', [@id]).empty?
!@pgsql.exec('SELECT author FROM message WHERE id = $1', [@id]).empty?
end

def text
raise Nb::Urror, "#{@human} can't read message ##{@id}" unless mine?
@pgsql.exec('SELECT text FROM message WHERE id = $1', [@id])[0]['text']
end

def created
raise Nb::Urror, "#{@human} can't read message ##{@id}" unless mine?
time = @pgsql.exec('SELECT created FROM message WHERE id = $1', [@id])[0]['created']
Time.parse(time)
end

def author
raise Nb::Urror, "#{@human} can't read message ##{@id}" unless mine?
author = @pgsql.exec('SELECT author FROM message WHERE id = $1', [@id])[0]['author']
require_relative 'humans'
Nb::Humans.new(@pgsql).take(author)
end

def bout
raise Nb::Urror, "#{@human} can't read message ##{@id}" unless mine?
bout = @pgsql.exec('SELECT bout FROM message WHERE id = $1', [@id])[0]['bout'].to_i
require_relative 'bout'
Nb::Bout.new(@pgsql, @human, bout)
end

def flags
require_relative 'flags'
raise Nb::Urror, "#{@human} can't read message ##{@id}" unless mine?
Nb::Flags.new(@pgsql, @human, self)
end

def to_h
raise Nb::Urror, "#{@human} can't serialize message ##{@id}" unless mine?
{
id: @id,
bout: bout.id,
text: text,
created: created,
author: author.identity,
Expand Down
1 change: 1 addition & 0 deletions test/test_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_message
get("/message/#{msg.id}")
json = JSON.parse(last_response.body)
assert_equal(msg.id, json['id'])
assert_equal(bout.id, json['bout'])
assert(Time.parse(json['created']) < Time.now)
assert_equal(human.identity, json['author'])
assert(json['text'].include?('товарищ'))
Expand Down

0 comments on commit 00cae0f

Please sign in to comment.