compnay select
This commit is contained in:
21
app/lib/company/company.go
Normal file
21
app/lib/company/company.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package company
|
||||
|
||||
import (
|
||||
"bordrobot/lib/db"
|
||||
)
|
||||
|
||||
type Company struct {
|
||||
Name string `db:"name"`
|
||||
MemberNumber string `db:"member_number"`
|
||||
Username string `db:"username"`
|
||||
Password string `db:"password"`
|
||||
}
|
||||
|
||||
func (c *Company) Create() error {
|
||||
sq := `
|
||||
insert into company (name, member_number, username, password)
|
||||
VALUES (:name, :member_number, :username, :password)
|
||||
`
|
||||
_, err := db.DB.NamedExec(sq, &c)
|
||||
return err
|
||||
}
|
||||
27
app/lib/company/company_test.go
Normal file
27
app/lib/company/company_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package company
|
||||
|
||||
import (
|
||||
"bordrobot/lib/db"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompany_Create(t *testing.T) {
|
||||
err := db.InitDB("../..")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
c := &Company{
|
||||
Name: "Deneme Firması",
|
||||
MemberNumber: "1234",
|
||||
Username: "u1",
|
||||
Password: "p1",
|
||||
}
|
||||
|
||||
err = c.Create()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db.DB.Close()
|
||||
}
|
||||
50
app/lib/db/db.go
Normal file
50
app/lib/db/db.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
var DB *sqlx.DB
|
||||
|
||||
func createTableIfNotExists(tableName string, schema string) error {
|
||||
var tmpName string
|
||||
sqCheck := "SELECT name FROM sqlite_master WHERE type='table' AND name=?"
|
||||
err := DB.QueryRow(sqCheck, tableName).Scan(&tmpName)
|
||||
|
||||
if err != nil {
|
||||
if err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err = DB.Exec(schema)
|
||||
return err
|
||||
}
|
||||
|
||||
func InitDB(path string) error {
|
||||
var err error
|
||||
|
||||
fname := fmt.Sprintf("%s/%s", path, "bordro_robot.db")
|
||||
|
||||
DB, err = sqlx.Open("sqlite", fname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
companySchema := `
|
||||
create table Company (
|
||||
name text not null unique,
|
||||
member_number text not null,
|
||||
username text not null,
|
||||
password text not null
|
||||
)
|
||||
`
|
||||
err = createTableIfNotExists("company", companySchema)
|
||||
|
||||
return err
|
||||
}
|
||||
25
app/lib/dbsrv/dbsrv.go
Normal file
25
app/lib/dbsrv/dbsrv.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package dbsrv
|
||||
|
||||
import (
|
||||
"bordrobot/lib/company"
|
||||
"bordrobot/lib/db"
|
||||
)
|
||||
|
||||
type Srv struct{}
|
||||
|
||||
func (ds *Srv) Companies() ([]company.Company, error) {
|
||||
var res []company.Company
|
||||
err := db.DB.Select(&res, "SELECT * FROM company order by 1")
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (ds *Srv) CreateCompany(name, memberNumber, username, password string) error {
|
||||
c := &company.Company{
|
||||
Name: name,
|
||||
MemberNumber: memberNumber,
|
||||
Username: username,
|
||||
Password: password,
|
||||
}
|
||||
|
||||
return c.Create()
|
||||
}
|
||||
Reference in New Issue
Block a user