feat: refactoring
This commit is contained in:
@@ -9,20 +9,20 @@ import (
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
)
|
||||
|
||||
//MySQLRepo mysql repo
|
||||
type MySQLRepo struct {
|
||||
//mySQLRepo mysql repo
|
||||
type mySQLRepo struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
//NewMySQLRepoRepository create new repository
|
||||
func NewMySQLRepoRepository(db *sql.DB) *MySQLRepo {
|
||||
return &MySQLRepo{
|
||||
func NewMySQLRepoRepository(db *sql.DB) *mySQLRepo {
|
||||
return &mySQLRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
//Create a book
|
||||
func (r *MySQLRepo) Create(e *Book) (entity.ID, error) {
|
||||
func (r *mySQLRepo) Create(e *Book) (entity.ID, error) {
|
||||
stmt, err := r.db.Prepare(`
|
||||
insert into book (id, title, author, pages, quantity, created_at)
|
||||
values(?,?,?,?,?,?)`)
|
||||
@@ -48,7 +48,7 @@ func (r *MySQLRepo) Create(e *Book) (entity.ID, error) {
|
||||
}
|
||||
|
||||
//Get a book
|
||||
func (r *MySQLRepo) Get(id entity.ID) (*Book, error) {
|
||||
func (r *mySQLRepo) Get(id entity.ID) (*Book, error) {
|
||||
stmt, err := r.db.Prepare(`select id, title, author, pages, quantity, created_at from book where id = ?`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -65,7 +65,7 @@ func (r *MySQLRepo) Get(id entity.ID) (*Book, error) {
|
||||
}
|
||||
|
||||
//Update a book
|
||||
func (r *MySQLRepo) Update(e *Book) error {
|
||||
func (r *mySQLRepo) Update(e *Book) error {
|
||||
e.UpdatedAt = time.Now()
|
||||
_, err := r.db.Exec("update book set title = ?, author = ?, pages = ?, quantity = ?, updated_at = ? where id = ?", e.Title, e.Author, e.Pages, e.Quantity, e.UpdatedAt.Format("2006-01-02"), e.ID)
|
||||
if err != nil {
|
||||
@@ -75,7 +75,7 @@ func (r *MySQLRepo) Update(e *Book) error {
|
||||
}
|
||||
|
||||
//Search books
|
||||
func (r *MySQLRepo) Search(query string) ([]*Book, error) {
|
||||
func (r *mySQLRepo) Search(query string) ([]*Book, error) {
|
||||
stmt, err := r.db.Prepare(`select id, title, author, pages, quantity, created_at from book where title like ?`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -100,7 +100,7 @@ func (r *MySQLRepo) Search(query string) ([]*Book, error) {
|
||||
}
|
||||
|
||||
//List books
|
||||
func (r *MySQLRepo) List() ([]*Book, error) {
|
||||
func (r *mySQLRepo) List() ([]*Book, error) {
|
||||
stmt, err := r.db.Prepare(`select id, title, author, pages, quantity, created_at from book`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -125,7 +125,7 @@ func (r *MySQLRepo) List() ([]*Book, error) {
|
||||
}
|
||||
|
||||
//Delete a book
|
||||
func (r *MySQLRepo) Delete(id entity.ID) error {
|
||||
func (r *mySQLRepo) Delete(id entity.ID) error {
|
||||
_, err := r.db.Exec("delete from book where id = ?", id)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user