Last active
February 7, 2018 23:17
-
-
Save collinalexbell/6b5627be7250656561e2220be3df76bf 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
package main | |
import ( | |
"database/sql" | |
"github.com/gin-gonic/gin" | |
_ "github.com/go-sql-driver/mysql" | |
"net/http" | |
"strconv" | |
"todo-service/todo" | |
) | |
var db *sql.DB | |
var err error | |
func main() { | |
db, err = sql.Open("mysql", "slightlycyborg:foobar@/todo_service") | |
//Initialize the Todo Model | |
todo.AddDB(db) | |
router := gin.Default() | |
v1 := router.Group("/api/v1/todos") | |
{ | |
v1.POST("/", handleCreateTodoRequest) | |
v1.GET("/", handleFetchAllTodosRequest) | |
v1.GET("/:id", handleFetchSingleTodoRequest) | |
v1.PUT("/:id", handleUpdateTodoRequest) | |
v1.DELETE("/:id", handleDeleteTodoRequest) | |
} | |
router.Run() | |
} | |
func handleCreateTodoRequest(c *gin.Context) { | |
completed, _ := strconv.ParseBool(c.PostForm("completed")) | |
title := c.PostForm("title") | |
t := todo.New(title, completed) | |
response := gin.H{ | |
"status": http.StatusCreated, | |
"message": "Todo item created successfully!", | |
"resourceId": t.ID} | |
c.JSON(http.StatusCreated, response) | |
} | |
func handleFetchAllTodosRequest(c *gin.Context) { | |
todos := todo.All() | |
response := gin.H{ | |
"data": todos} | |
c.JSON(http.StatusFound, response) | |
} | |
func handleFetchSingleTodoRequest(c *gin.Context) { | |
id, _ := strconv.ParseInt(c.Param("id"), 10, 64) | |
todos := todo.ByID(id) | |
if len(todos) > 0 { | |
response := gin.H{ | |
"data": todos[0]} | |
c.JSON(http.StatusFound, response) | |
} | |
} | |
func handleUpdateTodoRequest(c *gin.Context) { | |
} | |
func handleDeleteTodoRequest(c *gin.Context) { | |
} |
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
package todo | |
import ( | |
"database/sql" | |
"fmt" | |
sq "github.com/Masterminds/squirrel" | |
"log" | |
) | |
var db *sql.DB = nil | |
func AddDB(db_ *sql.DB) { | |
db = db_ | |
} | |
type Todo struct { | |
ID int64 | |
Title string | |
Completed bool | |
} | |
//PUBLIC | |
func New(title string, completed bool) Todo { | |
if db == nil { | |
log.Fatal("Todo model not connected to DB. Implement another for of persistence perhaps?") | |
} | |
todo := Todo{ID: -1, Title: title, Completed: completed} | |
id := todo.persist() | |
todo.ID = id | |
return todo | |
} | |
func All() []Todo { | |
sql_str, args, _ := sq.Select("*").From("todos").ToSql() | |
rv := fromSQL(sql_str, args) | |
return rv | |
} | |
func ByID(id int64) []Todo { | |
sql_str, args, _ := sq.Select("*").From("todos").Where(sq.Eq{"ID": id}).ToSql() | |
rv := fromSQL(sql_str, args) | |
return rv | |
} | |
//PRIVATE | |
func (t Todo) persist() int64 { | |
query, args, _ := sq.Insert("todos"). | |
Columns("title", "completed"). | |
Values(t.Title, t.Completed).ToSql() | |
result, _ := db.Exec(query, args...) | |
id, _ := result.LastInsertId() | |
return id | |
} | |
//This FN could be moved to a DB utility. It could return a map. | |
//Then Todo would have a fromMap object that converts the map to an obj. | |
func fromSQL(sql_str string, args []interface{}) []Todo { | |
fmt.Println(args) | |
var rows *sql.Rows | |
var err error | |
rows, err = db.Query(sql_str, args...) | |
var rv []Todo | |
print(rows) | |
if err == nil { | |
fmt.Println(err) | |
} | |
defer rows.Close() | |
for rows.Next() { | |
var id int64 | |
var title string | |
var completed bool | |
err = rows.Scan(&id, &title, &completed) | |
rv = append(rv, Todo{ID: id, Title: title, Completed: completed}) | |
} | |
return rv | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment