011-reservation_app.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import pathlib
  2. current = pathlib.Path(__file__).parent
  3. root = current.parent
  4. import sys
  5. sys.path.append(str(root))
  6. import assets
  7. import sqlmodel
  8. def drop_database() -> None:
  9. db = pathlib.Path("./009-reservation.db")
  10. if db.is_file():
  11. db.unlink()
  12. drop_database()
  13. connection = sqlmodel.create_engine("sqlite:///009-reservation.db")
  14. sqlmodel.SQLModel.metadata.create_all(connection)
  15. builder = assets.product_factory()
  16. builder.name = "Sample name"
  17. builder.author = "Simple UwU Artist"
  18. builder.description = "This is simple description"
  19. builder.barcode = "123456789012"
  20. builder.stock_count = 10
  21. product = builder.result
  22. print("Create first product:")
  23. print(product)
  24. print()
  25. with assets.product_loader(connection) as loader:
  26. print("Inserting product...")
  27. loader.store(product)
  28. print("Inserted:")
  29. print(product)
  30. print()
  31. with assets.product_loader(connection) as loader:
  32. print("Loading product to make reservation...")
  33. target = loader.get_by_barcode("123456789012")
  34. print("Loaded: ")
  35. print(target)
  36. before = target.on_stock
  37. reservation = assets \
  38. .reservation_factory() \
  39. .target(target) \
  40. .email("[email protected]") \
  41. .result()
  42. print(reservation)
  43. print()
  44. with assets.reservation_loader(connection) as loader:
  45. print("Inserting it into database...")
  46. loader.store(reservation)
  47. print(reservation)
  48. with assets.product_loader(connection) as loader:
  49. print("Loading product to make reservation...")
  50. target = loader.get_by_barcode("123456789012")
  51. print("Loaded: ")
  52. print(target)
  53. reservation = assets \
  54. .reservation_factory() \
  55. .target(target) \
  56. .email("[email protected]") \
  57. .result()
  58. print(reservation)
  59. print()
  60. with assets.reservation_loader(connection) as loader:
  61. print("Inserting it into database...")
  62. loader.store(reservation)
  63. print(reservation)
  64. users = assets.users_collection()
  65. factory = assets.user_factory()
  66. factory.nick = "test"
  67. factory.password = "12345678"
  68. users.add(factory.result)
  69. app = assets.reservation_app(connection, users)
  70. print("App initialized.")
  71. print()
  72. print("Loading all reservations:")
  73. print(app.get_by_product({
  74. "apikey": factory.apikey,
  75. "target_barcode": "123456789012",
  76. }))
  77. print(app.get_by_user({
  78. "apikey": factory.apikey,
  79. "email": "[email protected]"
  80. }))
  81. print()
  82. print("Giving back item:")
  83. print(app.give_back_product({
  84. "apikey": factory.apikey,
  85. "target_barcode": "123456789012",
  86. "email": "[email protected]"
  87. }))
  88. print("After:")
  89. print(app.get_by_product({
  90. "apikey": factory.apikey,
  91. "target_barcode": "123456789012",
  92. }))
  93. print()
  94. print("Rent product:")
  95. print(app.rent_product({
  96. "apikey": factory.apikey,
  97. "target_barcode": "123456789012",
  98. "email": "[email protected]"
  99. }))
  100. print(app.rent_product({
  101. "apikey": factory.apikey,
  102. "target_barcode": "123456789012",
  103. "email": "[email protected]",
  104. "phone_number": "+48 123456789"
  105. }))
  106. print("After:")
  107. print(app.get_by_product({
  108. "apikey": factory.apikey,
  109. "target_barcode": "123456789012",
  110. }))
  111. print()
  112. drop_database()