-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
19 changed files
with
1,066 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
language: go | ||
|
||
|
||
go: | ||
- 1.9.x | ||
- 1.8.x | ||
- 1.13.x | ||
- tip | ||
|
||
before_script: | ||
- go get -t -v github.com/smallnest/gen/... | ||
|
||
script: | ||
- go build . | ||
- go build . | ||
|
||
notifications: | ||
email: | ||
recipients: [email protected] | ||
on_success: change | ||
on_failure: always | ||
on_failure: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"generated/model" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/smallnest/gen/dbmeta" | ||
) | ||
|
||
func configDepartmentsRouter(router *httprouter.Router) { | ||
router.GET("/departments", GetAllDepartments) | ||
router.POST("/departments", AddDepartment) | ||
router.GET("/departments/:id", GetDepartment) | ||
router.PUT("/departments/:id", UpdateDepartment) | ||
router.DELETE("/departments/:id", DeleteDepartment) | ||
} | ||
|
||
func GetAllDepartments(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
page, err := readInt(r, "page", 1) | ||
if err != nil || page < 1 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
pagesize, err := readInt(r, "pagesize", 20) | ||
if err != nil || pagesize <= 0 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
offset := (page - 1) * pagesize | ||
|
||
order := r.FormValue("order") | ||
|
||
departments := []*model.Department{} | ||
|
||
if order != "" { | ||
err = DB.Model(&model.Department{}).Order(order).Offset(offset).Limit(pagesize).Find(&departments).Error | ||
} else { | ||
err = DB.Model(&model.Department{}).Offset(offset).Limit(pagesize).Find(&departments).Error | ||
} | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, &departments) | ||
} | ||
|
||
func GetDepartment(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
department := &model.Department{} | ||
if DB.First(department, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
writeJSON(w, department) | ||
} | ||
|
||
func AddDepartment(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
department := &model.Department{} | ||
|
||
if err := readJSON(r, department); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err := DB.Save(department).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, department) | ||
} | ||
|
||
func UpdateDepartment(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
|
||
department := &model.Department{} | ||
if DB.First(department, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
updated := &model.Department{} | ||
if err := readJSON(r, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := dbmeta.Copy(department, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := DB.Save(department).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, department) | ||
} | ||
|
||
func DeleteDepartment(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
department := &model.Department{} | ||
|
||
if DB.First(department, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
if err := DB.Delete(department).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"generated/model" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/smallnest/gen/dbmeta" | ||
) | ||
|
||
func configDeptEmpsRouter(router *httprouter.Router) { | ||
router.GET("/deptemps", GetAllDeptEmps) | ||
router.POST("/deptemps", AddDeptEmp) | ||
router.GET("/deptemps/:id", GetDeptEmp) | ||
router.PUT("/deptemps/:id", UpdateDeptEmp) | ||
router.DELETE("/deptemps/:id", DeleteDeptEmp) | ||
} | ||
|
||
func GetAllDeptEmps(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
page, err := readInt(r, "page", 1) | ||
if err != nil || page < 1 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
pagesize, err := readInt(r, "pagesize", 20) | ||
if err != nil || pagesize <= 0 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
offset := (page - 1) * pagesize | ||
|
||
order := r.FormValue("order") | ||
|
||
deptemps := []*model.DeptEmp{} | ||
|
||
if order != "" { | ||
err = DB.Model(&model.DeptEmp{}).Order(order).Offset(offset).Limit(pagesize).Find(&deptemps).Error | ||
} else { | ||
err = DB.Model(&model.DeptEmp{}).Offset(offset).Limit(pagesize).Find(&deptemps).Error | ||
} | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, &deptemps) | ||
} | ||
|
||
func GetDeptEmp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
deptemp := &model.DeptEmp{} | ||
if DB.First(deptemp, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
writeJSON(w, deptemp) | ||
} | ||
|
||
func AddDeptEmp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
deptemp := &model.DeptEmp{} | ||
|
||
if err := readJSON(r, deptemp); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err := DB.Save(deptemp).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, deptemp) | ||
} | ||
|
||
func UpdateDeptEmp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
|
||
deptemp := &model.DeptEmp{} | ||
if DB.First(deptemp, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
updated := &model.DeptEmp{} | ||
if err := readJSON(r, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := dbmeta.Copy(deptemp, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := DB.Save(deptemp).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, deptemp) | ||
} | ||
|
||
func DeleteDeptEmp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
deptemp := &model.DeptEmp{} | ||
|
||
if DB.First(deptemp, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
if err := DB.Delete(deptemp).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"generated/model" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/smallnest/gen/dbmeta" | ||
) | ||
|
||
func configDeptManagersRouter(router *httprouter.Router) { | ||
router.GET("/deptmanagers", GetAllDeptManagers) | ||
router.POST("/deptmanagers", AddDeptManager) | ||
router.GET("/deptmanagers/:id", GetDeptManager) | ||
router.PUT("/deptmanagers/:id", UpdateDeptManager) | ||
router.DELETE("/deptmanagers/:id", DeleteDeptManager) | ||
} | ||
|
||
func GetAllDeptManagers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
page, err := readInt(r, "page", 1) | ||
if err != nil || page < 1 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
pagesize, err := readInt(r, "pagesize", 20) | ||
if err != nil || pagesize <= 0 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
offset := (page - 1) * pagesize | ||
|
||
order := r.FormValue("order") | ||
|
||
deptmanagers := []*model.DeptManager{} | ||
|
||
if order != "" { | ||
err = DB.Model(&model.DeptManager{}).Order(order).Offset(offset).Limit(pagesize).Find(&deptmanagers).Error | ||
} else { | ||
err = DB.Model(&model.DeptManager{}).Offset(offset).Limit(pagesize).Find(&deptmanagers).Error | ||
} | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, &deptmanagers) | ||
} | ||
|
||
func GetDeptManager(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
deptmanager := &model.DeptManager{} | ||
if DB.First(deptmanager, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
writeJSON(w, deptmanager) | ||
} | ||
|
||
func AddDeptManager(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
deptmanager := &model.DeptManager{} | ||
|
||
if err := readJSON(r, deptmanager); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err := DB.Save(deptmanager).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, deptmanager) | ||
} | ||
|
||
func UpdateDeptManager(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
|
||
deptmanager := &model.DeptManager{} | ||
if DB.First(deptmanager, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
updated := &model.DeptManager{} | ||
if err := readJSON(r, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := dbmeta.Copy(deptmanager, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := DB.Save(deptmanager).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, deptmanager) | ||
} | ||
|
||
func DeleteDeptManager(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
deptmanager := &model.DeptManager{} | ||
|
||
if DB.First(deptmanager, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
if err := DB.Delete(deptmanager).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"generated/model" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/smallnest/gen/dbmeta" | ||
) | ||
|
||
func configEmployeesRouter(router *httprouter.Router) { | ||
router.GET("/employees", GetAllEmployees) | ||
router.POST("/employees", AddEmployee) | ||
router.GET("/employees/:id", GetEmployee) | ||
router.PUT("/employees/:id", UpdateEmployee) | ||
router.DELETE("/employees/:id", DeleteEmployee) | ||
} | ||
|
||
func GetAllEmployees(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
page, err := readInt(r, "page", 1) | ||
if err != nil || page < 1 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
pagesize, err := readInt(r, "pagesize", 20) | ||
if err != nil || pagesize <= 0 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
offset := (page - 1) * pagesize | ||
|
||
order := r.FormValue("order") | ||
|
||
employees := []*model.Employee{} | ||
|
||
if order != "" { | ||
err = DB.Model(&model.Employee{}).Order(order).Offset(offset).Limit(pagesize).Find(&employees).Error | ||
} else { | ||
err = DB.Model(&model.Employee{}).Offset(offset).Limit(pagesize).Find(&employees).Error | ||
} | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, &employees) | ||
} | ||
|
||
func GetEmployee(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
employee := &model.Employee{} | ||
if DB.First(employee, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
writeJSON(w, employee) | ||
} | ||
|
||
func AddEmployee(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
employee := &model.Employee{} | ||
|
||
if err := readJSON(r, employee); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err := DB.Save(employee).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, employee) | ||
} | ||
|
||
func UpdateEmployee(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
|
||
employee := &model.Employee{} | ||
if DB.First(employee, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
updated := &model.Employee{} | ||
if err := readJSON(r, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := dbmeta.Copy(employee, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := DB.Save(employee).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, employee) | ||
} | ||
|
||
func DeleteEmployee(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
employee := &model.Employee{} | ||
|
||
if DB.First(employee, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
if err := DB.Delete(employee).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"generated/model" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/smallnest/gen/dbmeta" | ||
) | ||
|
||
func configExamplesRouter(router *httprouter.Router) { | ||
router.GET("/examples", GetAllExamples) | ||
router.POST("/examples", AddExample) | ||
router.GET("/examples/:id", GetExample) | ||
router.PUT("/examples/:id", UpdateExample) | ||
router.DELETE("/examples/:id", DeleteExample) | ||
} | ||
|
||
func GetAllExamples(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
page, err := readInt(r, "page", 1) | ||
if err != nil || page < 1 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
pagesize, err := readInt(r, "pagesize", 20) | ||
if err != nil || pagesize <= 0 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
offset := (page - 1) * pagesize | ||
|
||
order := r.FormValue("order") | ||
|
||
examples := []*model.Example{} | ||
|
||
if order != "" { | ||
err = DB.Model(&model.Example{}).Order(order).Offset(offset).Limit(pagesize).Find(&examples).Error | ||
} else { | ||
err = DB.Model(&model.Example{}).Offset(offset).Limit(pagesize).Find(&examples).Error | ||
} | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, &examples) | ||
} | ||
|
||
func GetExample(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
example := &model.Example{} | ||
if DB.First(example, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
writeJSON(w, example) | ||
} | ||
|
||
func AddExample(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
example := &model.Example{} | ||
|
||
if err := readJSON(r, example); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err := DB.Save(example).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, example) | ||
} | ||
|
||
func UpdateExample(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
|
||
example := &model.Example{} | ||
if DB.First(example, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
updated := &model.Example{} | ||
if err := readJSON(r, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := dbmeta.Copy(example, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := DB.Save(example).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, example) | ||
} | ||
|
||
func DeleteExample(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
example := &model.Example{} | ||
|
||
if DB.First(example, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
if err := DB.Delete(example).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/jinzhu/gorm" | ||
"github.com/julienschmidt/httprouter" | ||
) | ||
|
||
// example for init the database: | ||
// | ||
// DB, err := gorm.Open("mysql", "root@tcp(127.0.0.1:3306)/employees?charset=utf8&parseTime=true") | ||
// if err != nil { | ||
// panic("failed to connect database: " + err.Error()) | ||
// } | ||
// defer db.Close() | ||
|
||
var DB *gorm.DB | ||
|
||
func ConfigRouter() http.Handler { | ||
router := httprouter.New() | ||
configDepartmentsRouter(router) | ||
configDeptEmpsRouter(router) | ||
configDeptManagersRouter(router) | ||
configEmployeesRouter(router) | ||
configExamplesRouter(router) | ||
configSalariesRouter(router) | ||
configTitlesRouter(router) | ||
|
||
return router | ||
} | ||
|
||
func readInt(r *http.Request, param string, v int64) (int64, error) { | ||
p := r.FormValue(param) | ||
if p == "" { | ||
return v, nil | ||
} | ||
return strconv.ParseInt(p, 10, 64) | ||
} | ||
|
||
func writeJSON(w http.ResponseWriter, v interface{}) { | ||
data, _ := json.Marshal(v) | ||
w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
w.Header().Set("Cache-Control", "no-cache") | ||
w.Write(data) | ||
} | ||
|
||
func readJSON(r *http.Request, v interface{}) error { | ||
buf, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
return err | ||
} | ||
return json.Unmarshal(buf, v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"generated/model" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/smallnest/gen/dbmeta" | ||
) | ||
|
||
func configSalariesRouter(router *httprouter.Router) { | ||
router.GET("/salaries", GetAllSalaries) | ||
router.POST("/salaries", AddSalary) | ||
router.GET("/salaries/:id", GetSalary) | ||
router.PUT("/salaries/:id", UpdateSalary) | ||
router.DELETE("/salaries/:id", DeleteSalary) | ||
} | ||
|
||
func GetAllSalaries(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
page, err := readInt(r, "page", 1) | ||
if err != nil || page < 1 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
pagesize, err := readInt(r, "pagesize", 20) | ||
if err != nil || pagesize <= 0 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
offset := (page - 1) * pagesize | ||
|
||
order := r.FormValue("order") | ||
|
||
salaries := []*model.Salary{} | ||
|
||
if order != "" { | ||
err = DB.Model(&model.Salary{}).Order(order).Offset(offset).Limit(pagesize).Find(&salaries).Error | ||
} else { | ||
err = DB.Model(&model.Salary{}).Offset(offset).Limit(pagesize).Find(&salaries).Error | ||
} | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, &salaries) | ||
} | ||
|
||
func GetSalary(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
salary := &model.Salary{} | ||
if DB.First(salary, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
writeJSON(w, salary) | ||
} | ||
|
||
func AddSalary(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
salary := &model.Salary{} | ||
|
||
if err := readJSON(r, salary); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err := DB.Save(salary).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, salary) | ||
} | ||
|
||
func UpdateSalary(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
|
||
salary := &model.Salary{} | ||
if DB.First(salary, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
updated := &model.Salary{} | ||
if err := readJSON(r, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := dbmeta.Copy(salary, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := DB.Save(salary).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, salary) | ||
} | ||
|
||
func DeleteSalary(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
salary := &model.Salary{} | ||
|
||
if DB.First(salary, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
if err := DB.Delete(salary).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"generated/model" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/smallnest/gen/dbmeta" | ||
) | ||
|
||
func configTitlesRouter(router *httprouter.Router) { | ||
router.GET("/titles", GetAllTitles) | ||
router.POST("/titles", AddTitle) | ||
router.GET("/titles/:id", GetTitle) | ||
router.PUT("/titles/:id", UpdateTitle) | ||
router.DELETE("/titles/:id", DeleteTitle) | ||
} | ||
|
||
func GetAllTitles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
page, err := readInt(r, "page", 1) | ||
if err != nil || page < 1 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
pagesize, err := readInt(r, "pagesize", 20) | ||
if err != nil || pagesize <= 0 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
offset := (page - 1) * pagesize | ||
|
||
order := r.FormValue("order") | ||
|
||
titles := []*model.Title{} | ||
|
||
if order != "" { | ||
err = DB.Model(&model.Title{}).Order(order).Offset(offset).Limit(pagesize).Find(&titles).Error | ||
} else { | ||
err = DB.Model(&model.Title{}).Offset(offset).Limit(pagesize).Find(&titles).Error | ||
} | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, &titles) | ||
} | ||
|
||
func GetTitle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
title := &model.Title{} | ||
if DB.First(title, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
writeJSON(w, title) | ||
} | ||
|
||
func AddTitle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
title := &model.Title{} | ||
|
||
if err := readJSON(r, title); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err := DB.Save(title).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, title) | ||
} | ||
|
||
func UpdateTitle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
|
||
title := &model.Title{} | ||
if DB.First(title, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
updated := &model.Title{} | ||
if err := readJSON(r, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := dbmeta.Copy(title, updated); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := DB.Save(title).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writeJSON(w, title) | ||
} | ||
|
||
func DeleteTitle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
id := ps.ByName("id") | ||
title := &model.Title{} | ||
|
||
if DB.First(title, id).Error != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
if err := DB.Delete(title).Error; err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/guregu/null" | ||
) | ||
|
||
var ( | ||
_ = time.Second | ||
_ = sql.LevelDefault | ||
_ = null.Bool{} | ||
) | ||
|
||
type Department struct { | ||
DeptNo string `gorm:"column:dept_no;primary_key" json:"dept_no"` | ||
DeptName string `gorm:"column:dept_name" json:"dept_name"` | ||
} | ||
|
||
// TableName sets the insert table name for this struct type | ||
func (d *Department) TableName() string { | ||
return "departments" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/guregu/null" | ||
) | ||
|
||
var ( | ||
_ = time.Second | ||
_ = sql.LevelDefault | ||
_ = null.Bool{} | ||
) | ||
|
||
type DeptEmp struct { | ||
EmpNo int `gorm:"column:emp_no;primary_key" json:"emp_no"` | ||
DeptNo string `gorm:"column:dept_no" json:"dept_no"` | ||
FromDate time.Time `gorm:"column:from_date" json:"from_date"` | ||
ToDate time.Time `gorm:"column:to_date" json:"to_date"` | ||
} | ||
|
||
// TableName sets the insert table name for this struct type | ||
func (d *DeptEmp) TableName() string { | ||
return "dept_emp" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/guregu/null" | ||
) | ||
|
||
var ( | ||
_ = time.Second | ||
_ = sql.LevelDefault | ||
_ = null.Bool{} | ||
) | ||
|
||
type DeptManager struct { | ||
EmpNo int `gorm:"column:emp_no;primary_key" json:"emp_no"` | ||
DeptNo string `gorm:"column:dept_no" json:"dept_no"` | ||
FromDate time.Time `gorm:"column:from_date" json:"from_date"` | ||
ToDate time.Time `gorm:"column:to_date" json:"to_date"` | ||
} | ||
|
||
// TableName sets the insert table name for this struct type | ||
func (d *DeptManager) TableName() string { | ||
return "dept_manager" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/guregu/null" | ||
) | ||
|
||
var ( | ||
_ = time.Second | ||
_ = sql.LevelDefault | ||
_ = null.Bool{} | ||
) | ||
|
||
type Employee struct { | ||
EmpNo int `gorm:"column:emp_no;primary_key" json:"emp_no"` | ||
BirthDate time.Time `gorm:"column:birth_date" json:"birth_date"` | ||
FirstName string `gorm:"column:first_name" json:"first_name"` | ||
LastName string `gorm:"column:last_name" json:"last_name"` | ||
Gender string `gorm:"column:gender" json:"gender"` | ||
HireDate time.Time `gorm:"column:hire_date" json:"hire_date"` | ||
} | ||
|
||
// TableName sets the insert table name for this struct type | ||
func (e *Employee) TableName() string { | ||
return "employees" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/guregu/null" | ||
) | ||
|
||
var ( | ||
_ = time.Second | ||
_ = sql.LevelDefault | ||
_ = null.Bool{} | ||
) | ||
|
||
type Example struct { | ||
ID int `gorm:"column:id;primary_key" json:"id"` | ||
C1 null.String `gorm:"column:c1" json:"c1"` | ||
C2 null.String `gorm:"column:c2" json:"c2"` | ||
C3 null.Int `gorm:"column:c3" json:"c3"` | ||
C4 null.Time `gorm:"column:c4" json:"c4"` | ||
C5 null.String `gorm:"column:c5" json:"c5"` | ||
} | ||
|
||
// TableName sets the insert table name for this struct type | ||
func (e *Example) TableName() string { | ||
return "example" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/guregu/null" | ||
) | ||
|
||
var ( | ||
_ = time.Second | ||
_ = sql.LevelDefault | ||
_ = null.Bool{} | ||
) | ||
|
||
type Salary struct { | ||
EmpNo int `gorm:"column:emp_no;primary_key" json:"emp_no"` | ||
Salary int `gorm:"column:salary" json:"salary"` | ||
FromDate time.Time `gorm:"column:from_date" json:"from_date"` | ||
ToDate time.Time `gorm:"column:to_date" json:"to_date"` | ||
} | ||
|
||
// TableName sets the insert table name for this struct type | ||
func (s *Salary) TableName() string { | ||
return "salaries" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/guregu/null" | ||
) | ||
|
||
var ( | ||
_ = time.Second | ||
_ = sql.LevelDefault | ||
_ = null.Bool{} | ||
) | ||
|
||
type Title struct { | ||
EmpNo int `gorm:"column:emp_no;primary_key" json:"emp_no"` | ||
Title string `gorm:"column:title" json:"title"` | ||
FromDate time.Time `gorm:"column:from_date" json:"from_date"` | ||
ToDate null.Time `gorm:"column:to_date" json:"to_date"` | ||
} | ||
|
||
// TableName sets the insert table name for this struct type | ||
func (t *Title) TableName() string { | ||
return "titles" | ||
} |