book_reading_tracker_gem 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/config/database_connection.rb +1 -1
- data/db/book_reading_tracker_gem.rake.rb +15 -11
- data/lib/book_reading_tracker_gem/clis/cli.rb +3 -4
- data/lib/book_reading_tracker_gem/models/author.rb +1 -1
- data/lib/book_reading_tracker_gem/services/book_service.rb +10 -3
- data/lib/book_reading_tracker_gem/utils/helpers.rb +1 -1
- data/lib/book_reading_tracker_gem/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd081c34660a6d2eca8b027c75ab74ed89efc7a56c30e6728b649e11a8230e18
|
4
|
+
data.tar.gz: 2f3af5996bd57c2585b30837edeb3acb519185bfe11e183dfa304ffc96690d6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f6e60e8236d2c76e1c7a6698f755779edd51b1345655d8e70af67204415edcdbaa9172a8ac5a23fce4e337e278c378a44388f7eb99009f0589fdbfb10cacd1f
|
7
|
+
data.tar.gz: 8b140358b0d48df22bf4f87f7cc43e8d037fe8009c6ac188a2d3346c450f028c1cef08fdc824441d16e999231c84e9d259e6f89240bc3442850cc9a2030b48f4
|
@@ -27,7 +27,7 @@ class DatabaseConnection
|
|
27
27
|
|
28
28
|
begin
|
29
29
|
ActiveRecord::Base.establish_connection(database_url)
|
30
|
-
ActiveRecord::Base.logger = Logger.new($stdout)
|
30
|
+
# ActiveRecord::Base.logger = Logger.new($stdout)
|
31
31
|
puts "Successfully connected to the database in #{database_mode} mode!"
|
32
32
|
rescue StandardError => e
|
33
33
|
puts "Failed to connect to the database: #{e.message}"
|
@@ -26,8 +26,12 @@ module BookReadingTrackerGem
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def migration_context
|
29
|
+
# bắt đầu từ thư mục chứa file database_tasks.rb, rồi đi từ đó tới ../db/migrate.
|
29
30
|
migrations_paths = File.expand_path('../db/migrate', __dir__)
|
30
|
-
|
31
|
+
|
32
|
+
@migration_context = ActiveRecord::MigrationContext.new(migrations_paths) if @migration_context.nil?
|
33
|
+
# puts migration_context.migrations.map(&:name)
|
34
|
+
@migration_context
|
31
35
|
end
|
32
36
|
|
33
37
|
def create
|
@@ -90,16 +94,6 @@ module BookReadingTrackerGem
|
|
90
94
|
puts "Lỗi khi migrate: #{e.message}"
|
91
95
|
end
|
92
96
|
|
93
|
-
def reset
|
94
|
-
drop
|
95
|
-
create
|
96
|
-
migrate
|
97
|
-
seed
|
98
|
-
puts 'Reset database thành công.'
|
99
|
-
rescue StandardError => e
|
100
|
-
puts "Lỗi khi reset database: #{e.message}"
|
101
|
-
end
|
102
|
-
|
103
97
|
def seed
|
104
98
|
connect
|
105
99
|
|
@@ -118,5 +112,15 @@ module BookReadingTrackerGem
|
|
118
112
|
rescue StandardError => e
|
119
113
|
puts "Lỗi khi seed dữ liệu: #{e.message}"
|
120
114
|
end
|
115
|
+
|
116
|
+
def reset
|
117
|
+
drop
|
118
|
+
create
|
119
|
+
migrate
|
120
|
+
seed
|
121
|
+
puts 'Reset database thành công.'
|
122
|
+
rescue StandardError => e
|
123
|
+
puts "Lỗi khi reset database: #{e.message}"
|
124
|
+
end
|
121
125
|
end
|
122
126
|
end
|
@@ -7,16 +7,15 @@ require_relative '../services/category_service'
|
|
7
7
|
|
8
8
|
module BookReadingTrackerGem
|
9
9
|
class CLI < Thor
|
10
|
-
|
11
|
-
desc 'add_book TITLE --author AUTHOR --pages PAGES [--description DESCRIPTION] [--isbn ISBN] [--published_year YEAR]',
|
10
|
+
desc 'add_book TITLE --author AUTHOR1 AUTHOR2 ... --pages PAGES [--description DESCRIPTION] [--isbn ISBN] [--published_year YEAR]',
|
12
11
|
'Thêm sách mới vào danh sách'
|
13
|
-
option :author, required: true
|
12
|
+
option :author, required: true, type: :array
|
14
13
|
option :pages, required: true, type: :numeric
|
15
14
|
option :description, required: false
|
16
15
|
option :isbn, required: false
|
17
16
|
option :published_year, required: false, type: :numeric
|
17
|
+
|
18
18
|
def add_book(title)
|
19
|
-
# ruby bin/book add_book "Ruby Programming" --author "David Heinemeier Hansson" --pages 300 --description "Learn Ruby with Rails" --isbn "978-0134596882" --published_year 2023
|
20
19
|
BookService.add_book(
|
21
20
|
title,
|
22
21
|
options[:author],
|
@@ -6,17 +6,24 @@ require_relative '../../../config/database_connection'
|
|
6
6
|
require_relative '../utils/helpers'
|
7
7
|
module BookReadingTrackerGem
|
8
8
|
class BookService
|
9
|
-
def self.add_book(title,
|
9
|
+
def self.add_book(title, authors, total_pages, description = nil, isbn = nil, published_year = nil)
|
10
10
|
DatabaseConnection.connect
|
11
|
+
|
11
12
|
book = Book.create!(
|
12
13
|
title: title,
|
13
14
|
description: description,
|
14
15
|
isbn: isbn,
|
15
16
|
published_year: published_year
|
16
17
|
)
|
17
|
-
|
18
|
+
|
19
|
+
authors.each do |author_name|
|
20
|
+
author = Author.find_or_create_by!(author_name: author_name)
|
21
|
+
book.authors << author unless book.authors.include?(author)
|
22
|
+
end
|
23
|
+
|
18
24
|
book.create_reading_progress!(total_pages: total_pages)
|
19
|
-
|
25
|
+
|
26
|
+
puts "Add book: #{book.title} successfully with #{authors.size} author(s)."
|
20
27
|
rescue StandardError => e
|
21
28
|
puts "Error adding book: #{e.message}"
|
22
29
|
ensure
|