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

Where to define the interfaces please? #3

Open
suntong opened this issue Aug 25, 2023 · 3 comments
Open

Where to define the interfaces please? #3

suntong opened this issue Aug 25, 2023 · 3 comments

Comments

@suntong
Copy link

suntong commented Aug 25, 2023

go-jwt-auth/README.md

Lines 60 to 73 in f52fb4a

6. Define your interfaces inside the `repository.go`,`handlers.go` and `application.go` file.
```go
package ports
import "github.com/your_user/your_project/internal/post/domain/model"
type PostRepository interface {
Create(post *model.Post) error
FindAll() ([]*model.Post, error)
FindByID(id string) (*model.Post, error)
Update(post *model.Post) error
Delete(id string) error
}
```

Define your interfaces inside the repository.go,handlers.go and application.go file.

Hi, I don't quite get where exactly the above interfaces code should be put -- it should be put in only one of the above three files, not all of them right? If so, which one then?

@suntong
Copy link
Author

suntong commented Aug 25, 2023

Oh, it seems that the PostRepository should go into the repository.go, while handlers.go and application.go will have their own interfaces too, right? Maybe the interface code for handlers.go and application.go are missing from the README?

UPDATE: the handlers.go file seems to be:

package ports

import (
	"github.com/gofiber/fiber/v2"
)

type PostHandler interface {
	CreatePost(ctx *fiber.Ctx) error
}

and I have not been able to figured out what to put into application.go yet.

BTW, it would be really great if the result of the 14 steps in README can be published into a branch. Thanks.

@solrac97gr
Copy link
Owner

Hi, @suntong since the interfaces are core part of the package (define the behavior of how the parts of the app should interact) they are inside of the internal/user/domain/ports, in recent projects I start to define the handlers interface outside of the domain since not always the app will communicate throw HTTP it can be also console app, etc.

In the case you wanna add a new context for example blogs the interface most be in internal/blogs/domain/ports I like to be very explicit creating a package specific for the interfaces, in other projects I also notice people define the interfaces only inside of domain, its also ok but when projects are huge models and interfaces start to be more difficult to find if they are not separeted.

@suntong
Copy link
Author

suntong commented Aug 26, 2023

I haven't been able to make it work yet,

Oh, it seems that the PostRepository should go into the repository.go

I put the above quoted code there, and here are what I've changed to pkg/server/server.go:

--- a/blog/pkg/server/server.go
+++ b/blog/pkg/server/server.go
@@ -9,6 +9,7 @@ import (
        "github.com/gofiber/swagger"
        "github.com/golang-jwt/jwt/v4"
        userHdl "backend/internal/user/domain/ports"
+       postHdl "backend/internal/post/domain/ports"
        configurator "backend/pkg/config/domain/ports"
        mdl "backend/pkg/middleware/domain/ports"
 )
@@ -16,6 +17,7 @@ import (
 type Server struct {
        mdl          mdl.MiddlewareHandlers
        user         userHdl.UserHandlers
+       postHandler  postHdl.HTTPPostHandlers
        configurator configurator.ConfigApplication
 }
 
@@ -42,6 +44,7 @@ func (s *Server) Run(port string) error {
 
        v1Private := app.Group("/api/v1").Use(s.mdl.Authenticate())
        // Test Endpoint
+       v1Private.Post("/posts", s.postHandler.CreatePost)
        v1Private.Get("/secret", func(c *fiber.Ctx) error {
                user := c.Locals("user").(*jwt.Token)
                claims := user.Claims.(jwt.MapClaims)

and I'm getting

pkg/server/server.go:20:23: undefined: postHdl.HTTPPostHandlers

If I use PostHandlers there, then the error is:

pkg/server/server.go:20:23: undefined: postHdl.PostHandlers

For the s.postHandler.CreatePost in the README it was: h.postHandler.CreatePost yet I don't see any variable named h in the function. IE I was trying to fill the missing pieces/gaps myself, but I'm a bit lost now when putting the puzzles together.

BTW, it would be really great if the result of the 14 steps in README can be published into a branch.

Or would you put back the missing pieces and correct the mistakes in the README file please?

Thanks.

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

No branches or pull requests

2 participants