diff --git a/internal/photos/api/account.go b/internal/photos/api/account.go index d0f4451..41fd342 100644 --- a/internal/photos/api/account.go +++ b/internal/photos/api/account.go @@ -1,9 +1,11 @@ package api import ( + "errors" "net/http" "github.com/gin-gonic/gin" + "gitlab.celogeek.com/photos/api/internal/photos/models" "gopkg.in/validator.v2" ) @@ -28,22 +30,33 @@ func (s *Service) Signup(c *gin.Context) { var account *SignupRequest if c.Request.ContentLength == 0 { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ - "error": "missing body", - }) + s.Error(c, http.StatusBadRequest, errors.New("missing body")) return } if err := c.ShouldBindJSON(&account); err != nil { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ - "error": err.Error(), - }) + s.Error(c, http.StatusBadRequest, err) return } if err := validator.Validate(account); err != nil { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ - "error": err.Error(), - }) + s.Error(c, http.StatusExpectationFailed, err) + return + } + + var accountExists int64 + if err := s.DB.Model(&models.Account{}).Where("login = ?", account.Login).Count(&accountExists).Error; err != nil { + s.Error(c, http.StatusInternalServerError, err) + return + } + if accountExists > 0 { + s.Error(c, http.StatusConflict, errors.New("account exists")) + return + } + if err := s.DB.Create(&models.Account{ + Login: account.Login, + Password: account.Password, + }).Error; err != nil { + s.Error(c, http.StatusConflict, err) return } diff --git a/internal/photos/api/helpers.go b/internal/photos/api/helpers.go index a66f262..b74c8b8 100644 --- a/internal/photos/api/helpers.go +++ b/internal/photos/api/helpers.go @@ -28,3 +28,9 @@ func (s *Service) Dump(o interface{}) { s.Logger.Printf("%s", b.Bytes()) } + +func (s *Service) Error(c *gin.Context, code int, err error) { + c.AbortWithStatusJSON(code, gin.H{ + "error": err.Error(), + }) +} diff --git a/internal/photos/api/main.go b/internal/photos/api/main.go index d78c5e8..b0539ef 100644 --- a/internal/photos/api/main.go +++ b/internal/photos/api/main.go @@ -2,6 +2,7 @@ package api import ( "context" + "errors" "log" "math/rand" "net/http" @@ -55,9 +56,7 @@ func (s *Service) SetupRoutes() { ac.POST("/logout", s.Logout) s.Gin.NoRoute(func(c *gin.Context) { - c.JSON(http.StatusNotFound, gin.H{ - "error": "this route doesn't exists", - }) + s.Error(c, http.StatusNotFound, errors.New("this route doesn't exists")) }) } diff --git a/internal/photos/models/account.go b/internal/photos/models/account.go index b5819a6..ffc45c1 100644 --- a/internal/photos/models/account.go +++ b/internal/photos/models/account.go @@ -1,14 +1,25 @@ package models import ( + "crypto" + "encoding/base64" "time" + + "gorm.io/gorm" ) type Account struct { ID uint32 `gorm:"primary_key"` Login string `gorm:"unique;size:64;not null"` Password string `gorm:"-"` - EncryptedPassword string `gorm:"size:28;not null"` + EncryptedPassword string `gorm:"size:44;not null"` CreatedAt time.Time UpdatedAt time.Time } + +func (a *Account) BeforeCreate(tx *gorm.DB) error { + sha1 := crypto.SHA256.New() + sha1.Write([]byte(a.Password)) + a.EncryptedPassword = base64.StdEncoding.EncodeToString(sha1.Sum(nil)) + return nil +}