initial commit for bookish-server and local environment

This commit is contained in:
David Thompson
2020-05-26 00:30:20 -04:00
parent d634e9d00f
commit 2bc790c87b
48 changed files with 15860 additions and 1 deletions
+66
View File
@@ -0,0 +1,66 @@
package mysql
import (
"database/sql"
"errors"
bookish "github.com/davidlick/bookish/bookish-server"
"github.com/go-sql-driver/mysql"
)
// ListAll returns all renter records.
func (s *store) ListAll() (rr []bookish.Renter, err error) {
q := `
SELECT
*
FROM
renters
`
err = s.Select(&rr, q)
if err != nil && errors.Is(sql.ErrNoRows, err) {
return nil, ErrNotFound
}
return
}
// FetchDetails queries the database for a specific renter.
func (s *store) FetchDetails(id int) (r bookish.Renter, err error) {
q := `
SELECT
*
FROM
renters
WHERE
id = ?
`
err = s.Get(&r, q, id)
if err != nil && errors.Is(sql.ErrNoRows, err) {
return bookish.Renter{}, ErrNotFound
}
return
}
// New inserts a record for a new renter. If that renter already exists it returns an ErrAlreadyExists.
func (s *store) New(name string, address string, email string, phoneNumber string) (id int, err error) {
q := `
INSERT INTO
renters (name, address, email, phone_number)
VALUES
(?, ?, ?, ?)
`
result, err := s.Exec(q, name, address, email, phoneNumber)
if err != nil {
if _, ok := err.(*mysql.MySQLError); ok {
return 0, ErrAlreadyExists
}
}
lastId, err := result.LastInsertId()
if err != nil {
return id, err
}
return int(lastId), nil
}