From b88a9f75bfea57fcfddda0aea332f708c5acd192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Wed, 7 Aug 2024 10:21:35 +0200 Subject: [PATCH] optimize code --- pom.xml | 5 ++++- src/main/java/wtf/beatrice/releasehive/Main.java | 13 ++++++++----- .../releasehive/resource/AccountResource.java | 6 ++++-- .../releasehive/service/AccountServiceImpl.java | 1 - 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 4583e96..10c31c7 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,7 @@ 0.0.1-SNAPSHOT + org.apache.logging.log4j log4j-api @@ -19,6 +20,8 @@ log4j-core 2.23.1 + + org.springframework.boot spring-boot-starter-web @@ -30,12 +33,12 @@ 3.3.2 + org.hibernate hibernate-core 6.6.0.CR1 - org.postgresql postgresql diff --git a/src/main/java/wtf/beatrice/releasehive/Main.java b/src/main/java/wtf/beatrice/releasehive/Main.java index 1d91f90..d75935a 100644 --- a/src/main/java/wtf/beatrice/releasehive/Main.java +++ b/src/main/java/wtf/beatrice/releasehive/Main.java @@ -18,22 +18,25 @@ public class Main { public static void main(String[] args) { LOGGER.info("Hello world!"); + + LOGGER.info("Registering shutdown hooks"); Runtime.getRuntime().addShutdownHook(shutdownHook); + LOGGER.info("Initializing database backend"); + HibernateManager.initialize(); + LOGGER.info("Initializing Spring Boot"); SpringApplication.run(Main.class, args); - LOGGER.info("Spring Boot initialized!"); - HibernateManager.initialize(); + LOGGER.info("Spring Boot & DB initialized!"); Session session = HibernateManager.getSession(); Transaction transaction = session.beginTransaction(); List users = session.createQuery("FROM User", User.class).getResultList(); - for (User user : users) { - LOGGER.info("ID: {}, Name: {}", user.getUuid(), user.getUsername()); - } transaction.commit(); + users.forEach(user -> LOGGER.info("ID: {}, Name: {}", user.getUuid(), user.getUsername())); + } private static final Thread shutdownHook = new Thread(() -> { diff --git a/src/main/java/wtf/beatrice/releasehive/resource/AccountResource.java b/src/main/java/wtf/beatrice/releasehive/resource/AccountResource.java index 60de277..68a6841 100644 --- a/src/main/java/wtf/beatrice/releasehive/resource/AccountResource.java +++ b/src/main/java/wtf/beatrice/releasehive/resource/AccountResource.java @@ -1,8 +1,10 @@ package wtf.beatrice.releasehive.resource; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; -import wtf.beatrice.releasehive.util.JsonUtil; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import wtf.beatrice.releasehive.model.User; import wtf.beatrice.releasehive.service.AccountService; diff --git a/src/main/java/wtf/beatrice/releasehive/service/AccountServiceImpl.java b/src/main/java/wtf/beatrice/releasehive/service/AccountServiceImpl.java index 7762da4..d9b2914 100644 --- a/src/main/java/wtf/beatrice/releasehive/service/AccountServiceImpl.java +++ b/src/main/java/wtf/beatrice/releasehive/service/AccountServiceImpl.java @@ -17,7 +17,6 @@ public class AccountServiceImpl implements AccountService { return JsonUtil.spawnJsonError("Cannot register user without username"); } - if(null == user.getPassword() || user.getPassword().isEmpty()) { return JsonUtil.spawnJsonError("Cannot register user without password"); }