Skip to content

Commit

Permalink
Merge pull request #34 from IvonneBenitesRodriguez/f_preserved
Browse files Browse the repository at this point in the history
f-preserved-PullRequest
  • Loading branch information
gilberthappi authored Nov 30, 2023
2 parents 82f81d3 + 4eb02fc commit 0c9c99a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
50 changes: 50 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,61 @@ def list_authors
def save_data
save_games
save_authors
save_books
save_labels
end

def load_data
load_games
load_authors
load_books
load_labels
end

private

# methods to save and load data from json files to store data after closing the app for books and labels
def load_books
if File.exist?('data/book.json')
data = JSON.parse(File.read('data/book.json'))
@books = data.map { |book| Book.new(book['publish_date'], book['publisher'], book['cover_state']) }
else
[]
end
end

def save_books
File.open('data/book.json', 'w') do |file|
data = @books.map do |book|
{
'publish_date' => book.publish_date,
'publisher' => book.publisher,
'cover_state' => book.cover_state
}
end
file.write(JSON.generate(data))
end
end

def load_labels
if File.exist?('data/label.json')
data = JSON.parse(File.read('data/label.json'))
@labels = data.map { |label| Label.new(label['title'], label['color']) }
else
[]
end
end

def save_labels
File.open('data/label.json', 'w') do |file|
data = @labels.map do |label|
{
'title' => label.title,
'color' => label.color
}
end
file.write(JSON.generate(data))
end
end

def save_games
Expand Down
2 changes: 2 additions & 0 deletions data/book.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[{"publish_date":"2020-11-11","publisher":"Microverse","cover_state":"good"}]

1 change: 1 addition & 0 deletions data/label.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"title":"Gift","color":"Red"}]

0 comments on commit 0c9c99a

Please sign in to comment.