to show edit task thing

This commit is contained in:
Suraj 2016-02-05 00:06:00 +05:30
parent 3113502041
commit 1752d74589
4 changed files with 38 additions and 7 deletions

View File

@ -118,12 +118,12 @@ func GetTaskByID(id int) (types.Context, error) {
var tasks []types.Task var tasks []types.Task
var task types.Task var task types.Task
getTasksql := "select id, title, content, priority from task where id=?" getTasksql := "select t.id, t.title, t.content, t.priority, c.name from task t left outer join category c where c.id = t.cat_id and t.id=?"
rows := database.query(getTasksql, id) rows := database.query(getTasksql, id)
defer rows.Close() defer rows.Close()
if rows.Next() { if rows.Next() {
err := rows.Scan(&task.Id, &task.Title, &task.Content, &task.Priority) err := rows.Scan(&task.Id, &task.Title, &task.Content, &task.Priority, &task.Category)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
//send email to respective people //send email to respective people

View File

@ -4,23 +4,30 @@
<div class="modal-header"> <div class="modal-header">
<h4 class="modal-title" id="newNoteLabel"><span class="glyphicon glyphicon-pencil"></span> Edit Task</h4> <h4 class="modal-title" id="newNoteLabel"><span class="glyphicon glyphicon-pencil"></span> Edit Task</h4>
</div> </div>
{{range .Tasks}} {{ $categories := .Categories }}
{{range $index, $task := .Tasks}}
<div class="modal-body"> <div class="modal-body">
<form action="/update/" method="POST"> <form action="/update/" method="POST">
<div class="form-group"> <div class="form-group">
<input type="text" name="title" value="{{.Title}}" class="form-control" id="add-note-title" placeholder="Title" style="border:none;border-bottom:1px solid gray; box-shadow:none;"> <input type="text" name="title" value="{{ $task.Title}}" class="form-control" id="add-note-title" placeholder="Title" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
</div> </div>
<div class="form-group"> <div class="form-group">
<textarea class="form-control" name="content" id="add-note-content" placeholder="Content" <textarea class="form-control" name="content" id="add-note-content" placeholder="Content"
rows="10" style="border:none;border-bottom:1px solid gray; box-shadow:none;">{{.Content}}</textarea> rows="10" style="border:none;border-bottom:1px solid gray; box-shadow:none;">{{ $task.Content}}</textarea>
<input type="text" name="id" value="{{.Id}}" class="hidden" /> <input type="text" name="id" value="{{.Id}}" class="hidden" />
Priority: Priority:
<input type="radio" name="priority" value="3" {{if eq .Priority "3"}} checked="checked" {{end}} /> High <input type="radio" name="priority" value="3" {{if eq .Priority "3"}} checked="checked" {{end}} /> High
<input type="radio" name="priority" value="2" {{if eq .Priority "2"}} checked="checked" {{end}} /> Medium <input type="radio" name="priority" value="2" {{if eq .Priority "2"}} checked="checked" {{end}} /> Medium
<input type="radio" name="priority" value="1" {{if eq .Priority "1"}} checked="checked" {{end}} /> Low <input type="radio" name="priority" value="1" {{if eq .Priority "1"}} checked="checked" {{end}} /> Low
</select>
</div> </div>
Category:
<select name="category">
<option>---</option>
{{range $cat := $categories}}
<option value="{{$cat}}" {{if eq $cat $task.Category}} selected="true" {{end}}> {{$cat}} </option>
{{end}}
</select>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

View File

@ -11,6 +11,7 @@ type Task struct {
Content string Content string
Created string Created string
Priority string Priority string
Category string
} }
//Context is the struct passed to templates //Context is the struct passed to templates

View File

@ -133,3 +133,26 @@ func AddCategoryFunc(w http.ResponseWriter, r *http.Request) {
} }
} }
} }
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil {
log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
} else {
task, err := db.GetTaskByID(id)
categories := db.GetCategories()
task.Categories = categories
if err != nil {
task.Message = "Error fetching Tasks"
}
editTemplate.Execute(w, task)
}
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
}
}