Add: update tag info
This commit is contained in:
@@ -86,6 +86,7 @@ func NewAPI(config Config) (*API, error) {
|
||||
apiMux.HandleFunc("/get_tags", api.HandleGetTags)
|
||||
apiMux.HandleFunc("/get_tag_info", api.HandleGetTagInfo)
|
||||
apiMux.HandleFunc("/insert_tag", api.HandleInsertTag)
|
||||
apiMux.HandleFunc("/update_tag", api.HandleUpdateTag)
|
||||
// below needs token
|
||||
apiMux.HandleFunc("/walk", api.HandleWalk)
|
||||
apiMux.HandleFunc("/reset", api.HandleReset)
|
||||
|
||||
@@ -91,3 +91,28 @@ func (api *API) HandleGetTagInfo(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (api *API) HandleUpdateTag(w http.ResponseWriter, r *http.Request) {
|
||||
// check if user is admin
|
||||
err := api.CheckAdmin(w, r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
req := &database.Tag{}
|
||||
err = json.NewDecoder(r.Body).Decode(req)
|
||||
if err != nil {
|
||||
api.HandleError(w, r, err)
|
||||
return
|
||||
}
|
||||
tag, err := api.Db.UpdateTag(req)
|
||||
if err != nil {
|
||||
api.HandleError(w, r, err)
|
||||
return
|
||||
}
|
||||
err = json.NewEncoder(w).Encode(tag)
|
||||
if err != nil {
|
||||
api.HandleError(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package database
|
||||
|
||||
import "errors"
|
||||
|
||||
func (database *Database) InsertTag(tag string, description string) (*Tag, error) {
|
||||
result, err := database.stmt.insertTag.Exec(tag, description)
|
||||
if err != nil {
|
||||
@@ -38,3 +40,18 @@ func (database *Database) GetTags() ([]Tag, error) {
|
||||
}
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
func (database *Database) UpdateTag(tag *Tag) (*Tag, error) {
|
||||
result, err := database.stmt.updateTag.Exec(tag.Name, tag.Description, tag.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return nil, errors.New("No rows affected")
|
||||
}
|
||||
return database.GetTag(tag.ID)
|
||||
}
|
||||
|
||||
@@ -176,6 +176,8 @@ var getTagQuery = `SELECT id, name, description FROM tags WHERE id = ? LIMIT 1;`
|
||||
|
||||
var getTagsQuery = `SELECT id, name, description FROM tags;`
|
||||
|
||||
var updateTagQuery = `UPDATE tags SET name = ?, description = ? WHERE id = ?;`
|
||||
|
||||
type Stmt struct {
|
||||
initFilesTable *sql.Stmt
|
||||
initFoldersTable *sql.Stmt
|
||||
@@ -211,6 +213,7 @@ type Stmt struct {
|
||||
insertTag *sql.Stmt
|
||||
getTag *sql.Stmt
|
||||
getTags *sql.Stmt
|
||||
updateTag *sql.Stmt
|
||||
}
|
||||
|
||||
func NewPreparedStatement(sqlConn *sql.DB) (*Stmt, error) {
|
||||
@@ -485,5 +488,11 @@ func NewPreparedStatement(sqlConn *sql.DB) (*Stmt, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// init updateTag
|
||||
stmt.updateTag, err = sqlConn.Prepare(updateTagQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return stmt, err
|
||||
}
|
||||
|
||||
@@ -3,9 +3,13 @@ import { useParams } from "react-router";
|
||||
|
||||
function EditTag() {
|
||||
let params = useParams();
|
||||
const [tag, setTag] = useState({});
|
||||
const [tag, setTag] = useState({
|
||||
id: "",
|
||||
name: "",
|
||||
description: "",
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
function refreshTagInfo() {
|
||||
fetch("/api/v1/get_tag_info", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -23,6 +27,33 @@ function EditTag() {
|
||||
setTag(data.tag);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function updateTagInfo() {
|
||||
fetch("/api/v1/update_tag", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: parseInt(params.id),
|
||||
name: tag.name,
|
||||
description: tag.description,
|
||||
}),
|
||||
})
|
||||
.then((res) => res.json())
|
||||
.then((data) => {
|
||||
if (data.error) {
|
||||
alert(data.error);
|
||||
} else {
|
||||
alert("Tag updated successfully");
|
||||
refreshTagInfo();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
refreshTagInfo();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
@@ -53,7 +84,7 @@ function EditTag() {
|
||||
value={tag.description}
|
||||
onChange={(e) => setTag({ ...tag, description: e.target.value })}
|
||||
/>
|
||||
<button onClick={() => {}}>Save</button>
|
||||
<button onClick={() => updateTagInfo()}>Save</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user