From 764cbee0dc216b2e0a7c15ea6dd85676a5498af3 Mon Sep 17 00:00:00 2001 From: 630811 Date: Tue, 9 Jul 2024 09:39:28 -0500 Subject: [PATCH] adding back delete --- src/BannerService.test.ts | 18 ++++++++++++++++++ src/bannerService.ts | 13 +++++++++++++ src/routes/types.ts | 15 +++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/BannerService.test.ts b/src/BannerService.test.ts index 868cf28d..0cc97243 100644 --- a/src/BannerService.test.ts +++ b/src/BannerService.test.ts @@ -123,4 +123,22 @@ describe("BannerServiceRedis", () => { expect(retrievedBanner!.startTime).toEqual(updatedBanner.startTime); expect(retrievedBanner!.endTime).toEqual(updatedBanner.endTime); }); + it("should delete banner", async () => { + const banner = new Banner( + "6", + 1625889231000, + 1625889231000, + "test message6", + true, + 0, + 0 + ); + await bannerService.createBanner(banner); + + const deleteResult = await bannerService.deleteBanner(banner.id); + expect(deleteResult).toBeTruthy(); + + const retrievedBanner = await bannerService.getBanner(banner.id); + expect(retrievedBanner).toBeNull(); + }); }); diff --git a/src/bannerService.ts b/src/bannerService.ts index 9ec4eff9..43bee62d 100644 --- a/src/bannerService.ts +++ b/src/bannerService.ts @@ -6,6 +6,7 @@ export interface BannerService { getBanners(): Promise; createBanner(banner: Banner): Promise; updateBanner(id: string, banner: Banner): Promise; + deleteBanner(id: string): Promise; } export class BannerServiceRedis implements BannerService { @@ -75,4 +76,16 @@ export class BannerServiceRedis implements BannerService { return false; } } + + async deleteBanner(id: string): Promise { + try { + const key = this.generateKey(id); + await this.cache.delete(key); + const exists = await this.cache.has(key); + return !exists; + } catch (error) { + console.error("Error deleting banner:", error); + return false; + } + } } diff --git a/src/routes/types.ts b/src/routes/types.ts index da6e5cd5..c4da801f 100644 --- a/src/routes/types.ts +++ b/src/routes/types.ts @@ -35,6 +35,7 @@ export interface ICache { ): Promise; get(Key: any): Promise; has(Key: any): Promise; + delete(Key: string): Promise; keys(pattern: string): Promise; } @@ -68,6 +69,14 @@ export class RedisCache implements ICache { const val = await getAsync(Key); return val != null; } + async delete(Key: string): Promise { + await new Promise((resolve, reject) => { + this.theCache.del(Key, (err) => { + if (err) return reject(err); + resolve(); + }); + }); + } async keys(pattern: string): Promise { const keysAsync = promisify(this.theCache.keys).bind(this.theCache); try { @@ -108,6 +117,12 @@ export class TestCache implements ICache { resolve(val); }); } + delete(Key: string): Promise { + return new Promise((resolve) => { + this.theCache.del(Key); + resolve(); + }); + } keys(pattern: string): Promise { return new Promise((resolve) => { const allKeys = this.theCache.keys();