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

feat: introduce v4 #88

Merged
merged 10 commits into from
Feb 18, 2024
Merged

feat: introduce v4 #88

merged 10 commits into from
Feb 18, 2024

Conversation

bxcodec
Copy link
Owner

@bxcodec bxcodec commented Feb 17, 2024

Code Directory V4

*ps I'm just updating this to my recent project structure. It may be different from the others, but it should be easy to learn as the first start before learning more techniques and pattern

New Code structure :

.
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── app
│   └── main.go
├── article
│   ├── mocks
│   │   ├── ArticleRepository.go
│   │   └── AuthorRepository.go
│   ├── service.go
│   └── service_test.go
├── article.sql
├── clean-arch.png
├── compose.yaml
├── domain
│   ├── article.go
│   ├── author.go
│   └── errors.go
├── example.env
├── go.mod
├── go.sum
├── internal
│   ├── README.md
│   ├── repository
│   │   ├── helper.go
│   │   └── mysql
│   │       ├── article.go
│   │       ├── article_test.go
│   │       ├── author.go
│   │       └── author_test.go
│   ├── rest
│   │   ├── article.go
│   │   ├── article_test.go
│   │   ├── middleware
│   │   │   ├── cors.go
│   │   │   ├── cors_test.go
│   │   │   └── timeout.go
│   │   └── mocks
│   │       └── ArticleService.go
│   └── workers
│       └── README.md
└── misc
    └── make
        ├── help.Makefile
        └── tools.Makefile

Changelogs

1. Declare Interface to the consuming side

Move all interfaces from domain to the consuming side. E.g., the rest/article.go layer depends on the service so that it will consume the ArticleService interface. Instead of referring to the domain, the interface will be declared in the rest/article.go package. The same goes for repositories interfaces that are declared in article/service.go

// path: internal/rest/article.go

type ArticleService interface {
	Fetch(ctx context.Context, cursor string, num int64) ([]domain.Article, string, error)
	GetByID(ctx context.Context, id int64) (domain.Article, error)
	Update(ctx context.Context, ar *domain.Article) error
	GetByTitle(ctx context.Context, title string) (domain.Article, error)
	Store(context.Context, *domain.Article) error
	Delete(ctx context.Context, id int64) error
}

type ArticleHandler struct {
	Service ArticleService
}

const defaultNum = 10

func NewArticleHandler(e *echo.Echo, svc ArticleService) {
	handler := &ArticleHandler{
		Service: svc,
	}
	e.GET("/articles", handler.FetchArticle)
	e.POST("/articles", handler.Store)
	e.GET("/articles/:id", handler.GetByID)
	e.DELETE("/articles/:id", handler.Delete)
}

2. internal package

The internal package hides the service-specific detail implementation, e.g., database, rest, cache, etc. Moving this implementation to internal will hide the implementations if the project is imported from another project. But it still shows all the core logic (domain and service).

├── internal
│   ├── README.md
│   ├── repository
│   │   ├── helper.go
│   │   └── mysql
│   │       ├── article.go
│   │       ├── article_test.go
│   │       ├── author.go
│   │       └── author_test.go
│   ├── rest
│   │   ├── article.go
│   │   ├── article_test.go
│   │   ├── middleware
│   │   │   ├── cors.go
│   │   │   ├── cors_test.go
│   │   │   └── timeout.go
│   │   └── mocks
│   │       └── ArticleService.go
│   └── workers
│       └── README.md

3. Service-focused package

The article package will only contain all business logic related to the article domain <> in the previous version it contains all (service, repository, controller).
And this package now will called as service layer.

Previously

article
├── delivery
│   └── http
│       ├── article_handler.go
│       └── article_test.go
├── mocks
│   ├── ArticleRepository.go
│   └── ArticleUsecase.go
├── repository //Encapsulated Implementation of Repository Interface
│   ├── mysql_article.go
│   └── mysqlarticle_test.go
├── repository.go // Repository Interface
├── usecase //Encapsulated Implementation of Usecase Interface
│   ├── articleucase_test.go
│   └── artilce_ucase.go
└── usecase.go // Usecase Interface.

New Structure

├── article
│   ├── mocks
│   │   ├── ArticleRepository.go
│   │   └── AuthorRepository.go
│   ├── service.go
│   └── service_test.go

@bxcodec bxcodec marked this pull request as draft February 17, 2024 15:51
@bxcodec bxcodec mentioned this pull request Feb 17, 2024
@bxcodec bxcodec changed the title [wip]feat: introduce v4 feat: introduce v4 Feb 17, 2024
@bxcodec bxcodec marked this pull request as ready for review February 17, 2024 16:43
@bxcodec bxcodec merged commit beb599f into master Feb 18, 2024
@bxcodec bxcodec deleted the feat/introduce-v4 branch February 18, 2024 13:15
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.

1 participant