diff --git a/handlers/badges.go b/handlers/badges.go index cba5016..4f402e0 100644 --- a/handlers/badges.go +++ b/handlers/badges.go @@ -43,14 +43,11 @@ const ( ColorSchemeBlue = ColorScheme("fill-opacity:1.00; fill:rgb(32, 99, 155);") BadgeTypePrometheus = "PROMETHEUS" pageViewsLabel = "Page Views" - radius = 0 - fontSize = 12 textMarginX = 6.0 textMarginY = 14 badgeHeight = 20 textStyle = "text-anchor:start;font-size:12px;fill:white;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;" badgeExpireSeconds = 10 - labelWidthPortion = 0.65 metricRequestTimeout = 10 * time.Second ErrorMessageInternalError = "Oops, there's something wrong, please try it again later." ) @@ -190,17 +187,23 @@ func (handler *BadgesHandler) queryBadgeFromDB(queryName string) (*models.Badge, return &badge, nil } -// func (handler *BadgesHandler) MyBadges(c *gin.Context) { -// var currentUser *models.User -// userInterface, exists := c.Get("user") -// if !exists { -// logrus.Error("user cannot be found from context") -// c.JSON(http.StatusInternalServerError, gin.H{"msg": ErrorMessageInternalError}) -// return -// } -// currentUser = userInterface.(*models.User) +func (handler *BadgesHandler) MyBadges(c *gin.Context) { + userInterface, exists := c.Get("currentUser") + if !exists { + logrus.Error("user cannot be found from context") + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"msg": ErrorMessageInternalError}) + return + } + currentUser := userInterface.(models.User) + badges := make([]*models.Badge, 0) -// } + if err := handler.DB.Find(&badges, "user_id = ?", currentUser.ID).Error; err != nil { + sentry.CaptureException(err) + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"msg": ErrorMessageInternalError}) + } + + c.JSON(http.StatusOK, badges) +} func (handler *BadgesHandler) CreateBadge(c *gin.Context) { newID := NewULID() diff --git a/handlers/users.go b/handlers/users.go index 6cdff41..7fd39ad 100644 --- a/handlers/users.go +++ b/handlers/users.go @@ -40,6 +40,7 @@ type UserAuthenticationRequest struct { type UserInfo struct { ID string `json:"id"` GithubID string `json:"githubId"` + Token string `json:"token"` } func (usersHandler *UsersHandler) AuthRequired(c *gin.Context) { @@ -124,7 +125,11 @@ func (usersHandler *UsersHandler) Authenticate(c *gin.Context) { c.JSON(http.StatusInternalServerError, GeneralError{"internal error, please try it later"}) return } - c.JSON(http.StatusOK, UserInfo{ID: user.ULID, GithubID: user.GithubID}) + c.JSON(http.StatusOK, UserInfo{ + ID: user.ULID, + GithubID: user.GithubID, + Token: user.Token.Token, + }) } func validateUser(token string) bool { diff --git a/main.go b/main.go index baf61f4..4ba6f19 100644 --- a/main.go +++ b/main.go @@ -88,6 +88,7 @@ func main() { api.Use(userHandler.AuthRequired) api.POST("/auth", userHandler.Authenticate) api.POST("/badges", handler.CreateBadge) + api.GET("/badges", handler.MyBadges) go func() { http.Handle("/metrics", promhttp.Handler()) diff --git a/models/badge.go b/models/badge.go index 2c80ccd..0668c38 100644 --- a/models/badge.go +++ b/models/badge.go @@ -7,11 +7,11 @@ import ( type Badge struct { gorm.Model - Label string - ULID string `gorm:"column:ulid;type:varchar;not null;unique_index"` - Type string - User User - UserID int - Options *postgres.Jsonb - OptionStruct interface{} `gorm:"-"` + Label string `json:"label"` + ULID string `gorm:"column:ulid;type:varchar;not null;unique_index" json:"ulid"` + Type string `json:"type"` + User User `json:"-"` + UserID int `json:"userId"` + Options *postgres.Jsonb `json:"-"` + OptionStruct interface{} `gorm:"-"` } diff --git a/www/layouts/default.vue b/www/layouts/default.vue index bfd9c72..9ef832a 100644 --- a/www/layouts/default.vue +++ b/www/layouts/default.vue @@ -7,7 +7,7 @@ - Badges + My badges @@ -17,7 +17,13 @@ Logout - +