forked from OrgGo/Tasks
Ability to add comments & a little HTML formatting
This commit is contained in:
parent
04640d0159
commit
ca9a110526
66
db/tasks.go
66
db/tasks.go
|
@ -68,16 +68,15 @@ func Close() {
|
|||
//GetTasks retrieves all the tasks depending on the
|
||||
//status pending or trashed or completed
|
||||
func GetTasks(status, category string) (types.Context, error) {
|
||||
var task []types.Task
|
||||
var context types.Context
|
||||
var TaskID int
|
||||
var TaskTitle string
|
||||
var TaskContent string
|
||||
var tasks []types.Task
|
||||
var task types.Task
|
||||
var TaskCreated time.Time
|
||||
var TaskPriority string
|
||||
var context types.Context
|
||||
var getTasksql string
|
||||
var rows *sql.Rows
|
||||
|
||||
comments := GetComments()
|
||||
|
||||
basicSQL := "select id, title, content, created_date, priority from task t"
|
||||
if status == "pending" && category == "" {
|
||||
getTasksql = basicSQL + " where finish_date is null and is_deleted='N' order by priority desc, created_date asc"
|
||||
|
@ -99,17 +98,24 @@ func GetTasks(status, category string) (types.Context, error) {
|
|||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated, &TaskPriority)
|
||||
TaskContent = string(md.Markdown([]byte(TaskContent)))
|
||||
task = types.Task{}
|
||||
err := rows.Scan(&task.Id, &task.Title, &task.Content, &TaskCreated, &task.Priority)
|
||||
task.Content = string(md.Markdown([]byte(task.Content)))
|
||||
// TaskContent = strings.Replace(TaskContent, "\n", "<br>", -1)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
TaskCreated = TaskCreated.Local()
|
||||
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20], Priority: TaskPriority}
|
||||
task = append(task, a)
|
||||
|
||||
if comments[task.Id] != nil {
|
||||
task.Comments = comments[task.Id]
|
||||
}
|
||||
context = types.Context{Tasks: task, Navigation: status}
|
||||
|
||||
TaskCreated = TaskCreated.Local()
|
||||
task.Created = TaskCreated.Format(time.UnixDate)[0:20]
|
||||
|
||||
tasks = append(tasks, task)
|
||||
}
|
||||
context = types.Context{Tasks: tasks, Navigation: status}
|
||||
return context, nil
|
||||
}
|
||||
|
||||
|
@ -248,3 +254,39 @@ func SearchTask(query string) types.Context {
|
|||
context = types.Context{Tasks: task, Search: query}
|
||||
return context
|
||||
}
|
||||
|
||||
//GetComments is used to get comments, all of them.
|
||||
//We do not want 100 different pages to show tasks, we want to use as few pages as possible
|
||||
//so we are going to populate everything on the damn home pages
|
||||
func GetComments() map[int][]types.Comment {
|
||||
commentMap := make(map[int][]types.Comment)
|
||||
|
||||
var id int
|
||||
var message types.Comment
|
||||
|
||||
stmt := "select taskID, content from comments;"
|
||||
rows := database.query(stmt)
|
||||
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&id, &message.Content)
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
commentMap[id] = append(commentMap[id], message)
|
||||
}
|
||||
return commentMap
|
||||
}
|
||||
|
||||
//AddComments will be used to add comments in the database
|
||||
func AddComments(id int, comment string) error {
|
||||
stmt := "insert into comments(taskID, content) values (?,?)"
|
||||
err := taskQuery(stmt, id, comment)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("added comment to task ID ", id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
1
main.go
1
main.go
|
@ -17,6 +17,7 @@ func main() {
|
|||
views.PopulateTemplates()
|
||||
http.HandleFunc("/", views.ShowAllTasksFunc)
|
||||
http.HandleFunc("/add-category/", views.AddCategoryFunc)
|
||||
http.HandleFunc("/add-comment/", views.AddCommentFunc)
|
||||
http.HandleFunc("/del-category/", views.DeleteCategoryFunc)
|
||||
http.HandleFunc("/upd-category/", views.UpdateCategoryFunc)
|
||||
http.HandleFunc("/category/", views.ShowCategoryFunc)
|
||||
|
|
|
@ -17,8 +17,14 @@ ul{
|
|||
list-style-type: none;
|
||||
}
|
||||
|
||||
input {
|
||||
border:none;
|
||||
border-bottom:1px solid gray;
|
||||
box-shadow:none;
|
||||
}
|
||||
|
||||
.badge{
|
||||
background-color: #7D8EF0;
|
||||
background-color: #1a78c9;
|
||||
margin-right:10px;
|
||||
float:right;
|
||||
}
|
||||
|
|
|
@ -35,7 +35,12 @@ $(document).ready(function(){
|
|||
$('#toggleAddFileGrp').addClass('hidden') ;
|
||||
});
|
||||
|
||||
if ($('#actlMsg').html()==' <button id="btnMessage" class="btn btn-default">OK</button>'){
|
||||
$("#noti").click(
|
||||
function(){
|
||||
this.fadeOut();
|
||||
}
|
||||
);
|
||||
if ($('#actlMsg').html()==''){
|
||||
$('.notification').addClass('hidden');
|
||||
} else {
|
||||
$('.notification').fadeOut(9000);
|
||||
|
@ -53,6 +58,22 @@ $(document).ready(function(){
|
|||
}
|
||||
}
|
||||
);*/
|
||||
|
||||
$("#addNoteBtn").on("click", function() {
|
||||
this.preventDefaults();
|
||||
var task_id = $("#task-id").val();
|
||||
$.ajax({
|
||||
url: "/tasks/" + task_id,
|
||||
type: "POST",
|
||||
data: {'title':'randome note', 'content':'this and that'}
|
||||
}).done(function(res, status) {
|
||||
console.log(status, res);
|
||||
var response = res
|
||||
$("#timeline").append(response)
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
$('.toggle').click(function(){
|
||||
$(this).next().toggle();
|
||||
});
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
|
||||
<body>
|
||||
<!-- The navigation bar-->
|
||||
<div class='notification {{if eq .Message ""}} hidden {{end}}'><span id="message"><p id="actlMsg">{{.Message}} <button id="btnMessage" class="btn btn-default">OK</button></p> </span> </div>
|
||||
<div id = "noti" class='notification {{if eq .Message ""}} hidden {{end}}'><span id="message"><p id="actlMsg">{{.Message}}</div>
|
||||
<nav class="navbar navbar-default navbar-fixed-top mainHeader">
|
||||
<div class="container-fluid">
|
||||
{{$url := ""}}
|
||||
|
@ -55,12 +55,12 @@
|
|||
</p>
|
||||
|
||||
<form action="/upd-category/{{.Navigation}}" method="POST" class="hidden" id="EditForm">
|
||||
<input type="text" name="catname" placeholder="new cat name" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
|
||||
<input type="text" name="catname" placeholder="new cat name" >
|
||||
<input type="submit" value="Submit" class="btn btn-default" />
|
||||
</form>
|
||||
|
||||
<form action="/search/" method="POST" class="hidden" id="SearchForm">
|
||||
<input type="text" name="query" placeholder="Search" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
|
||||
<input type="text" name="query" placeholder="Search" >
|
||||
<input type="submit" value="Submit" class="btn btn-default" />
|
||||
</form>
|
||||
|
||||
|
|
|
@ -8,10 +8,10 @@
|
|||
<div class="modal-body">
|
||||
<form action="/update/" method="POST">
|
||||
<div class="form-group">
|
||||
<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;">
|
||||
<input type="text" name="title" value="{{ $task.Title}}" class="form-control" id="add-note-title" placeholder="Title">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<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;">{{ $task.Content}}</textarea>
|
||||
<textarea class="form-control" name="content" id="add-note-content" placeholder="Content" rows="10" >{{ $task.Content}}</textarea>
|
||||
|
||||
<input type="text" name="id" value="{{.Id}}" class="hidden" /> Priority:
|
||||
<input type="radio" name="priority" value="3" {{if eq .Priority "3"}} checked="checked" {{end}} /> High
|
||||
|
|
|
@ -15,12 +15,12 @@
|
|||
<form enctype="multipart/form-data" action="/add/" method="POST">
|
||||
<div class="form-group">
|
||||
|
||||
<input type="text" name="title" class="form-control" id="add-note-title" placeholder="Title" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
|
||||
<input type="hidden" name="CSRFToken" value={{.CSRFToken}}>
|
||||
<input type="text" name="title" class="form-control" id="add-note-title" placeholder="Title" >
|
||||
<input class="hidden"" name="CSRFToken" value={{.CSRFToken}}>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
|
||||
<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;"></textarea>
|
||||
<textarea class="form-control" name="content" id="add-note-content" placeholder="Content" rows="10" ></textarea>
|
||||
<a id="toggleAddFileGrp">Add File</a>
|
||||
<span id="file-group" class="hidden">
|
||||
File: <input type="file" name="uploadfile" />
|
||||
|
@ -33,15 +33,14 @@
|
|||
Category:
|
||||
<select name="category" class="dropdown">
|
||||
<option>---</option>
|
||||
{{$navigation := .Navigation}} {{$categories := .Categories}}
|
||||
{{range $cat := $categories}}
|
||||
{{$navigation := .Navigation}} {{$categories := .Categories}} {{range $cat := $categories}}
|
||||
<option value="{{$cat.Name}}" {{if eq $cat.Name $navigation }} selected="true" {{end}}> {{$cat.Name}} </option>
|
||||
{{end}}
|
||||
</select>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
||||
<input type="submit" value="Submit" class="btn btn-default" id="addNoteBtn" />
|
||||
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
|
||||
<input type="submit" value="Submit" class="btn btn-primary" id="addNoteBtn" />
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
@ -53,7 +52,25 @@
|
|||
<div class="note" id="{{$value.Id}}">
|
||||
<p class="noteHeading">{{ $value.Title}}</p> <span class="toggle glyphicon glyphicon-resize-full"></span>
|
||||
|
||||
<span class="noteContent">{{$value.Content}}</span>
|
||||
<span class="noteContent">
|
||||
{{$value.Content}}
|
||||
<span class="noteComments">
|
||||
<ul>
|
||||
{{range $value.Comments}}
|
||||
<li>{{.Content}}</li>
|
||||
{{end}}
|
||||
|
||||
<li>
|
||||
<form method="POST" action="/add-comment/">
|
||||
<textarea rows="2" cols="30" name="commentText" placeholder="Add Comment"></textarea>
|
||||
<input type="text" class="hidden" name="taskID" value="{{$value.Id}}">
|
||||
<input type="submit" value="Comment" class="btn btn-primary" />
|
||||
</form>
|
||||
</li>
|
||||
</ul>
|
||||
</span>
|
||||
</span>
|
||||
|
||||
<span class="notefooter">
|
||||
<ul class="menu">
|
||||
<li role="presentation">Priority: {{$value.Priority}}</li>
|
||||
|
@ -84,7 +101,7 @@
|
|||
<div class="note">
|
||||
<p class="noteHeading">No Tasks here</p>
|
||||
<p class="notefooter">Create new task
|
||||
<button class="floating-action-icon-add" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
|
||||
<button class="floating-action-icon-add" >
|
||||
here </button>
|
||||
</p>
|
||||
</div>
|
||||
|
|
|
@ -13,6 +13,12 @@ type Task struct {
|
|||
Priority string
|
||||
Category string
|
||||
Referer string
|
||||
Comments []Comment
|
||||
}
|
||||
|
||||
//Comment is the struct used to populate comments per tasks
|
||||
type Comment struct {
|
||||
Content string
|
||||
}
|
||||
|
||||
//Context is the struct passed to templates
|
||||
|
|
|
@ -160,3 +160,31 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
|
|||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
}
|
||||
}
|
||||
|
||||
//AddCommentFunc will be used
|
||||
func AddCommentFunc(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
r.ParseForm()
|
||||
text := r.Form.Get("commentText")
|
||||
id := r.Form.Get("taskID")
|
||||
|
||||
idInt, err := strconv.Atoi(id)
|
||||
|
||||
if (err != nil) || (text == "") {
|
||||
log.Println("unable to convert into integer")
|
||||
message = "Error adding comment"
|
||||
} else {
|
||||
err = db.AddComments(idInt, text)
|
||||
|
||||
if err != nil {
|
||||
log.Println("unable to insert into db")
|
||||
message = "Comment not added"
|
||||
} else {
|
||||
message = "Comment added"
|
||||
}
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,9 +107,13 @@ func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
|
|||
message = "Error updating task"
|
||||
} else {
|
||||
message = "Task updated"
|
||||
log.Println(message)
|
||||
}
|
||||
log.Println("redirecting to somewhere else")
|
||||
http.Redirect(w, r, "/", 301)
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
|
||||
} else {
|
||||
message = "Method not allowed"
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue