formatted using gofmt

This commit is contained in:
thewhitetulip 2015-11-13 18:41:30 +05:30
parent e97b19237b
commit 59ab7953e5
3 changed files with 46 additions and 46 deletions

View File

@ -25,36 +25,36 @@ func Close() {
database.Close() database.Close()
} }
func GetTasks(deleted bool) []types.Task{ func GetTasks(deleted bool) []types.Task {
var task []types.Task var task []types.Task
var TaskId int var TaskId int
var TaskTitle string var TaskTitle string
var TaskContent string var TaskContent string
var getTasksql string var getTasksql string
if deleted == true{ if deleted == true {
getTasksql = "select id, title, content from task where is_deleted!='Y' order by created_date asc" getTasksql = "select id, title, content from task where is_deleted!='Y' order by created_date asc"
} else { } else {
getTasksql = "select id, title, content from task where is_deleted='Y' order by created_date asc" getTasksql = "select id, title, content from task where is_deleted='Y' order by created_date asc"
} }
rows, err := database.Query(getTasksql) rows, err := database.Query(getTasksql)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
defer rows.Close() defer rows.Close()
for rows.Next(){ for rows.Next() {
err := rows.Scan(&TaskId, &TaskTitle, &TaskContent) err := rows.Scan(&TaskId, &TaskTitle, &TaskContent)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
a := types.Task{Id:TaskId, Title:TaskTitle, Content:TaskContent} a := types.Task{Id: TaskId, Title: TaskTitle, Content: TaskContent}
task = append(task, a) task = append(task, a)
} }
return task return task
} }
func GetTaskById(id int) types.Task{ func GetTaskById(id int) types.Task {
var task types.Task var task types.Task
var TaskId int var TaskId int
var TaskTitle string var TaskTitle string
@ -62,31 +62,31 @@ func GetTaskById(id int) types.Task{
getTasksql := "select id, title, content from task where id=?" getTasksql := "select id, title, content from task where id=?"
rows, err := database.Query(getTasksql, id) rows, err := database.Query(getTasksql, id)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
defer rows.Close() defer rows.Close()
if rows.Next(){ if rows.Next() {
err := rows.Scan(&TaskId, &TaskTitle, &TaskContent) err := rows.Scan(&TaskId, &TaskTitle, &TaskContent)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
task = types.Task{Id:TaskId, Title:TaskTitle, Content:TaskContent} task = types.Task{Id: TaskId, Title: TaskTitle, Content: TaskContent}
} }
return task return task
} }
func ArchiveTask(id int) error { func ArchiveTask(id int) error {
stmt, err := database.Prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?") stmt, err := database.Prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?")
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
tx, err := database.Begin() tx, err := database.Begin()
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
_, err = tx.Stmt(stmt).Exec(id) _, err = tx.Stmt(stmt).Exec(id)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
tx.Rollback() tx.Rollback()
} else { } else {
@ -97,15 +97,15 @@ func ArchiveTask(id int) error {
func DeleteAll() error { func DeleteAll() error {
stmt, err := database.Prepare("delete from task where is_deleted='Y'") stmt, err := database.Prepare("delete from task where is_deleted='Y'")
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
tx, err := database.Begin() tx, err := database.Begin()
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
_, err = tx.Stmt(stmt).Exec() _, err = tx.Stmt(stmt).Exec()
if err!=nil{ if err != nil {
fmt.Println("doing rollback") fmt.Println("doing rollback")
tx.Rollback() tx.Rollback()
} else { } else {
@ -116,15 +116,15 @@ func DeleteAll() error {
func RestoreTask(id int) error { func RestoreTask(id int) error {
restoreSql, err := database.Prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?") restoreSql, err := database.Prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?")
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
tx, err := database.Begin() tx, err := database.Begin()
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
_, err = tx.Stmt(restoreSql).Exec(id) _, err = tx.Stmt(restoreSql).Exec(id)
if err!=nil{ if err != nil {
fmt.Println("doing rollback") fmt.Println("doing rollback")
tx.Rollback() tx.Rollback()
} else { } else {
@ -135,15 +135,15 @@ func RestoreTask(id int) error {
func DeleteTask(id int) error { func DeleteTask(id int) error {
deleteSQL, err := database.Prepare("delete from task where id = ?") deleteSQL, err := database.Prepare("delete from task where id = ?")
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
tx, err := database.Begin() tx, err := database.Begin()
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
_, err = tx.Stmt(deleteSQL).Exec(id) _, err = tx.Stmt(deleteSQL).Exec(id)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
tx.Rollback() tx.Rollback()
} else { } else {
@ -154,12 +154,12 @@ func DeleteTask(id int) error {
func AddTask(title, content string) error { func AddTask(title, content string) error {
restoreSql, err := database.Prepare("insert into task(title, content, created_date, last_modified_at) values(?,?,datetime(), datetime())") restoreSql, err := database.Prepare("insert into task(title, content, created_date, last_modified_at) values(?,?,datetime(), datetime())")
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
tx, err := database.Begin() tx, err := database.Begin()
_, err = tx.Stmt(restoreSql).Exec(title, content) _, err = tx.Stmt(restoreSql).Exec(title, content)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
tx.Rollback() tx.Rollback()
} else { } else {
@ -168,18 +168,18 @@ func AddTask(title, content string) error {
return err return err
} }
func UpdateTask(id int, title string, content string) error{ func UpdateTask(id int, title string, content string) error {
Sql, err := database.Prepare("update task set title=?, content=? where id=?") Sql, err := database.Prepare("update task set title=?, content=? where id=?")
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
tx, err := database.Begin() tx, err := database.Begin()
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
_, err = tx.Stmt(Sql).Exec(title, content, id) _, err = tx.Stmt(Sql).Exec(title, content, id)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
tx.Rollback() tx.Rollback()
} else { } else {
@ -188,25 +188,25 @@ func UpdateTask(id int, title string, content string) error{
return err return err
} }
func SearchTask(query string) []types.Task{ func SearchTask(query string) []types.Task {
stmt := "select id, title, content from task where title like '%"+query+"%' or content like '%"+query+"%'" stmt := "select id, title, content from task where title like '%" + query + "%' or content like '%" + query + "%'"
var task []types.Task var task []types.Task
var TaskId int var TaskId int
var TaskTitle string var TaskTitle string
var TaskContent string var TaskContent string
rows, err := database.Query(stmt, query, query) rows, err := database.Query(stmt, query, query)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
for rows.Next(){ for rows.Next() {
err := rows.Scan(&TaskId, &TaskTitle, &TaskContent) err := rows.Scan(&TaskId, &TaskTitle, &TaskContent)
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
TaskTitle = strings.Replace(TaskTitle, query, "<span class='highlight'>"+query+"</span>", -1) TaskTitle = strings.Replace(TaskTitle, query, "<span class='highlight'>"+query+"</span>", -1)
TaskContent = strings.Replace(TaskContent, query, "<span class='highlight'>"+query+"</span>", -1) TaskContent = strings.Replace(TaskContent, query, "<span class='highlight'>"+query+"</span>", -1)
a := types.Task{Id:TaskId, Title:TaskTitle, Content:TaskContent} a := types.Task{Id: TaskId, Title: TaskTitle, Content: TaskContent}
task = append(task, a) task = append(task, a)
} }
return task return task

View File

@ -59,7 +59,7 @@ func ShowAllTasks(w http.ResponseWriter, r *http.Request, parm httprouter.Params
homeTemplate.Execute(w, context) homeTemplate.Execute(w, context)
} }
func SearchTask(w http.ResponseWriter, r *http.Request, _ httprouter.Params){ func SearchTask(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
r.ParseForm() r.ParseForm()
query := r.Form.Get("query") query := r.Form.Get("query")
context := viewmodels.SearchTask(query) context := viewmodels.SearchTask(query)
@ -83,7 +83,7 @@ func ShowTrashTask(w http.ResponseWriter, r *http.Request, parm httprouter.Param
func EditTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) { func EditTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
id, err := strconv.Atoi(param.ByName("id")) id, err := strconv.Atoi(param.ByName("id"))
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} else { } else {
task := viewmodels.GetTaskById(id) task := viewmodels.GetTaskById(id)
@ -103,7 +103,7 @@ func ArchiveTask(w http.ResponseWriter, r *http.Request, param httprouter.Params
func DeleteTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) { func DeleteTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
id := param.ByName("id") id := param.ByName("id")
if id == "all"{ if id == "all" {
viewmodels.DeleteAll() viewmodels.DeleteAll()
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} else { } else {
@ -130,7 +130,7 @@ func RestoreTask(w http.ResponseWriter, r *http.Request, param httprouter.Params
func UpdateTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) { func UpdateTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
r.ParseForm() r.ParseForm()
id, err := strconv.Atoi(r.Form.Get("id")) id, err := strconv.Atoi(r.Form.Get("id"))
if err!=nil{ if err != nil {
fmt.Println(err) fmt.Println(err)
} }
title := r.Form.Get("title") title := r.Form.Get("title")

View File

@ -9,13 +9,13 @@ func GetTasks(deleted bool) []types.Task {
return db.GetTasks(deleted) return db.GetTasks(deleted)
} }
func SearchTask(query string) []types.Task{ func SearchTask(query string) []types.Task {
return db.SearchTask(query) return db.SearchTask(query)
} }
func AddTask(title, content string) bool { func AddTask(title, content string) bool {
err := db.AddTask(title, content) err := db.AddTask(title, content)
if err!=nil{ if err != nil {
return false return false
} }
return true return true
@ -37,9 +37,9 @@ func DeleteTask(id int) bool {
return true return true
} }
func DeleteAll() bool{ func DeleteAll() bool {
err := db.DeleteAll() err := db.DeleteAll()
if err!=nil{ if err != nil {
return false return false
} }
return true return true
@ -53,11 +53,11 @@ func ArchiveTask(id int) bool {
return true return true
} }
func GetTaskById(id int) types.Task{ func GetTaskById(id int) types.Task {
return db.GetTaskById(id) return db.GetTaskById(id)
} }
func UpdateTask(id int, title string, content string) bool{ func UpdateTask(id int, title string, content string) bool {
err := db.UpdateTask(id, title, content) err := db.UpdateTask(id, title, content)
if err != nil { if err != nil {
return false return false
@ -65,6 +65,6 @@ func UpdateTask(id int, title string, content string) bool{
return true return true
} }
func Close(){ func Close() {
db.Close() db.Close()
} }