Merge pull request #2 from Amnesia-debug/perf/get-username-from-token
perf:减少PublishVideo中的数据库查询
This commit is contained in:
@@ -124,3 +124,17 @@ func GetAccountID(c *gin.Context) (uint, error) {
|
|||||||
|
|
||||||
return accountID, nil
|
return accountID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUsername(c *gin.Context) (string, error) {
|
||||||
|
val, exists := c.Get("username")
|
||||||
|
if !exists {
|
||||||
|
return "", errors.New("username not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
username, ok := val.(string)
|
||||||
|
if !ok {
|
||||||
|
return "", errors.New("username has invalid type")
|
||||||
|
}
|
||||||
|
|
||||||
|
return username, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,14 +38,14 @@ func (vh *VideoHandler) PublishVideo(c *gin.Context) {
|
|||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user, err := vh.accountService.FindByID(c.Request.Context(), authorId)
|
username, err := jwt.GetUsername(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
video := &Video{
|
video := &Video{
|
||||||
AuthorID: authorId,
|
AuthorID: authorId,
|
||||||
Username: user.Username,
|
Username: username,
|
||||||
Title: req.Title,
|
Title: req.Title,
|
||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
PlayURL: req.PlayURL,
|
PlayURL: req.PlayURL,
|
||||||
|
|||||||
Reference in New Issue
Block a user