Add: put tag on file
This commit is contained in:
@@ -87,6 +87,8 @@ func NewAPI(config Config) (*API, error) {
|
|||||||
apiMux.HandleFunc("/get_tag_info", api.HandleGetTagInfo)
|
apiMux.HandleFunc("/get_tag_info", api.HandleGetTagInfo)
|
||||||
apiMux.HandleFunc("/insert_tag", api.HandleInsertTag)
|
apiMux.HandleFunc("/insert_tag", api.HandleInsertTag)
|
||||||
apiMux.HandleFunc("/update_tag", api.HandleUpdateTag)
|
apiMux.HandleFunc("/update_tag", api.HandleUpdateTag)
|
||||||
|
apiMux.HandleFunc("/put_tag_on_file", api.HandlePutTagOnFile)
|
||||||
|
apiMux.HandleFunc("/get_tags_on_file", api.HandleGetTagsOnFile)
|
||||||
// below needs token
|
// below needs token
|
||||||
apiMux.HandleFunc("/walk", api.HandleWalk)
|
apiMux.HandleFunc("/walk", api.HandleWalk)
|
||||||
apiMux.HandleFunc("/reset", api.HandleReset)
|
apiMux.HandleFunc("/reset", api.HandleReset)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
ErrNotLoggedIn = errors.New("not logged in")
|
ErrNotLoggedIn = errors.New("not logged in")
|
||||||
ErrNotAdmin = errors.New("not admin")
|
ErrNotAdmin = errors.New("not admin")
|
||||||
|
ErrEmpty = errors.New("Empty field detected, please fill in all fields")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Error struct {
|
type Error struct {
|
||||||
|
|||||||
82
pkg/api/handle_tag_and_file.go
Normal file
82
pkg/api/handle_tag_and_file.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"msw-open-music/pkg/database"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PutTagOnFileRequest struct {
|
||||||
|
TagID int64 `json:"tag_id"`
|
||||||
|
FileID int64 `json:"file_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) HandlePutTagOnFile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// check if the user is admin
|
||||||
|
err := api.CheckAdmin(w, r)
|
||||||
|
if err != nil {
|
||||||
|
api.HandleError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &PutTagOnFileRequest{}
|
||||||
|
err = json.NewDecoder(r.Body).Decode(req)
|
||||||
|
if err != nil {
|
||||||
|
api.HandleError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userID, err := api.GetUserID(w, r)
|
||||||
|
if err != nil {
|
||||||
|
api.HandleError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check empty
|
||||||
|
if req.TagID == 0 || req.FileID == 0 {
|
||||||
|
api.HandleError(w, r, ErrEmpty)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Put tag on file request:", req, "userID:", userID)
|
||||||
|
|
||||||
|
api.Db.PutTagOnFile(req.TagID, req.FileID, userID)
|
||||||
|
|
||||||
|
api.HandleOK(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetTagsOnFileRequest struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetTagsOnFileResponse struct {
|
||||||
|
Tags []*database.Tag `json:"tags"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) HandleGetTagsOnFile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
req := &GetTagsOnFileRequest{}
|
||||||
|
err := json.NewDecoder(r.Body).Decode(req)
|
||||||
|
if err != nil {
|
||||||
|
api.HandleError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Get tags on file request:", req)
|
||||||
|
|
||||||
|
tags, err := api.Db.GetTagsOnFile(req.ID)
|
||||||
|
if err != nil {
|
||||||
|
api.HandleError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := &GetTagsOnFileResponse{
|
||||||
|
Tags: tags,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.NewEncoder(w).Encode(resp)
|
||||||
|
if err != nil {
|
||||||
|
api.HandleError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
25
pkg/database/method_tag_and_file.go
Normal file
25
pkg/database/method_tag_and_file.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
func (database *Database) PutTagOnFile(tagID, fileID, userID int64) error {
|
||||||
|
_, err := database.stmt.putTagOnFile.Exec(tagID, fileID, userID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (database *Database) GetTagsOnFile(fileID int64) ([]*Tag, error) {
|
||||||
|
rows, err := database.stmt.getTagsOnFile.Query(fileID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
tags := make([]*Tag, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
tag := &Tag{}
|
||||||
|
err = rows.Scan(&tag.ID, &tag.Name, &tag.Description, &tag.CreatedByUserId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tags = append(tags, tag)
|
||||||
|
}
|
||||||
|
return tags, nil
|
||||||
|
}
|
||||||
@@ -190,6 +190,14 @@ JOIN users ON tags.created_by_user_id = users.id
|
|||||||
|
|
||||||
var updateTagQuery = `UPDATE tags SET name = ?, description = ? WHERE id = ?;`
|
var updateTagQuery = `UPDATE tags SET name = ?, description = ? WHERE id = ?;`
|
||||||
|
|
||||||
|
var putTagOnFileQuery = `INSERT INTO file_has_tag (tag_id, file_id, user_id) VALUES (?, ?, ?);`
|
||||||
|
|
||||||
|
var getTagsOnFileQuery = `SELECT
|
||||||
|
tags.id, tags.name, tags.description, tags.created_by_user_id
|
||||||
|
FROM file_has_tag
|
||||||
|
JOIN tags ON file_has_tag.tag_id = tags.id
|
||||||
|
WHERE file_has_tag.file_id = ?;`
|
||||||
|
|
||||||
type Stmt struct {
|
type Stmt struct {
|
||||||
initFilesTable *sql.Stmt
|
initFilesTable *sql.Stmt
|
||||||
initFoldersTable *sql.Stmt
|
initFoldersTable *sql.Stmt
|
||||||
@@ -226,6 +234,8 @@ type Stmt struct {
|
|||||||
getTag *sql.Stmt
|
getTag *sql.Stmt
|
||||||
getTags *sql.Stmt
|
getTags *sql.Stmt
|
||||||
updateTag *sql.Stmt
|
updateTag *sql.Stmt
|
||||||
|
putTagOnFile *sql.Stmt
|
||||||
|
getTagsOnFile *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPreparedStatement(sqlConn *sql.DB) (*Stmt, error) {
|
func NewPreparedStatement(sqlConn *sql.DB) (*Stmt, error) {
|
||||||
@@ -506,5 +516,17 @@ func NewPreparedStatement(sqlConn *sql.DB) (*Stmt, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// init putTagOnFile
|
||||||
|
stmt.putTagOnFile, err = sqlConn.Prepare(putTagOnFileQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// init getTagsOnFile
|
||||||
|
stmt.getTagsOnFile, err = sqlConn.Prepare(getTagsOnFileQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return stmt, err
|
return stmt, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ function FileInfo(props) {
|
|||||||
filename: "",
|
filename: "",
|
||||||
filesize: "",
|
filesize: "",
|
||||||
});
|
});
|
||||||
|
const [tags, setTags] = useState([]);
|
||||||
|
const [tagsOnFile, setTagsOnFile] = useState([]);
|
||||||
|
const [selectedTagID, setSelectedTagID] = useState("");
|
||||||
|
|
||||||
function refresh() {
|
function refresh() {
|
||||||
fetch(`/api/v1/get_file_info`, {
|
fetch(`/api/v1/get_file_info`, {
|
||||||
@@ -32,8 +35,42 @@ function FileInfo(props) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getTags() {
|
||||||
|
fetch(`/api/v1/get_tags`)
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
if (data.error) {
|
||||||
|
alert(data.error);
|
||||||
|
} else {
|
||||||
|
setTags(data.tags);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTagsOnFile() {
|
||||||
|
fetch(`/api/v1/get_tags_on_file`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
id: parseInt(params.id),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
if (data.error) {
|
||||||
|
alert(data.error);
|
||||||
|
} else {
|
||||||
|
setTagsOnFile(data.tags);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
refresh();
|
refresh();
|
||||||
|
getTags();
|
||||||
|
getTagsOnFile();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -41,9 +78,13 @@ function FileInfo(props) {
|
|||||||
<h3>File Details</h3>
|
<h3>File Details</h3>
|
||||||
<div>
|
<div>
|
||||||
<button>Download</button>
|
<button>Download</button>
|
||||||
<button onClick={() => {
|
<button
|
||||||
props.setPlayingFile(file);
|
onClick={() => {
|
||||||
}}>Play</button>
|
props.setPlayingFile(file);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Play
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
navigate(`/files/${params.id}/share`);
|
navigate(`/files/${params.id}/share`);
|
||||||
@@ -68,6 +109,64 @@ function FileInfo(props) {
|
|||||||
<label htmlFor="filesize">File Size:</label>
|
<label htmlFor="filesize">File Size:</label>
|
||||||
<input type="text" id="filesize" value={file.filesize} readOnly />
|
<input type="text" id="filesize" value={file.filesize} readOnly />
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<label>Tags:</label>
|
||||||
|
<ul>
|
||||||
|
{tagsOnFile.map((tag) => {
|
||||||
|
return (
|
||||||
|
<li key={tag.id}>
|
||||||
|
<button>{tag.name}</button>
|
||||||
|
<button>Remove</button>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
<div>
|
||||||
|
<select
|
||||||
|
onChange={(e) => {
|
||||||
|
setSelectedTagID(e.target.value);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<option value="">Select a tag</option>
|
||||||
|
{tags.map((tag) => {
|
||||||
|
return (
|
||||||
|
<option key={tag.id} value={tag.id}>
|
||||||
|
{tag.name}
|
||||||
|
</option>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</select>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
// check empty
|
||||||
|
if (selectedTagID === "") {
|
||||||
|
alert("Please select a tag");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fetch(`/api/v1/put_tag_on_file`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
file_id: parseInt(params.id),
|
||||||
|
tag_id: parseInt(selectedTagID),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
if (data.error) {
|
||||||
|
alert(data.error);
|
||||||
|
} else {
|
||||||
|
getTagsOnFile();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Add Tag
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user