Metadata-Version: 2.4
Name: halloween_crud
Version: 0.1.1
Summary: Modèles Pydantic, logique CRUD et sauvegarde JSON de l'API Halloween (collecte de bonbons)
Author: Mohsine ELGHANDOUR
Author-email: mohsine.elghandour@gmail.com
License: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastapi>=0.100
Requires-Dist: pydantic>=2
Dynamic: author
Dynamic: author-email
Dynamic: description
Dynamic: description-content-type
Dynamic: license
Dynamic: license-file
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# halloween_crud

Logique CRUD d'une API « Halloween : collecte de bonbons ». Des **habitants**
s'inscrivent avec un stock de bonbons et des **enfants** passent les visiter. Le
package contient tout ce qui n'est pas HTTP, pour qu'une application FastAPI
n'ait plus qu'à déclarer ses routes.

## Fonctionnalités

- **Modèles Pydantic** (`halloween_crud.models`) : entrées (`Habitant`, `Enfant`, `Reassort`), sorties (`DbHabitant`, `DbEnfant`, `StockHabitant`) et format du fichier JSON.
- **Une fonction par route** (`halloween_crud.crud`) :
  `create_habitant`, `read_stock`, `restock_habitant`, `delete_habitant`,
  `create_enfant`, `read_top10_enfants`, `read_non_visited_habitants`, `visit_habitant`.
- **Sauvegarde JSON** (`halloween_crud.utils`) : écriture atomique, verrou entre threads, et
  transaction (si une erreur survient, rien n'est enregistré).
- Les erreurs métier sont des `fastapi.HTTPException` : 404 (`Habitant not found`, `Enfant not found`)
  et 409 (`Habitant already visited`, `No candy left`).

## Installation

```bash
pip install halloween_crud
```

Depuis les sources (mode édition), dans le dossier qui contient `setup.py` :

```bash
pip install -e .
```

## Utilisation

```python
from halloween_crud import crud, Habitant, Enfant, Reassort

habitant = crud.create_habitant(Habitant(nom="Mme Dupont", stock_bonbons=2))
alice = crud.create_enfant(Enfant(nom="Martin", prenom="Alice"))

crud.visit_habitant(alice.id, habitant.id)            # Alice reçoit 1 bonbon
print(crud.read_stock(habitant.id))                   # stock_bonbons=1 reste_des_bonbons=True
print(crud.read_non_visited_habitants(alice.id))      # [] : Alice a visité tout le monde
print(crud.read_top10_enfants()[0].bonbons_collectes) # 1

crud.restock_habitant(habitant.id, Reassort(quantite=5))  # stock : 6
crud.delete_habitant(habitant.id)                     # désinscription
```

Dans une application FastAPI, chaque route appelle une seule fonction :

```python
from fastapi import FastAPI
from halloween_crud import crud, DbHabitant, Habitant

app = FastAPI()

@app.post("/habitants", response_model=DbHabitant)
def create_habitant(habitant: Habitant) -> DbHabitant:
    return crud.create_habitant(habitant)
```

## Où sont écrites les données ?

Dans `data/database.json`, dossier `data` du **répertoire courant** (celui d'où l'on lance le programme),
créé au premier ajout. Pour utiliser un autre dossier :

```bash
DATA_DIR=/chemin/vers/dossier fastapi dev main.py
```

## Limites connues

- Le verrou qui protège le fichier est interne à un processus : lancer l'API avec **un seul worker**.
- Un fichier JSON convient à un petit volume de données (il est relu et réécrit en entier à chaque modification).

## Contributors

- `Mohsine ELGHANDOUR`

## License

MIT, voir le fichier `LICENSE`.
