Skip to content

Commit

Permalink
rewrite settings module
Browse files Browse the repository at this point in the history
  • Loading branch information
wuttinanhi committed Sep 29, 2022
1 parent b319020 commit f442848
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"configurations": [
{
"name": "Debug Mock",
"type": "python",
"request": "launch",
"module": "mock.run",
"justMyCode": true
},
{
"name": "Python: Flask",
"type": "python",
Expand Down
4 changes: 2 additions & 2 deletions mock/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def mock():
print(intent.id)

PaymentService.handle_stripe_payment(intent)
except Exception as e:
print(str(e))
except Exception as err:
print(err)

# reservation_2 = ReservationService.create_reservation(
# user,
Expand Down
4 changes: 2 additions & 2 deletions settings/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
settings model
"""
from database import Base
from sqlalchemy import Column, Float
from sqlalchemy import Column, Float, Integer


class Setting(Base):
__tablename__ = 'settings'

id = Column(Float, primary_key=True)
id = Column(Integer, primary_key=True)
charge_within_hour = Column(Float)
charge_more_than_a_hour = Column(Float)
charge_more_than_a_day = Column(Float)
Expand Down
25 changes: 15 additions & 10 deletions settings/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
settings service
"""
from database.database import db_session
from werkzeug.exceptions import NotFound

from settings.model import Setting

Expand All @@ -10,15 +11,16 @@ class SettingService:
@staticmethod
def set_settings(settings: Setting):
try:
original = SettingService.get_settings()

if original is None:
db_session.add(settings)
else:
original.charge_within_hour = settings.charge_within_hour
original.charge_more_than_a_hour = settings.charge_more_than_a_hour
original.charge_more_than_a_day = settings.charge_more_than_a_day

get_settings = SettingService.get_settings()
Setting.query.filter(Setting.id == get_settings.id).update({
"charge_within_hour": settings.charge_within_hour,
"charge_more_than_a_hour": settings.charge_more_than_a_hour,
"charge_more_than_a_day": settings.charge_more_than_a_day
})
db_session.commit()
return settings
except NotFound as err:
db_session.add(settings)
db_session.commit()
return settings
except Exception as err:
Expand All @@ -27,4 +29,7 @@ def set_settings(settings: Setting):

@staticmethod
def get_settings() -> Setting:
return Setting.query.first()
settings = Setting.query.first()
if settings is None:
raise NotFound("Settings not found!")
return settings

0 comments on commit f442848

Please sign in to comment.