GWI challenge #47
Conversation
dzacharakis
left a comment
There was a problem hiding this comment.
Hi @stevenoddity 👋
Thanks for your work on this assignment!
I added some comments on your PR with a few questions. I’d love to hear your feedback when you get a chance.
Thanks,
Dimitris
| defer func() { | ||
| if err := recover(); err != nil { |
There was a problem hiding this comment.
Is recovering from panics in Go generally considered a good practice?
There was a problem hiding this comment.
In general no, as it "hides" the issue.
|
|
||
| // GetUsers handles the HTTP request to retrieve a list of users. | ||
| func GetUsers(w http.ResponseWriter, r *http.Request) { | ||
| // This function currently does not implement any logic. |
There was a problem hiding this comment.
These were just placeholders for enhancements that I didn't implement.
| func main() { | ||
| router := routes.RegisterRoutes() | ||
| fmt.Println("🚀 Server running on http://localhost:8080") | ||
| log.Fatal(http.ListenAndServe(":8080", middleware.RecoverMiddleware(router))) |
There was a problem hiding this comment.
Why did you choose log.Fatal here? Would log.Error have been more appropriate?
There was a problem hiding this comment.
if it fails, the program cannot recover, so that's why I chose it.
|
|
||
| // ValidateFavorite validates the given Favorite struct. | ||
| // It returns an error if the validation fails, otherwise it returns nil. | ||
| func ValidateFavorite(fav *favorite_model.Favorite) error { |
There was a problem hiding this comment.
Is this method being used anywhere?
There was a problem hiding this comment.
Nope, I didn't utilize it at the end.
| type Asset struct { | ||
| ID int `json:"id"` // Unique identifier for the asset | ||
| UserID int `json:"user_id"` // Identifier for the user who owns the asset | ||
| Description json.RawMessage `json:"description"` // Description of the asset |
There was a problem hiding this comment.
Using json.RawMessage for the Description field is a poor design choice. Why? Any better approaches?
There was a problem hiding this comment.
It hides the structure of description and it makes quering difficult with slow performance. A better approach would be to define a separate structure from the description.
| @@ -0,0 +1,11 @@ | |||
| package favorite_model | |||
|
|
|||
| import asset_model "gwi/models/asset" | |||
There was a problem hiding this comment.
Is an alternative name needed for this import declaration?
There was a problem hiding this comment.
I found out that import "gwi/models/asset" works as well.
| asset_model "gwi/models/asset" | ||
| favorite_model "gwi/models/favorite" |
There was a problem hiding this comment.
Don't you find your package structure over-granular? Could you point out a main principle it’s meant to support and how this granularity impacts it?
There was a problem hiding this comment.
My main goal is the separation of concerns: each package should be responsible of one thing and as much decoupled as possible.
This granularity adds too much noise and it's an overkill for such a small app, but I wanted to demonstrate a more generic approach.
| @@ -0,0 +1,10 @@ | |||
| package asset_model | |||
There was a problem hiding this comment.
What would you change if this had to grew to, let's say, 20 models?
There was a problem hiding this comment.
I would add a base struct.
stevenoddity
left a comment
There was a problem hiding this comment.
Hello Dimitris,
Thanks a lot for your time!
I answered your questions and looking forward for a follow-up.
| func main() { | ||
| router := routes.RegisterRoutes() | ||
| fmt.Println("🚀 Server running on http://localhost:8080") | ||
| log.Fatal(http.ListenAndServe(":8080", middleware.RecoverMiddleware(router))) |
There was a problem hiding this comment.
if it fails, the program cannot recover, so that's why I chose it.
| defer func() { | ||
| if err := recover(); err != nil { |
There was a problem hiding this comment.
In general no, as it "hides" the issue.
| @@ -0,0 +1,10 @@ | |||
| package asset_model | |||
There was a problem hiding this comment.
I would add a base struct.
| type Asset struct { | ||
| ID int `json:"id"` // Unique identifier for the asset | ||
| UserID int `json:"user_id"` // Identifier for the user who owns the asset | ||
| Description json.RawMessage `json:"description"` // Description of the asset |
There was a problem hiding this comment.
It hides the structure of description and it makes quering difficult with slow performance. A better approach would be to define a separate structure from the description.
| @@ -0,0 +1,11 @@ | |||
| package favorite_model | |||
|
|
|||
| import asset_model "gwi/models/asset" | |||
There was a problem hiding this comment.
I found out that import "gwi/models/asset" works as well.
| asset_model "gwi/models/asset" | ||
| favorite_model "gwi/models/favorite" |
There was a problem hiding this comment.
My main goal is the separation of concerns: each package should be responsible of one thing and as much decoupled as possible.
This granularity adds too much noise and it's an overkill for such a small app, but I wanted to demonstrate a more generic approach.
|
|
||
| // GetUsers handles the HTTP request to retrieve a list of users. | ||
| func GetUsers(w http.ResponseWriter, r *http.Request) { | ||
| // This function currently does not implement any logic. |
There was a problem hiding this comment.
These were just placeholders for enhancements that I didn't implement.
|
|
||
| // ValidateFavorite validates the given Favorite struct. | ||
| // It returns an error if the validation fails, otherwise it returns nil. | ||
| func ValidateFavorite(fav *favorite_model.Favorite) error { |
There was a problem hiding this comment.
Nope, I didn't utilize it at the end.
No description provided.