From e5901bc63a5f4bb2e0d96f9422581a4774434997 Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Mon, 9 Sep 2024 19:56:10 +0200 Subject: [PATCH] feat: created categories initializer --- .../initializers/CategoryInitializer.java | 61 +++++++++++++++++++ .../UserInitializer.java | 3 +- .../munera/services/CategoryService.java | 8 +++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/application/munera/initializers/CategoryInitializer.java rename src/main/java/com/application/munera/{security => initializers}/UserInitializer.java (90%) diff --git a/src/main/java/com/application/munera/initializers/CategoryInitializer.java b/src/main/java/com/application/munera/initializers/CategoryInitializer.java new file mode 100644 index 0000000..24cd660 --- /dev/null +++ b/src/main/java/com/application/munera/initializers/CategoryInitializer.java @@ -0,0 +1,61 @@ +package com.application.munera.initializers; + +import com.application.munera.data.Category; +import com.application.munera.services.CategoryService; +import jakarta.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class CategoryInitializer { + + @Autowired + private CategoryService categoryService; + + @PostConstruct + public void init() { + if (categoryService.count() == 0) { + // Create and save the Food category + Category foodCategory = new Category(); + foodCategory.setName("Food"); + foodCategory.setDescription("All expenses related to food"); + categoryService.save(foodCategory); + + // Create and save the Travel category + Category travelCategory = new Category(); + travelCategory.setName("Travel"); + travelCategory.setDescription("Expenses related to traveling, including transport and accommodation"); + categoryService.save(travelCategory); + + // Create and save the Electronics category + Category electronicsCategory = new Category(); + electronicsCategory.setName("Electronics"); + electronicsCategory.setDescription("All expenses related to electronic devices and gadgets"); + categoryService.save(electronicsCategory); + + // Create and save the Events category + Category eventsCategory = new Category(); + eventsCategory.setName("Events"); + eventsCategory.setDescription("Expenses related to attending or organizing events"); + categoryService.save(eventsCategory); + + // Create and save the Clothing category + Category clothingCategory = new Category(); + clothingCategory.setName("Clothing"); + clothingCategory.setDescription("Expenses related to clothes and accessories"); + categoryService.save(clothingCategory); + + // Create and save the Bills category + Category billsCategory = new Category(); + billsCategory.setName("Bills"); + billsCategory.setDescription("Recurring expenses like utilities, internet, and other bills"); + categoryService.save(billsCategory); + + // Create and save the Rent category + Category rentCategory = new Category(); + rentCategory.setName("Rent"); + rentCategory.setDescription("Expenses related to rental payments for housing or office space"); + categoryService.save(rentCategory); + } + } +} diff --git a/src/main/java/com/application/munera/security/UserInitializer.java b/src/main/java/com/application/munera/initializers/UserInitializer.java similarity index 90% rename from src/main/java/com/application/munera/security/UserInitializer.java rename to src/main/java/com/application/munera/initializers/UserInitializer.java index 10035ec..019b016 100644 --- a/src/main/java/com/application/munera/security/UserInitializer.java +++ b/src/main/java/com/application/munera/initializers/UserInitializer.java @@ -1,6 +1,7 @@ -package com.application.munera.security; +package com.application.munera.initializers; import com.application.munera.data.User; +import com.application.munera.security.AdminProperties; import com.application.munera.services.UserService; import jakarta.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/com/application/munera/services/CategoryService.java b/src/main/java/com/application/munera/services/CategoryService.java index a9dfbb4..8599e6f 100644 --- a/src/main/java/com/application/munera/services/CategoryService.java +++ b/src/main/java/com/application/munera/services/CategoryService.java @@ -37,4 +37,12 @@ public class CategoryService { public Page list(Pageable pageable){ return categoryRepository.findAll(pageable); } + + public Long count() { + return this.categoryRepository.count(); + } + + public Category save(Category category) { + return this.categoryRepository.save(category); + } }