From 770b5e14d042e82e0fa096f94c19d96f6456de5b Mon Sep 17 00:00:00 2001 From: celogeek Date: Sun, 30 Jan 2022 14:50:53 +0100 Subject: [PATCH] add account routes --- go.mod | 1 + go.sum | 2 ++ internal/photos/api/account.go | 53 ++++++++++++++++++++++++++++++++++ internal/photos/api/main.go | 6 ++++ 4 files changed, 62 insertions(+) create mode 100644 internal/photos/api/account.go diff --git a/go.mod b/go.mod index 8c5aa3e..f9e8433 100644 --- a/go.mod +++ b/go.mod @@ -25,5 +25,6 @@ require ( github.com/ugorji/go/codec v1.1.7 // indirect golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect + gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0 // indirect gopkg.in/yaml.v2 v2.2.8 // indirect ) diff --git a/go.sum b/go.sum index 01a6c0b..7baa364 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0 h1:EFLtLCwd8tGN+r/ePz3cvRtdsfYNhDEdt/vp6qsT+0A= +gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/photos/api/account.go b/internal/photos/api/account.go new file mode 100644 index 0000000..d0f4451 --- /dev/null +++ b/internal/photos/api/account.go @@ -0,0 +1,53 @@ +package api + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "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 { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ + "error": "missing body", + }) + return + } + + if err := c.ShouldBindJSON(&account); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ + "error": err.Error(), + }) + return + } + if err := validator.Validate(account); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ + "error": err.Error(), + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "status": "success", + }) +} diff --git a/internal/photos/api/main.go b/internal/photos/api/main.go index e68b440..d78c5e8 100644 --- a/internal/photos/api/main.go +++ b/internal/photos/api/main.go @@ -48,6 +48,12 @@ func New(logger *log.Logger, config *ServiceConfig) *Service { func (s *Service) SetupRoutes() { s.Gin.Use(gin.Logger()) s.Gin.Use(s.Recovery) + + ac := s.Gin.Group("/account") + ac.POST("/signup", s.Signup) + ac.POST("/login", s.Login) + ac.POST("/logout", s.Logout) + s.Gin.NoRoute(func(c *gin.Context) { c.JSON(http.StatusNotFound, gin.H{ "error": "this route doesn't exists",