module_blog_act_modify.go 4.5 KB

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