wip
This commit is contained in:
parent
5c2842c995
commit
1d6ba99a36
8 changed files with 945 additions and 83 deletions
63
file.go
63
file.go
|
|
@ -1,6 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
// External
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
// Standard
|
||||
"time"
|
||||
)
|
||||
|
|
@ -15,3 +18,63 @@ type File struct {
|
|||
MD5 string
|
||||
Uploaded time.Time
|
||||
}
|
||||
|
||||
func AddFile(userID int, file *File) (err error) { // {{{
|
||||
file.UserID = userID
|
||||
|
||||
var rows *sqlx.Rows
|
||||
rows, err = db.Queryx(`
|
||||
INSERT INTO file(user_id, node_id, filename, size, mime, md5)
|
||||
VALUES($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id
|
||||
`,
|
||||
file.UserID,
|
||||
file.NodeID,
|
||||
file.Filename,
|
||||
file.Size,
|
||||
file.MIME,
|
||||
file.MD5,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
rows.Next()
|
||||
err = rows.Scan(&file.ID)
|
||||
return
|
||||
} // }}}
|
||||
func Files(userID, nodeID, fileID int) (files []File, err error) { // {{{
|
||||
var rows *sqlx.Rows
|
||||
rows, err = db.Queryx(
|
||||
`SELECT *
|
||||
FROM file
|
||||
WHERE
|
||||
user_id = $1 AND
|
||||
node_id = $2 AND
|
||||
CASE $3::int
|
||||
WHEN 0 THEN true
|
||||
ELSE id = $3
|
||||
END`,
|
||||
userID,
|
||||
nodeID,
|
||||
fileID,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
files = []File{}
|
||||
for rows.Next() {
|
||||
file := File{}
|
||||
if err = rows.StructScan(&file); err != nil {
|
||||
return
|
||||
}
|
||||
files = append(files, file)
|
||||
}
|
||||
|
||||
return
|
||||
} // }}}
|
||||
|
||||
// vim: foldmethod=marker
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue