-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Leena #11
Open
LeenaBhegade
wants to merge
1
commit into
master
Choose a base branch
from
leena-acme-books
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Leena #11
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,127 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strconv" | ||
|
||
"cloud.google.com/go/datastore" | ||
"github.com/go-martini/martini" | ||
"google.golang.org/api/iterator" | ||
|
||
"acme-books/models" | ||
"acme-books/utils" | ||
) | ||
|
||
type LibraryController struct{} | ||
|
||
func (lc LibraryController) GetByKey(params martini.Params, w http.ResponseWriter) { | ||
ctx := context.Background() | ||
client, _ := datastore.NewClient(ctx, "acme-books") | ||
func (lc LibraryController) GetByKey(params martini.Params, w http.ResponseWriter, bookInt models.BookInterface) { | ||
id, err := strconv.Atoi(params["id"]) | ||
|
||
defer client.Close() | ||
if err != nil { | ||
utils.HandleError(err, w, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
id, err := strconv.Atoi(params["id"]) | ||
book, err := bookInt.GetByKey(id) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
if err == datastore.ErrNoSuchEntity { | ||
utils.HandleError(err, w, http.StatusNotFound) | ||
} else { | ||
utils.HandleError(err, w, http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
|
||
var book models.Book | ||
key := datastore.IDKey("Book", int64(id), nil) | ||
utils.WriteJsonResp(w, book) | ||
} | ||
|
||
err = client.Get(ctx, key, &book) | ||
func (lc LibraryController) ListAll(r *http.Request, w http.ResponseWriter, bookInt models.BookInterface) { | ||
title := r.URL.Query().Get("title"); | ||
|
||
books, err := bookInt.Query(title) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
utils.HandleError(err, w, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
jsonStr, err := json.MarshalIndent(book, "", " ") | ||
utils.WriteJsonResp(w, books) | ||
} | ||
|
||
func (lc LibraryController) Borrow(params martini.Params, w http.ResponseWriter, bookInt models.BookInterface) { | ||
id, err := strconv.Atoi(params["id"]) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
utils.HandleError(err, w, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write(jsonStr) | ||
getBookAndUpdateStatus(id, "borrow", w, bookInt) | ||
} | ||
|
||
func (lc LibraryController) ListAll(r *http.Request, w http.ResponseWriter) { | ||
ctx := context.Background() | ||
client, _ := datastore.NewClient(ctx, "acme-books") | ||
func (lc LibraryController) Return(params martini.Params, w http.ResponseWriter, bookInt models.BookInterface) { | ||
id, err := strconv.Atoi(params["id"]) | ||
|
||
if err != nil { | ||
utils.HandleError(err, w, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
defer client.Close() | ||
getBookAndUpdateStatus(id, "return", w, bookInt) | ||
} | ||
|
||
var output []models.Book | ||
func getBookAndUpdateStatus(id int, status string, w http.ResponseWriter, bookInt models.BookInterface) { | ||
book, err := bookInt.GetByKey(id) | ||
|
||
it := client.Run(ctx, datastore.NewQuery("Book")) | ||
for { | ||
var b models.Book | ||
_, err := it.Next(&b) | ||
if err == iterator.Done { | ||
fmt.Println(err) | ||
break | ||
if err != nil { | ||
if err == datastore.ErrNoSuchEntity { | ||
utils.HandleError(err, w, http.StatusBadRequest) | ||
} else { | ||
utils.HandleError(err, w, http.StatusInternalServerError) | ||
} | ||
output = append(output, b) | ||
return | ||
} | ||
|
||
jsonStr, err := json.MarshalIndent(output, "", " ") | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
if status == "borrow" && book.Borrowed { | ||
err := errors.New("Already Borrowed") | ||
utils.HandleError(err, w, http.StatusBadRequest) | ||
return | ||
} else if status == "return" && !book.Borrowed { | ||
err := errors.New("Not Borrowed") | ||
utils.HandleError(err, w, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write(jsonStr) | ||
book.Borrowed = !book.Borrowed | ||
|
||
_, error := bookInt.Put(book, false) | ||
|
||
if error != nil { | ||
utils.HandleError(err, w, http.StatusInternalServerError) | ||
} | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
} | ||
|
||
func (lc LibraryController) Create(r *http.Request, w http.ResponseWriter, bookInt models.BookInterface) { | ||
if body, err := ioutil.ReadAll(r.Body); err != nil { | ||
utils.HandleError(err, w, http.StatusInternalServerError) | ||
} else { | ||
var book models.Book | ||
|
||
if err = json.Unmarshal(body, &book); err != nil { | ||
utils.HandleError(err, w, http.StatusInternalServerError) | ||
} else { | ||
id, err := bookInt.Put(book, true) | ||
|
||
if err != nil { | ||
utils.HandleError(err, w, http.StatusInternalServerError) | ||
} | ||
book.Id = id | ||
utils.WriteJsonResp(w, book) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"cloud.google.com/go/datastore" | ||
|
||
"acme-books/models" | ||
"acme-books/server" | ||
|
||
|
@@ -36,25 +33,16 @@ func getEnvWithDefault(key, fallback string) string { | |
} | ||
|
||
func bootstrapBooks() { | ||
ctx := context.Background() | ||
client, _ := datastore.NewClient(ctx, "acme-books") | ||
|
||
defer client.Close() | ||
|
||
books := []models.Book{ | ||
{Id: 1, Author: "George Orwell", Title: "1984", Borrowed: false}, | ||
{Id: 2, Author: "George Orwell", Title: "Animal Farm", Borrowed: false}, | ||
{Id: 3, Author: "Robert Jordan", Title: "Eye of the world", Borrowed: false}, | ||
{Id: 4, Author: "Various", Title: "Collins Dictionary", Borrowed: false}, | ||
} | ||
|
||
var keys []*datastore.Key | ||
|
||
for _, book := range books { | ||
keys = append(keys, datastore.IDKey("Book", book.Id, nil)) | ||
} | ||
|
||
if _, err := client.PutMulti(ctx, keys, books); err != nil { | ||
|
||
bookInt := models.NewBookImplementation() | ||
if err := bookInt.PutMulti(books); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🥳 nice one |
||
fmt.Println(err) | ||
} | ||
bookInt.CloseClientConnection() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure i was meant to change this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same thing happened to me here probably @tomw-1 knows better what's happening