doc_article_api.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package v1
  2. import (
  3. "github.com/druidcaesa/gotool"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "ulink-admin/frame"
  8. "ulink-admin/modules/doc/models/model"
  9. "ulink-admin/modules/doc/models/req"
  10. "ulink-admin/modules/doc/service"
  11. "ulink-admin/pkg/excels"
  12. "ulink-admin/pkg/file"
  13. "ulink-admin/pkg/page"
  14. "ulink-admin/pkg/resp"
  15. )
  16. type DocArticleApi struct {
  17. docArticleService service.DocArticleService
  18. }
  19. // List 查询文章分页数据
  20. // @Summary 分页查询文章数据接口
  21. // @Description 分页查询文章数据接口
  22. // @Tags 文章相关接口
  23. // @Accept application/json
  24. // @Produce application/json
  25. // @Param Authorization header string false "Bearer 令牌"
  26. // @Param object query req.DocArticleQuery false "查询参数"
  27. // @Security ApiKeyAuth
  28. // @Success 200 {object} resp.Response{data=page.Page{list=model.DocArticle},msg=string} "分页获取文章列表,返回包括列表,总数,页码,每页数量"
  29. // @Router /docarticle/list [get]
  30. func (a DocArticleApi) List(c *frame.Context) {
  31. query := req.DocArticleQuery{}
  32. if c.Bind(&query) != nil {
  33. resp.ParamError(c)
  34. return
  35. }
  36. list, i := a.docArticleService.FindList(query)
  37. resp.OK(c, page.Page{
  38. List: list,
  39. Total: i,
  40. Size: query.PageSize,
  41. })
  42. }
  43. // List 查询文章所有数据
  44. // @Summary 查询全部数据文章数据接口
  45. // @Description 查询全部数据文章数据接口
  46. // @Tags 文章相关接口
  47. // @Accept application/json
  48. // @Produce application/json
  49. // @Param Authorization header string false "Bearer 令牌"
  50. // @Security ApiKeyAuth
  51. // @Success 200 {object} resp.Response{data=model.DocArticle,msg=string} "分页获取文章列表,返回包括列表,总数,页码,每页数量"
  52. // @Router /docarticle/listAll [get]
  53. func (a DocArticleApi) ListAll(c *frame.Context) {
  54. list := a.docArticleService.FindAll()
  55. resp.OK(c, list)
  56. }
  57. // Get 根据文章Id获取详细信息
  58. // @Summary 文章详情查询接口
  59. // @Description 文章详情查询接口
  60. // @Tags 文章相关接口
  61. // @Accept application/json
  62. // @Produce application/json
  63. // @Param Authorization header string false "Bearer 令牌"
  64. // @Param id path int true "id" id
  65. // @Security ApiKeyAuth
  66. // @Success 200 {object} resp.Response{data=model.DocArticle,msg=string} "返回文章详情查询"
  67. // @Router /docarticle [get]
  68. func (a DocArticleApi) Get(c *frame.Context) {
  69. param := c.Param("id")
  70. id, _ := strconv.ParseInt(param, 10, 64)
  71. resp.OK(c, a.docArticleService.GetDocArticleById(id))
  72. }
  73. // Add 新增文章
  74. // @Summary 新增文章接口
  75. // @Description 新增文章接口
  76. // @Tags 文章相关接口
  77. // @Accept application/json
  78. // @Produce application/json
  79. // @Param Authorization header string false "Bearer 令牌"
  80. // @Param data body model.DocArticle true "文章实体对象"
  81. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  82. // @Router /docarticle/add [post]
  83. func (a DocArticleApi) Add(c *frame.Context) {
  84. docArticle := model.DocArticle{}
  85. if err := c.Bind(&docArticle); err != nil {
  86. resp.ParamError(c, map[string]string{"error": err.Error()})
  87. return
  88. }
  89. docArticle.CreateBy = admin.GetUserInfo(c).UserName
  90. docArticle.CreateTime = time.Now()
  91. docArticle.UpdateBy = admin.GetUserInfo(c).UserName
  92. docArticle.UpdateTime = time.Now()
  93. if a.docArticleService.Insert(&docArticle) {
  94. resp.OK(c)
  95. } else {
  96. resp.Error(c)
  97. }
  98. }
  99. // Edit 修改文章数据接口
  100. // @Summary 修改文章接口
  101. // @Description 新增文章接口
  102. // @Tags 文章相关接口
  103. // @Accept application/json
  104. // @Produce application/json
  105. // @Param Authorization header string false "Bearer 令牌"
  106. // @Param data body model.DocArticle true "文章实体对象"
  107. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  108. // @Router /docarticle/edit [put]
  109. func (a DocArticleApi) Edit(c *frame.Context) {
  110. docArticle := model.DocArticle{}
  111. if c.Bind(&docArticle) != nil {
  112. resp.ParamError(c)
  113. return
  114. }
  115. docArticle.UpdateBy = admin.GetUserInfo(c).UserName
  116. docArticle.UpdateTime = time.Now()
  117. if a.docArticleService.Update(&docArticle) {
  118. resp.OK(c)
  119. } else {
  120. resp.Error(c)
  121. }
  122. }
  123. // Delete 删除文章数据
  124. // @Summary 删除文章接口
  125. // @Description 删除文章接口
  126. // @Tags 文章相关接口
  127. // @Accept application/json
  128. // @Produce application/json
  129. // @Param Authorization header string false "Bearer 令牌"
  130. // @Param id path int true "id" id
  131. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  132. // @Router /docarticle [delete]
  133. func (a DocArticleApi) Delete(c *frame.Context) {
  134. //获取docArticleId
  135. param := c.Param("id")
  136. list := make([]int64, 0)
  137. if gotool.StrUtils.HasNotEmpty(param) {
  138. strs := strings.Split(param, ",")
  139. for _, str := range strs {
  140. id, _ := strconv.ParseInt(str, 10, 64)
  141. list = append(list, id)
  142. }
  143. }
  144. //判断是否可以删除
  145. if a.docArticleService.Delete(list) {
  146. resp.OK(c)
  147. } else {
  148. resp.Error(c)
  149. }
  150. }
  151. // Export 导出excel
  152. func (a DocArticleApi) Export(c *frame.Context) {
  153. query := req.DocArticleQuery{}
  154. if c.Bind(&query) != nil {
  155. resp.ParamError(c)
  156. return
  157. }
  158. list, _ := a.docArticleService.FindList(query)
  159. excelList := make([]interface{}, 0)
  160. for _, docArticle := range *list {
  161. excelList = append(excelList, docArticle)
  162. }
  163. _, files := excels.ExportExcel(excelList, "文章数据表")
  164. file.DownloadExcel(c, files)
  165. }