Skip to content
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

Alain #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Alain #8

wants to merge 4 commits into from

Conversation

wildeng
Copy link

@wildeng wildeng commented Mar 4, 2022

My first try at it :)

Alain Mauri added 4 commits March 2, 2022 16:00
  * Added return and borrow endpoints
  * Added custom errors for return and borrow
libraryController := new(controllers.LibraryController)

router := martini.Classic()
router.Map(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, i didn't think of handing the ctx/client to martini to allow DI in the endpoints

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I've seen that with martini you can map whatever and I said why not? In this case is more Rails :D

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite Spring as well (@Autowired and @service or @component)

github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab
github.com/joho/godotenv v1.4.0
google.golang.org/api v0.60.0
)

require (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite the require block. Did the IDE pull these in for you? I'd be interested to know what libs it thought it needed all these for

Copy link
Author

@wildeng wildeng Mar 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using neovim as IDE and I have this plugin set up:

" Go development
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something we need to keep an eye on then. It maybe that these are indirect dependencies that end up in the go.sum file when we build it (but GoLand handles it differently to neovim)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will keep an eye on it and maybe is just a question of config

@@ -0,0 +1,35 @@
package helpers

type BoorrowError struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: oo

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

return true
}

type IBorrowError interface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'm not a big fan of the I notation to show an interface (and I don't think its a pattern you'll see in the pendo-appengine codebase)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case what is the common usage?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BorrowErrorInterface and then BorrowErrorImplementation for the concrete version. Naming conventions is on my list of things to talk through with the core team soon and document so we can have a consistent standard

}

type IBorrowError interface {
error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of the anonymous field in the struct

func createQuery(filters url.Values) *datastore.Query {
if len(filters) > 0 {
if author := filters.Get("author"); author != "" {
return datastore.NewQuery("Book").Filter("Author=", author).Order("Id")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this change if you were to allow people to filter by author AND title?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the docs should be

datastore.NewQuery("Book").Filter("Author=", author).Filter("Title=", title).Order("Id")

@@ -1,8 +1,97 @@
package models

import (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wider point for discussion, but I wouldn't have these methods in the model book object. I would have them in a book service, with the model simply there to define the struct (maybe a Ruby thing as these methods would sit in the ActiveRecord model class?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wider point for discussion, but I wouldn't have these methods in the model book object. I would have them in a book service, with the model simply there to define the struct (maybe a Ruby thing as these methods would sit in the ActiveRecord model class?)

yes, it's a wider point of discussion this.
I can say that it's not a Ruby thing because the bare minimum ActiveRecord file is just the class definition itself and honestly I also had this kind of discussion while I was using Java. What's the right place for the business logic? Not in the controller imho, but where between the model and a service class? Here I applied what I've been using more in Java and was the model. Not sure which one is best practice in Go, but it would be nice to learn

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, interesting because if were doing this in Java, i would have had a data access layer that would do the get/list etc. So it would be controller -> service -> data access. So the service is business logic, the data access handles the actual getting/updating. With the model on the side being referenced by all the above. Something to nail down in the coming weeks

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where a common style guide would be useful

if err != nil {
return book, err
}
if book.Borrowed == true {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if book.Borrowed == true is the same as if book.Borrowed

if err != nil {
return book, err
}
if book.Borrowed == false {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above by if !book.Borrowed


book, err := models.BorrowBook(client, ctx, id)

if err, isBorrow := err.(helpers.IBorrowError); isBorrow && err.IsBorrowed() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see what you are trying to do here. but as you are already throwing a quite specific error, with a appropriate error message, i don't think this block is very useful and can be covered by the generic err != nil below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my bad, I wanted to have a specific error for BadRequest and then do

if err != nil {
		writeError(err, http. StatusInternalServerError, w)
		return
	}

but I didn't change the latter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants