-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.go
154 lines (110 loc) · 3.52 KB
/
book.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package services
import (
"database/sql"
"fmt"
"time"
)
type Book struct {
ID int
Title string
Author string
Genre string
Pages int
PublicationYear int
}
type BookService struct {
database *sql.DB
}
func NewBookService(database *sql.DB) *BookService {
return &BookService{database: database}
}
func (Service *BookService) CreateBook(book *Book) error {
query := "Insert into books (title, author, genre, pages, publicationyear) values (?, ?, ?, ?, ?)"
result, error := Service.database.Exec(query, book.Title, book.Author, book.Genre, book.Pages, book.PublicationYear)
if error != nil {
return error
}
lastInsertID, error := result.LastInsertId()
if error != nil {
return error
}
book.ID = int(lastInsertID)
return nil
}
func (Service *BookService) GetBooks() ([]Book, error) {
query := "Select id, title, author, genre, pages, publicationyear from books"
rows, error := Service.database.Query(query)
if error != nil {
return nil, error
}
var books []Book
for rows.Next() {
var book Book
error := rows.Scan(&book.ID, &book.Title, &book.Author, &book.Genre, &book.Pages, &book.PublicationYear)
if error != nil {
return nil, error
}
books = append(books, book)
}
return books, nil
}
func (Service *BookService) GetBookByID(ID int) (*Book, error) {
query := "Select id, title, author, genre, pages, publicationyear from books where id = ?"
row := Service.database.QueryRow(query, ID)
var book Book
error := row.Scan(&book.ID, &book.Title, &book.Author, &book.Genre, &book.Pages, &book.PublicationYear)
if error != nil {
return nil, error
}
return &book, nil
}
func (Service *BookService) UpdateBook(book *Book) error {
query := "Update books set title = ?, author = ?, genre = ?, pages = ?, publicationyear = ? where id = ?"
_, error := Service.database.Exec(query, book.Title, book.Author, book.Genre, book.Pages, book.PublicationYear, book.ID)
return error
}
func (Service *BookService) DeleteBook(ID int) error {
query := "Delete from books where id = ?"
_, error := Service.database.Exec(query, ID)
return error
}
func (Service *BookService) SearchBooksByName(nameBook string) ([]Book, error) {
query := "Select id, title, author, genre, pages, publicationyear from books where title like ?"
rows, error := Service.database.Query(query, "%"+nameBook+"%")
if error != nil {
return nil, error
}
defer rows.Close()
var books []Book
for rows.Next() {
var book Book
error := rows.Scan(&book.ID, &book.Title, &book.Author, &book.Genre, &book.Pages, &book.PublicationYear)
if error != nil {
return nil, error
}
books = append(books, book)
}
return books, nil
}
func (Service *BookService) SimulateReading(BookID int, duration time.Duration, results chan<- string) {
book, error := Service.GetBookByID(BookID)
if error != nil || book == nil {
results <- fmt.Sprintf("Book %d not found", BookID)
}
time.Sleep(duration)
results <- fmt.Sprintf("Book %s readed", book.Title)
}
func (Service *BookService) SimulateMultipleReading(BookIDs []int, duration time.Duration) []string {
results := make(chan string, len(BookIDs))
for _, ID := range BookIDs {
go func(BookID int) {
Service.SimulateReading(BookID, duration, results)
}(ID)
}
var responses []string
for range BookIDs {
responses = append(responses, <-results)
}
close(results)
return responses
}