chore: finish profile
This commit is contained in:
@@ -17,6 +17,8 @@ func NewGormStore(db *gorm.DB) *GormStore {
|
||||
type Store interface {
|
||||
UpdateUsername(id, newUsername string) (auth.User, error)
|
||||
UpdatePassword(id, passwordHahs string) (auth.User, error)
|
||||
GetTelegram(id string) (auth.User, error)
|
||||
SetTelegram(id, telegram string) (auth.User, error)
|
||||
}
|
||||
|
||||
func (s *GormStore) UpdateUsername(id, newUsername string) (auth.User, error) {
|
||||
@@ -36,8 +38,16 @@ func (s *GormStore) UpdateUsername(id, newUsername string) (auth.User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *GormStore) UpdatePassword(id, passwordHash string) (auth.User, error) {
|
||||
result := s.db.Model(&auth.User{}).Where("id = ?", id).Update("password", passwordHash)
|
||||
func (s *GormStore) GetTelegram(id string) (auth.User, error) {
|
||||
var user auth.User
|
||||
if err := s.db.First(&user, "id = ?", id).Error; err != nil {
|
||||
return auth.User{}, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *GormStore) SetTelegram(id, telegram string) (auth.User, error) {
|
||||
result := s.db.Model(&auth.User{}).Where("id = ?", id).Update("telegram", telegram)
|
||||
if result.Error != nil {
|
||||
return auth.User{}, result.Error
|
||||
}
|
||||
|
||||
@@ -23,6 +23,10 @@ type newPassword struct {
|
||||
Password string `json:"password" binding:"required,min=8"`
|
||||
}
|
||||
|
||||
type newTelegram struct {
|
||||
Telegram string `json:"telegram" binding:"required,min=3,max=64"`
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateUsernameById(c *gin.Context) {
|
||||
var u newUsername
|
||||
|
||||
@@ -73,3 +77,36 @@ func (h *Handler) UpdatePasswordById(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
func (h *Handler) GetTelegramById(c *gin.Context) {
|
||||
id, ok := c.MustGet("id").(string)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "id invalide"})
|
||||
return
|
||||
}
|
||||
user, err := h.store.GetTelegram(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"telegram": user.Telegram})
|
||||
}
|
||||
|
||||
func (h *Handler) SetTelegramById(c *gin.Context) {
|
||||
var t newTelegram
|
||||
if err := c.ShouldBindJSON(&t); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
||||
return
|
||||
}
|
||||
id, ok := c.MustGet("id").(string)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "id invalide"})
|
||||
return
|
||||
}
|
||||
user, err := h.store.SetTelegram(id, t.Telegram)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "erreur serveur"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"telegram": user.Telegram})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user