-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
408 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,45 @@ | ||
package book | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"context" | ||
|
||
t "github.com/sikozonpc/notebase/types" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
const ( | ||
DbName = "notebase" | ||
CollName = "books" | ||
) | ||
|
||
type Store struct { | ||
db *sql.DB | ||
db *mongo.Client | ||
} | ||
|
||
func NewStore(db *sql.DB) *Store { | ||
func NewStore(db *mongo.Client) *Store { | ||
return &Store{db: db} | ||
} | ||
|
||
func (s *Store) GetBookByISBN(ISBN string) (*t.Book, error) { | ||
rows, err := s.db.Query("SELECT * FROM books WHERE isbn = ?", ISBN) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
book := new(t.Book) | ||
for rows.Next() { | ||
book, err = scanRowsIntoBook(rows) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if book.ISBN == "" { | ||
return nil, fmt.Errorf("book not found") | ||
} | ||
|
||
return book, nil | ||
} | ||
func (s *Store) GetByISBN(ctx context.Context, isbn string) (*t.Book, error) { | ||
col := s.db.Database(DbName).Collection(CollName) | ||
|
||
func (s *Store) CreateBook(book t.Book) error { | ||
_, err := s.db.Exec("INSERT INTO books (isbn, title, authors) VALUES (?, ?, ?)", book.ISBN, book.Title, book.Authors) | ||
if err != nil { | ||
return err | ||
} | ||
oID, _ := primitive.ObjectIDFromHex(isbn) | ||
|
||
return nil | ||
var b t.Book | ||
err := col.FindOne(ctx, bson.M{ | ||
"isbn": oID, | ||
}).Decode(&b) | ||
|
||
return &b, err | ||
} | ||
|
||
func scanRowsIntoBook(rows *sql.Rows) (*t.Book, error) { | ||
b := new(t.Book) | ||
err := rows.Scan(&b.ISBN, &b.Title, &b.Authors, &b.CreatedAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
func (s *Store) Create(ctx context.Context, b *t.CreateBookRequest) (primitive.ObjectID, error) { | ||
col := s.db.Database(DbName).Collection(CollName) | ||
|
||
newBook, err := col.InsertOne(ctx, b) | ||
|
||
return b, nil | ||
id := newBook.InsertedID.(primitive.ObjectID) | ||
return id, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
steps: | ||
- name: "gcr.io/cloud-builders/docker" | ||
args: ["build", "-t", "gcr.io/$PROJECT_ID/${_SERVICE_NAME}", "${_PATH}"] | ||
- name: "gcr.io/cloud-builders/docker" | ||
args: ["push", "gcr.io/$PROJECT_ID/${_SERVICE_NAME}"] | ||
- name: "gcr.io/cloud-builders/gcloud" | ||
args: | ||
[ | ||
"run", | ||
"deploy", | ||
"notemap", | ||
"--image", | ||
"gcr.io/$PROJECT_ID/${_SERVICE_NAME}", | ||
"--region", | ||
"europe-west4", | ||
"--platform", | ||
"managed", | ||
"--allow-unauthenticated", | ||
] | ||
|
||
substitutions: | ||
_SERVICE_NAME: notemap | ||
_PATH: . | ||
|
||
images: | ||
- "gcr.io/$PROJECT_ID/${_SERVICE_NAME}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.