change parent works
This commit is contained in:
		
							parent
							
								
									47d1bdadd1
								
							
						
					
					
						commit
						d8f2c326a4
					
				| 
						 | 
				
			
			@ -48,6 +48,20 @@ func LoadCategories(db *sql.DB) {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (category *Category) ChangeParent(db *sql.DB, parent *Category) error {
 | 
			
		||||
    var err error 
 | 
			
		||||
    if parent == nil {
 | 
			
		||||
        _, err = db.Exec("UPDATE categories SET parent_id = NULL WHERE id = $1", category.Id)
 | 
			
		||||
    } else {
 | 
			
		||||
        _, err = db.Exec("UPDATE categories SET parent_id = $2 WHERE id = $1", category.Id, parent.Id)
 | 
			
		||||
    }
 | 
			
		||||
      
 | 
			
		||||
    if err != nil {
 | 
			
		||||
        fmt.Println("Categories DB Error: ", err)
 | 
			
		||||
    }
 | 
			
		||||
    return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// get the number of parents a category has
 | 
			
		||||
func (category *Category) Depth() int {
 | 
			
		||||
    depth := 0
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,10 +23,6 @@ $(document).ready( function () {
 | 
			
		|||
  });
 | 
			
		||||
  
 | 
			
		||||
  $(".category-change-parent").change(function (e) {
 | 
			
		||||
    $.post("/categories/change-parent", 
 | 
			
		||||
        {target: this.name, 
 | 
			
		||||
        value: this.value}, 
 | 
			
		||||
        function(data) { alert("reload"); location.reload(); }, 
 | 
			
		||||
        "json");
 | 
			
		||||
       e.target.parentElement.submit();
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -257,11 +257,37 @@ func categoriesPostHandler(w http.ResponseWriter, r *http.Request, user *user.Us
 | 
			
		|||
    http.Redirect(w, r, "/categories", http.StatusFound)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func categoryFromReqArg(arg string) *categories.Category {
 | 
			
		||||
    if cid, err := strconv.Atoi(arg); err != nil {
 | 
			
		||||
        return nil
 | 
			
		||||
    } else if category, ok := categories.CategoriesFlat[cid]; !ok {
 | 
			
		||||
        return nil
 | 
			
		||||
    } else {
 | 
			
		||||
        return category
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func categoryChangeParentHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
 | 
			
		||||
    fmt.Println("categoryChangeParentHandler")
 | 
			
		||||
    session, _ := store.Get(r, "c_user")
 | 
			
		||||
    session.AddFlash("change parent")
 | 
			
		||||
 | 
			
		||||
	categories.LoadCategories(db)
 | 
			
		||||
	category := categoryFromReqArg(r.FormValue("cid"))
 | 
			
		||||
	parent := categoryFromReqArg(r.FormValue("parent"))
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
	if category == nil {
 | 
			
		||||
		session.AddFlash("Invalid category", flash_err)	
 | 
			
		||||
	} else {
 | 
			
		||||
	    err := category.ChangeParent(db, parent)
 | 
			
		||||
	    if err != nil {
 | 
			
		||||
	        session.AddFlash("Error commiting to Database", flash_err)
 | 
			
		||||
	    } else {
 | 
			
		||||
	    	session.AddFlash("Changed category parent", flash_info)
 | 
			
		||||
	    }	
 | 
			
		||||
    }
 | 
			
		||||
	
 | 
			
		||||
    session.Save(r, w)
 | 
			
		||||
    http.Redirect(w, r, "/categories", http.StatusFound)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ServeFileHandler(res http.ResponseWriter, req *http.Request) {  
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,8 +23,9 @@
 | 
			
		|||
            <a href="/categories/delete?id={{.category.Id}}">delete</a>
 | 
			
		||||
        </div>
 | 
			
		||||
        <div class="col-xs-3">
 | 
			
		||||
    <select class="form-control category-change-parent" name="category-change-parent-{{.category.Id}}" >
 | 
			
		||||
        <option value="-1" {{if not .category.Parent.Valid}} selected="true"{{end}}>-- None --</option>
 | 
			
		||||
        <form action="/categories/change-parent?cid={{.category.Id}}" method="POST">
 | 
			
		||||
            <select class="form-control category-change-parent" name="parent" >
 | 
			
		||||
                <option value="none" {{if not .category.Parent.Valid}} selected="true"{{end}}>-- None --</option>
 | 
			
		||||
                {{range $category := .categories}}
 | 
			
		||||
                    {{if $.category.Parent.Valid }}
 | 
			
		||||
                        {{template "option-category" dict "category" $category "id" $.category.Parent.Value}}
 | 
			
		||||
| 
						 | 
				
			
			@ -33,6 +34,7 @@
 | 
			
		|||
                    {{end}}
 | 
			
		||||
                {{end}}
 | 
			
		||||
            </select>
 | 
			
		||||
        </form>
 | 
			
		||||
        </div>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue