[JAVA-12943] Improvement

This commit is contained in:
panagiotiskakos 2022-07-14 08:23:11 +03:00
parent 56fd1ef7b4
commit e80218d1e0
2 changed files with 6 additions and 5 deletions

View File

@ -1,5 +1,5 @@
package com.baeldung.javalin.User;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
public int id;

View File

@ -3,6 +3,7 @@ package com.baeldung.javalin.User;
import io.javalin.Handler;
import java.util.Objects;
import java.util.Optional;
public class UserController {
public static Handler fetchAllUsernames = ctx -> {
@ -14,11 +15,11 @@ public class UserController {
public static Handler fetchById = ctx -> {
int id = Integer.parseInt(Objects.requireNonNull(ctx.param("id")));
UserDao dao = UserDao.instance();
User user = dao.getUserById(id).get();
if (user == null) {
ctx.html("Not Found");
} else {
Optional<User> user = dao.getUserById(id);
if(user.isPresent()){
ctx.json(user);
} else {
ctx.html("User Not Found");
}
};
}