Skip to content

Commit

Permalink
Add 404 response type and refactor async handling
Browse files Browse the repository at this point in the history
Updated UserController to include 404 response type for Get method.
Refactored AuthenticatedApp in App.tsx to use Promise-based syntax
instead of async/await. Moved useEffect hook to the end of the function
and adjusted fetchUserRole to call createUserIfNotExists on 404 status.
  • Loading branch information
fwickert committed Nov 25, 2024
1 parent 2d43412 commit a0ca899
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public UserController(UserService userService)
//getUser
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<UserItem>> Get(string id)
{
var user = await _userService.GetUserAsync(id);
Expand Down
85 changes: 46 additions & 39 deletions virtualteachergenaidemo.client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,54 +51,61 @@ function AuthenticatedApp(props: any) {
const { setRole } = useUserRole();

useEffect(() => {
const fetchUserRole = async () => {
const fetchUserRole = () => {
if (email) {
try {
const response = await fetch(`/api/User/${email}`);
if (!response.ok) {
throw new Error('Failed to fetch user role');
}
const userData = await response.json();
setRole(userData.role);
} catch (error) {
console.error('Error fetching user role:', error);
}
fetch(`/api/User/${email}`)
.then(response => {
if (!response.ok) {
if (response.status === 404) {
return createUserIfNotExists();
} else {
console.error('Failed to fetch user role:', response.statusText);
return Promise.reject('Failed to fetch user role');
}
}
return response.json();
})
.then(userData => {
if (userData) {
setRole(userData.role);
}
})
.catch(error => {
console.error('Error fetching user role:', error);
});
}
};

fetchUserRole();
}, [email, setRole]);

useEffect(() => {
const createUserIfNotExists = async () => {
const createUserIfNotExists = () => {
if (email) {
try {
const response = await fetch('/api/User', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: email,
name: email,
role: UserRoleEnum.User,
settings: {}
})
fetch('/api/User', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: email,
name: email,
role: UserRoleEnum.User,
settings: {}
})
})
.then(response => {
if (!response.ok) {
console.error('Failed to create or get user:', response.statusText);
return Promise.reject('Failed to create or get user');
} else {
setRole(UserRoleEnum.User);
}
})
.catch(error => {
console.error('Error creating user:', error);
});
if (!response.ok) {
throw new Error('Failed to create or get user');
}
else {
setRole(UserRoleEnum.User);
}
} catch (error) {
console.error('Error creating user:', error);
}
}
};

createUserIfNotExists();
}, []);
fetchUserRole();
}, [email, setRole]);

return (
<Router>
Expand Down

0 comments on commit a0ca899

Please sign in to comment.