mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
chore: trailing slash and associate clients with user
This commit is contained in:
+72
-31
@@ -4,13 +4,26 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func (api *API) CreateClientHandler(context echo.Context) error {
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
var body types.CreateClientRequestBody
|
||||
|
||||
if err := context.Bind(&body); err != nil {
|
||||
@@ -22,14 +35,26 @@ func (api *API) CreateClientHandler(context echo.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
client := api.db.CreateClient(body)
|
||||
client := api.db.CreateClient(id, body)
|
||||
|
||||
log.Println(fmt.Sprintf("Client created with ID %d.", client.ID))
|
||||
log.Println(fmt.Sprintf("Client created with ID %s.", client.ID))
|
||||
return context.JSON(http.StatusOK, client)
|
||||
}
|
||||
|
||||
func (api *API) GetAllClientsHandler(context echo.Context) error {
|
||||
clients, err := api.db.GetClients()
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
clients, err := api.db.GetClients(id)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting Clients!")
|
||||
@@ -40,39 +65,51 @@ func (api *API) GetAllClientsHandler(context echo.Context) error {
|
||||
}
|
||||
|
||||
func (api *API) GetClientHandler(context echo.Context) error {
|
||||
idString := context.Param("id")
|
||||
userIdString, ok := context.Get("id").(string)
|
||||
|
||||
if idString == "" {
|
||||
return context.String(http.StatusBadRequest, "ID is required to get a Client!")
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(idString, 10, 32)
|
||||
userId, err := uuid.Parse(userIdString)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting Client!")
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(context.Param("id"))
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusBadRequest, "ID is required to get a Client!")
|
||||
}
|
||||
|
||||
var client types.Client
|
||||
|
||||
if err := api.db.GetClient(uint32(id), &client); err != nil {
|
||||
if err := api.db.GetClientById(userId, id, &client); err != nil {
|
||||
return context.String(http.StatusNotFound, "Client not found!")
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Got User with ID %d.", client.ID))
|
||||
log.Println(fmt.Sprintf("Got User with ID %s.", client.ID))
|
||||
return context.JSON(http.StatusOK, client)
|
||||
}
|
||||
|
||||
func (api *API) UpdateClientHandler(context echo.Context) error {
|
||||
idString := context.Param("id")
|
||||
userIdString, ok := context.Get("id").(string)
|
||||
|
||||
if idString == "" {
|
||||
return context.String(http.StatusBadRequest, "ID is required to update a Client!")
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(idString, 10, 32)
|
||||
userId, err := uuid.Parse(userIdString)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating Client!")
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(context.Param("id"))
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusBadRequest, "ID is required to update a Client!")
|
||||
}
|
||||
|
||||
var body types.UpdateClientRequestBody
|
||||
@@ -88,33 +125,37 @@ func (api *API) UpdateClientHandler(context echo.Context) error {
|
||||
|
||||
var client types.Client
|
||||
|
||||
if err := api.db.UpdateClient(uint32(id), body, &client); err != nil {
|
||||
if err := api.db.UpdateClient(userId, id, body, &client); err != nil {
|
||||
return context.String(http.StatusNotFound, "Client not found!")
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Updated Client with ID %d.", client.ID))
|
||||
log.Println(fmt.Sprintf("Updated Client with ID %s.", client.ID))
|
||||
return context.JSON(http.StatusOK, client)
|
||||
}
|
||||
|
||||
func (api *API) DeleteClientHandler(context echo.Context) error {
|
||||
idString := context.Param("id")
|
||||
userIdString, ok := context.Get("id").(string)
|
||||
|
||||
if idString == "" {
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
userId, err := uuid.Parse(userIdString)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(context.Param("id"))
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusBadRequest, "ID is required to delete a Client!")
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(idString, 10, 32)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting Client!")
|
||||
}
|
||||
|
||||
var client types.Client
|
||||
|
||||
if err := api.db.DeleteClient(uint32(id)); err != nil {
|
||||
if err := api.db.DeleteClient(userId, id); err != nil {
|
||||
return context.String(http.StatusNotFound, "Client not found!")
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Deleted Client with ID %d.", client.ID))
|
||||
log.Println(fmt.Sprintf("Deleted Client with ID %s.", id))
|
||||
return context.String(http.StatusOK, "Client deleted successfully!")
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func (api *API) AuthSessionMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
|
||||
context.Set("id", sess.Values["id"])
|
||||
context.Set("sessionId", sess.Values["sessionId"])
|
||||
context.Set("username", sess.Values["username"])
|
||||
context.Set("name", sess.Values["name"])
|
||||
|
||||
return next(context)
|
||||
}
|
||||
|
||||
@@ -15,14 +15,13 @@ func (api *API) ClientRoutes() {
|
||||
func (api *API) UserRoutes() {
|
||||
users := api.instance.Group("/api/users")
|
||||
|
||||
users.GET("/", api.GetAllUsersHandler)
|
||||
users.POST("/", api.CreateUserHandler)
|
||||
users.GET("/:id/", api.GetUserHandler)
|
||||
users.PUT("/:id/", api.UpdateUserHandler, api.AuthSessionMiddleware)
|
||||
users.PUT("/:id/security/", api.UpdateUserSecurityDetailsHandler)
|
||||
users.PUT("/:id/logo/", api.UpdateUserLogoHandler, middleware.BodyLimit("2M"))
|
||||
users.DELETE("/:id/", api.DeleteUserHandler, api.AuthSessionMiddleware)
|
||||
users.DELETE("/:id/logo/", api.DeleteUserLogoHandler, api.AuthSessionMiddleware)
|
||||
users.GET("/", api.GetUserHandler, api.AuthSessionMiddleware)
|
||||
users.PUT("/", api.UpdateUserHandler, api.AuthSessionMiddleware)
|
||||
users.PUT("/security/", api.UpdateUserSecurityCredentialsHandler, api.AuthSessionMiddleware)
|
||||
users.PUT("/logo/", api.UpdateUserLogoHandler, middleware.BodyLimit("2M"), api.AuthSessionMiddleware)
|
||||
users.DELETE("/", api.DeleteUserHandler, api.AuthSessionMiddleware)
|
||||
users.DELETE("/logo/", api.DeleteUserLogoHandler, api.AuthSessionMiddleware)
|
||||
}
|
||||
|
||||
func (api *API) AuthRoutes() {
|
||||
|
||||
+56
-19
@@ -10,10 +10,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hazemKrimi/crimson-vault/internal/lib"
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"github.com/hazemKrimi/crimson-vault/internal/lib"
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
)
|
||||
|
||||
func (api *API) CreateUserHandler(context echo.Context) error {
|
||||
@@ -63,10 +64,16 @@ func (api *API) GetAllUsersHandler(context echo.Context) error {
|
||||
}
|
||||
|
||||
func (api *API) GetUserHandler(context echo.Context) error {
|
||||
id, err := uuid.Parse(context.Param("id"))
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusBadRequest, "ID is required to get a User!")
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
|
||||
}
|
||||
|
||||
var user types.User
|
||||
@@ -80,10 +87,16 @@ func (api *API) GetUserHandler(context echo.Context) error {
|
||||
}
|
||||
|
||||
func (api *API) UpdateUserHandler(context echo.Context) error {
|
||||
id, err := uuid.Parse(context.Param("id"))
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusBadRequest, "ID is required to update a User!")
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User!")
|
||||
}
|
||||
|
||||
var body types.UpdateUserRequestBody
|
||||
@@ -107,14 +120,20 @@ func (api *API) UpdateUserHandler(context echo.Context) error {
|
||||
return context.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
func (api *API) UpdateUserSecurityDetailsHandler(context echo.Context) error {
|
||||
id, err := uuid.Parse(context.Param("id"))
|
||||
func (api *API) UpdateUserSecurityCredentialsHandler(context echo.Context) error {
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusBadRequest, "ID is required to create security details for a User!")
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User security credentials!")
|
||||
}
|
||||
|
||||
var body types.UpdateUserSecurityDetailsBody
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User security credentials!")
|
||||
}
|
||||
|
||||
var body types.UpdateUserSecurityCredentialsBody
|
||||
|
||||
if err := context.Bind(&body); err != nil {
|
||||
log.Println(fmt.Sprintf("Error creating security details for User: %v.", err))
|
||||
@@ -127,7 +146,7 @@ func (api *API) UpdateUserSecurityDetailsHandler(context echo.Context) error {
|
||||
|
||||
var user types.User
|
||||
|
||||
if err := api.db.UpdateUserSecurityDetails(id, body, &user); err != nil {
|
||||
if err := api.db.UpdateUserSecurityCredentials(id, body, &user); err != nil {
|
||||
return context.String(http.StatusNotFound, "User not found!")
|
||||
}
|
||||
|
||||
@@ -136,10 +155,16 @@ func (api *API) UpdateUserSecurityDetailsHandler(context echo.Context) error {
|
||||
}
|
||||
|
||||
func (api *API) UpdateUserLogoHandler(context echo.Context) error {
|
||||
id, err := uuid.Parse(context.Param("id"))
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User logo!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusBadRequest, "ID is required to update logo for User!")
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User logo!")
|
||||
}
|
||||
|
||||
var user types.User
|
||||
@@ -156,7 +181,7 @@ func (api *API) UpdateUserLogoHandler(context echo.Context) error {
|
||||
|
||||
if err != nil {
|
||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error while updating logo for User!")
|
||||
return context.String(http.StatusBadRequest, "No image has been uploaded!")
|
||||
}
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(file.Filename))
|
||||
@@ -220,10 +245,16 @@ func (api *API) UpdateUserLogoHandler(context echo.Context) error {
|
||||
}
|
||||
|
||||
func (api *API) DeleteUserHandler(context echo.Context) error {
|
||||
id, err := uuid.Parse(context.Param("id"))
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting User!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusBadRequest, "ID is required to delete a User!")
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting User!")
|
||||
}
|
||||
|
||||
if err := api.db.DeleteUser(id); err != nil {
|
||||
@@ -235,10 +266,16 @@ func (api *API) DeleteUserHandler(context echo.Context) error {
|
||||
}
|
||||
|
||||
func (api *API) DeleteUserLogoHandler(context echo.Context) error {
|
||||
id, err := uuid.Parse(context.Param("id"))
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting User logo!")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusBadRequest, "ID is required to delete logo of User!")
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting User logo!")
|
||||
}
|
||||
|
||||
var user types.User
|
||||
|
||||
Reference in New Issue
Block a user