Last active
October 5, 2016 21:01
-
-
Save steventen/d868ac7abea79f5aab64e0b32aa0bb50 to your computer and use it in GitHub Desktop.
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
func GetCompany(db *sql.DB, key string) (Company, int) { | |
var company_id int | |
var api_key string | |
err := db.QueryRow("select id, api from companies where api = ? limit 1", key).Scan(&company_id, &api_key) | |
switch { | |
case err == sql.ErrNoRows: | |
return Company{}, 0 | |
case err != nil: | |
fmt.Println(err) | |
return Company{}, -1 | |
default: | |
return Company{company_id, api_key}, company_id | |
} | |
} | |
func GetProject(db *sql.DB, company_id int, project_id int) (Project, int) { | |
var ( | |
id int | |
name string | |
) | |
err := db.QueryRow("select id, name from projects where id = ? and company_id = ? limit 1", project_id, company_id).Scan(&id, &name) | |
switch { | |
case err == sql.ErrNoRows: | |
return Project{}, 0 | |
case err != nil: | |
fmt.Println(err) | |
return Project{}, -1 | |
default: | |
return Project{id, name}, id | |
} | |
} | |
func GetProjects(db *sql.DB, companyId int) []Project { | |
projects, err := db.Query("select id, name from projects where company_id = ?", companyId) | |
if err != nil { | |
fmt.Println(err) | |
} | |
var ( | |
id int | |
name string | |
) | |
p := make([]Project, 0) | |
defer projects.Close() | |
for projects.Next() { | |
err := projects.Scan(&id, &name) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
p = append(p, Project{id, name}) | |
} | |
} | |
return p | |
} | |
m.Get("/projects", func(current_company Company, r render.Render) { | |
projects := GetProjects(db, current_company.Id) | |
r.JSON(200, map[string]interface{}{"status": "Success", "data": projects}) | |
}) | |
m.Get("/projects/:id", func(current_company Company, params martini.Params, r render.Render) { | |
paramId, err := strconv.Atoi(params["id"]) | |
if err != nil { | |
r.JSON(404, map[string]interface{}{"status": "Fail", "error_message": err.Error()}) | |
return | |
} | |
project, id := GetProject(db, current_company.Id, paramId) | |
if id > 0 { | |
r.JSON(200, map[string]interface{}{"status": "Success", "data": project}) | |
} else { | |
r.JSON(404, map[string]interface{}{"status": "Fail", "error_message": "Project not found"}) | |
} | |
}) | |
m.Use(render.Renderer()) | |
m.Use(func(res http.ResponseWriter, req *http.Request, r render.Render) { | |
api_key := "" | |
api_key = req.URL.Query().Get("key") | |
if api_key == "" { | |
r.JSON(404, map[string]interface{}{"status": "Fail", "error_message": "Need api key"}) | |
} else { | |
current_company, company_id := GetCompany(db, api_key) | |
if company_id < 0 { | |
r.JSON(404, map[string]interface{}{"status": "Fail", "error_message": "Bad api key"}) | |
} else { | |
m.Map(current_company) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment