Skip to content

Commit

Permalink
Prepared ground for service implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhamdi committed Oct 3, 2023
1 parent 1ff0693 commit afb47b1
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 44 deletions.
18 changes: 18 additions & 0 deletions src/db/dao/IBaseDAO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface IBaseDAO<D extends object> {
// Count documents that match a query
count(query: object): Promise<number>;

// Create a new document
create(data: Partial<D>): Promise<D>;

// Delete a document by ID
deleteById(userId: string): Promise<boolean>;

// Find documents that match a query
find(query: object): Promise<D[]>;

findById(userId: object): Promise<D | null>;

// Update a document by ID
updateById(userId: string, update: Partial<D>): Promise<D | null>;
}
17 changes: 17 additions & 0 deletions src/db/dao/IDaoFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IOffer } from "../model/IOffer";
import { IOrder } from "../model/IOrder";
import { ITokenTimetable } from "../model/ITokenTimetable";
import { IUserCredits } from "../model/IUserCredits";
import {
IOfferDao,
IOrderDao,
ITokenTimetableDao,
IUserCreditsDao,
} from "./index";

export interface IDaoFactory<K> {
getOfferDao(): IOfferDao<K, IOffer<K>>;
getOrderDao(): IOrderDao<K, IOrder<K>>;
getTokenTimetableDao(): ITokenTimetableDao<K, ITokenTimetable<K>>;
getUserCreditsDao(): IUserCreditsDao<K, IUserCredits<K>>;
}
6 changes: 6 additions & 0 deletions src/db/dao/IOfferDao.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {IOffer} from "../model/IOffer";
import {IBaseDAO} from "./IBaseDAO";

export interface IOfferDao<K extends object, D extends IOffer<K>>
extends IBaseDAO<D> {
}
6 changes: 6 additions & 0 deletions src/db/dao/IOrderDao.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {IOrder} from "../model/IOrder";
import {IBaseDAO} from "./IBaseDAO";

export interface IOrderDao<K extends object, D extends IOrder<K>>
extends IBaseDAO<D> {
}
7 changes: 7 additions & 0 deletions src/db/dao/ITokenTimetableDao.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ITokenTimetable } from "../model/ITokenTimetable";
import {IBaseDAO} from "./IBaseDAO";

export interface ITokenTimetableDao<
K extends object,
D extends ITokenTimetable<K>,
> extends IBaseDAO<D> {}
7 changes: 7 additions & 0 deletions src/db/dao/IUserCreditsDao.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {ISubscription, IUserCredits} from "../model/IUserCredits";
import {IBaseDAO} from "./IBaseDAO";

export interface IUserCreditsDao<K extends object, D extends IUserCredits<K>>
extends IBaseDAO<D> {
findByUserId(userId: K): Promise<ISubscription<K>[]>;
}
40 changes: 6 additions & 34 deletions src/db/dao/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
import { IOffer } from "../model/IOffer";
import { IOrder } from "../model/IOrder";
import { ISubscription, IUserCredits } from "../model/IUserCredits";
// index.ts

export interface IUserCreditsDao<K extends object, D extends IUserCredits<K>>
extends BaseDAO<D> {
findByUserId(userId: K): Promise<ISubscription<K>[]>;
}

export interface IOfferDao<K extends object, D extends IOffer<K>>
extends BaseDAO<D> {}

export interface IOrderDao<K extends object, D extends IOrder<K>>
extends BaseDAO<D> {}

export interface ISubscriptionDao<K extends object> extends BaseDAO<K> {}

export interface BaseDAO<D extends object> {
// Count documents that match a query
count(query: object): Promise<number>;

// Create a new document
create(data: Partial<D>): Promise<D>;

// Delete a document by ID
deleteById(userId: string): Promise<boolean>;

// Find documents that match a query
find(query: object): Promise<D[]>;

findById(userId: object): Promise<D | null>;

// Update a document by ID
updateById(userId: string, update: Partial<D>): Promise<D | null>;
}
export { IDaoFactory } from "./IDaoFactory";
export { IOfferDao } from "./IOfferDao";
export { IOrderDao } from "./IOrderDao";
export { ITokenTimetableDao } from "./ITokenTimetableDao";
export { IUserCreditsDao } from "./IUserCreditsDao";
6 changes: 6 additions & 0 deletions src/db/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// index.ts

export { IOffer } from "./IOffer";
export { IOrder } from "./IOrder";
export { ITokenTimetable } from "./ITokenTimetable";
export { IUserCredits } from "./IUserCredits";
6 changes: 3 additions & 3 deletions src/impl/mongoose/dao/BaseMongooseDao.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Document, Model } from "mongoose";

import { BaseDAO } from "../../../db/dao";
import { IBaseDAO } from "../../../db/dao/IBaseDAO";

/**
* Delegates all calls to mongoose in the constructor passed model property. This is an adapter to allow subclasses to implement local interfaces
*/
export class BaseMongooseDao<D extends Document> implements BaseDAO<D> {
export class BaseMongooseDao<D extends Document> implements IBaseDAO<D> {
model: Model<D>;

constructor(model: Model<D>) {
this.model = model;
}

// Find document by ID
async findById(userId: string): Promise<D | null> {
async findById(userId: object): Promise<D | null> {
const result = await this.model.findById(userId).exec();
return result ? result.toObject() : null;
}
Expand Down
52 changes: 52 additions & 0 deletions src/impl/mongoose/dao/MongooseDaoFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ObjectId } from "mongoose";

import {
IDaoFactory,
IOfferDao,
IOrderDao,
ITokenTimetableDao,
IUserCreditsDao,
} from "../../../db/dao";
import {
IOffer,
IOrder,
ITokenTimetable,
IUserCredits,
} from "../../../db/model";
import { OfferDao, OrderDao, TokenTimetableDao, UserCreditsDao } from ".";

export class MongooseDaoFactory implements IDaoFactory<ObjectId> {
private readonly offerDao;
private readonly orderDao;
private readonly tokenTimetableDao;
private readonly userCreditsDao;

constructor(
private uri: string,
private dbName: string,
) {
this.offerDao = new OfferDao(uri, dbName);
this.orderDao = new OrderDao(uri, dbName);
this.tokenTimetableDao = new TokenTimetableDao(uri, dbName);
this.userCreditsDao = new UserCreditsDao(uri, dbName);
}

getOfferDao(): IOfferDao<ObjectId, IOffer<ObjectId>> {
return this.offerDao;
}

getOrderDao(): IOrderDao<ObjectId, IOrder<ObjectId>> {
return this.orderDao;
}

getTokenTimetableDao(): ITokenTimetableDao<
ObjectId,
ITokenTimetable<ObjectId>
> {
return this.tokenTimetableDao;
}

getUserCreditsDao(): IUserCreditsDao<ObjectId, IUserCredits<ObjectId>> {
return this.userCreditsDao;
}
}
2 changes: 1 addition & 1 deletion src/impl/mongoose/dao/OfferDao.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObjectId } from "mongoose";

import { IOfferDao } from "../../../db/dao";
import { IOfferDao } from "../../../db/dao/IOfferDao";
import { MongooseModels } from "../model";
import { IMongooseOffer } from "../model/Offer";
import { BaseMongooseDao } from "./BaseMongooseDao";
Expand Down
4 changes: 2 additions & 2 deletions src/impl/mongoose/dao/OrderDao.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ObjectId } from "mongoose";

import { IOrderDao } from "../../../db/dao";
import { MongooseModels } from "../model";
import { IMongooseOrder } from "../model/Order";
import { BaseMongooseDao } from "./BaseMongooseDao";
import {IOrderDao} from "../../../db/dao/IOrderDao";

export class OfferDao
export class OrderDao
extends BaseMongooseDao<IMongooseOrder>
implements IOrderDao<ObjectId, IMongooseOrder>
{
Expand Down
4 changes: 2 additions & 2 deletions src/impl/mongoose/dao/TokenTimetableDao.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ObjectId } from "mongoose";

import { IOrderDao } from "../../../db/dao";
import { MongooseModels } from "../model";
import { IMongooseTokenTimetable } from "../model/TokenTimetable";
import { BaseMongooseDao } from "./BaseMongooseDao";
import { ITokenTimetableDao } from "../../../db/dao";

export class TokenTimetableDao
extends BaseMongooseDao<IMongooseTokenTimetable>
implements IOrderDao<ObjectId, IMongooseTokenTimetable>
implements ITokenTimetableDao<ObjectId, IMongooseTokenTimetable>
{
constructor(uri: string, dbName: string) {
super(MongooseModels.getInstance(uri, dbName).tokenTimetableDao());
Expand Down
2 changes: 1 addition & 1 deletion src/impl/mongoose/dao/UserCreditsDao.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ObjectId } from "mongoose";

import { IUserCreditsDao } from "../../../db/dao";
import { ISubscription } from "../../../db/model/IUserCredits";
import { MongooseModels } from "../model";
import { IMongooseUserCredits } from "../model/UserCredits";
import { BaseMongooseDao } from "./BaseMongooseDao";
import {IUserCreditsDao} from "../../../db/dao/IUserCreditsDao";

export class UserCreditsDao
extends BaseMongooseDao<IMongooseUserCredits>
Expand Down
6 changes: 6 additions & 0 deletions src/impl/mongoose/dao/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// index.ts

export { OfferDao } from "./OfferDao";
export { OrderDao } from "./OrderDao";
export { TokenTimetableDao } from "./TokenTimetableDao";
export { UserCreditsDao } from "./UserCreditsDao";
4 changes: 3 additions & 1 deletion src/service/BaseService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { IOfferDao, ISubscriptionDao, IUserCreditsDao } from "../db/dao";
import { ISubscriptionDao} from "../db/dao";
import { IOffer } from "../db/model/IOffer";
import { IOrder } from "../db/model/IOrder";
import { ISubscription, IUserCredits } from "../db/model/IUserCredits";
import { IPayment } from "./IPayment";
import {IOfferDao} from "../db/dao/IOfferDao";
import {IUserCreditsDao} from "../db/dao/IUserCreditsDao";

export class BaseService<K extends object> implements IPayment<K> {
private readonly userCreditsDAO: IUserCreditsDao<K, IUserCredits<K>>;
Expand Down

0 comments on commit afb47b1

Please sign in to comment.