module_blog_act_modify.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package modules
  2. import (
  3. "errors"
  4. "strings"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. func (this *Modules) RegisterAction_BlogModify() *Action {
  9. return this.newAction(AInfo{
  10. WantDB: true,
  11. Mount: "blog-modify",
  12. WantAdmin: true,
  13. }, func(wrap *wrapper.Wrapper) {
  14. pf_id := wrap.R.FormValue("id")
  15. pf_name := wrap.R.FormValue("name")
  16. pf_alias := wrap.R.FormValue("alias")
  17. pf_content := wrap.R.FormValue("content")
  18. pf_active := wrap.R.FormValue("active")
  19. if pf_active == "" {
  20. pf_active = "0"
  21. }
  22. if !utils.IsNumeric(pf_id) {
  23. wrap.MsgError(`Inner system error`)
  24. return
  25. }
  26. if pf_name == "" {
  27. wrap.MsgError(`Please specify page name`)
  28. return
  29. }
  30. if pf_alias == "" {
  31. pf_alias = utils.GenerateSingleAlias(pf_name)
  32. }
  33. if !utils.IsValidSingleAlias(pf_alias) {
  34. wrap.MsgError(`Please specify correct post alias`)
  35. return
  36. }
  37. if pf_id == "0" {
  38. if err := wrap.DBTrans(func(tx *wrapper.Tx) error {
  39. // Insert row
  40. res, err := tx.Exec(
  41. `INSERT INTO blog_posts SET
  42. user = ?,
  43. name = ?,
  44. alias = ?,
  45. content = ?,
  46. datetime = ?,
  47. active = ?
  48. ;`,
  49. wrap.User.A_id,
  50. pf_name,
  51. pf_alias,
  52. pf_content,
  53. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  54. pf_active,
  55. )
  56. if err != nil {
  57. return err
  58. }
  59. // Get inserted post id
  60. lastID, err := res.LastInsertId()
  61. if err != nil {
  62. return err
  63. }
  64. // Block rows
  65. if _, err := tx.Exec("SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", lastID); err != nil {
  66. return err
  67. }
  68. // Insert post and categories relations
  69. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  70. if len(catids) > 0 {
  71. var catsCount int
  72. err = tx.QueryRow(`
  73. SELECT
  74. COUNT(*)
  75. FROM
  76. blog_cats
  77. WHERE
  78. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  79. FOR UPDATE;`,
  80. ).Scan(
  81. &catsCount,
  82. )
  83. if err != nil {
  84. return err
  85. }
  86. if len(catids) != catsCount {
  87. return errors.New("Inner system error")
  88. }
  89. var balkInsertArr []string
  90. for _, el := range catids {
  91. balkInsertArr = append(balkInsertArr, `(NULL,`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  92. }
  93. if _, err = tx.Exec(
  94. `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  95. ); err != nil {
  96. return err
  97. }
  98. }
  99. return nil
  100. }); err != nil {
  101. wrap.MsgError(err.Error())
  102. return
  103. }
  104. wrap.Write(`window.location='/cp/blog/';`)
  105. } else {
  106. if err := wrap.DBTrans(func(tx *wrapper.Tx) error {
  107. // Block rows
  108. if _, err := tx.Exec("SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", pf_id); err != nil {
  109. return err
  110. }
  111. if _, err := tx.Exec("SELECT id FROM blog_cat_post_rel WHERE post_id = ? FOR UPDATE;", pf_id); err != nil {
  112. return err
  113. }
  114. // Update row
  115. if _, err := tx.Exec(
  116. `UPDATE blog_posts SET
  117. name = ?,
  118. alias = ?,
  119. content = ?,
  120. active = ?
  121. WHERE
  122. id = ?
  123. ;`,
  124. pf_name,
  125. pf_alias,
  126. pf_content,
  127. pf_active,
  128. utils.StrToInt(pf_id),
  129. ); err != nil {
  130. return err
  131. }
  132. // Delete post and categories relations
  133. if _, err := tx.Exec("DELETE FROM blog_cat_post_rel WHERE post_id = ?;", pf_id); err != nil {
  134. return err
  135. }
  136. // Insert post and categories relations
  137. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  138. if len(catids) > 0 {
  139. var catsCount int
  140. err := tx.QueryRow(`
  141. SELECT
  142. COUNT(*)
  143. FROM
  144. blog_cats
  145. WHERE
  146. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  147. FOR UPDATE;`,
  148. ).Scan(
  149. &catsCount,
  150. )
  151. if err != nil {
  152. return err
  153. }
  154. if len(catids) != catsCount {
  155. return errors.New("Inner system error")
  156. }
  157. var balkInsertArr []string
  158. for _, el := range catids {
  159. balkInsertArr = append(balkInsertArr, `(NULL,`+pf_id+`,`+utils.IntToStr(el)+`)`)
  160. }
  161. if _, err := tx.Exec(
  162. `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  163. ); err != nil {
  164. return err
  165. }
  166. }
  167. return nil
  168. }); err != nil {
  169. wrap.MsgError(err.Error())
  170. return
  171. }
  172. wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
  173. }
  174. })
  175. }