module_blog_act_modify.go 4.5 KB

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