E
E
Emmet12020-03-14 21:19:24
MySQL
Emmet1, 2020-03-14 21:19:24

How to make the right architecture of paid subscribers on the site?

The task is to make it possible on the site for users of one group to subscribe (donate money) for a month to users of another group.
All users are already divided into the necessary groups. Some materials will be available to the subscriber, let's say a blog or news.
This is the first time I am faced with such a task, so I'm wondering how to correctly write a list of users who donated to a specific user into the database. And even more so, how to remove them from there, from donators, after a month.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Dokin, 2020-03-15
@alnidok

In a DB it is logical to store this business in the form of tables of users and subscriptions. When subscribing, create a record where to store user ids (to whom and who subscribed), as well as the start and end times of the subscription.

Structure example
CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `type` enum('AUT','SUB') NOT NULL DEFAULT 'SUB',
  PRIMARY KEY (`id`)
);

CREATE TABLE `subscription` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `author_id` int(11) NOT NULL COMMENT 'На кого подписываются',
  `subscriber_id` int(11) NOT NULL COMMENT 'Кто подписывается',
  `created_at` timestamp NOT NULL COMMENT 'Время подписки',
  `expired_at` timestamp NOT NULL COMMENT 'Когда истекает подписка',
  PRIMARY KEY (`id`)
);

# Пусть у нас есть один автор и два пользователя:
INSERT INTO `user` (`id`, `name`, `type`)
VALUES
  (1, 'Alex Pushkin', 'AUT'),
  (2, 'Vasya Pupkin', 'SUB'),
  (3, 'Jane Doe', 'SUB');

# Один из них подписался на Автора месяцем ранее, второй недавно
INSERT INTO `subscription` (`author_id`, `subscriber_id`, `created_at`, `expired_at`)
VALUES
  (1, 2, '2020-02-15 13:46:12', '2020-03-15 13:46:12'),
  (1, 3, '2020-03-15 13:46:12', '2020-04-15 13:46:12');

If we need to find out active subscriptions, we simply select posts with the right author, whose subscription time is right.
SELECT * 
FROM subscription
WHERE author_id = 1 and expired_at < CURRENT_TIMESTAMP and created_at < CURRENT_TIMESTAMP;

You can probably figure out the rest of the logic yourself.
You can play in the sandbox:
sqlfiddle.com/#!9/f4fc27/3/0

A
Alexander Stepanov, 2020-03-14
@Exebeche

Well, the user status field donate / not donate, a connecting table with 2 fields who and to whom (user_id), well, come up with something where the date and paid time will be stored ...
I would do this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question