new_sample_structure

This commit is contained in:
Alireza Mokhtari Garakani 2023-12-16 10:50:01 +03:30
parent 8614245d15
commit f28c3a6c42
48 changed files with 247 additions and 363 deletions

4
.idea/.gitignore vendored
View File

@ -1,8 +1,8 @@
# Default ignored files # Default ignored files
/shelf/ /shelf/
/workspace.xml /workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files # Datasource local storage ignored files
/dataSources/ /dataSources/
/dataSources.local.xml /dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="description" value="" />
</component>
</project>

View File

@ -2,7 +2,7 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/.idea/niki.iml" filepath="$PROJECT_DIR$/.idea/niki.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/amir-niki.iml" filepath="$PROJECT_DIR$/.idea/amir-niki.iml" />
</modules> </modules>
</component> </component>
</project> </project>

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="Go" enabled="true" />
</module>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,23 +0,0 @@
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

@ -1,30 +0,0 @@
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
}

View File

@ -1,22 +0,0 @@
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

@ -1,32 +0,0 @@
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,
}
}

View File

@ -1,33 +0,0 @@
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
}

14
config/config.go Normal file
View File

@ -0,0 +1,14 @@
package config
import (
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)
type HTTPServer struct {
Port int `koanf:"port"`
}
type Config struct {
HTTPServer HTTPServer `koanf:"http_server"`
Mysql mysql.Config `koanf:"mysql"`
}

View File

@ -1,12 +0,0 @@
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

@ -1,12 +0,0 @@
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

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

View File

@ -1,13 +0,0 @@
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, "")
}
}

View File

@ -1,20 +0,0 @@
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"))
}

View File

@ -0,0 +1,17 @@
package httpserver
import "git.gocasts.ir/ebhomengo/niki/config"
type Server struct {
config config.Config
}
func New(config config.Config) Server {
return Server{
config: config,
}
}
func (s Server) Serve() {
}

View File

@ -1,17 +0,0 @@
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
}
)

11
entity/piggy_bank.go Normal file
View File

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

View File

@ -1,9 +0,0 @@
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,9 +0,0 @@
package entitypiggybank
type (
PiggyBank struct {
ID uint
TotalAmount uint
SerialNumber string
}
)

View File

@ -1,9 +0,0 @@
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,12 +0,0 @@
package entitypiggybankrequest
type (
PiggyBankRequest struct {
ID uint
TypeId uint
CountRequested uint
CountAccepted uint
BenefactorID uint
Description string
}
)

25
go.mod
View File

@ -1,26 +1,3 @@
module git.gocasts.ir/ebhomengo/niki module git.gocasts.ir/ebhomengo/niki
go 1.21.0 go 1.21.3
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
)

12
main.go
View File

@ -1,13 +1,5 @@
package niki package main
import (
"git.gocasts.ir/ebhomengo/niki/adapter/storage"
"git.gocasts.ir/ebhomengo/niki/delivery/http"
)
func main() { func main() {
//initialize D.B.M.S. (Database management system)
storage.Init()
//initialize new Echo (web framework)
http.Init()
} }

View File

@ -0,0 +1,7 @@
package param
type PiggyBankDeleteRequest struct {
}
type PiggyBankDeleteResponse struct {
}

View File

@ -0,0 +1,7 @@
package param
type PiggyBankGetAllRequest struct {
}
type PiggyBankGetAllResponse struct {
}

View File

@ -0,0 +1,7 @@
package param
type PiggyBankGetByIdRequest struct {
}
type PiggyBankGetByIdResponse struct {
}

View File

@ -0,0 +1,7 @@
package param
type PiggyBankStoreRequest struct {
}
type PiggyBankStoreResponse struct {
}

View File

@ -0,0 +1,7 @@
package param
type PiggyBankUpdateRequest struct {
}
type PiggyBankUpdateResponse struct {
}

42
repository/mysql/db.go Normal file
View File

@ -0,0 +1,42 @@
package mysql
import (
"database/sql"
"fmt"
"time"
)
type Config struct {
Username string `koanf:"username"`
Password string `koanf:"password"`
Port int `koanf:"port"`
Host string `koanf:"host"`
DBName string `koanf:"db_name"`
}
type MySQLDB struct {
config Config
db *sql.DB
}
func (m *MySQLDB) Conn() *sql.DB {
return m.db
}
func New(config Config) *MySQLDB {
// parseTime=true changes the output type of DATE and DATETIME values to time.Time
// instead of []byte / string
// The date or datetime like 0000-00-00 00:00:00 is converted into zero value of time.Time
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true",
config.Username, config.Password, config.Host, config.Port, config.DBName))
if err != nil {
panic(fmt.Errorf("can't open mysql db: %v", err))
}
// See "Important settings" section.
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
return &MySQLDB{config: config, db: db}
}

View File

View File

@ -0,0 +1,13 @@
package mysqlpiggybank
import "git.gocasts.ir/ebhomengo/niki/repository/mysql"
type DB struct {
conn *mysql.MySQLDB
}
func New(conn *mysql.MySQLDB) *DB {
return &DB{
conn: conn,
}
}

View File

@ -0,0 +1 @@
package mysqlpiggybank

View File

@ -1,11 +0,0 @@
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,14 +0,0 @@
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}
}

View File

@ -1,11 +0,0 @@
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
}

View File

@ -0,0 +1,7 @@
package piggybankservice
import "git.gocasts.ir/ebhomengo/niki/param"
func (s Service) Delete(req param.PiggyBankDeleteRequest) (param.PiggyBankDeleteResponse, error) {
return param.PiggyBankDeleteResponse{}, nil
}

View File

@ -0,0 +1,7 @@
package piggybankservice
import "git.gocasts.ir/ebhomengo/niki/param"
func (s Service) GetAll(req param.PiggyBankGetAllRequest) (param.PiggyBankGetAllResponse, error) {
return param.PiggyBankGetAllResponse{}, nil
}

View File

@ -0,0 +1,7 @@
package piggybankservice
import "git.gocasts.ir/ebhomengo/niki/param"
func (s Service) GetById(req param.PiggyBankGetByIdRequest) (param.PiggyBankGetByIdResponse, error) {
return param.PiggyBankGetByIdResponse{}, nil
}

View File

@ -0,0 +1,21 @@
package piggybankservice
import "git.gocasts.ir/ebhomengo/niki/entity"
type Repository interface {
AddPiggyBank(piggy entity.PiggyBank) (entity.PiggyBank, error)
EditPiggyBank(piggyID uint, piggyBankInput entity.PiggyBank) (entity.PiggyBank, error)
DeletePiggyBank(piggyID uint) error
GetAllPiggyBank() ([]entity.PiggyBank, error)
GetPiggyBankByID(piggyBank uint) (entity.PiggyBank, error)
}
type Service struct {
repo Repository
}
func New(repository Repository) Service {
return Service{
repo: repository,
}
}

View File

@ -0,0 +1,7 @@
package piggybankservice
import "git.gocasts.ir/ebhomengo/niki/param"
func (s Service) Store(req param.PiggyBankStoreRequest) (param.PiggyBankStoreResponse, error) {
return param.PiggyBankStoreResponse{}, nil
}

View File

@ -0,0 +1,7 @@
package piggybankservice
import "git.gocasts.ir/ebhomengo/niki/param"
func (s Service) Update(req param.PiggyBankUpdateRequest) (param.PiggyBankUpdateResponse, error) {
return param.PiggyBankUpdateResponse{}, nil
}

View File

@ -0,0 +1,7 @@
package piggybankvalidator
import "git.gocasts.ir/ebhomengo/niki/param"
func (v Validator) ValidateDeleteRequest(req param.PiggyBankDeleteRequest) {
}

View File

@ -0,0 +1,7 @@
package piggybankvalidator
import "git.gocasts.ir/ebhomengo/niki/param"
func (v Validator) ValidateGetByIdRequest(req param.PiggyBankGetByIdRequest) {
}

View File

@ -0,0 +1,7 @@
package piggybankvalidator
import "git.gocasts.ir/ebhomengo/niki/param"
func (v Validator) ValidateStoreRequest(req param.PiggyBankStoreRequest) {
}

View File

@ -0,0 +1,7 @@
package piggybankvalidator
import "git.gocasts.ir/ebhomengo/niki/param"
func (v Validator) ValidateUpdateRequest(req param.PiggyBankUpdateRequest) {
}

View File

@ -0,0 +1,12 @@
package piggybankvalidator
type Repository interface {
}
type Validator struct {
repo Repository
}
func New(repo Repository) Validator {
return Validator{repo: repo}
}