A More Secure Internet Connection for Your Home https://fen.gg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

81 lines
2.1 KiB

  1. package model
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "fmt"
  20. "github.com/jmoiron/sqlx"
  21. _ "github.com/lib/pq"
  22. )
  23. const (
  24. PatternCategoryListQueryTmpl = `
  25. SELECT * FROM pattern_category_lists %s;`
  26. PatternCategoryListNamedInsertTmpl = `
  27. INSERT INTO pattern_category_lists (
  28. pattern_id, category_id
  29. ) VALUES (
  30. :pattern_id, :category_id
  31. );`
  32. PatternCategoryListCopyCSVQuery = `
  33. copy pattern_category_lists
  34. from '/usr/share/fengg-pattern/pattern-category-lists.csv'
  35. delimiter ',' csv header;`
  36. )
  37. type PatternCategoryList struct {
  38. PatternID uint `db:"pattern_id" json:"patternId"`
  39. CategoryID uint `db:"category_id" json:"categoryId"`
  40. }
  41. type PatternCategoryLists []PatternCategoryList
  42. func NewPatternCategoryList() *PatternCategoryList {
  43. return &PatternCategoryList{}
  44. }
  45. func (list *PatternCategoryList) Exists() bool {
  46. db, err := sqlx.Connect(dbDriver, dbConnect)
  47. if err != nil {
  48. logger.Error().Err(err).Msg("cannot connect to database")
  49. return false
  50. }
  51. defer db.Close()
  52. return db.Get(list, fmt.Sprintf(
  53. PatternCategoryListQueryTmpl,
  54. "WHERE pattern_id = $1 AND category_id = $2",
  55. ), list.PatternID, list.CategoryID) == nil
  56. }
  57. func (list *PatternCategoryList) Create() error {
  58. db, err := sqlx.Connect(dbDriver, dbConnect)
  59. if err != nil {
  60. return err
  61. }
  62. defer db.Close()
  63. rows, err := db.NamedQuery(PatternCategoryListNamedInsertTmpl, list)
  64. if err != nil {
  65. return err
  66. }
  67. defer rows.Close()
  68. return nil
  69. }