storage.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. package storage
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "log"
  8. "mime/multipart"
  9. "os"
  10. "path"
  11. "strings"
  12. "time"
  13. "git.aetherial.dev/aeth/keiji/pkg/env"
  14. "github.com/google/uuid"
  15. )
  16. const TECHNICAL = "technical"
  17. const CONFIGURATION = "configuration"
  18. const BLOG = "blog"
  19. const CREATIVE = "creative"
  20. const DIGITAL_ART = "digital_art"
  21. const MISC_ASSET = "misc_asset"
  22. const HOMEPAGE = "homepage"
  23. var Topics = []string{
  24. TECHNICAL,
  25. BLOG,
  26. CREATIVE,
  27. HOMEPAGE,
  28. }
  29. var ImageCategories = []string{
  30. MISC_ASSET,
  31. DIGITAL_ART,
  32. }
  33. type DatabaseSchema struct {
  34. // Gotta figure out what this looks like
  35. // so that the ExtractAll() function gets
  36. // all of the data from the database
  37. }
  38. type MenuElement struct {
  39. Png string `json:"png"`
  40. Category string `json:"category"`
  41. MenuLinks []LinkPair `json:"menu_links"`
  42. }
  43. type AdminPage struct {
  44. Tables map[string][]TableData `json:"tables"`
  45. }
  46. type TableData struct { // TODO: add this to the database io interface
  47. DisplayName string `json:"display_name"`
  48. Link string `json:"link"`
  49. }
  50. type LinkPair struct {
  51. Link string `json:"link"`
  52. Text string `json:"text"`
  53. }
  54. type NavBarItem struct {
  55. Png []byte `json:"png"`
  56. File *multipart.FileHeader `form:"file"`
  57. Link string `json:"link" form:"link"`
  58. Redirect string `json:"redirect" form:"redirect"`
  59. }
  60. type Asset struct {
  61. Name string
  62. Data []byte
  63. }
  64. type Identifier string
  65. type Document struct {
  66. Row int
  67. Ident Identifier `json:"id"`
  68. Title string `json:"title"`
  69. Created string `json:"created"`
  70. Body string `json:"body"`
  71. Category string `json:"category"`
  72. Sample string `json:"sample"`
  73. }
  74. /*
  75. Truncates a text post into a 256 character long 'sample' for displaying posts
  76. */
  77. func (d *Document) MakeSample() string {
  78. t := strings.Split(d.Body, "")
  79. var sample []string
  80. if len(d.Body) < 256 {
  81. return d.Body
  82. }
  83. for i := 0; i < 256; i++ {
  84. sample = append(sample, t[i])
  85. }
  86. sample = append(sample, " ...")
  87. return strings.Join(sample, "")
  88. }
  89. type Image struct {
  90. Ident Identifier `json:"identifier"`
  91. Location string
  92. Title string `json:"title" form:"title"`
  93. File *multipart.FileHeader `form:"file"`
  94. Desc string `json:"description" form:"description"`
  95. Created string
  96. Category string `json:"category" form:"category"`
  97. Data []byte
  98. }
  99. type DocumentIO interface {
  100. GetDocument(id Identifier) (Document, error)
  101. GetImage(id Identifier) (Image, error)
  102. GetAllImages() []Image
  103. GetImagesByCategory(category string) []Image
  104. UpdateDocument(doc Document) error
  105. DeleteDocument(id Identifier) error
  106. DeleteNavbarItem(id Identifier) error
  107. AddDocument(doc Document) (Identifier, error)
  108. AddImage(data []byte, title, desc, category string) (Identifier, error)
  109. AddAsset(name string, data []byte) error
  110. AddAdminTableEntry(TableData, string) error
  111. AddNavbarItem(NavBarItem) error
  112. AddMenuItem(LinkPair) error
  113. GetByCategory(category string) []Document
  114. AllDocuments() []Document
  115. GetDropdownElements() []LinkPair
  116. GetNavBarLinks() []NavBarItem
  117. GetAssets() []Asset
  118. GetAdminTables() AdminPage
  119. }
  120. var (
  121. ErrDuplicate = errors.New("record already exists")
  122. ErrNotExists = errors.New("row not exists")
  123. ErrUpdateFailed = errors.New("update failed")
  124. ErrDeleteFailed = errors.New("delete failed")
  125. )
  126. type SQLiteRepo struct {
  127. db *sql.DB
  128. imageIO ImageIO
  129. }
  130. type ImageIO interface {
  131. Put([]byte, Identifier) error
  132. Get(Identifier) ([]byte, error)
  133. }
  134. type FilesystemImageIO struct {
  135. RootDir string
  136. }
  137. /*
  138. create a new FilesystemImageIO struct
  139. */
  140. func MustNewFilesystemImageIO(path string) FilesystemImageIO {
  141. var filemode os.FileMode
  142. filemode = 0775
  143. err := os.MkdirAll(path, filemode)
  144. if err != nil {
  145. log.Fatalf("Error creating path: '%s' with permissions: '%d'. Error: %s\n", path, filemode, err.Error())
  146. }
  147. return FilesystemImageIO{RootDir: path}
  148. }
  149. /*
  150. Put a data blob on the filesystem
  151. :param b: the
  152. */
  153. func (f FilesystemImageIO) Put(b []byte, id Identifier) error {
  154. fh, err := os.OpenFile(path.Join(f.RootDir, string(id)), os.O_CREATE|os.O_RDWR, os.ModePerm)
  155. if err != nil {
  156. return err
  157. }
  158. defer fh.Close()
  159. _, err = fh.Write(b)
  160. if err != nil {
  161. return err
  162. }
  163. return nil
  164. }
  165. /*
  166. Get a data blob from the filesystem
  167. :param id: the identifier of the image to retrieve
  168. */
  169. func (f FilesystemImageIO) Get(id Identifier) ([]byte, error) {
  170. fh, err := os.Open(path.Join(f.RootDir, string(id)))
  171. if err != nil {
  172. return nil, err
  173. }
  174. b, err := io.ReadAll(fh)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return b, nil
  179. }
  180. // Instantiate a new SQLiteRepo struct
  181. func NewSQLiteRepo(db *sql.DB, imgIo ImageIO) *SQLiteRepo {
  182. return &SQLiteRepo{
  183. db: db,
  184. imageIO: imgIo,
  185. }
  186. }
  187. // Creates a new SQL table for text posts
  188. func (r *SQLiteRepo) Migrate(seedQueries []string) error {
  189. for i := range seedQueries {
  190. _, err := r.db.Exec(seedQueries[i])
  191. if err != nil {
  192. return err
  193. }
  194. }
  195. return nil
  196. }
  197. /*
  198. Get all dropdown menu elements. Returns a list of LinkPair structs with the text and redirect location
  199. */
  200. func (s *SQLiteRepo) GetDropdownElements() []LinkPair {
  201. rows, err := s.db.Query("SELECT * FROM menu")
  202. var menuItems []LinkPair
  203. defer rows.Close()
  204. for rows.Next() {
  205. var id int
  206. var item LinkPair
  207. err = rows.Scan(&id, &item.Link, &item.Text)
  208. if err != nil {
  209. log.Fatal(err)
  210. }
  211. menuItems = append(menuItems, item)
  212. }
  213. return menuItems
  214. }
  215. /*
  216. Retrieve a dropdown element by its text name on the UI
  217. */
  218. func (s *SQLiteRepo) GetMenuItemByName(link, text string) (LinkPair, bool) {
  219. fmt.Printf("From GetMenuItemByName: Link: %s Text: %s\n", link, text)
  220. rows := s.db.QueryRow("SELECT * FROM menu WHERE link = ? AND text = ?", link, text)
  221. var item LinkPair
  222. var id int
  223. err := rows.Scan(&id, &item.Link, &item.Text)
  224. if err != nil {
  225. if errors.Is(err, sql.ErrNoRows) {
  226. return item, false
  227. } else {
  228. return item, false
  229. }
  230. }
  231. return item, true
  232. }
  233. // get Admin table entry by its display name, link, and category.
  234. func (s *SQLiteRepo) GetAdminTableEntry(displayName, link, category string) (TableData, bool) {
  235. rows := s.db.QueryRow("SELECT * FROM admin WHERE display_name = ? AND link = ? AND category = ?", displayName, link, category)
  236. var item TableData
  237. var id int
  238. err := rows.Scan(&id, &item.DisplayName, &item.Link, &category)
  239. if err != nil {
  240. if errors.Is(err, sql.ErrNoRows) {
  241. return item, false
  242. }
  243. log.Printf("Error getting admin table entry: %s", err.Error())
  244. return item, false
  245. }
  246. return item, true
  247. }
  248. // get navbar entry.
  249. func (s *SQLiteRepo) GetNavbarLink(link, redirect string) (NavBarItem, bool) {
  250. rows := s.db.QueryRow("SELECT * FROM navbar WHERE link = ? AND redirect = ?", link, redirect)
  251. var item NavBarItem
  252. var id int
  253. err := rows.Scan(&id, &item.Png, &item.Link, &item.Redirect)
  254. if err != nil {
  255. if errors.Is(err, sql.ErrNoRows) {
  256. return item, false
  257. }
  258. log.Printf("Error scanning for item: %s", err.Error())
  259. return item, false
  260. }
  261. return item, true
  262. }
  263. // get an asset from the store
  264. func (s *SQLiteRepo) GetAsset(name string) (Asset, bool) {
  265. rows := s.db.QueryRow("SELECT * FROM assets WHERE name = ?", name)
  266. var item Asset
  267. var id int
  268. err := rows.Scan(&id, &item.Name, &item.Data)
  269. if err != nil {
  270. if errors.Is(err, sql.ErrNoRows) {
  271. return item, false
  272. }
  273. log.Printf("Error getting asset: %s", err.Error())
  274. return item, false
  275. }
  276. return item, true
  277. }
  278. /*
  279. Get all nav bar items. Returns a list of NavBarItem structs with the png data, the file name, and the redirect location of the icon
  280. /*
  281. Get all nav bar items. Returns a list of NavBarItem structs with the png data, the file name, and the redirect location of the icon
  282. */
  283. func (s *SQLiteRepo) GetNavBarLinks() []NavBarItem {
  284. rows, err := s.db.Query("SELECT * FROM navbar")
  285. var navbarItems []NavBarItem
  286. defer rows.Close()
  287. for rows.Next() {
  288. var item NavBarItem
  289. var id int
  290. err = rows.Scan(&id, &item.Png, &item.Link, &item.Redirect)
  291. if err != nil {
  292. log.Fatal(err)
  293. }
  294. navbarItems = append(navbarItems, item)
  295. }
  296. return navbarItems
  297. }
  298. /*
  299. get all assets from the asset table
  300. */
  301. func (s *SQLiteRepo) GetAssets() []Asset {
  302. rows, err := s.db.Query("SELECT * FROM assets")
  303. var assets []Asset
  304. defer rows.Close()
  305. for rows.Next() {
  306. var item Asset
  307. var id int
  308. err = rows.Scan(&id, &item.Name, &item.Data)
  309. if err != nil {
  310. log.Fatal(err)
  311. }
  312. assets = append(assets, item)
  313. }
  314. return assets
  315. }
  316. /*
  317. get all assets from the asset table
  318. */
  319. func (s *SQLiteRepo) GetAdminTables() AdminPage {
  320. rows, err := s.db.Query("SELECT * FROM admin")
  321. adminPage := AdminPage{Tables: map[string][]TableData{}}
  322. defer rows.Close()
  323. for rows.Next() {
  324. var item TableData
  325. var id int
  326. var category string
  327. err = rows.Scan(&id, &item.DisplayName, &item.Link, &category)
  328. if err != nil {
  329. log.Fatal(err)
  330. }
  331. adminPage.Tables[category] = append(adminPage.Tables[category], item)
  332. }
  333. return adminPage
  334. }
  335. /*
  336. Retrieve a document from the sqlite db
  337. :param id: the Identifier of the post
  338. */
  339. func (s *SQLiteRepo) GetDocument(id Identifier) (Document, error) {
  340. row := s.db.QueryRow("SELECT * FROM posts WHERE id = ?", id)
  341. var post Document
  342. var rowNum int
  343. if err := row.Scan(&rowNum, &post.Ident, &post.Title, &post.Created, &post.Body, &post.Category, &post.Sample); err != nil {
  344. if errors.Is(err, sql.ErrNoRows) {
  345. return post, ErrNotExists
  346. }
  347. return post, err
  348. }
  349. return post, nil
  350. }
  351. /*
  352. Get all documents by category
  353. :param category: the category to retrieve all docs from
  354. */
  355. func (s *SQLiteRepo) GetByCategory(category string) []Document {
  356. rows, err := s.db.Query("SELECT * FROM posts WHERE category = ?", category)
  357. if err != nil {
  358. log.Fatal(err)
  359. }
  360. var docs []Document
  361. defer rows.Close()
  362. for rows.Next() {
  363. var doc Document
  364. err := rows.Scan(&doc.Row, &doc.Ident, &doc.Title, &doc.Created, &doc.Body, &doc.Category, &doc.Sample)
  365. if err != nil {
  366. log.Fatal(err)
  367. }
  368. docs = append(docs, doc)
  369. }
  370. err = rows.Err()
  371. if err != nil {
  372. log.Fatal(err)
  373. }
  374. return docs
  375. }
  376. /*
  377. get image data from the images table
  378. :param id: the serial identifier of the post
  379. */
  380. func (s *SQLiteRepo) GetImage(id Identifier) (Image, error) {
  381. row := s.db.QueryRow("SELECT * FROM images WHERE id = ?", id)
  382. var rowNum int
  383. var title, desc, created, category string
  384. if err := row.Scan(&rowNum, &id, &title, &desc, &created, &category); err != nil {
  385. if errors.Is(err, sql.ErrNoRows) {
  386. return Image{}, ErrNotExists
  387. }
  388. return Image{}, err
  389. }
  390. data, err := s.imageIO.Get(id)
  391. if err != nil {
  392. return Image{}, err
  393. }
  394. return Image{Ident: id, Title: title, Desc: desc, Data: data, Created: created, Category: category}, nil
  395. }
  396. /*
  397. Get all of the images from the datastore
  398. */
  399. func (s *SQLiteRepo) GetAllImages() []Image {
  400. rows, err := s.db.Query("SELECT * FROM images")
  401. if err != nil {
  402. log.Fatal(err)
  403. }
  404. imgs := []Image{}
  405. for rows.Next() {
  406. var img Image
  407. var rowNum int
  408. err := rows.Scan(&rowNum, &img.Ident, &img.Title, &img.Desc, &img.Created, &img.Category)
  409. if err != nil {
  410. log.Fatal(err)
  411. }
  412. b, err := s.imageIO.Get(img.Ident)
  413. if err != nil {
  414. log.Fatal(err)
  415. }
  416. imgs = append(imgs, Image{Ident: img.Ident, Title: img.Title, Desc: img.Desc, Data: b, Created: img.Created, Category: img.Category})
  417. }
  418. err = rows.Err()
  419. if err != nil {
  420. log.Fatal(err)
  421. }
  422. return imgs
  423. }
  424. func (s *SQLiteRepo) GetImagesByCategory(category string) []Image {
  425. rows, err := s.db.Query("SELECT * FROM images WHERE category = ?", category)
  426. if err != nil {
  427. log.Fatal(err)
  428. }
  429. imgs := []Image{}
  430. for rows.Next() {
  431. var img Image
  432. var rowNum int
  433. err := rows.Scan(&rowNum, &img.Ident, &img.Title, &img.Desc, &img.Created, &img.Category)
  434. if err != nil {
  435. log.Fatal(err)
  436. }
  437. b, err := s.imageIO.Get(img.Ident)
  438. if err != nil {
  439. log.Fatal(err)
  440. }
  441. imgs = append(imgs, Image{Ident: img.Ident, Title: img.Title, Desc: img.Desc, Data: b, Created: img.Created, Category: img.Category})
  442. }
  443. err = rows.Err()
  444. if err != nil {
  445. log.Fatal(err)
  446. }
  447. return imgs
  448. }
  449. /*
  450. Add an image to the database
  451. :param title: the title of the image
  452. :param location: the location to save the image to
  453. :param desc: the description of the image, if any
  454. :param data: the binary data for the image
  455. */
  456. func (s *SQLiteRepo) AddImage(data []byte, title string, desc string, category string) (Identifier, error) {
  457. id := newIdentifier()
  458. err := s.imageIO.Put(data, id)
  459. if err != nil {
  460. return Identifier(""), err
  461. }
  462. _, err = s.db.Exec("INSERT INTO images (id, title, desc, created, category) VALUES (?,?,?,?,?)", string(id), title, desc, time.Now().String(), category)
  463. if err != nil {
  464. return Identifier(""), err
  465. }
  466. return id, nil
  467. }
  468. /*
  469. Updates a document in the database with the supplied. Only changes the title, the body, category. Keys off of the documents Identifier
  470. :param doc: the Document to upload into the database
  471. */
  472. func (s *SQLiteRepo) UpdateDocument(doc Document) error {
  473. tx, err := s.db.Begin()
  474. if err != nil {
  475. return err
  476. }
  477. stmt, err := tx.Prepare("UPDATE posts SET title = ?, body = ?, category = ?, sample = ? WHERE id = ?;")
  478. if err != nil {
  479. tx.Rollback()
  480. return err
  481. }
  482. res, err := stmt.Exec(doc.Title, doc.Body, doc.Category, doc.MakeSample(), doc.Ident)
  483. if err != nil {
  484. tx.Rollback()
  485. return err
  486. }
  487. affected, _ := res.RowsAffected()
  488. if affected != 1 {
  489. return ErrNotExists
  490. }
  491. tx.Commit()
  492. return nil
  493. }
  494. /*
  495. Adds a LinkPair to the menu database table
  496. :param item: the LinkPair to upload
  497. */
  498. func (s *SQLiteRepo) AddMenuItem(item LinkPair) error {
  499. tx, err := s.db.Begin()
  500. if err != nil {
  501. return err
  502. }
  503. fmt.Printf("from AddMenuItem: %+v\n", item)
  504. foundItem, found := s.GetMenuItemByName(item.Link, item.Text)
  505. if found {
  506. fmt.Printf("from error in AddMenuItem: %+v\n", foundItem)
  507. tx.Rollback()
  508. return ErrDuplicate
  509. }
  510. stmt, _ := tx.Prepare("INSERT INTO menu(link, text) VALUES (?,?)")
  511. _, err = stmt.Exec(item.Link, item.Text)
  512. if err != nil {
  513. tx.Rollback()
  514. return err
  515. }
  516. tx.Commit()
  517. return nil
  518. }
  519. /*
  520. Adds an item to the navbar database table
  521. :param item: the NavBarItem to upload
  522. */
  523. func (s *SQLiteRepo) AddNavbarItem(item NavBarItem) error {
  524. tx, err := s.db.Begin()
  525. if err != nil {
  526. return err
  527. }
  528. _, found := s.GetNavbarLink(item.Link, item.Redirect)
  529. if found {
  530. tx.Rollback()
  531. return ErrDuplicate
  532. }
  533. stmt, err := tx.Prepare("INSERT INTO navbar(png, link, redirect) VALUES (?,?,?)")
  534. if err != nil {
  535. tx.Rollback()
  536. return err
  537. }
  538. _, err = stmt.Exec(item.Png, item.Link, item.Redirect)
  539. if err != nil {
  540. tx.Rollback()
  541. return err
  542. }
  543. tx.Commit()
  544. return nil
  545. }
  546. /*
  547. Adds an asset to the asset database table asset
  548. :param name: the name of the asset (filename)
  549. :param data: the byte array of the PNG to upload TODO: limit this to 256kb
  550. */
  551. func (s *SQLiteRepo) AddAsset(name string, data []byte) error {
  552. tx, err := s.db.Begin()
  553. if err != nil {
  554. return err
  555. }
  556. _, found := s.GetAsset(name)
  557. if found {
  558. tx.Rollback()
  559. return ErrDuplicate
  560. }
  561. stmt, _ := tx.Prepare("INSERT INTO assets(name, data) VALUES (?,?)")
  562. _, err = stmt.Exec(name, data)
  563. if err != nil {
  564. tx.Rollback()
  565. return err
  566. }
  567. tx.Commit()
  568. return nil
  569. }
  570. /*
  571. Adds a document to the database (for text posts)
  572. :param doc: the Document to add
  573. */
  574. func (s *SQLiteRepo) AddDocument(doc Document) (Identifier, error) {
  575. id := newIdentifier()
  576. tx, err := s.db.Begin()
  577. if err != nil {
  578. return Identifier(""), err
  579. }
  580. stmt, _ := tx.Prepare("INSERT INTO posts(id, title, created, body, category, sample) VALUES (?,?,?,?,?,?)")
  581. _, err = stmt.Exec(id, doc.Title, doc.Created, doc.Body, doc.Category, doc.MakeSample())
  582. if err != nil {
  583. tx.Rollback()
  584. return Identifier(""), err
  585. }
  586. tx.Commit()
  587. return id, nil
  588. }
  589. /*
  590. Add an entry to the 'admin' table in the database
  591. :param item: an admin table k/v text to redirect pair
  592. :param tableName: the name of the table to populate the link in on the UI
  593. */
  594. func (s *SQLiteRepo) AddAdminTableEntry(item TableData, category string) error {
  595. tx, err := s.db.Begin()
  596. if err != nil {
  597. return err
  598. }
  599. _, found := s.GetAdminTableEntry(item.DisplayName, item.Link, category)
  600. if found {
  601. tx.Rollback()
  602. return ErrDuplicate
  603. }
  604. stmt, _ := tx.Prepare("INSERT INTO admin (display_name, link, category) VALUES (?,?,?)")
  605. _, err = stmt.Exec(item.DisplayName, item.Link, category)
  606. if err != nil {
  607. tx.Rollback()
  608. return err
  609. }
  610. tx.Commit()
  611. return nil
  612. }
  613. /*
  614. Delete a document from the db
  615. :param id: the identifier of the document to remove
  616. */
  617. func (s *SQLiteRepo) DeleteDocument(id Identifier) error {
  618. tx, err := s.db.Begin()
  619. if err != nil {
  620. return err
  621. }
  622. stmt, _ := tx.Prepare("DELETE FROM posts WHERE id=?")
  623. _, err = stmt.Exec(id)
  624. if err != nil {
  625. tx.Rollback()
  626. return err
  627. }
  628. tx.Commit()
  629. return nil
  630. }
  631. /*
  632. Delete navigation bar item
  633. :param id: the name or 'id' of the navbar item to remove
  634. */
  635. func (s *SQLiteRepo) DeleteNavbarItem(id Identifier) error {
  636. tx, err := s.db.Begin()
  637. if err != nil {
  638. return err
  639. }
  640. stmt, _ := tx.Prepare("DELETE FROM navbar WHERE link=?")
  641. result, err := stmt.Exec(id)
  642. if err != nil {
  643. tx.Rollback()
  644. return err
  645. }
  646. rowsAffected, _ := result.RowsAffected()
  647. if rowsAffected < 1 {
  648. return ErrNotExists
  649. }
  650. tx.Commit()
  651. return nil
  652. }
  653. // Get all Hosts from the host table
  654. func (s *SQLiteRepo) AllDocuments() []Document {
  655. rows, err := s.db.Query("SELECT * FROM posts")
  656. if err != nil {
  657. fmt.Printf("There was an issue getting all posts. %s", err.Error())
  658. return nil
  659. }
  660. defer rows.Close()
  661. all := []Document{}
  662. for rows.Next() {
  663. var post Document
  664. if err := rows.Scan(&post.Row, &post.Ident, &post.Title, &post.Created, &post.Body, &post.Category, &post.Sample); err != nil {
  665. fmt.Printf("There was an error getting all documents. %s", err.Error())
  666. return nil
  667. }
  668. all = append(all, post)
  669. }
  670. return all
  671. }
  672. type InvalidSkipArg struct{ Skip int }
  673. func (i *InvalidSkipArg) Error() string {
  674. return fmt.Sprintf("Invalid skip amount was passed: %v", i.Skip)
  675. }
  676. type ImageStoreItem struct {
  677. Identifier string `json:"identifier"`
  678. Filename string `json:"filename"`
  679. AbsolutePath string `json:"absolute_path"`
  680. Title string `json:"title" form:"title"`
  681. Created string `json:"created"`
  682. Desc string `json:"description" form:"description"`
  683. Category string `json:"category"`
  684. ApiPath string
  685. }
  686. /*
  687. Function to return the location of the image store. Wrapping the env call in
  688. a function so that refactoring is easier
  689. */
  690. func GetImageStore() string {
  691. return os.Getenv(env.IMAGE_STORE)
  692. }
  693. // Wrapping the new id call in a function to make refactoring easier
  694. func newIdentifier() Identifier {
  695. return Identifier(uuid.NewString())
  696. }