I have started a new side project called "Very Simple SNS" to strengthen my full-stack development skills using Spring Boot and React. The goal is not just to write code, but to understand the "Why" behind every architectural decision, such as database indexing, traffic handling, and system design.
In this first step, I focused on setting up the environment and implementing the User Registration feature.
Tech Stack
- Backend: Spring Boot 3.x, Spring Data JPA, MySQL
- Frontend: React (Vite)
- Collaboration: Git (Monorepo structure)
1. The Core Logic: Forbidden Word Filter
One of the key requirements for Step 1 was to prevent users from using specific keywords (e.g., "admin", "operator") in their nicknames to avoid confusion.
I faced a design decision: Where should I put this validation logic?
- Inside the Entity? (User.java)
- Inside the Service Layer? (UserService.java)
My Decision: The Service Layer.
Entities should focus on their own internal integrity (e.g., length check, null check). Checking against a list of "forbidden words" is a business policy that might change over time or require external data sources (like a DB table or Redis). Therefore, it belongs in the Service layer to keep the Entity pure.
@Service
@RequiredArgsConstructor
public class UserService {
// In a real-world scenario, this would be fetched from a DB or Cache
private static final List<String> FORBIDDEN_WORDS = List.of("admin", "operator");
@Transactional
public Long signup(SignupRequest request) {
// Validation Logic
if (containsForbiddenWord(request.getNickname())) {
throw new IllegalArgumentException("This nickname is not allowed.");
}
// ... saving user logic
}
private boolean containsForbiddenWord(String nickname) {
return FORBIDDEN_WORDS.stream().anyMatch(nickname::contains);
}
}
2. What I Learned
- Monorepo Setup: Managing both Backend and Frontend in a single Git repository makes it easier to track changes for a specific feature.
- Separation of Concerns: Distinguishing between Domain logic (Entity) and Business logic (Service) is crucial for maintainability.
Top comments (0)