Skip to content

Commit

Permalink
expenses in Database folder
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreAnders committed Dec 29, 2023
1 parent 2941896 commit babc20e
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions app/expenses/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,46 @@
from app.extensions import db
from app.expenses.model import Expense
from sqlalchemy.exc import IntegrityError
from app.folders.model import Folder
from sqlalchemy.orm.exc import NoResultFound


def write_user_data(user_id):
user_subfolder_info_db = os.path.join('data', str(user_id), f"info-{user_id}")
user_subfolder_info_db = os.path.join('data', str(user_id), "Database")

try:
os.makedirs(user_subfolder_info_db, exist_ok=True)
print(f"Dossier '{user_subfolder_info_db}' créé avec succès.")

folder = Folder.query.filter_by(name='Database', user_id=user_id).first()

if folder is None:
folder = Folder(name='Database', user_id=user_id)
try:
db.session.add(folder)
db.session.commit()
print({"message": "Dossier ajouté avec succès"}, 201)
except IntegrityError as e:
db.session.rollback()
print({"message": "Erreur de base de données : " + str(e)}, 500)
else:
print({"message": "Le dossier existe déjà"}, 200)

file_path = os.path.join(user_subfolder_info_db, 'expenses.txt')

with open(file_path, 'w', encoding='utf-8') as file:
file.write(f"MES DEPENSES MENSUELLES :\n\n")
expenses = Expense.query.filter_by(user_id=user_id).all()
expense_number = 0
for expense in expenses:
expense_number += 1
file.write(f"Dépense {expense_number}: {expense.title}, {expense.description}, {expense.price} euros\n")

except FileExistsError:
print(f"Le dossier '{user_subfolder_info_db}' existe déjà.")
except Exception as e:
print(f"Une erreur s'est produite lors de la création du dossier : {str(e)}")

file_path = os.path.join(user_subfolder_info_db, 'expenses.txt')

with open(file_path, 'w', encoding='utf-8') as file:
file.write(f"MES DEPENCES MENSUELLES :\n\n")
expenses = Expense.query.filter_by(user_id=user_id).all()
expense_number = 0
for expense in expenses:
expense_number += 1
file.write(f"Dépense {expense_number}: {expense.title}, {expense.description}, {expense.price} euros\n")


def add_expense_service(title, description, price, user_id):
expense = Expense(title=title, description=description, price=price, user_id=user_id)
Expand Down

0 comments on commit babc20e

Please sign in to comment.