Documentation ¶
Index ¶
- Variables
- func CreateEmployee(db *sqlx.DB, e *EmployeeData) (string, error)
- func CreateTask(db *sqlx.DB, t *TaskData) (string, error)
- func CreateTaskComment(db *sqlx.DB, taskID string, c *TaskCommentData) error
- func GetEmployee(db *sqlx.DB, ID string) ([]byte, error)
- func GetEmployeeByIDCardNo(db *sqlx.DB, IDCardNo string) ([]byte, error)
- func GetEmployeeByMobilePhoneNum(db *sqlx.DB, mobilePhoneNum string) ([]byte, error)
- func GetJSONData(db *sqlx.DB, sqlStat string, args ...interface{}) ([]byte, error)
- func GetTask(db *sqlx.DB, ID string) ([]byte, error)
- func GetTaskCountByAssignee(db *sqlx.DB, assignee string) (int64, error)
- func GetTaskCountByAssigner(db *sqlx.DB, assigner string) (int64, error)
- func GetTasksByAssignee(db *sqlx.DB, assignee string, limit, offset int64) ([][]byte, error)
- func GetTasksByAssigner(db *sqlx.DB, assigner string, limit, offset int64) ([][]byte, error)
- func InitDB(db *sqlx.DB) error
- func RemoveAllEmployees(db *sqlx.DB) error
- func RemoveAllTasks(db *sqlx.DB) error
- func SelectJSONData(db *sqlx.DB, sqlStat string, args ...interface{}) ([][]byte, error)
- func UpdateSex(sex string) string
- type Employee
- type EmployeeData
- type Task
- type TaskComment
- type TaskCommentData
- type TaskData
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrInvalidName = fmt.Errorf("invalid name") ErrInvalidSex = fmt.Errorf("invalid sex") ErrInvalidIDCardNo = fmt.Errorf("invalid ID card number") ErrInvalidMobilePhoneNum = fmt.Errorf("invalid mobile phone number") ErrNotUnique = fmt.Errorf("at least one unique item is not unique") )
Functions ¶
func CreateEmployee ¶
func CreateEmployee(db *sqlx.DB, e *EmployeeData) (string, error)
Example ¶
var ( IDs []string ) employees := []hr.EmployeeData{ hr.EmployeeData{"Frank", "m", "310104198101010000", "13100000000"}, hr.EmployeeData{"Bob", "m", "310104198201010000", "13300000000"}, hr.EmployeeData{"Alice", "f", "310104198302020000", "13500000000"}, } // Remove all employees in the table. if err = hr.RemoveAllEmployees(db); err != nil { log.Printf("RemoveAllEmployees() error: %v", err) return } // Create employees. for _, e := range employees { ID, err := hr.CreateEmployee(db, &e) if err != nil { log.Printf("CreateEmployee() error: %v", err) return } IDs = append(IDs, ID) log.Printf("CreateEmployee() OK. ID = %v, employee: %v", ID, e) } // Get employee data by ID. for _, ID := range IDs { jsonData, err := hr.GetEmployee(db, ID) if err != nil { log.Printf("GetEmployee(%v) error: %v", ID, err) return } log.Printf("GetEmployee(%v) OK. JSON: %s", ID, jsonData) } // Get employee by ID card number. for _, e := range employees { jsonData, err := hr.GetEmployeeByIDCardNo(db, e.IDCardNo) if err != nil { log.Printf("GetEmployeeByIDCardNo(%v) error: %v", e.IDCardNo, err) return } log.Printf("GetEmployeeByIDCardNo(%v) OK. JSON: %s", e.IDCardNo, jsonData) } // Test of invalid ID card number. jsonData, err := hr.GetEmployeeByIDCardNo(db, "0000") log.Printf("\nTest of invalid ID card number: JSON: %s, err: %v", jsonData, err) // Get employee by mobile phone number. for _, e := range employees { jsonData, err := hr.GetEmployeeByMobilePhoneNum(db, e.MobilePhoneNum) if err != nil { log.Printf("GetEmployeeByMobilePhoneNum(%v) error: %v", e.MobilePhoneNum, err) return } log.Printf("GetEmployeeByMobilePhoneNum(%v) OK. JSON: %s", e.MobilePhoneNum, jsonData) } // Test of invalid mobile phone number. jsonData, err = hr.GetEmployeeByMobilePhoneNum(db, "0000") log.Printf("\nTest of invalid mobile phone number: JSON: %s, err: %v", jsonData, err)
Output:
func CreateTask ¶
Example ¶
var ( IDs []string taskIDs []string ) employees := []hr.EmployeeData{ hr.EmployeeData{"Frank", "m", "310104198101010000", "13100000000"}, hr.EmployeeData{"Bob", "m", "310104198201010000", "13300000000"}, hr.EmployeeData{"Alice", "f", "310104198302020000", "13500000000"}, } // Remove all employees in the table. if err := hr.RemoveAllEmployees(db); err != nil { log.Printf("RemoveAllEmployees() error: %v", err) return } // Create employees. for _, e := range employees { ID, err := hr.CreateEmployee(db, &e) if err != nil { log.Printf("CreateEmployee() error: %v", err) return } IDs = append(IDs, ID) log.Printf("CreateEmployee() OK. ID = %v, employee: %v", ID, e) } // Create task. tasks := []hr.TaskData{ hr.TaskData{ Assigner: IDs[0], Assignees: []string{IDs[1], IDs[2]}, Priority: 1, Closed: false, Tags: []string{"IT", "PC"}, Title: "Purchase PC", Content: "Purchase 10 PCs", }, hr.TaskData{ Assigner: IDs[0], Assignees: []string{IDs[1]}, Priority: 2, Closed: false, Tags: []string{"IT", "Printer"}, Title: "Fix the Printer", Content: "Laser Printer does not work", }, } // Remove all tasks in the table. if err := hr.RemoveAllTasks(db); err != nil { log.Printf("RemoveAllTasks() error: %v", err) return } // Create tasks. for _, t := range tasks { ID, err := hr.CreateTask(db, &t) if err != nil { log.Printf("CreateTask() error: %v", err) return } taskIDs = append(taskIDs, ID) log.Printf("CreateTask() OK. ID = %v, task: %v", ID, t) } // Create task comments commentDatas := []hr.TaskCommentData{ hr.TaskCommentData{ Author: IDs[0], Content: "Please get this done ASAP.", }, hr.TaskCommentData{ Author: IDs[1], Content: "I need 3 - 5 days.", }, } for _, comment := range commentDatas { if err := hr.CreateTaskComment(db, taskIDs[0], &comment); err != nil { log.Printf("CreateTaskComment() error: %v", err) return } } // Get task data by ID. for _, ID := range taskIDs { jsonData, err := hr.GetTask(db, ID) if err != nil { log.Printf("GetTask(%v) error: %v", ID, err) return } log.Printf("GetTask(%v) OK. JSON: %s", ID, jsonData) } // Get tasks by assigner. assigner := IDs[0] n, err := hr.GetTaskCountByAssigner(db, assigner) if err != nil { log.Printf("GetTaskCountByAssigner() error: %v", err) return } log.Printf("GetTaskCountByAssigner() OK. assigner: %v, count: %v", assigner, n) limit := int64(2) offset := int64(0) for offset = 0; offset < n; offset += limit { jsonDataArr, err := hr.GetTasksByAssigner(db, assigner, limit, offset) if err != nil { log.Printf("GetTasksByAssigner() error: %v", err) return } log.Printf("GetTasksByAssigner() OK. LIMIT: %v, OFFSET: %v", limit, offset) for _, data := range jsonDataArr { log.Printf("JSON: %s\n", data) } } // Get tasks by assignee. assignee := IDs[1] n, err = hr.GetTaskCountByAssignee(db, assignee) if err != nil { log.Printf("GetTaskCountByAssignee() error: %v", err) return } log.Printf("GetTaskCountByAssignee() OK. assignee: %v, count: %v", assignee, n) limit = int64(1) offset = int64(0) for offset = 0; offset < n; offset += limit { jsonDataArr, err := hr.GetTasksByAssignee(db, assignee, limit, offset) if err != nil { log.Printf("GetTasksByAssignee() error: %v", err) return } log.Printf("GetTasksByAssignee() OK. LIMIT: %v, OFFSET: %v", limit, offset) for _, data := range jsonDataArr { log.Printf("JSON: %s\n", data) } }
Output:
func CreateTaskComment ¶
func CreateTaskComment(db *sqlx.DB, taskID string, c *TaskCommentData) error
func GetEmployeeByIDCardNo ¶
func GetJSONData ¶
func GetTaskCountByAssignee ¶
func GetTaskCountByAssigner ¶
func GetTasksByAssignee ¶
func GetTasksByAssigner ¶
func InitDB ¶
Example ¶
package main import ( "fmt" "log" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" "github.com/northbright/hr" ) func main() { host := "localhost" port := "5432" user := "postgres" dbname := "test" log.Printf(`Make sure to run the command before test: psql -U postgres -c 'CREATE DATABASE test;'`) psqlInfo := fmt.Sprintf("host=%v port=%v user=%v dbname=%v sslmode=disable", host, port, user, dbname) db, err := sqlx.Open("postgres", psqlInfo) if err != nil { log.Printf("sqlx.Open() error: %v", err) return } defer db.Close() if err = hr.InitDB(db); err != nil { log.Printf("hr.InitDB() error: %v", err) return } log.Printf("hr.InitDB() OK") }
Output:
func RemoveAllEmployees ¶
func RemoveAllTasks ¶
func SelectJSONData ¶
Types ¶
type Employee ¶
type Employee struct { ID string `json:"id"` Created int64 `json:"created"` *EmployeeData }
Employee contains ID and full employee data.
type EmployeeData ¶
type EmployeeData struct { Name string `json:"name"` Sex string `json:"sex"` IDCardNo string `json:"id_card_no"` MobilePhoneNum string `json:"mobile_phone_num"` }
func (*EmployeeData) CheckUniqueItems ¶
func (e *EmployeeData) CheckUniqueItems(db *sqlx.DB) error
func (*EmployeeData) Valid ¶
func (e *EmployeeData) Valid() error
type Task ¶
type Task struct { ID string `json:"id"` Created int64 `json:"created"` Comments []TaskComment `json:"comments,omitempty"` *TaskData }
type TaskComment ¶
type TaskComment struct { Created int64 `json:"created"` *TaskCommentData }
type TaskCommentData ¶
Click to show internal directories.
Click to hide internal directories.