This commit is contained in:
Suraj Patil 2016-08-30 21:00:26 +05:30
parent f94da496eb
commit 2281f2ffe5
3 changed files with 66 additions and 43 deletions

View File

@ -9,8 +9,8 @@ CREATE TABLE task (
finish_date timestamp,
priority integer,
cat_id references category(id),
user_id references user(id)
);
user_id references user(id),
hide int);
CREATE TABLE status (
id integer primary key autoincrement,
status varchar(50) not null

View File

@ -150,52 +150,59 @@ 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 {
redirectURL := utils.GetRedirectUrl(r.Referer())
username := sessions.GetCurrentUserName(r)
task, err := db.GetTaskByID(username, id)
categories := db.GetCategories(username)
task.Categories = categories
task.Referer = redirectURL
if r.Method != "GET" {
http.Redirect(w, r, "/", http.StatusBadRequest)
return
}
if err != nil {
task.Message = "Error fetching Tasks"
}
editTemplate.Execute(w, task)
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil {
log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
return
} else {
redirectURL := utils.GetRedirectUrl(r.Referer())
username := sessions.GetCurrentUserName(r)
task, err := db.GetTaskByID(username, id)
categories := db.GetCategories(username)
task.Categories = categories
task.Referer = redirectURL
if err != nil {
task.Message = "Error fetching Tasks"
}
editTemplate.Execute(w, task)
}
}
//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 {
username := sessions.GetCurrentUserName(r)
err = db.AddComments(username, 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)
if r.Method != "POST" {
log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
return
}
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 {
username := sessions.GetCurrentUserName(r)
err = db.AddComments(username, 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)
}

View File

@ -56,6 +56,7 @@ func TestAddEmptyCategory(t *testing.T) {
func TestEditTaskWithWrongMethod(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(EditTaskFunc))
defer ts.Close()
ts.URL = ts.URL + "/edit/"
req, err := http.NewRequest("OPTIONS", ts.URL, nil)
if err != nil {
t.Errorf("Error occured while constracting request:%s", err)
@ -64,7 +65,7 @@ func TestEditTaskWithWrongMethod(t *testing.T) {
EditTaskFunc(w, req)
if w.Code != http.StatusFound && message != "Method not allowed" {
t.Errorf("Message was: %s Return code was: %d. Should have been message: %s return code: %d", message, w.Code, "Method not allowed", http.StatusFound)
t.Errorf("Message was: %s Return code was: %d. Should have been message: %s return code: %d", message, w.Code, "Method not allowed", http.StatusBadRequest)
}
}
@ -84,3 +85,18 @@ func TestEditTaskWrongTaskName(t *testing.T) {
t.Errorf("Actual status: (%d); Expected status:(%d)", w.Code, http.StatusBadRequest)
}
}
func TestAddCommentWithWrongMethod(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(AddCommentFunc))
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Errorf("Error occured while constructing request: %s", err)
}
w := httptest.NewRecorder()
AddCommentFunc(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("Actual status: (%d); Expected status:(%d)", w.Code, http.StatusBadRequest)
}
}