module_blog_act_modify.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package modules
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "golang-fave/engine/utils"
  7. "golang-fave/engine/wrapper"
  8. )
  9. func (this *Modules) RegisterAction_BlogModify() *Action {
  10. return this.newAction(AInfo{
  11. Mount: "blog-modify",
  12. WantAdmin: true,
  13. }, func(wrap *wrapper.Wrapper) {
  14. pf_id := utils.Trim(wrap.R.FormValue("id"))
  15. pf_name := utils.Trim(wrap.R.FormValue("name"))
  16. pf_alias := utils.Trim(wrap.R.FormValue("alias"))
  17. pf_category := utils.Trim(wrap.R.FormValue("category"))
  18. pf_briefly := utils.Trim(wrap.R.FormValue("briefly"))
  19. pf_content := utils.Trim(wrap.R.FormValue("content"))
  20. pf_active := utils.Trim(wrap.R.FormValue("active"))
  21. if pf_active == "" {
  22. pf_active = "0"
  23. }
  24. if !utils.IsNumeric(pf_id) {
  25. wrap.MsgError(`Inner system error`)
  26. return
  27. }
  28. if !utils.IsNumeric(pf_category) {
  29. wrap.MsgError(`Inner system error`)
  30. return
  31. }
  32. if pf_name == "" {
  33. wrap.MsgError(`Please specify post name`)
  34. return
  35. }
  36. if pf_alias == "" {
  37. pf_alias = utils.GenerateSingleAlias(pf_name)
  38. }
  39. if !utils.IsValidSingleAlias(pf_alias) {
  40. wrap.MsgError(`Please specify correct post alias`)
  41. return
  42. }
  43. // Default is ROOT
  44. if pf_category == "0" {
  45. pf_category = "1"
  46. }
  47. if pf_id == "0" {
  48. var lastID int64 = 0
  49. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  50. // Insert row
  51. res, err := tx.Exec(
  52. ctx,
  53. `INSERT INTO fave_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(ctx, "SELECT id FROM fave_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. ctx,
  90. `SELECT
  91. COUNT(*)
  92. FROM
  93. fave_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 fave_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 fave_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 fave_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 fave_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 fave_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. ctx,
  166. `SELECT
  167. COUNT(*)
  168. FROM
  169. fave_blog_cats
  170. WHERE
  171. id IN(`+strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",")+`)
  172. FOR UPDATE;`,
  173. ).Scan(
  174. &catsCount,
  175. )
  176. if *wrap.LogCpError(&err) != nil {
  177. return err
  178. }
  179. if len(catids) != catsCount {
  180. return errors.New("Inner system error")
  181. }
  182. var balkInsertArr []string
  183. for _, el := range catids {
  184. balkInsertArr = append(balkInsertArr, `(`+pf_id+`,`+utils.IntToStr(el)+`)`)
  185. }
  186. if _, err := tx.Exec(
  187. ctx,
  188. `INSERT INTO fave_blog_cat_post_rel (post_id,category_id) VALUES `+strings.Join(balkInsertArr, ",")+`;`,
  189. ); err != nil {
  190. return err
  191. }
  192. }
  193. return nil
  194. }); err != nil {
  195. wrap.MsgError(err.Error())
  196. return
  197. }
  198. wrap.ResetCacheBlocks()
  199. wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
  200. }
  201. })
  202. }