Skip to content

Commit

Permalink
add services
Browse files Browse the repository at this point in the history
  • Loading branch information
rcole1919 committed Oct 14, 2024
1 parent 7bbcca4 commit 0e02e79
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/shared/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
ROOMS_NUMBER,
PRICE,
DEFAULT_OFFER_COUNT,
INC_COMMENT_COUNT_NUMBER,
} from './offer.js';

export {
Expand Down
2 changes: 2 additions & 0 deletions src/shared/constants/offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export const OFFER_RATING = {
};

export const DEFAULT_OFFER_COUNT = 60;

export const INC_COMMENT_COUNT_NUMBER = 1;
10 changes: 4 additions & 6 deletions src/shared/modules/comment/default-comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DocumentType, types } from '@typegoose/typegoose';
import { inject, injectable } from 'inversify';

import { ICommentService } from './types/index.js';
import { CommentEntity, CreateCommentDTO } from './index.js';
import { CommentEntity, CreateCommentDTO, UpdateCommentDTO } from './index.js';
import { COMPONENT, DEFAULT_COMMENTS_COUNT } from '../../constants/index.js';
import { ILogger } from '../../libs/logger/types/index.js';
import { ESortType } from '../../types/sort-type.enum.js';
Expand Down Expand Up @@ -30,11 +30,9 @@ export class DefaultCommentService implements ICommentService {
.exec();
}

public async deleteByOfferId(offerId: string): Promise<number | null> {
const result = await this.commentModel
.deleteMany({ offerId })
public async updateRating(offerId: string, dto: UpdateCommentDTO): Promise<DocumentType<CommentEntity> | null> {
return this.commentModel
.findByIdAndUpdate(offerId, dto, { new: true })
.exec();

return result.deletedCount;
}
}
3 changes: 3 additions & 0 deletions src/shared/modules/comment/dto/update-comment.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class UpdateCommentDTO {
public rating!: number;
}
1 change: 1 addition & 0 deletions src/shared/modules/comment/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { CommentEntity, CommentModel } from './comment.entity.js';
export { CreateCommentDTO } from './dto/create-comment.dto.js';
export { UpdateCommentDTO } from './dto/update-comment.dto.js';
export { DefaultCommentService } from './default-comment.service.js';
export { createCommentContainer } from './comment.container.js';
4 changes: 2 additions & 2 deletions src/shared/modules/comment/types/comment-service.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DocumentType } from '@typegoose/typegoose';

import { CreateCommentDTO, CommentEntity } from '../index.js';
import { CreateCommentDTO, CommentEntity, UpdateCommentDTO } from '../index.js';

export interface ICommentService {
create(dto: CreateCommentDTO): Promise<DocumentType<CommentEntity>>;
findByOfferId(offerId: string): Promise<DocumentType<CommentEntity>[]>;
deleteByOfferId(offerId: string): Promise<number | null>;
updateRating(offerId: string, dto: UpdateCommentDTO): Promise<DocumentType<CommentEntity> | null>;
}
22 changes: 18 additions & 4 deletions src/shared/modules/offer/default-offer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import { DocumentType, types } from '@typegoose/typegoose';
import { inject, injectable } from 'inversify';

import { IOfferService } from './types/index.js';
import { OfferEntity, CreateOfferDTO, UpdateOfferDTO } from './index.js';
import { COMPONENT, DEFAULT_OFFER_COUNT } from '../../constants/index.js';
import {
OfferEntity,
CreateOfferDTO,
UpdateOfferDTO,
populateAuthor,
populateCommentsCount,
populateComments,
} from './index.js';
import { COMPONENT, DEFAULT_OFFER_COUNT, INC_COMMENT_COUNT_NUMBER } from '../../constants/index.js';
import { ILogger } from '../../libs/logger/types/index.js';
import { ESortType } from '../../types/index.js';

Expand All @@ -24,7 +31,7 @@ export class DefaultOfferService implements IOfferService {
public async findById(offerId: string): Promise<DocumentType<OfferEntity> | null> {
return this.offerModel
.findById(offerId)
.populate('authorId')
.aggregate([populateAuthor, ...populateComments])
.exec();
}

Expand All @@ -35,7 +42,7 @@ export class DefaultOfferService implements IOfferService {
.find()
.sort({ createdDate: ESortType.DESC })
.limit(limit)
.populate('authorId')
.aggregate([populateAuthor, ...populateCommentsCount])
.exec();
}

Expand All @@ -49,4 +56,11 @@ export class DefaultOfferService implements IOfferService {
.findByIdAndDelete(offerId)
.exec();
}

public async incCommentCount(offerId: string): Promise<DocumentType<OfferEntity> | null> {
return this.offerModel
.findByIdAndUpdate(offerId, {'$inc': {
commentCount: INC_COMMENT_COUNT_NUMBER,
}}).exec();
}
}
27 changes: 14 additions & 13 deletions src/shared/modules/offer/dto/update-offer.dto.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { EHousing, EFacilities, TCoords } from '../../../types/index.js';

export class UpdateOfferDTO {
public title!: string;
public description!: string;
public createdDate!: Date;
public city!: string;
public previewImagePath!: string;
public photos!: string[];
public isPremium!: boolean;
public housingType!: EHousing;
public roomsNumber!: number;
public visitorsNumber!: number;
public price!: number;
public facilities!: EFacilities[];
public coords!: TCoords;
public title?: string;
public description?: string;
public createdDate?: Date;
public city?: string;
public previewImagePath?: string;
public photos?: string[];
public isPremium?: boolean;
public housingType?: EHousing;
public roomsNumber?: number;
public visitorsNumber?: number;
public price?: number;
public facilities?: EFacilities[];
public coords?: TCoords;
public rating?: number;
}
1 change: 1 addition & 0 deletions src/shared/modules/offer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { CreateOfferDTO } from './dto/create-offer.dto.js';
export { UpdateOfferDTO } from './dto/update-offer.dto.js';
export { DefaultOfferService } from './default-offer.service.js';
export { createOfferContainer } from './offer.container.js';
export { populateAuthor, populateComments, populateCommentsCount } from './offer.aggregation.js';
43 changes: 43 additions & 0 deletions src/shared/modules/offer/offer.aggregation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { DEFAULT_COMMENTS_COUNT } from '../../constants/index.js';
import { ESortType } from '../../types/index.js';

export const populateAuthor = {
$lookup: {
from: 'users',
localField: 'authorId',
foreignField: '_id',
as: 'author',
},
};

export const populateCommentsCount = [
{
$lookup: {
from: 'comments',
let: { offerId: '$_id' },
pipeline: [
{ $match: { $expr: { $eq: ['$offerId', '$$offerId'] } } },
{ $project: { _id: 1 } },
],
as: 'comments',
}
},
{ id: { $toString: '$_id'}, commentsCount: { $size: '$comments'} },
{ $unset: 'comments' }
];

export const populateComments = [
{
$lookup: {
from: 'comments',
let: { offerId: '$_id' },
pipeline: [
{ $match: { $expr: { $eq: ['$offerId', '$$offerId'] } } },
{ $project: { _id: 1, text: 1, rating: 1 } },
],
as: 'comments',
}
},
{ $limit: DEFAULT_COMMENTS_COUNT },
{ $sort: { createdDate: ESortType.DESC } }
];
1 change: 1 addition & 0 deletions src/shared/modules/offer/types/offer-service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface IOfferService {
find(count?: number): Promise<DocumentType<OfferEntity>[]>;
deleteById(offerId: string): Promise<DocumentType<OfferEntity> | null>;
updateById(offerId: string, dto: UpdateOfferDTO): Promise<DocumentType<OfferEntity> | null>;
incCommentCount(offerId: string): Promise<DocumentType<OfferEntity> | null>;
}
29 changes: 27 additions & 2 deletions src/shared/modules/user/default-user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DocumentType, types } from '@typegoose/typegoose';
import { inject, injectable } from 'inversify';

import { IUserService } from './types/index.js';
import { UserEntity, CreateUserDTO } from './index.js';
import { UserEntity, CreateUserDTO, UpdateUserDTO, populateFavorites } from './index.js';
import { COMPONENT } from '../../constants/index.js';
import { ILogger } from '../../libs/logger/types/index.js';

Expand All @@ -23,7 +23,10 @@ export class DefaultUserService implements IUserService {
}

public async findByEmail(email: string): Promise<DocumentType<UserEntity> | null> {
return this.userModel.findOne({ email });
return this.userModel
.findOne({ email })
.aggregate([populateFavorites])
.exec();
}

public async findOrCreate(dto: CreateUserDTO, salt: string): Promise<DocumentType<UserEntity>> {
Expand All @@ -35,4 +38,26 @@ export class DefaultUserService implements IUserService {

return this.create(dto, salt);
}

public async updateById(userId: string, dto: UpdateUserDTO): Promise<DocumentType<UserEntity> | null> {
return this.userModel
.findByIdAndUpdate(userId, dto, { new: true })
.exec();
}

public async addFavorite(userId: string ,offerId: string): Promise<DocumentType<UserEntity> | null> {
return this.userModel
.findByIdAndUpdate(userId, {
$addToSet: { favorites: offerId },
}, { new: true })
.exec();
}

public async deleteFavorite(userId: string, offerId: string): Promise<DocumentType<UserEntity> | null> {
return this.userModel
.findByIdAndUpdate(userId, {
$pull: { favorites: offerId },
}, { new: true })
.exec();
}
}
9 changes: 9 additions & 0 deletions src/shared/modules/user/dto/update-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { EUserType } from '../../../types/index.js';

export class UpdateUserDTO {
public email?: string;
public avatarPath?: string;
public name?: string;
public type?: EUserType;
public password?: string;
}
2 changes: 2 additions & 0 deletions src/shared/modules/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { UserEntity, UserModel } from './user.entity.js';
export { CreateUserDTO } from './dto/create-user.dto.js';
export { UpdateUserDTO } from './dto/update-user.dto.js';
export { DefaultUserService } from './default-user.service.js';
export { createUserContainer } from './user.container.js';
export { populateFavorites } from './user.aggregation.js';
5 changes: 4 additions & 1 deletion src/shared/modules/user/types/user-service.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { DocumentType } from '@typegoose/typegoose';

import { CreateUserDTO, UserEntity } from '../index.js';
import { CreateUserDTO, UserEntity, UpdateUserDTO } from '../index.js';

export interface IUserService {
create(dto: CreateUserDTO, salt: string): Promise<DocumentType<UserEntity>>;
findByEmail(email: string): Promise<DocumentType<UserEntity> | null>;
findOrCreate(dto: CreateUserDTO, salt: string): Promise<DocumentType<UserEntity>>;
updateById(userId: string, dto: UpdateUserDTO): Promise<DocumentType<UserEntity> | null>;
addFavorite(userId: string, offerId: string): Promise<DocumentType<UserEntity> | null>;
deleteFavorite(userId: string, offerId: string): Promise<DocumentType<UserEntity> | null>;
}
8 changes: 8 additions & 0 deletions src/shared/modules/user/user.aggregation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const populateFavorites = {
$lookup: {
from: 'offers',
localField: 'favorites',
foreignField: '_id',
as: 'favorites',
},
};

0 comments on commit 0e02e79

Please sign in to comment.