Last active
April 6, 2020 22:57
-
-
Save victorsteven/33a9b3b67f140d2023ad9d0d98b66419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interfaces | |
import ( | |
"food-app/application" | |
"food-app/domain/entity" | |
"food-app/infrastructure/auth" | |
"github.com/gin-gonic/gin" | |
"net/http" | |
"strconv" | |
) | |
//Users struct defines the dependencies that will be used | |
type Users struct { | |
us application.UserAppInterface | |
rd auth.AuthInterface | |
tk auth.TokenInterface | |
} | |
//Users constructor | |
func NewUsers(us application.UserAppInterface, rd auth.AuthInterface, tk auth.TokenInterface) *Users { | |
return &Users{ | |
us: us, | |
rd: rd, | |
tk: tk, | |
} | |
} | |
func (s *Users) SaveUser(c *gin.Context) { | |
var user entity.User | |
if err := c.ShouldBindJSON(&user); err != nil { | |
c.JSON(http.StatusUnprocessableEntity, gin.H{ | |
"invalid_json": "invalid json", | |
}) | |
return | |
} | |
//validate the request: | |
validateErr := user.Validate("") | |
if len(validateErr) > 0 { | |
c.JSON(http.StatusUnprocessableEntity, validateErr) | |
return | |
} | |
newUser, err := s.us.SaveUser(&user) | |
if err != nil { | |
c.JSON(http.StatusInternalServerError, err) | |
return | |
} | |
c.JSON(http.StatusCreated, newUser.PublicUser()) | |
} | |
func (s *Users) GetUsers(c *gin.Context) { | |
users := entity.Users{} //customize user | |
var err error | |
//us, err = application.UserApp.GetUsers() | |
users, err = s.us.GetUsers() | |
if err != nil { | |
c.JSON(http.StatusInternalServerError, err.Error()) | |
return | |
} | |
c.JSON(http.StatusOK, users.PublicUsers()) | |
} | |
func (s *Users) GetUser(c *gin.Context) { | |
userId, err := strconv.ParseUint(c.Param("user_id"), 10, 64) | |
if err != nil { | |
c.JSON(http.StatusBadRequest, err.Error()) | |
return | |
} | |
user, err := s.us.GetUser(userId) | |
if err != nil { | |
c.JSON(http.StatusInternalServerError, err.Error()) | |
return | |
} | |
c.JSON(http.StatusOK, user.PublicUser()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment