diff --git a/src/controllers/NotificationsController.ts b/src/controllers/NotificationsController.ts new file mode 100644 index 0000000..5615ca0 --- /dev/null +++ b/src/controllers/NotificationsController.ts @@ -0,0 +1,23 @@ +import { RequestHandler } from "../entities/RequestHandler"; +import { NotificationsHelper } from "../helpers/NotificationsHelper"; + +/** + * Notifications controller + * + * @author Pierre HUBERT + */ + +export class NotificationsController { + + /** + * Get the number of unread notifications + * + * @param h Request handler + */ + public static async CountUnread(h: RequestHandler) { + h.send({ + number: await NotificationsHelper.CountUnread(h.getUserId()) + }); + } + +} \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 47c8dec..e2f217e 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -5,6 +5,7 @@ import { UserController } from "./UserController"; import { ConversationsController } from "./ConversationsController"; import { SearchController } from "./SearchController"; import { GroupsController } from "./GroupsController"; +import { NotificationsController } from "./NotificationsController"; /** * Controllers routes @@ -130,4 +131,9 @@ export const Routes : Route[] = [ {path: "/groups/set_following", cb: (h) => GroupsController.SetFollowing(h)}, {path: "/groups/delete", cb: (h) => GroupsController.DeleteGroup(h)}, + + + + // Notifications controller + {path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)}, ] \ No newline at end of file diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts new file mode 100644 index 0000000..35a8e97 --- /dev/null +++ b/src/helpers/NotificationsHelper.ts @@ -0,0 +1,28 @@ +import { DatabaseHelper } from "./DatabaseHelper"; + +/** + * Notifications helper + * + * @author Pierre HUBERT + */ + +const NOTIFICATIONS_TABLE = "comunic_notifications"; + +export class NotificationsHelper { + + /** + * Count the number of unread notifications of a user + * + * @param userID Target user ID + */ + public static async CountUnread(userID: number) : Promise { + return await DatabaseHelper.Count({ + table: NOTIFICATIONS_TABLE, + where: { + dest_user_id: userID, + seen: 0 + } + }); + } + +} \ No newline at end of file