-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add go todo app task #7
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
TODO App for BaliGophers | ||
============= | ||
Sample of TODO app simple CRUD as REST API | ||
|
||
How to use: | ||
-------- | ||
|
||
1. Run `go build` | ||
2. Import the postman collection and execute the endpoint. No Auth needed. | ||
|
||
TODO | ||
--- | ||
|
||
1. How to handle `error` in cleaner way | ||
2. Add log |
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,18 @@ | ||
package connector | ||
|
||
import ( | ||
"github.com/jinzhu/gorm" | ||
_ "github.com/jinzhu/gorm/dialects/mysql" | ||
) | ||
|
||
func Connect() (*gorm.DB, error) { | ||
db, err := gorm.Open("mysql", "root@/go-todo-app?charset=utf8&parseTime=True&loc=Local") | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
db.SingularTable(true) | ||
|
||
return db, err | ||
} |
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,19 @@ | ||
package migration | ||
|
||
import ( | ||
"github.com/gusdecool/backpack/examples/to-do-app/db/connector" | ||
"github.com/gusdecool/backpack/examples/to-do-app/db/model" | ||
) | ||
|
||
func Migrate() error { | ||
db, err := connector.Connect() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer db.Close() | ||
db.AutoMigrate(&model.Task{}) | ||
|
||
return nil | ||
} |
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,11 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Task struct { | ||
Id int `gorm:"AUTO_INCREMENT"` | ||
Name string `gorm:"type:text;not null"` | ||
CreatedAt time.Time `gorm:"type:timestamp;not null"` | ||
} |
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,86 @@ | ||
package task | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/gusdecool/backpack/examples/to-do-app/db/connector" | ||
"github.com/gusdecool/backpack/examples/to-do-app/db/model" | ||
) | ||
|
||
func FindAll() ([]model.Task, error) { | ||
db, err := connector.Connect() | ||
var tasks []model.Task | ||
|
||
if err != nil { | ||
return tasks, err | ||
} | ||
|
||
defer db.Close() | ||
db.Find(&tasks) | ||
|
||
return tasks, nil | ||
} | ||
|
||
func GetOneById(id int) (model.Task, error) { | ||
db, err := connector.Connect() | ||
var task model.Task | ||
|
||
if err != nil { | ||
return task, err | ||
} | ||
|
||
defer db.Close() | ||
|
||
if db.First(&task, id).RowsAffected == 0 { | ||
return task, errors.New(fmt.Sprintf("can't find task with id %d", id)) | ||
} | ||
|
||
return task, nil | ||
} | ||
|
||
func Create(task *model.Task) (*model.Task, error) { | ||
db, err := connector.Connect() | ||
|
||
if err != nil { | ||
return task, err | ||
} | ||
|
||
defer db.Close() | ||
|
||
if db.NewRecord(task) == false { | ||
return task, errors.New("primary key not blank") | ||
} | ||
|
||
db.Create(&task) | ||
|
||
return task, nil | ||
} | ||
|
||
func Update(task *model.Task) (*model.Task, error) { | ||
db, err := connector.Connect() | ||
|
||
if err != nil { | ||
return task, err | ||
} | ||
|
||
defer db.Close() | ||
err = db.Save(task).Error | ||
|
||
if err != nil { | ||
return task, err | ||
} | ||
|
||
return task, nil | ||
} | ||
|
||
func Delete(task *model.Task) error { | ||
db, err := connector.Connect() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer db.Close() | ||
|
||
return db.Delete(task).Error | ||
} |
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,133 @@ | ||
{ | ||
"info": { | ||
"_postman_id": "e9a72a8d-ec0d-4863-82e5-ce0cc571940f", | ||
"name": "go-todo-app", | ||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" | ||
}, | ||
"item": [ | ||
{ | ||
"name": "/task", | ||
"request": { | ||
"method": "GET", | ||
"header": [], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "" | ||
}, | ||
"url": { | ||
"raw": "http://127.0.0.1:8080/task", | ||
"protocol": "http", | ||
"host": [ | ||
"127", | ||
"0", | ||
"0", | ||
"1" | ||
], | ||
"port": "8080", | ||
"path": [ | ||
"task" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "/task", | ||
"request": { | ||
"method": "POST", | ||
"header": [ | ||
{ | ||
"key": "Content-Type", | ||
"value": "application/json", | ||
"type": "text" | ||
} | ||
], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "{\n\t\"name\": \"foo again\"\n}" | ||
}, | ||
"url": { | ||
"raw": "http://127.0.0.1:8080/task", | ||
"protocol": "http", | ||
"host": [ | ||
"127", | ||
"0", | ||
"0", | ||
"1" | ||
], | ||
"port": "8080", | ||
"path": [ | ||
"task" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "/task/{id}", | ||
"request": { | ||
"method": "PUT", | ||
"header": [ | ||
{ | ||
"key": "Content-Type", | ||
"type": "text", | ||
"value": "application/json" | ||
} | ||
], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "{\n\t\"name\": \"foo-1\"\n}" | ||
}, | ||
"url": { | ||
"raw": "http://127.0.0.1:8080/task/2", | ||
"protocol": "http", | ||
"host": [ | ||
"127", | ||
"0", | ||
"0", | ||
"1" | ||
], | ||
"port": "8080", | ||
"path": [ | ||
"task", | ||
"2" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "/task/{id}", | ||
"request": { | ||
"method": "DELETE", | ||
"header": [ | ||
{ | ||
"key": "Content-Type", | ||
"type": "text", | ||
"value": "application/json" | ||
} | ||
], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "" | ||
}, | ||
"url": { | ||
"raw": "http://127.0.0.1:8080/task/1", | ||
"protocol": "http", | ||
"host": [ | ||
"127", | ||
"0", | ||
"0", | ||
"1" | ||
], | ||
"port": "8080", | ||
"path": [ | ||
"task", | ||
"1" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before use
db
here, please checkerr
first.db
could be nil ifgorm.Open
returns error.Then
db.SingularTable(true)
can cause panic error ifdb
is nilThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i've added
err
check here https://github.com/bali-gophers/backpack/pull/7/files#diff-d15b797a03ae0fe42442c4308adfd7d4R11