Skip to content

Modern Docker Management, Designed for Everyone

feat: add copier utility functions for struct mapping

Kyle Mendell477c2d5d7e9a
diff --git a/backend/go.mod b/backend/go.mod
index 992e69ed7..c2aa00dc4 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -52,6 +52,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
+ github.com/jinzhu/copier v0.4.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.5.0 // indirect
diff --git a/backend/go.sum b/backend/go.sum
index 4eb77e6f0..1cd04875b 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -101,6 +101,8 @@ github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
+github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
+github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
diff --git a/backend/internal/api/user_handler.go b/backend/internal/api/user_handler.go
index 51c64b09b..d9a6ecdad 100644
--- a/backend/internal/api/user_handler.go
+++ b/backend/internal/api/user_handler.go
@@ -26,7 +26,7 @@ func (h *UserHandler) ListUsers(c *gin.Context) {
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
- "error": "Invalid pagination or sort parameters: " + err.Error(),
+ "data": gin.H{"error": "Invalid pagination or sort parameters: " + err.Error()},
})
return
}
@@ -44,7 +44,7 @@ func (h *UserHandler) ListUsers(c *gin.Context) {
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
- "error": "Failed to fetch users",
+ "data": gin.H{"error": "Failed to fetch users"},
})
return
}
@@ -72,7 +72,7 @@ func (h *UserHandler) ListUsers(c *gin.Context) {
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
- "error": "Failed to list users: " + err.Error(),
+ "data": gin.H{"error": "Failed to list users: " + err.Error()},
})
return
}
@@ -89,7 +89,7 @@ func (h *UserHandler) CreateUser(c *gin.Context) {
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
- "error": "Invalid request format",
+ "data": gin.H{"error": "Invalid request format"},
})
return
}
@@ -98,7 +98,7 @@ func (h *UserHandler) CreateUser(c *gin.Context) {
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
- "error": "Failed to hash password",
+ "data": gin.H{"error": "Failed to hash password"},
})
return
}
@@ -122,14 +122,14 @@ func (h *UserHandler) CreateUser(c *gin.Context) {
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
- "error": "Failed to create user",
+ "data": gin.H{"error": "Failed to create user"},
})
return
}
c.JSON(http.StatusCreated, gin.H{
"success": true,
- "user": dto.UserResponseDto{
+ "data": dto.UserResponseDto{
ID: createdUser.ID,
Username: createdUser.Username,
DisplayName: createdUser.DisplayName,
@@ -147,14 +147,14 @@ func (h *UserHandler) GetUser(c *gin.Context) {
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"success": false,
- "error": "User not found",
+ "data": gin.H{"error": "User not found"},
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
- "user": dto.UserResponseDto{
+ "data": dto.UserResponseDto{
ID: user.ID,
Username: user.Username,
DisplayName: user.DisplayName,
@@ -172,7 +172,7 @@ func (h *UserHandler) UpdateUser(c *gin.Context) {
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
- "error": "Invalid request format",
+ "data": gin.H{"error": "Invalid request format"},
})
return
}
@@ -181,7 +181,7 @@ func (h *UserHandler) UpdateUser(c *gin.Context) {
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"success": false,
- "error": "User not found",
+ "data": gin.H{"error": "User not found"},
})
return
}
@@ -203,14 +203,14 @@ func (h *UserHandler) UpdateUser(c *gin.Context) {
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
- "error": "Failed to update user",
+ "data": gin.H{"error": "Failed to update user"},
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
- "user": dto.UserResponseDto{
+ "data": dto.UserResponseDto{
ID: updatedUser.ID,
Username: updatedUser.Username,
DisplayName: updatedUser.DisplayName,
@@ -228,13 +228,13 @@ func (h *UserHandler) DeleteUser(c *gin.Context) {
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
- "error": "Failed to delete user",
+ "data": gin.H{"error": "Failed to delete user"},
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
- "message": "User deleted successfully",
+ "data": gin.H{"message": "User deleted successfully"},
})
}
diff --git a/backend/internal/dto/dto_map.go b/backend/internal/dto/dto_map.go
new file mode 100644
index 000000000..ba0534313
--- /dev/null
+++ b/backend/internal/dto/dto_map.go
@@ -0,0 +1,27 @@
+//Originally from pocket-id
+
+package dto
+
+import (
+ "fmt"
+
+ "github.com/jinzhu/copier"
+)
+
+func MapStructList[S any, D any](source []S, destination *[]D) (err error) {
+ *destination = make([]D, len(source))
+
+ for i, item := range source {
+ err = MapStruct(item, &((*destination)[i]))
+ if err != nil {
+ return fmt.Errorf("failed to map field %d: %w", i, err)
+ }
+ }
+ return nil
+}
+
+func MapStruct(source any, destination any) error {
+ return copier.CopyWithOption(destination, source, copier.Option{
+ DeepCopy: true,
+ })
+}
diff --git a/frontend/src/lib/services/api/user-api-service.ts b/frontend/src/lib/services/api/user-api-service.ts
index d01ad12df..e1c51539f 100644
--- a/frontend/src/lib/services/api/user-api-service.ts
+++ b/frontend/src/lib/services/api/user-api-service.ts
@@ -13,16 +13,7 @@ export interface Role {
description?: string;
}
-interface UsersResponse {
- users: User[];
-}
-
export default class UserAPIService extends BaseAPIService {
- async list(): Promise<User[]> {
- const response = (await this.handleResponse(this.api.get('/users'))) as UsersResponse;
- return response.users;
- }
-
async getUsers(
pagination?: PaginationRequest,
sort?: SortRequest,
diff --git a/frontend/src/routes/settings/users/user-table.svelte b/frontend/src/routes/settings/users/user-table.svelte
index 8e2c5e380..e81c4f2a4 100644
--- a/frontend/src/routes/settings/users/user-table.svelte
+++ b/frontend/src/routes/settings/users/user-table.svelte
@@ -1,7 +1,7 @@
<script lang="ts">
import ArcaneTable from '$lib/components/arcane-table.svelte';
import { Button } from '$lib/components/ui/button/index.js';
- import { Trash2, Users, Ellipsis, Edit, ShieldEllipsis } from '@lucide/svelte';
+ import { Trash2, Users, Ellipsis, Edit } from '@lucide/svelte';
import * as Card from '$lib/components/ui/card/index.js';
import { toast } from 'svelte-sonner';
import * as DropdownMenu from '$lib/components/ui/dropdown-menu/index.js';