N
N
Nikita Undefined2021-03-10 16:33:52
PHP
Nikita Undefined, 2021-03-10 16:33:52

How to properly link the Products, Users, Orders classes for a php application?

The task is the simplest telegram bot in which, with the start command, the bot creates an entry in the User table , which contains the columns id*, username, city, product, payment, order, order_status . If there is already a user with the same id, its username is simply updated, the rest of the fields are NULL by default. ID and username I take from api.
Next, the user is asked to select a city, which is selected from the products table , in which the columns are: id, city, product_name, price, product_link, buyer . Product is product_link, and buyer is NULL by default, but user_id will be entered after sale.
When the user answers which city he is interested in - in the users table in the row with his ID, the city field is updated, then it is proposed to select a product, in the same way we fill in the product field in the user table. It also comes with payment. After that, the user is prompted to pay for the order, which is formed in the third table orders with the fields: order_id, user_id, product_id, date_create, date_close, status . Cron smokes every 2 minutes all orders that are not overdue, checks the payment and if the payment was successful - updates the date_close and status, as well as the buyer in the product table and order_status in the user table. After that, he smokes the link of the product, and the product goes to the client.

So here's the crux of the matter: That's all - I quickly did it in a hurry in one file (well, I included the config with access separately), and of course the spaghetti code that turned out worked for itself quietly, but I already forgot about this bot. Half a year has passed, and it needs to be finalized ... In the form that it is now, it is unrealistic to support it, therefore it was decided to refactor it and make it according to the canons of OOP. But I'm weak in this, and the head just swells from all this.
Tell me, how best to refactor this whole thing? What classes to create?

PS: offhand created the classes Products, Users, Orders, Db, Bot. In Db - I have a private PDO connection, and database manipulation methods (all SQL queries). Bot - Holds only two methods - checkMessage and sendMessage. The class user, product and order have properties like columns of tables, but the methods for manipulating them are in Db. And I can't put it all together. Explain on your fingers approximately what should be the architecture of this whole booth?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2021-03-10
@SilenceOfWinter

Take DB/ORM and Auth packages from a framework available to you and make an application based on them. You will have to write half as much, including tests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question