2022-02-01 09:40:27 +01:00

67 lines
1.4 KiB
Go

package api
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"gitlab.celogeek.com/photos/api/internal/photos/models"
"gopkg.in/validator.v2"
)
func (s *Service) Login(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{
"status": "todo",
})
}
func (s *Service) Logout(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{
"status": "todo",
})
}
type SignupRequest struct {
Login string `validate:"min=3,max=40,regexp=^[a-zA-Z0-9]*$"`
Password string `validate:"min=8,max=40"`
}
func (s *Service) Signup(c *gin.Context) {
var account *SignupRequest
if c.Request.ContentLength == 0 {
s.Error(c, http.StatusBadRequest, errors.New("missing body"))
return
}
if err := c.ShouldBindJSON(&account); err != nil {
s.Error(c, http.StatusBadRequest, err)
return
}
if err := validator.Validate(account); err != nil {
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
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
}