views:

30

answers:

2

I am trying to create a group notification system. If I am in a group, then anyone who comment on the group's wall, a notification will send out to every group member. Here is my database design: I have two tables: Notification and NotificationRead.

NotificationRead
+userId (String)
+lastRead (int) - default is 0

Notification
...
+time(int)
...

Every user has one entry in NotificationRead, it keep track of when is the last time I read my notification. The logic is: for a particular user, if Notification.time > NotificationRead.lastRead, then that notification is considered unread. Let say that in group A, there are 4 notifications I have not read, and their time is 7, 8, 9, 10, then when I click onto group A, I set my NotificationRead.lastRead = 10 (the largest time), so I wont read them again. New notifications will have their time start at 11. Now, here is my problem. Let say I have 3 groups, A, B and C

A (4): largest time is 10
B (1): largest time is 14
C (1): largest time is 12

if I click onto A, my NotificationRead.lastRead = 10, the 4 next to A clear off, 1 next to B and C stay put. Now if I click on B, my lastRead now is 14, so not only it clear off the 1 next to B but also the 1 next to C since 14 > 12. Can anyone help me think of a way to solve this. I am open to completely redesign everything

+1  A: 

Cant you just add a groupID column to your NotificationRead table so you know the lastRead value for each User\Group combination>?

Sage
Great idea. Thank you +1
Harry Pham
+1  A: 

If you wish to know the last notification time per user per group, you must store that information. Therefore, each user must have more than one record in NotificationRead, which must become a separate table from the user table. This table will have three columns, the user_id, the group_id, and the lastread value for that user/group.

Larry Lustig
I ran into a problem with this approach. First of all, I search for all unread notification, then create a `hashMap`, to map the list of unread notifications onto the group list. IMHO, this is faster than looping from my group list and shoot request to the database. (Please read below)
Harry Pham
(Please first read the above) Now The way I search for all unread notification is `Notification.time > NotificationRead.lastRead` which make sense since `lastRead` associate with a unique `userId`. But now `lastRead` associate with `userId and groupId`, meaning there are multiple `lastRead per userId, cant do `Notification.time > NotificationRead.lastRead`
Harry Pham
Assuming you are using an SQL-based database the natural way (at least to me, a database programmer) would be to get the unread notifications using an SQL query. To get the unread notifications for user_id 100 in group A, the SQL would be SELECT * FROM notifications N INNER JOIN NotificatoinRead NR ON N.user_id = NR.user_id AND N.group_id = NR.group_id WHERE N.user_id = 100 AND N.group_id = A AND N.time > NR.lastRead. To get all unread notifications across groups remove "AND N.group_id = A" from the query.
Larry Lustig
Thank you I got it working :D +1
Harry Pham