-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from FoxMalder-coder/module5-task2
- Loading branch information
Showing
17 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { DocumentType } from '@typegoose/typegoose'; | ||
import { CommentEntity } from './comment.entity.js'; | ||
import { CreateCommentDto } from './dto/create-comment.dto.js'; | ||
|
||
export interface CommentService { | ||
create(dto: CreateCommentDto): Promise<DocumentType<CommentEntity>>; | ||
findByOfferId(offerId: string): Promise<DocumentType<CommentEntity>[]>; | ||
deleteByOfferId(offerId: string): Promise<number | null>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Container } from 'inversify'; | ||
import { Component } from '../../const.js'; | ||
import { CommentService } from './comment-service.interface.js'; | ||
import { DefaultCommentService } from './default-comment.service.js'; | ||
import { types } from '@typegoose/typegoose'; | ||
import { CommentEntity, CommentModel } from './comment.entity.js'; | ||
|
||
export function createCommentContainer() { | ||
const commentContainer = new Container(); | ||
|
||
commentContainer.bind<CommentService>(Component.CommentService).to(DefaultCommentService).inSingletonScope(); | ||
commentContainer.bind<types.ModelType<CommentEntity>>(Component.CommentModel).toConstantValue(CommentModel); | ||
|
||
return commentContainer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { defaultClasses, getModelForClass, modelOptions, prop, Ref } from '@typegoose/typegoose'; | ||
import { OfferEntity } from '../offer/index.js'; | ||
import { UserEntity } from '../user/index.js'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging | ||
export interface CommentEntity extends defaultClasses.Base { } | ||
|
||
@modelOptions({ | ||
schemaOptions: { | ||
collection: 'comments', | ||
timestamps: true, | ||
} | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging | ||
export class CommentEntity extends defaultClasses.TimeStamps { | ||
@prop({ trim: true, required: true }) | ||
public text: string; | ||
|
||
@prop({ | ||
ref: OfferEntity, | ||
required: true | ||
}) | ||
public offerId: Ref<OfferEntity>; | ||
|
||
@prop({ | ||
ref: UserEntity, | ||
required: true, | ||
}) | ||
public userId: Ref<UserEntity>; | ||
|
||
@prop({ required: true }) | ||
public rating: number; | ||
} | ||
|
||
export const CommentModel = getModelForClass(CommentEntity); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { DocumentType, types } from '@typegoose/typegoose'; | ||
import { inject, injectable } from 'inversify'; | ||
import { CommentService } from './comment-service.interface.js'; | ||
import { CreateCommentDto } from './dto/create-comment.dto.js'; | ||
import { CommentEntity } from './comment.entity.js'; | ||
import { Component } from '../../const.js'; | ||
|
||
@injectable() | ||
export class DefaultCommentService implements CommentService { | ||
constructor( | ||
@inject(Component.CommentModel) private readonly commentModel: types.ModelType<CommentEntity> | ||
) { } | ||
|
||
public async create(dto: CreateCommentDto): Promise<DocumentType<CommentEntity>> { | ||
const comment = await this.commentModel.create(dto); | ||
return comment.populate('userId'); | ||
} | ||
|
||
public async findByOfferId(offerId: string): Promise<DocumentType<CommentEntity>[]> { | ||
return this.commentModel.find({ offerId }).populate('userId').exec(); | ||
} | ||
|
||
public async deleteByOfferId(offerId: string): Promise<number> { | ||
const result = await this.commentModel.deleteMany({ offerId }).exec(); | ||
return result.deletedCount; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class CreateCommentDto { | ||
public offerId: string; | ||
public text: string; | ||
public rating: number; | ||
public userId: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { CommentService } from './comment-service.interface.js'; | ||
export { CommentEntity, CommentModel } from './comment.entity.js'; | ||
export { DefaultCommentService } from './default-comment.service.js'; | ||
export { createCommentContainer } from './comment.container.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { City, Location, OfferType } from '../../../types/index.js'; | ||
|
||
export class UpdateOfferDto { | ||
public title?: string; | ||
public description?: string; | ||
public postDate?: Date; | ||
public city?: City; | ||
public preview?: string; | ||
public images?: string[]; | ||
public isPremium?: boolean; | ||
public isFavorite?: boolean; | ||
public type?: OfferType; | ||
public rooms?: number; | ||
public guests?: number; | ||
public price?: number; | ||
public facilities?: string[]; | ||
public userId?: string; | ||
public location?: Location; | ||
public rating?: number; | ||
public commentCount?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import { DocumentType } from '@typegoose/typegoose'; | ||
import { CreateOfferDto } from './dto/create-offer.dto.js'; | ||
import { OfferEntity } from './offer.entity.js'; | ||
import { UpdateOfferDto } from './dto/update-offer.dto.js'; | ||
import { City } from '../../types/index.js'; | ||
|
||
export interface OfferService { | ||
create(dto: CreateOfferDto): Promise<DocumentType<OfferEntity>>; | ||
find(): Promise<DocumentType<OfferEntity>[] | null>; | ||
findById(offerId: string): Promise<DocumentType<OfferEntity> | null>; | ||
findPremiumByCity(city: City): Promise<DocumentType<OfferEntity>[] | null>; | ||
deleteById(offerId: string): Promise<DocumentType<OfferEntity> | null>; | ||
updateById(offerId: string, dto: UpdateOfferDto): Promise<DocumentType<OfferEntity> | null>; | ||
exists(documentId: string): Promise<boolean>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export class UpdateUserDto { | ||
public name?: string; | ||
public avatar?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { DocumentType } from '@typegoose/typegoose'; | ||
import { UserEntity } from './user.entity.js'; | ||
import { CreateUserDto } from './dto/create-user.dto.js'; | ||
import { UpdateUserDto } from './dto/update-user.dto.js'; | ||
|
||
export interface UserService { | ||
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>; | ||
} |