Files
GKNB_MSTM071/Backend/elso gyakorlat_minta
2026-03-04 20:02:39 +01:00
..
2026-03-04 20:02:39 +01:00
2026-03-04 20:02:39 +01:00
2026-03-04 20:02:39 +01:00
2026-03-04 20:02:39 +01:00

Első Gyakorlat - MINTA Megoldás

Ez a mappa tartalmazza az első gyakorlat teljes, működő megoldását.

Amit implementáltunk:

1. Layered Architecture (Rétegezett Architektúra)

  • API Layer: Controllers, Routers - HTTP kérések kezelése
  • Application Layer: Commands, Queries, Handlers - Üzleti logika
  • Domain Layer: Interfaces (Repository pattern)
  • Infrastructure Layer: Repositories - Adatkezelés (JSON file-ok)

2. CQRS Pattern (Command Query Responsibility Segregation)

  • Commands: Írási műveletek (Create, Update, Delete)
  • Queries: Olvasási műveletek (GetAll, GetById)
  • Handlers: Command és Query végrehajtók

3. Repository Pattern

  • IUserRepository és IPostRepository interfészek
  • UserRepository és PostRepository implementációk
  • Adatok tárolása JSON file-okban

Struktúra

src/
├── API/
│   ├── server.js                    # Express szerver
│   ├── controllers/
│   │   ├── userController.js        # User endpoint logika
│   │   └── postController.js        # Post endpoint logika
│   └── routers/
│       ├── userRouter.js            # User route-ok
│       └── postRouter.js            # Post route-ok
├── Application/
│   ├── users/
│   │   ├── command/                 # User írási műveletek
│   │   │   ├── createUserCommand.js
│   │   │   ├── createUserCommandHandler.js
│   │   │   ├── updateUserCommand.js
│   │   │   ├── updateUserCommandHandler.js
│   │   │   ├── deleteUserCommand.js
│   │   │   └── deleteUserCommandHandler.js
│   │   └── query/                   # User olvasási műveletek
│   │       ├── getAllUsersQuery.js
│   │       ├── getAllUsersQueryHandler.js
│   │       ├── getUserByIdQuery.js
│   │       └── getUserByIdQueryHandler.js
│   └── blogs/
│       ├── command/                 # Post írási műveletek
│       └── query/                   # Post olvasási műveletek
├── Domain/
│   ├── IUserRepository.js           # User repository interface
│   └── IPostRepository.js           # Post repository interface
└── Infrastructure/
    ├── userRepository.js            # User repository implementáció
    ├── postRepository.js            # Post repository implementáció
    ├── user.json                    # User adatok
    └── post.json                    # Post adatok

API Endpoints

Users

  • GET /api/users - Összes user lekérése
  • GET /api/users/:id - Egy user lekérése
  • POST /api/users - Új user létrehozása
  • PUT /api/users/:id - User módosítása
  • DELETE /api/users/:id - User törlése

Posts

  • GET /api/posts - Összes post lekérése
  • GET /api/posts/:id - Egy post lekérése
  • GET /api/posts/user/:userId - User összes postja
  • POST /api/posts - Új post létrehozása
  • PUT /api/posts/:id - Post módosítása
  • DELETE /api/posts/:id - Post törlése

Indítás

npm install
npm start

A szerver elindul a http://localhost:3000 címen.

Példa Használat

User létrehozása

curl -X POST http://localhost:3000/api/users \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "john@example.com",
    "age": 25
  }'

Post létrehozása

curl -X POST http://localhost:3000/api/posts \
  -H "Content-Type: application/json" \
  -d '{
    "title": "My First Post",
    "content": "Hello World!",
    "author": "John Doe"
  }'

Tanulási Pontok

  1. Separation of Concerns: Minden réteg saját felelősséggel rendelkezik
  2. CQRS: Írási és olvasási műveletek szétválasztása
  3. Repository Pattern: Adatkezelés absztrakciója
  4. Dependency Injection: Handler-ek megkapják a repository-t
  5. Express.js: RESTful API építése
  6. File-based Storage: JSON file-ok használata adatbázis helyett