Skip to content

Commit

Permalink
Get User and Projects in single call
Browse files Browse the repository at this point in the history
  • Loading branch information
macano committed Jan 27, 2025
1 parent 87a170b commit cedfd7f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
18 changes: 3 additions & 15 deletions frontend/src/admin/user-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const UserForm: FunctionComponent = () => {
const fetchUser = async () => {
setLoading(true);
try {
const response = await fetch(`${import.meta.env.VITE_ZTOR_URL}/users/${userId}`, {
const response = await fetch(`${import.meta.env.VITE_ZTOR_URL}/users/${userId}/projects`, {
credentials: "include",
});
if (response.status === 401) {
Expand All @@ -50,6 +50,8 @@ export const UserForm: FunctionComponent = () => {
const userData = await response.json();
setUser(userData);
setOriginalData(userData);
setUserProjects(userData.projects)

} else {
alert(`Error fetching user: ${response.statusText}`);
}
Expand All @@ -60,20 +62,6 @@ export const UserForm: FunctionComponent = () => {
}
};

const fetchProjects = async () => {
try {
const response = await fetch(`${import.meta.env.VITE_ZTOR_URL}/users/${userId}/projects`, {
credentials: "include",
});
if (!response.ok) return

setUserProjects(projectsFromJson(await response.text()))
} catch (error) {
console.error("Error fetching projects:", error);
}
};

fetchProjects();
fetchUser();
} else {
setIsEditing(true);
Expand Down
6 changes: 6 additions & 0 deletions zorm/src/main/kotlin/com/zenmo/orm/user/UserRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class UserRepository(
}
}
}

fun getUserAndProjects(userId: UUID): User {
return getUsersAndProjects(
(UserTable.id eq userId)
).first()
}

fun getUsersAndProjects(filter: Op<Boolean> = Op.TRUE): List<User> {
return transaction(db) {
Expand Down
2 changes: 1 addition & 1 deletion ztor/src/main/kotlin/com/zenmo/ztor/plugins/Databases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fun Application.configureDatabases(): Database {
get("/users/{userId}/projects") {
asAdmin {
val userId = UUID.fromString(call.parameters["userId"])
val user = projectRepository.getProjectsByUserId(userId)
val user = userRepository.getUserAndProjects(userId)
call.respond(HttpStatusCode.OK, user)
}
}
Expand Down

0 comments on commit cedfd7f

Please sign in to comment.