Skip to content

Commit

Permalink
adding back delete
Browse files Browse the repository at this point in the history
  • Loading branch information
dmekala-va committed Jul 9, 2024
1 parent 57f5f14 commit 764cbee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/BannerService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
13 changes: 13 additions & 0 deletions src/bannerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface BannerService {
getBanners(): Promise<Banner[]>;
createBanner(banner: Banner): Promise<Banner | null>;
updateBanner(id: string, banner: Banner): Promise<boolean>;
deleteBanner(id: string): Promise<boolean>;
}

export class BannerServiceRedis implements BannerService {
Expand Down Expand Up @@ -75,4 +76,16 @@ export class BannerServiceRedis implements BannerService {
return false;
}
}

async deleteBanner(id: string): Promise<boolean> {
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;
}
}
}
15 changes: 15 additions & 0 deletions src/routes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface ICache {
): Promise<unknown>;
get(Key: any): Promise<any>;
has(Key: any): Promise<boolean>;
delete(Key: string): Promise<void>;
keys(pattern: string): Promise<string[]>;
}

Expand Down Expand Up @@ -68,6 +69,14 @@ export class RedisCache implements ICache {
const val = await getAsync(Key);
return val != null;
}
async delete(Key: string): Promise<void> {
await new Promise<void>((resolve, reject) => {
this.theCache.del(Key, (err) => {
if (err) return reject(err);
resolve();
});
});
}
async keys(pattern: string): Promise<string[]> {
const keysAsync = promisify(this.theCache.keys).bind(this.theCache);
try {
Expand Down Expand Up @@ -108,6 +117,12 @@ export class TestCache implements ICache {
resolve(val);
});
}
delete(Key: string): Promise<void> {
return new Promise((resolve) => {
this.theCache.del(Key);
resolve();
});
}
keys(pattern: string): Promise<string[]> {
return new Promise<string[]>((resolve) => {
const allKeys = this.theCache.keys();
Expand Down

0 comments on commit 764cbee

Please sign in to comment.