log.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package admin
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/metarare/metarare_api/common"
  6. "github.com/metarare/metarare_api/helpers/gauth"
  7. "github.com/metarare/metarare_api/helpers/gerror"
  8. "github.com/metarare/metarare_api/models"
  9. "gorm.io/gorm"
  10. )
  11. type AdminLogV1Router struct {
  12. group *gin.RouterGroup
  13. mDB *gorm.DB
  14. rDB *gorm.DB
  15. }
  16. func NewAdminLogV1Router(r common.Router, basePath string) AdminLogV1Router {
  17. l := AdminLogV1Router{
  18. group: r.Version.Group(basePath),
  19. mDB: r.Db.MasterDB,
  20. rDB: r.Db.ReadDB,
  21. }
  22. l.group.GET("", l.getLog)
  23. return l
  24. }
  25. // getLog godoc
  26. // @Summary get log list
  27. // @Description 어드민 로그 가져오기
  28. // @Schemes
  29. // @security ApiKeyAuth
  30. // @Tags admin
  31. // @Accept json
  32. // @Produce json
  33. // @Success 200 {object} models.AdminLog
  34. // @Router /admin/log [get]
  35. func (l AdminLogV1Router) getLog(c *gin.Context) {
  36. admin, err := gauth.ConfirmAdminInfo(c, l.rDB)
  37. if err != nil || admin.ID == 0 {
  38. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  39. return
  40. }
  41. if admin.AdminPermission.Log == 0 {
  42. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  43. return
  44. }
  45. response := []models.AdminLog{}
  46. if err := l.rDB.Order("id desc").Find(&response).Error; err != nil {
  47. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  48. return
  49. }
  50. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, response, nil)
  51. return
  52. }