Files
simrsx-be/internal/use-case/_use-case-template/crud/README.md
T

60 lines
2.2 KiB
Markdown

# Use-Case
Standard pattern to be used to maintain the code scalability by providing
middleware-based flow. Each function can have its own middlewares for
pre- and post-processing.
According to the pattern above all of the case-functions runs only basic
rdbms operations, like getting data or writing data. And it's all based
on the data structure. The middlewares are used to for additional actions.
## How It's Done
The use-case is divided into at least 2 parts:
1. Case (`case.go`), where the `case` functions (the main functions) are stored
2. Tycovar (`tycovar.go`), where package scoped types, const, and variables
are stored
Optionally, there can be other parts, such as:
1. Helper (`helper.go`), where internal functions are stored
2. Middleware (`middleware.go`), where the pre- and post-processing functions
are stored
Then proceed the following steps (example):
1. Inside the `tycovar.go`, define the middleware types for each case
functions. Make sure to:
1. Include dto, table definition, and transaction for the parameters
since the middlewares are to utilize or modifies one ot the parameters.
2. Make all of the parameter pointer for easier handling.
3. Return `error` for blocking purpose when error occurs.
```go
type createMw func(input *e.Createdto, tx *gorm.DB) error
.
.
```
2. Inside the `tycovar.go`, create variables in form of array to make it
possible to have more than one middleware. There are 2 modes (pre- and
post-processing) for each case functions:
```go
var createPreMw []createMw
var createPostMw []createMw
.
.
```
3. Inside the `middleware.go`, create the necessary middlewares
```go
func createPreMwAddNamePrefix(input *e.Createdto, tx *gorm.DB) error {
input.Name = "Prefix_" + input.Name
return nil
}
```
4. Inside the `middleware.go`, append the middlewares during initialization.
The order of the does not really is important, except there are steps that
are dependent on each other.
```go
func init() {
createPreMw = append(createPreMw, createPreMwAddNamePrefix)
}
```
5. Loop through the middleware variables and call each middleware. Since there
are 2 modes (pre- and post-processing), the order of the calls is important.