Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/update user api #97

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/api-gateway/handlers/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
SUCCESS_USER_FOUND = "User Found!"
SUCCESS_USER_CREATED = "User Created Successfully!"
SUCCESS_USER_DELETED = "User Deleted Successfully!"
SUCCESS_USER_UPDATED = "User Updated Successfully!"
SUCCESS_LOGIN = "Login Successfully!"
SUCCESS_LOGOUT = "Logout Successfully!"
SUCCESS_ROLE_UPGRADED = "User Role Upgraded Successfully!"
Expand Down
35 changes: 35 additions & 0 deletions backend/api-gateway/handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import (
"api-gateway/utils/client"
"api-gateway/utils/cookie"
"api-gateway/utils/message"
"bytes"
"encoding/json"
"net/http"
"os"
"strconv"

"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -140,3 +144,34 @@ func CreateUser(c echo.Context) error {

return c.JSON(http.StatusCreated, message.CreateSuccessUserMessage(SUCCESS_USER_CREATED, newUser))
}

func UpdateUser(c echo.Context) error {
tokenClaims := c.Get(TOKEN_CLAIMS_CONTEXT_KEY).(*models.Claims)

currentUser := tokenClaims.User
authId := currentUser.ID

requestBody := new(models.UpdateUser)
if err := c.Bind(requestBody); err != nil {
return c.JSON(http.StatusBadRequest, message.CreateErrorMessage(INVALID_JSON_REQUEST))
}

requestBodyJSON, err := json.Marshal(requestBody)
if err != nil {
return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage("Error marshalling request body"))
}

resp, err := http.Post(os.Getenv("USER_SERVICE_URL") + "/users/" + strconv.FormatUint(uint64(authId), 10), "application/json", bytes.NewBuffer(requestBodyJSON))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be posting to the url, it should be forwarding.

if err != nil {
return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage("Error creating request"))
}
defer resp.Body.Close()

var updateUserResponse models.UpdateUserResponse
err = json.NewDecoder(resp.Body).Decode(&updateUserResponse)
if err != nil {
return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage(INVALID_DB_ERROR))
}

return c.JSON(http.StatusOK, message.CreateSuccessUupdateUserMessage(SUCCESS_USER_UPDATED, updateUserResponse.User))
}
1 change: 1 addition & 0 deletions backend/api-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func main() {
API_GATEWAY.GET(path.AUTH_USER, handlers.GetUser)
API_GATEWAY.DELETE(path.AUTH_USER, handlers.DeleteUser)

API_GATEWAY.PUT(path.AUTH_USER_UPDATE, handlers.UpdateUser)
API_GATEWAY.POST(path.AUTH_USER_UPGRADE, handlers.UpgradeUser)
API_GATEWAY.POST(path.AUTH_USER_DOWNGRADE, handlers.DowngradeUser)
API_GATEWAY.GET(path.AUTH_USER_UPGRADE_SUPER_ADMIN, handlers.UpgradeSuperAdmin)
Expand Down
5 changes: 5 additions & 0 deletions backend/api-gateway/models/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ type SuccessUsersMessage struct {
type SuccessMessage struct {
Message string `json:"message"`
}

type SuccessUpdateUserMessage struct {
Message string `json:"message"`
User UserServiceUser `json:"user,omitempty"`
}
19 changes: 19 additions & 0 deletions backend/api-gateway/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,22 @@ type UserServiceCreateUserRequestBody struct {
PhotoUrl string `json:"photo_url"`
PreferredLanguage string `json:"preferred_language"`
}

type UpdateUser struct {
Username string `json:"username"`
PhotoUrl string `json:"photo_url"`
PreferredLanguage string `json:"preferred_language"`
}

type UserServiceUser struct {
gorm.Model
AuthUserID uint `json:"auth_user_id"`
Username string `json:"username"`
PhotoUrl string `json:"photo_url"`
PreferredLanguage string `json:"preferred_language"`
}

type UpdateUserResponse struct {
Message string `json:"message"`
User UserServiceUser `json:"user"`
}
7 changes: 7 additions & 0 deletions backend/api-gateway/utils/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ func CreateSuccessUsersMessage(message string, users []models.User) models.Succe
Users: users,
}
}

func CreateSuccessUupdateUserMessage(message string, user models.UserServiceUser) models.SuccessUpdateUserMessage {
return models.SuccessUpdateUserMessage{
Message: message,
User: user,
}
}
1 change: 1 addition & 0 deletions backend/api-gateway/utils/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
AUTH_USER_UPGRADE = "/auth/user/upgrade"
AUTH_USER_UPGRADE_SUPER_ADMIN = "/auth/user/upgrade-super-admin"
AUTH_USER_DOWNGRADE = "/auth/user/downgrade"
AUTH_USER_UPDATE = "/auth/user/update"
ALL_USER_SERVICE = "/users*"
ALL_QUESTION_SERVICE = "/questions*"
ALL_MATCHING_SERVICE = "/match*"
Expand Down
3 changes: 1 addition & 2 deletions backend/user-service/handlers/users_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,14 @@ func CreateUser(c echo.Context) error {
}

func UpdateUser(c echo.Context) error {
authUserID := c.Param("id")
authUserID := c.Param("authId")

intAuthUserId, err := strconv.Atoi(authUserID)
if err != nil {
return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage("Internal Server Error: Unable to Convert String to Uint"))
}

uintAuthUserId := uint(intAuthUserId)

var user model.User
if err := config.DB.Where("auth_user_id = ?", authUserID).First(&user).Error; err != nil {
return c.JSON(http.StatusNotFound, message.CreateErrorMessage(INVALID_USER_NOT_FOUND))
Expand Down
2 changes: 1 addition & 1 deletion backend/user-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
e.POST("/users", handlers.CreateUser)
e.GET("/users", handlers.GetUsers)
e.GET("/users/:id", handlers.GetUser)
e.PUT("/users/:id", handlers.UpdateUser)
e.POST("/users/:authId", handlers.UpdateUser)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The id has to be removed as a whole, if cannot fix then never mind.

e.DELETE("/users/:id", handlers.DeleteUser)

e.Logger.Fatal(e.Start(":8080"))
Expand Down
Loading