pre-dev: initial commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of base-entity, results of the extraction of the attributes from the
|
||||
original entity.
|
||||
|
||||
NOTE:
|
||||
Make sure to use 'case-sensitive' option when searching
|
||||
|
||||
TODO:
|
||||
create several types according to the needs
|
||||
*/
|
||||
package base // adjust this
|
||||
|
||||
type Basic struct {
|
||||
Code string `json:"code" gorm:"uniqueIndex;not null;size:10"`
|
||||
Name string `json:"name" gorm:"not null;size:100"`
|
||||
}
|
||||
|
||||
type Default struct {
|
||||
Type_Code TypeCode `json:"type" gorm:"not null;size:10"`
|
||||
Status_Code StatusCode `json:"status" gorm:"not null;size:10"`
|
||||
}
|
||||
|
||||
type Extra struct {
|
||||
Description string `json:"description"`
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
A sample, part of the package that contains type, constants, and/or variables.
|
||||
*/
|
||||
package base // adjust this
|
||||
|
||||
type StatusCode string
|
||||
type TypeCode string
|
||||
|
||||
const (
|
||||
SCActive StatusCode = "active" // prefixed with SC that stands for StatusCode
|
||||
SCInactive StatusCode = "inactive"
|
||||
|
||||
TCPrimary TypeCode = "prim" // prefixed with TC that stands for TypeCode
|
||||
TCScondary TypeCode = "seco"
|
||||
TCTertiary TypeCode = "tert"
|
||||
)
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of struct to define an entity both in the application and database.
|
||||
Uses the base struct from the core package for the standard attributes.
|
||||
|
||||
The sampel is part of the the workaround sample on how to avoid the
|
||||
circle-depency. The child is used to define the relationship for the database.
|
||||
|
||||
TODO:
|
||||
- replace 'child' with the name of the package, ie: myentity
|
||||
- replace 'Child' with the name of the entity, ie: MyEntity
|
||||
*/
|
||||
package child
|
||||
|
||||
import (
|
||||
ecore "simrs-vx/internal/domain/base-entities/core"
|
||||
eb "simrs-vx/internal/domain/main-entities/child/base"
|
||||
ep "simrs-vx/internal/domain/main-entities/parent"
|
||||
)
|
||||
|
||||
type Child struct {
|
||||
ecore.Main
|
||||
eb.Basic
|
||||
Parent_Id int `json:"parent_id"`
|
||||
Pareng ep.Parent `json:"parent" gorm:"foreignKey:Parent_Id;references:Id"`
|
||||
eb.Default
|
||||
eb.Extra
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of a case-entity that utilized the base according to the needs.
|
||||
|
||||
NOTE:
|
||||
Make sure to use 'case-sensitive' option when searching
|
||||
|
||||
TODO:
|
||||
replace 'child' with the name of the package, ie: myentity
|
||||
replace 'Child' with the name of the entity, ie: MyEntity
|
||||
*/
|
||||
package child
|
||||
|
||||
import (
|
||||
eb "simrs-vx/internal/domain/main-entities/child/base"
|
||||
)
|
||||
|
||||
type Child struct {
|
||||
Id int `json:"id"` // in many cases the usage of
|
||||
eb.Basic
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of base-entity, results of the extraction of the attributes from the
|
||||
original entity.
|
||||
|
||||
NOTE:
|
||||
Make sure to use 'case-sensitive' option when searching
|
||||
|
||||
TODO:
|
||||
create several types according to the needs
|
||||
*/
|
||||
package base // adjust this
|
||||
|
||||
type Basic struct {
|
||||
Code string `json:"code" gorm:"uniqueIndex;not null;size:10"`
|
||||
Name string `json:"name" gorm:"not null;size:100"`
|
||||
}
|
||||
|
||||
type Default struct {
|
||||
Type_Code TypeCode `json:"type" gorm:"not null;size:10"`
|
||||
Status_Code StatusCode `json:"status" gorm:"not null;size:10"`
|
||||
}
|
||||
|
||||
type Extra struct {
|
||||
Description string `json:"description"`
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
A sample, part of the package that contains type, constants, and/or variables.
|
||||
*/
|
||||
package base // adjust this
|
||||
|
||||
type StatusCode string
|
||||
type TypeCode string
|
||||
|
||||
const (
|
||||
SCActive StatusCode = "active" // prefixed with SC that stands for StatusCode
|
||||
SCInactive StatusCode = "inactive"
|
||||
|
||||
TCPrimary TypeCode = "prim" // prefixed with TC that stands for TypeCode
|
||||
TCScondary TypeCode = "seco"
|
||||
TCTertiary TypeCode = "tert"
|
||||
)
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of struct to define an entity both in the application and database.
|
||||
Uses the base struct from the core package for the standard attributes.
|
||||
|
||||
The sampel shows a workaround on how to avoid the circle-depency which is a
|
||||
common problem when it involves the parent-child relationship. By default,
|
||||
the relationship is defined in the child entity, and when the parent entity
|
||||
needs to use the child as an attribute (array for example), the circle-depency
|
||||
happens.
|
||||
|
||||
To avoid this problem, the parent will have a case-entity like 'full' entity,
|
||||
which embeds the 'base' and the 'child' structs.
|
||||
|
||||
TODO:
|
||||
- replace 'parent' with the name of the package, ie: myentity
|
||||
- replace 'Parent' with the name of the entity, ie: MyEntity
|
||||
*/
|
||||
package parent
|
||||
|
||||
import (
|
||||
ecore "simrs-vx/internal/domain/base-entities/core"
|
||||
eb "simrs-vx/internal/domain/main-entities/parent/base"
|
||||
)
|
||||
|
||||
type Parent struct {
|
||||
ecore.Main
|
||||
eb.Basic
|
||||
eb.Default
|
||||
eb.Extra
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of a case-entity that utilized the base according to the needs. In this
|
||||
sample, it imports the child entity, and uses it as an attribute. And by
|
||||
separating this entity from the original entity, it avoids the circle dependency.
|
||||
|
||||
TODO:
|
||||
replace 'parent' with the name of the package, ie: patient
|
||||
replace 'Parent' with the name of the entity, ie: Patient
|
||||
*/
|
||||
package parent
|
||||
|
||||
import (
|
||||
ec "simrs-vx/internal/domain/main-entities/child"
|
||||
eb "simrs-vx/internal/domain/main-entities/parent/base"
|
||||
// ec "simrs-vx/internal/domain/main-entities/child/minimal" // can use other case
|
||||
)
|
||||
|
||||
type Parent struct {
|
||||
Id int `json:"id"`
|
||||
eb.Basic
|
||||
eb.Default
|
||||
Childs []ec.Child `json:"childs" gorm:"foreignKey:Parent_Id;references:Id"`
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of a case-entity that utilized the base according to the needs.
|
||||
|
||||
TODO:
|
||||
replace 'parent' with the name of the package, ie: patient
|
||||
replace 'Parent' with the name of the entity, ie: Patient
|
||||
*/
|
||||
package parent
|
||||
|
||||
import (
|
||||
eb "simrs-vx/internal/domain/main-entities/parent/base"
|
||||
)
|
||||
|
||||
type Parent struct {
|
||||
Id int `json:"id"`
|
||||
eb.Basic
|
||||
eb.Default
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of base-entity, results of the extraction of the attributes from the
|
||||
original entity.
|
||||
|
||||
NOTE:
|
||||
Make sure to use 'case-sensitive' option when searching
|
||||
|
||||
TODO:
|
||||
create several types according to the needs
|
||||
*/
|
||||
package base // adjust this
|
||||
|
||||
type Basic struct {
|
||||
Code string `json:"code" gorm:"uniqueIndex;not null;size:10"`
|
||||
Name string `json:"name" gorm:"not null;size:100"`
|
||||
}
|
||||
|
||||
type Default struct {
|
||||
Type_Code TypeCode `json:"type" gorm:"not null;size:10"`
|
||||
Status_Code StatusCode `json:"status" gorm:"not null;size:10"`
|
||||
}
|
||||
|
||||
type Extra struct {
|
||||
Description string `json:"description"`
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
A sample, part of the package that contains functions.
|
||||
*/
|
||||
package base
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (b *Basic) ExtractName() []string {
|
||||
return strings.Split(b.Name, " ")
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
A sample, part of the package that contains type, constants, and/or variables.
|
||||
*/
|
||||
package base // adjust this
|
||||
|
||||
type StatusCode string
|
||||
type TypeCode string
|
||||
|
||||
const (
|
||||
SCActive StatusCode = "active" // prefixed with SC that stands for StatusCode
|
||||
SCInactive StatusCode = "inactive"
|
||||
|
||||
TCPrimary TypeCode = "prim" // prefixed with TC that stands for TypeCode
|
||||
TCScondary TypeCode = "seco"
|
||||
TCTertiary TypeCode = "tert"
|
||||
)
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of struct to define an entity both in the application and database.
|
||||
Uses the base struct from the core package for the standard attributes.
|
||||
|
||||
The original attributes are extracted and grouped into several parts (structs),
|
||||
which later embeded. The extracted attributes are stored into the subdirectory
|
||||
'base'.
|
||||
|
||||
The extraction is done to cover several cases regarding the eficiency the of
|
||||
the data retreiving by using only needed attributes. Each case has its own
|
||||
directory with the same struct name with the main entity that embeds the
|
||||
needed attributes from the 'base'.
|
||||
|
||||
NOTES:
|
||||
Make sure to use 'case-sensitive' option when doing search-replace
|
||||
|
||||
TODO:
|
||||
- replace 'separated' with the name of the package, ie: myentity
|
||||
- replace 'Separated' with the name of the entity, ie: MyEntity
|
||||
*/
|
||||
package separated
|
||||
|
||||
import (
|
||||
ecore "simrs-vx/internal/domain/base-entities/core"
|
||||
eb "simrs-vx/internal/domain/main-entities/separated/base"
|
||||
)
|
||||
|
||||
type Separated struct {
|
||||
ecore.Main // adjust this according to the needs
|
||||
eb.Basic
|
||||
eb.Default
|
||||
eb.Extra
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of a case-entity that utilized the base according to the needs.
|
||||
|
||||
NOTE:
|
||||
Make sure to use 'case-sensitive' option when searching
|
||||
|
||||
TODO:
|
||||
replace 'separated' with the name of the package, ie: myentity
|
||||
replace 'Separated' with the name of the entity, ie: MyEntity
|
||||
*/
|
||||
package separated
|
||||
|
||||
import (
|
||||
eb "simrs-vx/internal/domain/main-entities/separated/base"
|
||||
)
|
||||
|
||||
type Separated struct {
|
||||
Id int `json:"id"`
|
||||
eb.Basic
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of a case-entity that utilized the base according to the needs.
|
||||
|
||||
NOTE:
|
||||
Make sure to use 'case-sensitive' option when searching
|
||||
|
||||
TODO:
|
||||
replace 'separated' with the name of the package, ie: myentity
|
||||
replace 'Separated' with the name of the entity, ie: MyEntity
|
||||
*/
|
||||
package separated
|
||||
|
||||
import (
|
||||
eb "simrs-vx/internal/domain/main-entities/separated/base"
|
||||
)
|
||||
|
||||
type Separated struct {
|
||||
Id int `json:"id"` // in many cases the usage of
|
||||
eb.Basic
|
||||
eb.Default
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package single
|
||||
|
||||
type Createdto struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type ReadListDto struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type ReadDetailDto struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Updatedto struct {
|
||||
Id uint `json:"id"`
|
||||
Createdto
|
||||
}
|
||||
|
||||
type Deletedto struct {
|
||||
Id uint `json:"id"`
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of struct to define an entity both in the application and database.
|
||||
Uses the base struct from the core package for the standard
|
||||
attributes.
|
||||
|
||||
NOTES:
|
||||
Make sure to use 'case-sensitive' option when doing search-replace
|
||||
|
||||
TODO:
|
||||
- replace 'single' with the name of the package, ie: 'patient'
|
||||
- replace 'Single' with the name of the entity, ie: 'Patient'
|
||||
*/
|
||||
package single
|
||||
|
||||
import (
|
||||
ecore "simrs-vx/internal/domain/base-entities/core"
|
||||
)
|
||||
|
||||
type Single struct {
|
||||
ecore.Main // adjust this according to the needs
|
||||
Code string `json:"code" gorm:"uniqueIndex;not null;size:10"`
|
||||
Name string `json:"name" gorm:"not null;size:100"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package actor
|
||||
|
||||
type Actor struct {
|
||||
CreatedBy_User_Id uint `json:"createdBy_user_id"`
|
||||
UpdatedBy_User_Id uint `json:"updatedBy_user_id"`
|
||||
DeletedBy_User_Id uint `json:"deletedBy_user_id"`
|
||||
}
|
||||
|
||||
type Owner struct {
|
||||
OwnedBy_User_Id uint `json:"ownedBy_user_id"`
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package core
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Base struct {
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
DeteledAt gorm.DeletedAt `json:"deletedAt,omitempty"`
|
||||
}
|
||||
|
||||
//
|
||||
type Main struct {
|
||||
Id uint `json:"id" gorm:"primaryKey"` // depends on the system architecture
|
||||
Base
|
||||
}
|
||||
|
||||
type TinyMain struct {
|
||||
Id uint8 `json:"id" gorm:"primaryKey"`
|
||||
Base
|
||||
}
|
||||
|
||||
type SmallMain struct {
|
||||
Id uint16 `json:"id" gorm:"primaryKey"`
|
||||
Base
|
||||
}
|
||||
|
||||
// Mainly for transaction
|
||||
type BigMain struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey"`
|
||||
Base
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of the use-case that has fully CRUD functionality. Uses object with CRUD
|
||||
functions to satisfy CRUD interface so it will be able to be generated into
|
||||
CRUD routing.
|
||||
|
||||
Functions other than CRUD can be writen outside the object.
|
||||
*/
|
||||
package crud
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type myBase struct{}
|
||||
|
||||
var O myBase
|
||||
|
||||
func (obj myBase) Create(w http.ResponseWriter, r *http.Request) {
|
||||
// MULAI MEETING E
|
||||
// JIKA SUATU SAAT MAU OUTPUT XML, HANDLER YG MELAKUKAN
|
||||
// BUKAN CASE
|
||||
// YES MIRIP HANDLER
|
||||
// MEETING E MULAI
|
||||
}
|
||||
|
||||
func (obj myBase) Read(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (obj myBase) Update(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func OtherFunc(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func AnotherFunc(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Sample of a use-case that has fully CRUD functionality. User Id and the other
|
||||
things related to credentials are passed within the dto.
|
||||
*/
|
||||
package invokeauth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SomeFunc(w http.ResponseWriter, r *http.Request) {
|
||||
// if dto.User_Id > 0 {
|
||||
// query = query.Where("User_Id = ?", dto.User_Id)
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package crud
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type myBase struct{}
|
||||
|
||||
var O myBase
|
||||
|
||||
func SomeFunc(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func Read(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package home
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Home(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
jsonText, _ := json.Marshal(map[string]string{"message": "Resource can not be found!!"})
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write(jsonText)
|
||||
return
|
||||
}
|
||||
|
||||
jsonText, _ := json.Marshal(map[string]string{"message": "Welcome to the API"})
|
||||
w.Write(jsonText)
|
||||
}
|
||||
|
||||
func TempResponse(w http.ResponseWriter, r *http.Request) {
|
||||
jsonText, _ := json.Marshal(map[string]string{"message": "Nice move by the programmer, now tell him that he forgot this part"})
|
||||
w.Write(jsonText)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
///// PKG
|
||||
handlerlogger "simrs-vx/pkg/middleware/handler-logger"
|
||||
|
||||
///// Internal
|
||||
"simrs-vx/internal/interface/main-handler/home"
|
||||
)
|
||||
|
||||
// One place route to relatively easier to manage, ESPECIALLY in tracking
|
||||
func SetRoutes() http.Handler {
|
||||
/////
|
||||
// a.RegisterExtCall(gs.Adjust)
|
||||
|
||||
r := http.NewServeMux()
|
||||
|
||||
/******************** Main ********************/
|
||||
r.HandleFunc("/", home.Home)
|
||||
|
||||
/////
|
||||
return handlerlogger.SetLog(r)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package modifier
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
e "simrs-vx/internal/domain/main-entities/single"
|
||||
)
|
||||
|
||||
// a sampel of modifying some input
|
||||
func ModifInput(input *e.Createdto, data *e.Single, tx *gorm.DB) error {
|
||||
input.Name = "Prefix_" + input.Name
|
||||
return nil
|
||||
}
|
||||
|
||||
// a sampel of utilizing transaction
|
||||
func CheckData(input *e.Createdto, data *e.Single, tx *gorm.DB) error {
|
||||
tx.Where("name = ?", input.Name)
|
||||
input.Name = "Prefix_" + input.Name
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
# 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.
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Any functions that are available to be used externally.
|
||||
*/
|
||||
package crud
|
||||
|
||||
import (
|
||||
dg "github.com/karincake/apem/db-gorm-pg"
|
||||
d "github.com/karincake/dodol"
|
||||
|
||||
e "simrs-vx/internal/domain/main-entities/single"
|
||||
)
|
||||
|
||||
const source = "crud"
|
||||
|
||||
func Create(input e.Createdto) (*d.Data, error) {
|
||||
result, err := CreateData(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &d.Data{
|
||||
Meta: d.II{
|
||||
"source": source,
|
||||
"type": "list",
|
||||
"status": "created",
|
||||
},
|
||||
Data: result,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ReadList(input e.ReadListDto) (*d.Data, error) {
|
||||
data := d.Data{}
|
||||
query := dg.I
|
||||
query.Find(&data)
|
||||
if err := query.Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &d.Data{
|
||||
Meta: d.II{},
|
||||
Data: data,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ReadDetail() {
|
||||
|
||||
}
|
||||
|
||||
func Update() {
|
||||
|
||||
}
|
||||
|
||||
func Delete() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
Any functions that are used internally by the use-case
|
||||
*/
|
||||
package crud
|
||||
|
||||
import (
|
||||
e "simrs-vx/internal/domain/main-entities/single"
|
||||
)
|
||||
|
||||
func setData(src e.Createdto, dst *e.Single) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package crud
|
||||
|
||||
import (
|
||||
e "simrs-vx/internal/domain/main-entities/single"
|
||||
|
||||
dg "github.com/karincake/apem/db-gorm-pg"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func CreateData(input e.Createdto) (*e.Single, error) {
|
||||
data := e.Single{}
|
||||
|
||||
err := dg.I.Transaction(func(tx *gorm.DB) error {
|
||||
for i := range createPreMw {
|
||||
if err := createPreMw[i](&input, &data, dg.I); err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := dg.I.Create(&data).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := range createPostMw {
|
||||
if err := createPostMw[i](&input, &data, dg.I); err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return &data, err
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package crud
|
||||
|
||||
import (
|
||||
pm "simrs-vx/internal/use-case/plugin/modifier"
|
||||
)
|
||||
|
||||
func init() {
|
||||
createPreMw = append(createPreMw, pm.ModifInput)
|
||||
createPreMw = append(createPreMw, pm.CheckData)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
DESCRIPTION:
|
||||
A sample, part of the package that contains type, constants, and/or variables.
|
||||
|
||||
In this sample it also provides type and variable regarding the needs of the
|
||||
middleware to separate from main use-case which has the basic CRUD
|
||||
functionality. The purpose of this is to make the code more maintainable.
|
||||
*/
|
||||
package crud
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
e "simrs-vx/internal/domain/main-entities/single"
|
||||
)
|
||||
|
||||
type createMw func(input *e.Createdto, data *e.Single, tx *gorm.DB) error
|
||||
type readListMw func(input *e.ReadListDto, data *e.Single, tx *gorm.DB) error
|
||||
type readDetailMw func(input *e.ReadDetailDto, data *e.Single, tx *gorm.DB) error
|
||||
type updateMw func(input *e.ReadDetailDto, data *e.Single, tx *gorm.DB) error
|
||||
type deleteMw func(input *e.ReadDetailDto, data *e.Single, tx *gorm.DB) error
|
||||
|
||||
var createPreMw []createMw // preprocess middleware
|
||||
var createPostMw []createMw // postprocess middleware
|
||||
var readListPreMw []readListMw // ..
|
||||
var readListPostMw []readListMw // ..
|
||||
var readDetailPreMw []readDetailMw
|
||||
var readDetailPostMw []readDetailMw
|
||||
var udpatePreMw []readDetailMw
|
||||
var udpatePostMw []readDetailMw
|
||||
var deletePreMw []readDetailMw
|
||||
var deletePostMw []readDetailMw
|
||||
Reference in New Issue
Block a user