2022-02-02 21:46:20 +01:00

88 lines
2.0 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"
"gorm.io/gorm"
)
type SignupOrLoginRequest 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 *SignupOrLoginRequest
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.NewAccount(account.Login, account.Password)).Error; err != nil {
s.Error(c, http.StatusConflict, err)
return
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
}
func (s *Service) Login(c *gin.Context) {
var account *SignupOrLoginRequest
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
}
session, err := models.NewSession(s.DB, account.Login, account.Password)
if err != nil {
switch err {
case gorm.ErrRecordNotFound:
s.Error(c, http.StatusNotFound, errors.New("login or password incorrect"))
default:
s.Error(c, http.StatusInternalServerError, err)
}
return
}
c.JSON(http.StatusOK, gin.H{
"token": session.Token,
})
}
func (s *Service) Logout(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{
"status": "todo",
})
}