Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
git-lab-remote2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
large-scale-dev
git-lab-remote2
Commits
4637f9df
Commit
4637f9df
authored
9 years ago
by
Simon Lindblad
Browse files
Options
Downloads
Patches
Plain Diff
Document code
parent
17e287d0
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
todo.go
+22
-0
22 additions, 0 deletions
todo.go
with
22 additions
and
0 deletions
todo.go
+
22
−
0
View file @
4637f9df
...
@@ -48,6 +48,9 @@ type Database struct {
...
@@ -48,6 +48,9 @@ type Database struct {
}
}
// getURLParameter takes out the first URL parameter from the path
// path should be formated as /type/param.
// It returns a parameter representing a string.
func
getURLParameter
(
path
string
)
*
string
{
func
getURLParameter
(
path
string
)
*
string
{
param
:=
strings
.
Split
(
path
,
"/"
)
param
:=
strings
.
Split
(
path
,
"/"
)
if
len
(
param
)
==
3
{
if
len
(
param
)
==
3
{
...
@@ -57,6 +60,8 @@ func getURLParameter(path string) *string {
...
@@ -57,6 +60,8 @@ func getURLParameter(path string) *string {
}
}
}
}
// getLists retrieves all the lists from the database.
// It returns a slice of List structs.
func
getLists
(
db
*
sql
.
DB
)
[]
List
{
func
getLists
(
db
*
sql
.
DB
)
[]
List
{
rows
,
err
:=
db
.
Query
(
"select * from list"
)
rows
,
err
:=
db
.
Query
(
"select * from list"
)
CheckFatal
(
err
)
CheckFatal
(
err
)
...
@@ -73,6 +78,8 @@ func getLists(db *sql.DB) []List {
...
@@ -73,6 +78,8 @@ func getLists(db *sql.DB) []List {
return
res
return
res
}
}
// getTasks retrieves all the tasks from the database.
// It returns a slice of Task structs.
func
getTasks
(
db
*
sql
.
DB
,
listId
int
)
[]
Task
{
func
getTasks
(
db
*
sql
.
DB
,
listId
int
)
[]
Task
{
// Query the database for all tasks that references the specified list
// Query the database for all tasks that references the specified list
rows
,
err
:=
db
.
Query
(
"select * from task where list=$1"
,
listId
)
rows
,
err
:=
db
.
Query
(
"select * from task where list=$1"
,
listId
)
...
@@ -92,6 +99,8 @@ func getTasks(db *sql.DB, listId int) []Task {
...
@@ -92,6 +99,8 @@ func getTasks(db *sql.DB, listId int) []Task {
return
res
return
res
}
}
// insertList adds a list to the database with listName as its name.
// It returns the Id of the list.
func
insertList
(
db
*
sql
.
DB
,
listName
string
)
int
{
func
insertList
(
db
*
sql
.
DB
,
listName
string
)
int
{
var
listId
int
var
listId
int
err
:=
db
.
QueryRow
(
"insert into list (name) values ($1) returning id"
,
listName
)
.
Scan
(
&
listId
)
err
:=
db
.
QueryRow
(
"insert into list (name) values ($1) returning id"
,
listName
)
.
Scan
(
&
listId
)
...
@@ -100,12 +109,19 @@ func insertList(db *sql.DB, listName string) int {
...
@@ -100,12 +109,19 @@ func insertList(db *sql.DB, listName string) int {
return
listId
return
listId
}
}
// insertTask adds a task to the database.
// taskName specifies the name of the task, and listId the list that it belongs to.
func
insertTask
(
db
*
sql
.
DB
,
taskName
string
,
listId
int
)
{
func
insertTask
(
db
*
sql
.
DB
,
taskName
string
,
listId
int
)
{
_
,
err
:=
db
.
Exec
(
"insert into task (name, list) values ($1, $2)"
,
taskName
,
listId
)
_
,
err
:=
db
.
Exec
(
"insert into task (name, list) values ($1, $2)"
,
taskName
,
listId
)
// Handle non-existing list id
// Handle non-existing list id
CheckFatal
(
err
)
CheckFatal
(
err
)
}
}
// listHandler manages requests with regards to the lists.
// A GET request to /list will retrieve all the lists.
// A GET request to /list/<id> will retrieve all the tasks of the list with id <id>.
// A POST request to /list will create a new list with the name provided in the Post Body
// in the format {"name": "listName"}
func
(
db
*
Database
)
listHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
db
*
Database
)
listHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Method
==
"GET"
{
if
r
.
Method
==
"GET"
{
// Handle GET Request
// Handle GET Request
...
@@ -140,6 +156,9 @@ func (db *Database) listHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -140,6 +156,9 @@ func (db *Database) listHandler(w http.ResponseWriter, r *http.Request) {
}
}
}
}
// taskHandler manages requests with regards to the tasks.
// A POST request to /task will create a new task with the name and list provided in
// the Post Body. The Body should be in the format {"name": "taskName", "list_id": 123}
func
(
db
*
Database
)
taskHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
db
*
Database
)
taskHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Method
==
"POST"
{
if
r
.
Method
==
"POST"
{
body
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
body
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
...
@@ -154,6 +173,8 @@ func (db *Database) taskHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -154,6 +173,8 @@ func (db *Database) taskHandler(w http.ResponseWriter, r *http.Request) {
}
}
}
}
// ConnectDb connects to a postgres database.
// it returns a database handle
func
ConnectDb
()
*
sql
.
DB
{
func
ConnectDb
()
*
sql
.
DB
{
db
,
err
:=
sql
.
Open
(
"postgres"
,
"postgres://simon@localhost/todo?sslmode=disable"
)
db
,
err
:=
sql
.
Open
(
"postgres"
,
"postgres://simon@localhost/todo?sslmode=disable"
)
CheckFatal
(
err
)
CheckFatal
(
err
)
...
@@ -161,6 +182,7 @@ func ConnectDb() *sql.DB {
...
@@ -161,6 +182,7 @@ func ConnectDb() *sql.DB {
return
db
return
db
}
}
// Handlers retrieves all handlers for the server.
func
Handlers
()
*
http
.
ServeMux
{
func
Handlers
()
*
http
.
ServeMux
{
db
:=
Database
{
Db
:
ConnectDb
()}
db
:=
Database
{
Db
:
ConnectDb
()}
mux
:=
http
.
NewServeMux
()
mux
:=
http
.
NewServeMux
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment