module_blog_act_modify.go 5.2 KB

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