material.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package back
  2. import (
  3. "database/sql"
  4. "errors"
  5. "html/template"
  6. )
  7. type limit struct {
  8. Limit int `json:"Limit"`
  9. Offset int `json:"Offset"`
  10. }
  11. type search struct {
  12. Phrase string `json:"Phrase"`
  13. }
  14. type material struct {
  15. Link string `json:"Link"`
  16. Caption string `json:"Caption"`
  17. Content template.HTML `json:"Content"`
  18. Source string `json:"Source"`
  19. SourceImages string `json:"SourceImages"`
  20. Preview sql.NullString `json:"Preview"`
  21. Image sql.NullString `json:"Image"`
  22. Published string `json:"Published"`
  23. Active bool `json:"Active"`
  24. Category string `json:"Category"`
  25. Author int32 `json:"Author"`
  26. Rows []material
  27. }
  28. func prepareQueriesMaterial() []string {
  29. var e error
  30. var ebox []string
  31. queries["Insert#Material#"], e = db.Prepare(`INSERT INTO "Material"(
  32. "Link", "Caption", "Content", "Source", "SourceImages", "Preview", "Image", "Published", "Active", "Category", "Author")
  33. VALUES ($1, $2, $3, $4, $5, $6, $7, CURRENT_TIMESTAMP, TRUE, $8, $9)`)
  34. if e != nil {
  35. ebox = append(ebox, "Insert#Material#"+e.Error())
  36. }
  37. queries["Update#Material#"], e = db.Prepare(`UPDATE "Material" SET "Caption"=$1, "Content"=$2, "Source"=$3, "Preview"=$4 WHERE "Link"=$5`)
  38. if e != nil {
  39. ebox = append(ebox, "Update#Material#"+e.Error())
  40. }
  41. queries["Delete#Material#"], e = db.Prepare(`UPDATE "Material" SET "Active"=$2 WHERE "Link"=$1`)
  42. if e != nil {
  43. ebox = append(ebox, "Delete#Material#"+e.Error())
  44. }
  45. queries["Select#Material#"], e = db.Prepare(`SELECT "Link", "Caption", "Category", "Active" FROM "Material" ORDER BY "Published" DESC`)
  46. if e != nil {
  47. ebox = append(ebox, "Select#Material# - "+e.Error())
  48. }
  49. queries["Select#Material#Limit"], e = db.Prepare(`SELECT "Link", "Caption", "Preview", "Image", "Published" FROM "Material" WHERE "Active"=TRUE AND "Category"=$3 ORDER BY "Published" DESC OFFSET $1 LIMIT $2`)
  50. if e != nil {
  51. ebox = append(ebox, "Select#Material#Limit - "+e.Error())
  52. }
  53. queries["Select#Material#LimitWithContent"], e = db.Prepare(`SELECT "Link", "Caption", "Preview", "Content", "Image", "Published" FROM "Material" WHERE "Active"=TRUE AND "Category"=$3 ORDER BY "Published" DESC OFFSET $1 LIMIT $2`)
  54. if e != nil {
  55. ebox = append(ebox, "Select#Material#LimitWithContent - "+e.Error())
  56. }
  57. queries["Select#Material#One"], e = db.Prepare(`SELECT "Caption", "Content", "Published", "Category", "Active" FROM "Material" WHERE "Link"=$1`)
  58. if e != nil {
  59. ebox = append(ebox, "Select#Material#One - "+e.Error())
  60. }
  61. queries["Select#Material#Count"], e = db.Prepare(`SELECT count(*) FROM "Material" WHERE "Active"=TRUE AND "Category"=$1`)
  62. if e != nil {
  63. ebox = append(ebox, "Select#Material#Count - "+e.Error())
  64. }
  65. queries["Select#Material#CountAll"], e = db.Prepare(`SELECT count(*) FROM "Material" WHERE "Category"=$1`)
  66. if e != nil {
  67. ebox = append(ebox, "Select#Material#CountAll - "+e.Error())
  68. }
  69. queries["Select#Material#Search"], e = db.Prepare(`SELECT "Link", "Caption", "Published", "Category" FROM "Material" WHERE ("Caption" ILIKE $1 OR "Content" ILIKE $1) AND "Active"=TRUE`)
  70. if e != nil {
  71. ebox = append(ebox, "Select#Material#Search - "+e.Error())
  72. }
  73. queries["Select#Material#Source"], e = db.Prepare(`SELECT "Source", "SourceImages", "Category", "Caption" FROM "Material" WHERE "Link"=$1`)
  74. if e != nil {
  75. ebox = append(ebox, "Select#Material#Source - "+e.Error())
  76. }
  77. return ebox
  78. }
  79. func (m *material) Insert() error {
  80. stmt, ok := queries["Insert#Material#"]
  81. if !ok {
  82. return errors.New("Запрос Insert#Material# не найден")
  83. }
  84. logger.Println(m.SourceImages)
  85. _, e := stmt.Exec(m.Link, m.Caption, m.Content, m.Source, m.SourceImages, m.Preview, m.Image, m.Category, m.Author)
  86. if e != nil {
  87. return e
  88. }
  89. return nil
  90. }
  91. func (m *material) Update() error {
  92. stmt, ok := queries["Update#Material#"]
  93. if !ok {
  94. return errors.New("Запрос Update#Material# не найден")
  95. }
  96. _, e := stmt.Exec(m.Caption, m.Content, m.Source, m.Preview, m.Link)
  97. if e != nil {
  98. return e
  99. }
  100. return nil
  101. }
  102. func (m *material) Delete() error {
  103. stmt, ok := queries["Delete#Material#"]
  104. if !ok {
  105. return errors.New("Запрос Delete#Material# не найден")
  106. }
  107. _, e := stmt.Exec(m.Link, m.Active)
  108. if e != nil {
  109. return e
  110. }
  111. return nil
  112. }
  113. func (m *material) Select(offset, limit int) error {
  114. stmt, ok := queries["Select#Material#Limit"]
  115. if !ok {
  116. return errors.New("Запрос Select#Material#Limit не найден")
  117. }
  118. rows, e := stmt.Query(offset, limit, m.Category)
  119. if e != nil {
  120. return e
  121. }
  122. defer rows.Close()
  123. for rows.Next() {
  124. // "Link", "Caption", "Content", "Image", "Published"
  125. e = rows.Scan(
  126. &m.Link,
  127. &m.Caption,
  128. &m.Preview,
  129. &m.Image,
  130. &m.Published,
  131. )
  132. if e != nil {
  133. return e
  134. }
  135. m.Rows = append(m.Rows, material{
  136. Link: m.Link,
  137. Caption: m.Caption,
  138. Preview: m.Preview,
  139. Image: m.Image,
  140. Published: m.Published,
  141. })
  142. }
  143. return nil
  144. }
  145. func (m *material) SelectWithContent(offset, limit int) error {
  146. stmt, ok := queries["Select#Material#LimitWithContent"]
  147. if !ok {
  148. return errors.New("Запрос Select#Material#LimitWithContent не найден")
  149. }
  150. rows, e := stmt.Query(offset, limit, m.Category)
  151. if e != nil {
  152. return e
  153. }
  154. defer rows.Close()
  155. for rows.Next() {
  156. // "Link", "Caption", "Content", "Image", "Published"
  157. e = rows.Scan(
  158. &m.Link,
  159. &m.Caption,
  160. &m.Preview,
  161. &m.Content,
  162. &m.Image,
  163. &m.Published,
  164. )
  165. if e != nil {
  166. return e
  167. }
  168. m.Rows = append(m.Rows, material{
  169. Link: m.Link,
  170. Caption: m.Caption,
  171. Preview: m.Preview,
  172. Content: m.Content,
  173. Image: m.Image,
  174. Published: m.Published,
  175. })
  176. }
  177. return nil
  178. }
  179. func (m *material) SelectAll() error {
  180. stmt, ok := queries["Select#Material#"]
  181. if !ok {
  182. return errors.New("Запрос Select#Material# не найден")
  183. }
  184. rows, e := stmt.Query()
  185. if e != nil {
  186. return e
  187. }
  188. defer rows.Close()
  189. for rows.Next() {
  190. e = rows.Scan(
  191. &m.Link,
  192. &m.Caption,
  193. &m.Category,
  194. &m.Active,
  195. )
  196. if e != nil {
  197. return e
  198. }
  199. m.Rows = append(m.Rows, material{
  200. Link: m.Link,
  201. Caption: m.Caption,
  202. Category: m.Category,
  203. Active: m.Active,
  204. })
  205. }
  206. return nil
  207. }
  208. func (m *material) SelectOne() error {
  209. stmt, ok := queries["Select#Material#One"]
  210. if !ok {
  211. return errors.New("Запрос Select#Material#One не найден")
  212. }
  213. row := stmt.QueryRow(m.Link)
  214. // "Link", "Caption", "Content", "Image", "Published"
  215. e := row.Scan(
  216. &m.Caption,
  217. &m.Content,
  218. &m.Published,
  219. &m.Category,
  220. &m.Active,
  221. )
  222. if e != nil {
  223. return e
  224. }
  225. m.Published = "Опубликовано " + getDate(m.Published)
  226. return nil
  227. }
  228. func (m *material) SelectCount() (int, error) {
  229. stmt, ok := queries["Select#Material#Count"]
  230. if !ok {
  231. return 0, errors.New("Запрос Select#Material#Count не найден")
  232. }
  233. row := stmt.QueryRow(m.Category)
  234. var count int
  235. e := row.Scan(&count)
  236. if e != nil {
  237. return 0, e
  238. }
  239. return count, nil
  240. }
  241. func (m *material) SelectCountAll() (int, error) {
  242. stmt, ok := queries["Select#Material#CountAll"]
  243. if !ok {
  244. return 0, errors.New("Запрос Select#Material#CountAll не найден")
  245. }
  246. row := stmt.QueryRow(m.Category)
  247. var count int
  248. e := row.Scan(&count)
  249. if e != nil {
  250. return 0, e
  251. }
  252. return count, nil
  253. }
  254. func (m *material) Search(phrase string) error {
  255. stmt, ok := queries["Select#Material#Search"]
  256. if !ok {
  257. return errors.New("Запрос Select#Material#Search не найден")
  258. }
  259. ilike := "%" + phrase + "%"
  260. rows, e := stmt.Query(ilike)
  261. if e != nil {
  262. logger.Println("ERROR", e)
  263. return e
  264. }
  265. defer rows.Close()
  266. for rows.Next() {
  267. // "Link", "Caption", "Published", "Category"
  268. e = rows.Scan(
  269. &m.Link,
  270. &m.Caption,
  271. &m.Published,
  272. &m.Category,
  273. )
  274. if e != nil {
  275. logger.Println("ERROR", e)
  276. return e
  277. }
  278. m.Rows = append(m.Rows, material{
  279. Link: m.Link,
  280. Caption: m.Caption,
  281. Published: m.Published,
  282. Category: m.Category,
  283. })
  284. }
  285. return nil
  286. }
  287. func (m *material) SelectSource() error {
  288. stmt, ok := queries["Select#Material#Source"]
  289. if !ok {
  290. return errors.New("Запрос Select#Material#Source не найден")
  291. }
  292. row := stmt.QueryRow(m.Link)
  293. e := row.Scan(&m.Source, &m.SourceImages, &m.Category, &m.Caption)
  294. if e != nil {
  295. return e
  296. }
  297. return nil
  298. }