diff --git a/.idea/niki.iml b/.idea/niki.iml index 5e764c4..7ee078d 100644 --- a/.idea/niki.iml +++ b/.idea/niki.iml @@ -1,9 +1,4 @@ - + - - - - - \ No newline at end of file diff --git a/adapter/storage/action_piggy_bank.go b/adapter/storage/action_piggy_bank.go new file mode 100644 index 0000000..654b923 --- /dev/null +++ b/adapter/storage/action_piggy_bank.go @@ -0,0 +1,23 @@ +package storage + +import entitypiggybankrequest "git.gocasts.ir/ebhomengo/niki/entity/piggy_bank_request" + +func (d *DBMS) AddPiggyRequestBank([]entitypiggybankrequest.PiggyBankRequest) error { + return nil + +} +func (d *DBMS) EditPiggyRequestBank(PiggyRequestID uint, PiggyRequestBankInput entitypiggybankrequest.PiggyBankRequest) error { + return nil + +} +func (d *DBMS) DeletePiggyRequestBank(PiggyRequestID uint) error { + return nil + +} +func (d *DBMS) GetAllPiggyRequestBank() ([]entitypiggybankrequest.PiggyBankRequest, error) { + return []entitypiggybankrequest.PiggyBankRequest{}, nil + +} +func (d *DBMS) GetPiggyRequestBankByID(PiggyRequestBank uint) (entitypiggybankrequest.PiggyBankRequest, error) { + return entitypiggybankrequest.PiggyBankRequest{}, nil +} diff --git a/adapter/storage/action_piggy_bank_request.go b/adapter/storage/action_piggy_bank_request.go new file mode 100644 index 0000000..3305125 --- /dev/null +++ b/adapter/storage/action_piggy_bank_request.go @@ -0,0 +1,30 @@ +package storage + +import ( + storage "git.gocasts.ir/ebhomengo/niki/adapter/storage/models" + entitypiggybank "git.gocasts.ir/ebhomengo/niki/entity/piggy_bank" +) + +func (d *DBMS) AddPiggyBank([]entitypiggybank.PiggyBank) error { + return nil +} +func (d *DBMS) EditPiggyBank(piggyID uint, piggyBankInput entitypiggybank.PiggyBank) error { + return nil +} +func (d *DBMS) DeletePiggyBank(piggyID uint) error { + return nil +} +func (d *DBMS) GetAllPiggyBank() ([]entitypiggybank.PiggyBank, error) { + var storeResponse []storage.PiggyBank + if err := d.db.Find(&storeResponse).Error; err != nil { + return nil, err + } + actionsEntities := make([]entitypiggybank.PiggyBank, len(storeResponse)) + for i := range storeResponse { + actionsEntities[i] = storage.MapActionsStoreToEntity(storeResponse[i]) + } + return actionsEntities, nil +} +func (d *DBMS) GetPiggyBankByID(piggyBankID uint) (entitypiggybank.PiggyBank, error) { + return entitypiggybank.PiggyBank{}, nil +} diff --git a/adapter/storage/init.go b/adapter/storage/init.go new file mode 100644 index 0000000..0091ef4 --- /dev/null +++ b/adapter/storage/init.go @@ -0,0 +1,22 @@ +package storage + +import ( + "fmt" +) + +var DB DBMS + +func Init() { + var eErr error + //I. Define a data source name (DSN) + dsn := "niki_user:NIKI_user@123@tcp(127.0.0.1:3306)/niki?charset=utf8mb4&parseTime=True&loc=Local" + //II. Try to connect to dsn address + DB, eErr = GormConnect(dsn) + //III. If gorm connect successfully then try to migrate database + if eErr == nil { + err := GormAutoMigrate(*DB.db) + if err != nil { + fmt.Errorf("some error .... %#v", err) + } + } +} diff --git a/adapter/storage/models/actions.go b/adapter/storage/models/actions.go new file mode 100644 index 0000000..4b96eaf --- /dev/null +++ b/adapter/storage/models/actions.go @@ -0,0 +1,32 @@ +package storage + +import ( + entitypiggybank "git.gocasts.ir/ebhomengo/niki/entity/piggy_bank" + "gorm.io/gorm" + "time" +) + +type PiggyBank struct { + gorm.Model + TotalAmount uint + SerialNumber string + //to don't insert value in "updated_at" at "insert mode" : + //see more: https://gorm.io/docs/update.html + UpdatedAt *time.Time `json:"updated_at" gorm:"<-:update"` +} + +func MapActionsStoreToEntity(piggyBank PiggyBank) entitypiggybank.PiggyBank { + returnModel := entitypiggybank.PiggyBank{ + ID: piggyBank.ID, + SerialNumber: piggyBank.SerialNumber, + TotalAmount: piggyBank.TotalAmount, + } + return returnModel +} + +func MapEntityToActionsStore(input entitypiggybank.PiggyBank) PiggyBank { + return PiggyBank{ + SerialNumber: input.SerialNumber, + TotalAmount: input.TotalAmount, + } +} diff --git a/adapter/storage/setup.go b/adapter/storage/setup.go new file mode 100644 index 0000000..3dec335 --- /dev/null +++ b/adapter/storage/setup.go @@ -0,0 +1,33 @@ +package storage + +import ( + storage "git.gocasts.ir/ebhomengo/niki/adapter/storage/models" + "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +type DBMS struct { + db *gorm.DB +} + +func GormConnect(dsn string) (DBMS, error) { + gorm, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ + Logger: logger.Default.LogMode(logger.Info), + }) + if err != nil { + return DBMS{}, err + } + return DBMS{db: gorm}, nil +} + +func GormAutoMigrate(db gorm.DB) error { + // Migrate the schema + err := db.AutoMigrate( + storage.PiggyBank{}, + ) + if err != nil { + panic(any("Storage auto migrate has error")) + } + return err +} diff --git a/delivery/http/V1/init.go b/delivery/http/V1/init.go new file mode 100644 index 0000000..1eda484 --- /dev/null +++ b/delivery/http/V1/init.go @@ -0,0 +1,12 @@ +package V1 + +import ( + deliverypiggybank "git.gocasts.ir/ebhomengo/niki/delivery/http/V1/piggy_bank" + "github.com/labstack/echo/v4" +) + +func Init(echo *echo.Echo) { + + deliverypiggybank.Routs(echo) + +} diff --git a/delivery/http/V1/piggy_bank/edit.go b/delivery/http/V1/piggy_bank/edit.go new file mode 100644 index 0000000..6b35b48 --- /dev/null +++ b/delivery/http/V1/piggy_bank/edit.go @@ -0,0 +1,12 @@ +package deliverypiggybank + +import ( + "github.com/labstack/echo/v4" + "net/http" +) + +func EditPiggyBank() echo.HandlerFunc { + return func(c echo.Context) error { + return c.JSON(http.StatusOK, nil) + } +} diff --git a/delivery/http/V1/piggy_bank/init.go b/delivery/http/V1/piggy_bank/init.go new file mode 100644 index 0000000..e9f4bac --- /dev/null +++ b/delivery/http/V1/piggy_bank/init.go @@ -0,0 +1,8 @@ +package deliverypiggybank + +import "github.com/labstack/echo/v4" + +func Routs(e *echo.Echo) { + //initialise piggy_bank details routes + +} diff --git a/delivery/http/V1/piggy_bank/new.go b/delivery/http/V1/piggy_bank/new.go new file mode 100644 index 0000000..ff3cde0 --- /dev/null +++ b/delivery/http/V1/piggy_bank/new.go @@ -0,0 +1,13 @@ +package deliverypiggybank + +import ( + "github.com/labstack/echo/v4" + "net/http" +) + +func NewPiggyBank() echo.HandlerFunc { + return func(c echo.Context) error { + + return c.JSON(http.StatusCreated, "") + } +} diff --git a/delivery/http/init.go b/delivery/http/init.go new file mode 100644 index 0000000..6df267f --- /dev/null +++ b/delivery/http/init.go @@ -0,0 +1,20 @@ +package http + +import ( + "git.gocasts.ir/ebhomengo/niki/delivery/http/V1" + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" +) + +func Init() { + //initialise new Echo (web framework) + e := echo.New() + e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ + AllowOrigins: []string{"*" /*, "https://labstack.net"*/}, + AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept}, + })) + //handle incoming requests: + V1.Init(e) + //serve with port + e.Logger.Fatal(e.Start(":1313")) +} diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..d653bdd --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,30 @@ +version: '3.9' + +services: + mysql: + platform: linux/amd64 + image: mysql:8.0 + ports: + - 3305:3305 + volumes: + - ~/apps/mysql:/var/lib/mysql + restart: always + hostname: mysql + container_name: niki_mysql + environment: + - MYSQL_ROOT_PASSWORD=niki_user + - MYSQL_PASSWORD=NIKI_user@123 + - MYSQL_USER=user + - MYSQL_DATABASE=niki + + +#set mysql in ubuntu +#//https://www.tecmint.com/fix-mysql-error-1819-hy000/ +# +# sudo mysql_secure_installation +# sudo systemctl start mysql +# mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'g#Y!298mKwz85'; +# mysql> create database orginfo; +# mysql> grant all privileges on orginfo.* to user@localhost; +# mysql> set password for user@localhost = password("g#Y!298mKwz85"); + diff --git a/dto/piggy_bank/piggy_bank.go b/dto/piggy_bank/piggy_bank.go new file mode 100644 index 0000000..648e31c --- /dev/null +++ b/dto/piggy_bank/piggy_bank.go @@ -0,0 +1,17 @@ +package dto + +import ( + entitypiggybank "git.gocasts.ir/ebhomengo/niki/entity/piggy_bank" +) + +type ( + //get + GetPiggyBankRequest struct { + ID uint `json:"id"` + TotalAmount uint `json:"totalAmount"` + SerialNumber string `json:"serialNumber"` + } + GetPiggyBankResponse struct { + Games []entitypiggybank.PiggyBank + } +) diff --git a/entity/piggy_bank/contract.go b/entity/piggy_bank/contract.go new file mode 100644 index 0000000..9ab4c07 --- /dev/null +++ b/entity/piggy_bank/contract.go @@ -0,0 +1,9 @@ +package entitypiggybank + +type PiggyBankServices interface { + AddPiggyBank([]PiggyBank) error + EditPiggyBank(piggyID uint, piggyBankInput PiggyBank) error + DeletePiggyBank(piggyID uint) error + GetAllPiggyBank() ([]PiggyBank, error) + GetPiggyBankByID(piggyBankID uint) (PiggyBank, error) +} diff --git a/entity/piggy_bank/contract/piggy_bank_request_service.go b/entity/piggy_bank/contract/piggy_bank_request_service.go deleted file mode 100644 index 194d02a..0000000 --- a/entity/piggy_bank/contract/piggy_bank_request_service.go +++ /dev/null @@ -1,13 +0,0 @@ -package contract - -import ( - entityppigybank "niki/entity/piggy_bank/model" -) - -type PiggyRequestBankRequestServices interface { - AddPiggyRequestBank([]entityppigybank.PiggyBankRequest) error - EditPiggyRequestBank(PiggyRequestID uint, PiggyRequestBankInput entityppigybank.PiggyBankRequest) error - DeletePiggyRequestBank(PiggyRequestID uint) error - GetAllPiggyRequestBank() ([]entityppigybank.PiggyBankRequest, error) - GetPiggyRequestBankByID(PiggyRequestBank uint) (entityppigybank.PiggyBankRequest, error) -} diff --git a/entity/piggy_bank/contract/piggy_bank_service.go b/entity/piggy_bank/contract/piggy_bank_service.go deleted file mode 100644 index 236f9e8..0000000 --- a/entity/piggy_bank/contract/piggy_bank_service.go +++ /dev/null @@ -1,11 +0,0 @@ -package contract - -import entityppigybank "niki/entity/piggy_bank/model" - -type PiggyBankServices interface { - AddPiggyBank([]entityppigybank.PiggyBank) error - EditPiggyBank(piggyID uint, piggyBankInput entityppigybank.PiggyBank) error - DeletePiggyBank(piggyID uint) error - GetAllPiggyBank() ([]entityppigybank.PiggyBankRequest, error) - GetPiggyBankByID(piggyBank uint) (entityppigybank.PiggyBank, error) -} diff --git a/entity/piggy_bank/model.go b/entity/piggy_bank/model.go new file mode 100644 index 0000000..335d391 --- /dev/null +++ b/entity/piggy_bank/model.go @@ -0,0 +1,9 @@ +package entitypiggybank + +type ( + PiggyBank struct { + ID uint + TotalAmount uint + SerialNumber string + } +) diff --git a/entity/piggy_bank/model/piggy_bank.go b/entity/piggy_bank/model/piggy_bank.go deleted file mode 100644 index 94525c1..0000000 --- a/entity/piggy_bank/model/piggy_bank.go +++ /dev/null @@ -1,13 +0,0 @@ -package entityppigybank - -import "time" - -type ( - PiggyBank struct { - ID uint - TotalAmount uint - SerialNumber string - Status uint - StatusChangedAt *time.Time - } -) diff --git a/entity/piggy_bank_request/contract.go b/entity/piggy_bank_request/contract.go new file mode 100644 index 0000000..6edad4b --- /dev/null +++ b/entity/piggy_bank_request/contract.go @@ -0,0 +1,9 @@ +package entitypiggybankrequest + +type PiggyRequestBankRequestServices interface { + AddPiggyRequestBank([]PiggyBankRequest) error + EditPiggyRequestBank(PiggyRequestID uint, PiggyRequestBankInput PiggyBankRequest) error + DeletePiggyRequestBank(PiggyRequestID uint) error + GetAllPiggyRequestBank() ([]PiggyBankRequest, error) + GetPiggyRequestBankByID(PiggyRequestBank uint) (PiggyBankRequest, error) +} diff --git a/entity/piggy_bank/model/piggy_bank_request.go b/entity/piggy_bank_request/model.go similarity index 84% rename from entity/piggy_bank/model/piggy_bank_request.go rename to entity/piggy_bank_request/model.go index e52c6cd..fc6e7d7 100644 --- a/entity/piggy_bank/model/piggy_bank_request.go +++ b/entity/piggy_bank_request/model.go @@ -1,4 +1,4 @@ -package entityppigybank +package entitypiggybankrequest type ( PiggyBankRequest struct { diff --git a/go.mod b/go.mod index 0499f74..0f16f59 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,26 @@ -module https://git.gocasts.ir/ebhomengo/niki +module git.gocasts.ir/ebhomengo/niki -go 1.19 +go 1.21.0 + +require ( + github.com/labstack/echo/v4 v4.11.3 + gorm.io/driver/mysql v1.5.2 + gorm.io/gorm v1.25.5 +) + +require ( + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/golang-jwt/jwt v3.2.2+incompatible // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/labstack/gommon v0.4.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + golang.org/x/crypto v0.14.0 // indirect + golang.org/x/net v0.17.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/text v0.13.0 // indirect + golang.org/x/time v0.3.0 // indirect +) diff --git a/main.go b/main.go index c6217af..07b7901 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,13 @@ package niki -func main() { +import ( + "git.gocasts.ir/ebhomengo/niki/adapter/storage" + "git.gocasts.ir/ebhomengo/niki/delivery/http" +) +func main() { + //initialize D.B.M.S. (Database management system) + storage.Init() + //initialize new Echo (web framework) + http.Init() } diff --git a/service/piggy_bank/edit.go b/service/piggy_bank/edit.go new file mode 100644 index 0000000..10d4e4a --- /dev/null +++ b/service/piggy_bank/edit.go @@ -0,0 +1,11 @@ +package piggy_bank + +import ( + dto "git.gocasts.ir/ebhomengo/niki/dto/piggy_bank" +) + +func (uc Usecase) EditPiggyBank(req dto.GetPiggyBankRequest) error { + //validation DTO request + + return nil +} diff --git a/service/piggy_bank/init.go b/service/piggy_bank/init.go index 17da363..7e90d23 100644 --- a/service/piggy_bank/init.go +++ b/service/piggy_bank/init.go @@ -1,3 +1,14 @@ package piggy_bank +import ( + "git.gocasts.ir/ebhomengo/niki/adapter/storage" + entitypiggybank "git.gocasts.ir/ebhomengo/niki/entity/piggy_bank" +) +type Usecase struct { + storage entitypiggybank.PiggyBankServices +} + +func InitUsecase() Usecase { + return Usecase{storage: &storage.DB} +} diff --git a/service/piggy_bank/new.go b/service/piggy_bank/new.go new file mode 100644 index 0000000..420954e --- /dev/null +++ b/service/piggy_bank/new.go @@ -0,0 +1,11 @@ +package piggy_bank + +import ( + dto "git.gocasts.ir/ebhomengo/niki/dto/piggy_bank" +) + +func (uc Usecase) NewPiggyBank(req dto.GetPiggyBankRequest) error { + //validation DTO request + + return nil +}