action

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2023 License: CC0-1.0 Imports: 7 Imported by: 0

README

Database Base Action (CRUD)

How to Use

package main

import (
	"context"
	"log"
	
	"github.com/kodekoding/phastos/go/database"
	"github.com/kodekoding/phastos/go/database/action"
)

func main() {
	db, err := database.Connect(&database.SQLConfig{
		Username:        "root",
		Password:        "...",
		Host:            "localhost",
		Port:            "3306",
		DBName:          "...",
		Engine:          "mysql",
		MaxConnLifetime: 199,
		MaxIdleTime:     100,
		MaxOpenConn:     10,
		MaxIdleConn:     10,
	})

	if err != nil {
		log.Fatalln("error db", err.Error())
	}

	repoOrang := NewOrangRepo(db)
	orangUc := NewUc(repoOrang)

	orangUc.Insert(context.Background(), &OrangEntity{
		Nama:   "test",
		Alamat: "testing",
		Telp:   "2123xx",
	})

	result, err := orangUc.GetList(context.Background(), &OrangRequest{
		TableRequest: database.TableRequest{},
	})

	log.Printf("%#v", result.Data)
}

type (
	Orangs interface {
		Base() *action.Base
	}
	OrangRepo struct {
		base *action.Base
	}
)

func NewOrangRepo(db *database.SQL) *OrangRepo {
	return &OrangRepo{
		base: action.NewBase(db, "orang"),
	}
}

func (o *OrangRepo) Base() *action.Base {
	return o.base
}

type (
	OrangUsecases interface {
		GetList(ctx context.Context, requestData interface{}) (*database.SelectResponse, error)
		Insert(ctx context.Context, requestData interface{}) (*database.CUDResponse, error)
	}
	orangUc struct {
		orangRepo Orangs
	}

	OrangEntity struct {
		database.BaseColumn
		Nama   string `db:"nama"`
		Alamat string `db:"alamat"`
		Telp   string `db:"phone_number"`
	}
	OrangRequest struct {
		OrangEntity
		database.TableRequest
	}
)

func NewUc(repo Orangs) *orangUc {
	return &orangUc{orangRepo: repo}
}

func (o *orangUc) GetList(ctx context.Context, requestData interface{}) (*database.SelectResponse, error) {
	orangRequest := requestData.(*OrangRequest)
	var orangList []*OrangEntity
	if err := o.orangRepo.Base().GetList(ctx, &database.QueryOpts{
		IsList:        true,
		SelectRequest: &orangRequest.TableRequest,
		ResultStruct:  &orangList,
	}); err != nil {
		return nil, err
	}

	result := new(database.SelectResponse)
	result.Data = orangList
	return result, nil
}

func (o *orangUc) Insert(ctx context.Context, requestData interface{}) (*database.CUDResponse, error) {
	result, err := o.orangRepo.Base().Insert(ctx, requestData)
	if err != nil {
		return nil, err
	}
	return result, nil
}



Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base struct {
	BaseWrites
	common.ReadRepo
}

func NewBase

func NewBase(db *database.SQL, tableName string, isSoftDelete ...bool) *Base

type BaseRead

type BaseRead struct {
	// contains filtered or unexported fields
}

func NewBaseRead

func NewBaseRead(db *database.SQL, tableName string, isSoftDelete ...bool) *BaseRead

func (*BaseRead) Count added in v0.1.9

func (b *BaseRead) Count(ctx context.Context, tableName string, reqData *database.TableRequest) (totalData, totalFiltered int, err error)

func (*BaseRead) GetDetail added in v0.2.7

func (b *BaseRead) GetDetail(ctx context.Context, opts *database.QueryOpts) error

GetDetail - Query Detail with specific Query and return single data

func (*BaseRead) GetDetailById

func (b *BaseRead) GetDetailById(ctx context.Context, resultStruct interface{}, id int, optionalTableName ...string) error

GetDetailById - Generate Query "SELECT * FROM <table_name | optional_table_name> WHERE id = ?"

func (*BaseRead) GetList

func (b *BaseRead) GetList(ctx context.Context, opts *database.QueryOpts) error

type BaseWrite

type BaseWrite struct {
	// contains filtered or unexported fields
}

func NewBaseWrite

func NewBaseWrite(db *database.SQL, tableName string, isSoftDelete ...bool) *BaseWrite

func (*BaseWrite) BulkInsert added in v0.5.0

func (b *BaseWrite) BulkInsert(ctx context.Context, data interface{}, trx ...*sql.Tx) (*database.CUDResponse, error)

func (*BaseWrite) Delete

func (b *BaseWrite) Delete(ctx context.Context, condition map[string]interface{}, trx ...*sql.Tx) (*database.CUDResponse, error)

func (*BaseWrite) DeleteById added in v0.5.0

func (b *BaseWrite) DeleteById(ctx context.Context, id int, trx ...*sql.Tx) (*database.CUDResponse, error)

func (*BaseWrite) Insert

func (b *BaseWrite) Insert(ctx context.Context, data interface{}, trx ...*sql.Tx) (*database.CUDResponse, error)

func (*BaseWrite) Update

func (b *BaseWrite) Update(ctx context.Context, data interface{}, condition map[string]interface{}, trx ...*sql.Tx) (*database.CUDResponse, error)

func (*BaseWrite) UpdateById added in v0.5.0

func (b *BaseWrite) UpdateById(ctx context.Context, data interface{}, id int, trx ...*sql.Tx) (*database.CUDResponse, error)

func (*BaseWrite) Upsert

func (b *BaseWrite) Upsert(ctx context.Context, data interface{}, trx ...*sql.Tx) (*database.CUDResponse, error)

type BaseWrites

type BaseWrites interface {
	common.WriteRepo
	Upsert(ctx context.Context, data interface{}, trx ...*sql.Tx) (*database.CUDResponse, error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL