/
book_repository.go
84 lines (62 loc) 路 1.68 KB
/
book_repository.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
package repository
import (
"context"
"github.com/jmoiron/sqlx"
"github.com/techwithmat/bookstore-api/internal/domain"
"github.com/techwithmat/bookstore-api/pkg/utils/pagination"
)
type BooksRepo struct {
db *sqlx.DB
}
func NewBookRepository(db *sqlx.DB) domain.BookRepository {
return &BooksRepo{
db: db,
}
}
func (r *BooksRepo) GetAll(ctx context.Context, p *pagination.PaginationQuery) ([]domain.Books, error) {
var books []domain.Books
err := r.db.SelectContext(ctx, &books, getBooksQuery, p.Size, p.Page*p.Size)
if err != nil {
return nil, err
}
return books, nil
}
func (r *BooksRepo) GetByCategory(ctx context.Context, category string, p *pagination.PaginationQuery) ([]domain.Books, error) {
var books []domain.Books
err := r.db.SelectContext(ctx, &books, getBookByCategoryQuery, category, p.Size, p.Page*p.Size)
if err != nil {
return nil, err
}
return books, nil
}
func (r *BooksRepo) GetByID(ctx context.Context, id int64) (*domain.Book, error) {
var book domain.Book
err := r.db.GetContext(ctx, &book, getBookByIdQuery, id)
if err != nil {
return nil, err
}
return &book, nil
}
func (r *BooksRepo) InsertBook(ctx context.Context, book *domain.Book) (int64, error) {
var id int64
err := r.db.QueryRowContext(ctx, InsertBookQuery,
book.Title,
book.Subtitle,
book.AboutTheBook,
book.PageCount,
book.Price,
book.Image,
book.Language,
book.AuthorName,
book.AuthorAvatar,
book.CategoryId,
).Scan(&id)
return id, err
}
func (r *BooksRepo) DeleteBook(ctx context.Context, id int64) error {
_, err := r.db.ExecContext(ctx, DeleteBookQuery, id)
return err
}
func (r *BooksRepo) UpdateBook(ctx context.Context, id int64) error {
return nil
}