make sample structure

This commit is contained in:
Alireza Mokhtari Garakani 2023-12-16 09:39:50 +03:30
parent 760a9821c5
commit c9810e1f90
25 changed files with 348 additions and 47 deletions

View File

@ -1,9 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4"> <module version="4">
<component name="Go" enabled="true" /> <component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module> </module>

View File

@ -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
}

View File

@ -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
}

22
adapter/storage/init.go Normal file
View File

@ -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)
}
}
}

View File

@ -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,
}
}

33
adapter/storage/setup.go Normal file
View File

@ -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
}

12
delivery/http/V1/init.go Normal file
View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -0,0 +1,8 @@
package deliverypiggybank
import "github.com/labstack/echo/v4"
func Routs(e *echo.Echo) {
//initialise piggy_bank details routes
}

View File

@ -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, "")
}
}

20
delivery/http/init.go Normal file
View File

@ -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"))
}

30
docker-compose.yaml Normal file
View File

@ -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");

View File

@ -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
}
)

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -0,0 +1,9 @@
package entitypiggybank
type (
PiggyBank struct {
ID uint
TotalAmount uint
SerialNumber string
}
)

View File

@ -1,13 +0,0 @@
package entityppigybank
import "time"
type (
PiggyBank struct {
ID uint
TotalAmount uint
SerialNumber string
Status uint
StatusChangedAt *time.Time
}
)

View File

@ -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)
}

View File

@ -1,4 +1,4 @@
package entityppigybank package entitypiggybankrequest
type ( type (
PiggyBankRequest struct { PiggyBankRequest struct {

27
go.mod
View File

@ -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
)

10
main.go
View File

@ -1,5 +1,13 @@
package niki 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()
} }

View File

@ -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
}

View File

@ -1,3 +1,14 @@
package piggy_bank 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}
}

11
service/piggy_bank/new.go Normal file
View File

@ -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
}