module_blog_act_modify.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. ctx,
  54. `INSERT INTO blog_posts SET
  55. user = ?,
  56. name = ?,
  57. alias = ?,
  58. category = ?,
  59. briefly = ?,
  60. content = ?,
  61. datetime = ?,
  62. active = ?
  63. ;`,
  64. wrap.User.A_id,
  65. pf_name,
  66. pf_alias,
  67. utils.StrToInt(pf_category),
  68. pf_briefly,
  69. pf_content,
  70. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  71. utils.StrToInt(pf_active),
  72. )
  73. if err != nil {
  74. return err
  75. }
  76. // Get inserted post id
  77. lastID, err = res.LastInsertId()
  78. if err != nil {
  79. return err
  80. }
  81. // Block rows
  82. if _, err := tx.Exec(ctx, "SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", lastID); err != nil {
  83. return err
  84. }
  85. // Insert post and categories relations
  86. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  87. if len(catids) > 0 {
  88. var catsCount int
  89. err = tx.QueryRow(`
  90. SELECT
  91. COUNT(*)
  92. FROM
  93. blog_cats
  94. WHERE
  95. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  96. FOR UPDATE;`,
  97. ).Scan(
  98. &catsCount,
  99. )
  100. if *wrap.LogCpError(&err) != nil {
  101. return err
  102. }
  103. if len(catids) != catsCount {
  104. return errors.New("Inner system error")
  105. }
  106. var balkInsertArr []string
  107. for _, el := range catids {
  108. balkInsertArr = append(balkInsertArr, `(`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  109. }
  110. if _, err = tx.Exec(
  111. ctx,
  112. `INSERT INTO blog_cat_post_rel (post_id,category_id) VALUES `+strings.Join(balkInsertArr, ",")+`;`,
  113. ); err != nil {
  114. return err
  115. }
  116. }
  117. return nil
  118. }); err != nil {
  119. wrap.MsgError(err.Error())
  120. return
  121. }
  122. wrap.ResetCacheBlocks()
  123. wrap.Write(`window.location='/cp/blog/modify/` + utils.Int64ToStr(lastID) + `/';`)
  124. } else {
  125. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  126. // Block rows
  127. if _, err := tx.Exec(ctx, "SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  128. return err
  129. }
  130. if _, err := tx.Exec(ctx, "SELECT post_id FROM blog_cat_post_rel WHERE post_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  131. return err
  132. }
  133. // Update row
  134. if _, err := tx.Exec(
  135. ctx,
  136. `UPDATE blog_posts SET
  137. name = ?,
  138. alias = ?,
  139. category = ?,
  140. briefly = ?,
  141. content = ?,
  142. active = ?
  143. WHERE
  144. id = ?
  145. ;`,
  146. pf_name,
  147. pf_alias,
  148. utils.StrToInt(pf_category),
  149. pf_briefly,
  150. pf_content,
  151. utils.StrToInt(pf_active),
  152. utils.StrToInt(pf_id),
  153. ); err != nil {
  154. return err
  155. }
  156. // Delete post and categories relations
  157. if _, err := tx.Exec(ctx, "DELETE FROM blog_cat_post_rel WHERE post_id = ?;", utils.StrToInt(pf_id)); err != nil {
  158. return err
  159. }
  160. // Insert post and categories relations
  161. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  162. if len(catids) > 0 {
  163. var catsCount int
  164. err := tx.QueryRow(`
  165. SELECT
  166. COUNT(*)
  167. FROM
  168. blog_cats
  169. WHERE
  170. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  171. FOR UPDATE;`,
  172. ).Scan(
  173. &catsCount,
  174. )
  175. if *wrap.LogCpError(&err) != nil {
  176. return err
  177. }
  178. if len(catids) != catsCount {
  179. return errors.New("Inner system error")
  180. }
  181. var balkInsertArr []string
  182. for _, el := range catids {
  183. balkInsertArr = append(balkInsertArr, `(`+pf_id+`,`+utils.IntToStr(el)+`)`)
  184. }
  185. if _, err := tx.Exec(
  186. ctx,
  187. `INSERT INTO blog_cat_post_rel (post_id,category_id) VALUES `+strings.Join(balkInsertArr, ",")+`;`,
  188. ); err != nil {
  189. return err
  190. }
  191. }
  192. return nil
  193. }); err != nil {
  194. wrap.MsgError(err.Error())
  195. return
  196. }
  197. wrap.ResetCacheBlocks()
  198. wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
  199. }
  200. })
  201. }