mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
Add bio management feature with UI and backend support
This commit is contained in:
@@ -298,6 +298,26 @@ func handleChangeUsername(c fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
func handleSetUserBio(c fiber.Ctx) error {
|
||||
uid, ok := c.Locals("uid").(uint)
|
||||
if !ok {
|
||||
return model.NewUnAuthorizedError("Unauthorized")
|
||||
}
|
||||
bio := c.FormValue("bio")
|
||||
if bio == "" {
|
||||
return model.NewRequestError("Bio is required")
|
||||
}
|
||||
user, err := service.SetUserBio(uid, bio)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(model.Response[model.UserView]{
|
||||
Success: true,
|
||||
Data: user,
|
||||
Message: "Bio updated successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func AddUserRoutes(r fiber.Router) {
|
||||
u := r.Group("user")
|
||||
u.Post("/register", handleUserRegister)
|
||||
@@ -312,4 +332,5 @@ func AddUserRoutes(r fiber.Router) {
|
||||
u.Post("/delete", handleDeleteUser)
|
||||
u.Get("/info", handleGetUserInfo)
|
||||
u.Post("/username", handleChangeUsername)
|
||||
u.Post("/bio", handleSetUserBio)
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ type User struct {
|
||||
UploadsCount int
|
||||
CommentsCount int
|
||||
Resources []Resource `gorm:"foreignKey:UserID"`
|
||||
Bio string
|
||||
}
|
||||
|
||||
type UserView struct {
|
||||
@@ -27,6 +28,7 @@ type UserView struct {
|
||||
CanUpload bool `json:"can_upload"`
|
||||
UploadsCount int `json:"uploads_count"`
|
||||
CommentsCount int `json:"comments_count"`
|
||||
Bio string `json:"bio"`
|
||||
}
|
||||
|
||||
type UserViewWithToken struct {
|
||||
@@ -44,6 +46,7 @@ func (u User) ToView() UserView {
|
||||
CanUpload: u.CanUpload || u.IsAdmin,
|
||||
UploadsCount: u.UploadsCount,
|
||||
CommentsCount: u.CommentsCount,
|
||||
Bio: u.Bio,
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -292,3 +292,18 @@ func ChangeUsername(uid uint, newUsername string) (model.UserView, error) {
|
||||
}
|
||||
return user.ToView(), nil
|
||||
}
|
||||
|
||||
func SetUserBio(uid uint, bio string) (model.UserView, error) {
|
||||
if len(bio) > 200 {
|
||||
return model.UserView{}, model.NewRequestError("Bio must be less than 200 characters")
|
||||
}
|
||||
user, err := dao.GetUserByID(uid)
|
||||
if err != nil {
|
||||
return model.UserView{}, err
|
||||
}
|
||||
user.Bio = bio
|
||||
if err := dao.UpdateUser(user); err != nil {
|
||||
return model.UserView{}, err
|
||||
}
|
||||
return user.ToView(), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user