loading words...

Aug 15, 2019 11:36:41

How to This: Share Feature

by @basilesamel PATRON | 275 words | 302🔥 | 340💌

Basile Samel

Current day streak: 302🔥
Total posts: 340💌
Total words: 155297 (621 pages 📄)

User story: A user U can share a resource R owned by user O to a space S.

Description: How can I can reproduce Facebook's share feature?

1) Relational database

Table User_U: id

Table Resource_R: id, fk_user_o_id (foreign key to user O)

Table SharedResource_SR: id, fk_resource_id (foreign key to resource R), fk_user_u_id (foreign key to user U), fk_space_s_id (foreign key to space S), shared_at (datetime)

Table Space_S: id

Observations: We go with a denormalized schema. The data redundancy is quite small (a regular user shares a given resource in a space once or twice) so we will prefer increasing the Read performance.

2) SCRUD functions

Share a resource

Requirements:

  1. A resource can be shared several times in a space. (interactions should be encouraged, spam can be handled by moderators)

Pseudo-algorithm:

  1. Insert SharedResource_SR row

Unshare a resource

Requirements:

  1. Can't unshare someone else's shared resource.

Pseudo-algorithm:

  1. delete the SharedResource_SR row by id where the fk_user_u_id field and the requesting user's id correspond

Get all resources shared in a given space

SQL:

SELECT r.id, IF(sr.fk_user_u_id = '{{user_id}}', true, false) AS can_unshare FROM Resource_R as r JOIN SharedResource_SR AS sr ON sr.fk_resource_id = r.id WHERE sr.fk_space_s_id = '{{given space id}}'

Observations:

In this SQL statement we add a condition to tell the view whether or not the current user can Unshare a given text. If that's the case, we will display an Unshare button when it's appropriate.

From Basile Samel's collection:

contact: email - twitter / Terms / Privacy