module_blog_act_modify.go 5.3 KB

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