Skip to content
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 2 commits into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/to-do-app/README.md
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
18 changes: 18 additions & 0 deletions examples/to-do-app/db/connector/connector.go
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)
Copy link
Contributor

@rakateja rakateja Oct 29, 2018

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 check err first. db could be nil if gorm.Open returns error.

Then db.SingularTable(true) can cause panic error if db is nil

Go: panic: runtime error: invalid memory address or nil pointer dereference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return db, err
}
19 changes: 19 additions & 0 deletions examples/to-do-app/db/migration/migration.go
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
}
11 changes: 11 additions & 0 deletions examples/to-do-app/db/model/task.go
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"`
}
86 changes: 86 additions & 0 deletions examples/to-do-app/db/repository/task/task.go
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
}
133 changes: 133 additions & 0 deletions examples/to-do-app/go-todo-app.postman_collection.json
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": []
}
]
}
Loading