feat: created categories initializer

This commit is contained in:
filippo-ferrari 2024-09-09 19:56:10 +02:00
parent d9142c7492
commit e5901bc63a
3 changed files with 71 additions and 1 deletions

View file

@ -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);
}
}
}

View file

@ -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;

View file

@ -37,4 +37,12 @@ public class CategoryService {
public Page<Category> list(Pageable pageable){
return categoryRepository.findAll(pageable);
}
public Long count() {
return this.categoryRepository.count();
}
public Category save(Category category) {
return this.categoryRepository.save(category);
}
}