-
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.
- Loading branch information
Showing
20 changed files
with
640 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
openapi: 3.1.0 | ||
|
||
info: | ||
title: API-сервер для демо-проекта «Шесть городов» | ||
description: Список ресурсов и маршрутов сервера «Шесть городов» | ||
license: | ||
name: MIT | ||
url: https://opensource.org/licenses/MIT | ||
version: 2.0.0 | ||
|
||
tags: | ||
- name: offers | ||
description: Действия с объявлениями. | ||
- name: categories | ||
description: Действия с категориями. | ||
- name: comments | ||
description: Действия с комментариями. | ||
- name: users | ||
description: Действия с пользователем. | ||
|
||
paths: | ||
/users/register: | ||
post: | ||
tags: | ||
- users | ||
summary: Регистрация пользователя | ||
description: Регистрирует нового пользователя. | ||
|
||
requestBody: | ||
description: Информация для создания нового пользователя. | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/createUser' | ||
required: true | ||
|
||
responses: | ||
"201": | ||
description: Пользователь зарегистрирован. Объект пользователя. | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/user' | ||
|
||
"409": | ||
description: Пользователь с таким email уже существует. | ||
|
||
/users/login: | ||
post: | ||
tags: | ||
- users | ||
summary: Авторизация пользователя | ||
description: Авторизует пользователя на основе логина и пароля | ||
|
||
get: | ||
tags: | ||
- users | ||
summary: Проверка состояния пользователя | ||
description: Возвращает информацию по авторизованному пользователю | ||
|
||
/users/{userId}/avatar: | ||
post: | ||
tags: | ||
- users | ||
summary: Загрузить изображение аватара | ||
description: Загружает изображение аватара пользователя. | ||
Изображение аватара должно быть в формате `png` или `jpg`. | ||
|
||
components: | ||
schemas: | ||
createUser: | ||
type: object | ||
|
||
properties: | ||
email: | ||
type: string | ||
example: [email protected] | ||
|
||
firstname: | ||
type: string | ||
example: Keks | ||
|
||
lastname: | ||
type: string | ||
example: Cat | ||
|
||
password: | ||
type: string | ||
example: 123456 | ||
|
||
user: | ||
type: object | ||
|
||
properties: | ||
id: | ||
type: string | ||
example: 6329c3d6a04ab1061c6425ea | ||
|
||
email: | ||
type: string | ||
example: [email protected] |
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,14 @@ | ||
import { DocumentType } from '@typegoose/typegoose'; | ||
import { CreateCategoryDto } from './dto/create-category.dto.js'; | ||
import { CategoryEntity } from './category.entity.js'; | ||
|
||
export interface CategoryService { | ||
create(dto: CreateCategoryDto): Promise<DocumentType<CategoryEntity>>; | ||
findByCategoryId(categoryId: string): Promise<DocumentType<CategoryEntity> | null>; | ||
findByCategoryName(categoryName: string): Promise<DocumentType<CategoryEntity> | null>; | ||
findByCategoryNameOrCreate( | ||
categoryName: string, | ||
dto: CreateCategoryDto | ||
): Promise<DocumentType<CategoryEntity>>; | ||
find(): Promise<DocumentType<CategoryEntity>[]>; | ||
} |
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,18 @@ | ||
import { Container } from 'inversify'; | ||
import { types } from '@typegoose/typegoose'; | ||
|
||
import { Component } from '../../types/index.js'; | ||
import { CategoryService } from './category-service.interface.js'; | ||
import { DefaultCategoryService } from './default-category.service.js'; | ||
import { CategoryEntity, CategoryModel } from './category.entity.js'; | ||
|
||
export function createCategoryContainer() { | ||
const categoryContainer = new Container(); | ||
|
||
categoryContainer.bind<CategoryService>(Component.CategoryService).to(DefaultCategoryService); | ||
categoryContainer | ||
.bind<types.ModelType<CategoryEntity>>(Component.CategoryModel) | ||
.toConstantValue(CategoryModel); | ||
|
||
return categoryContainer; | ||
} |
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,14 @@ | ||
import { defaultClasses, getModelForClass, modelOptions, prop } from '@typegoose/typegoose'; | ||
import { Category } from '../../types/index.js'; | ||
|
||
export interface CategoryEntity extends defaultClasses.Base {} | ||
|
||
@modelOptions({ | ||
schemaOptions: { collection: 'categories', timestamps: true }, | ||
}) | ||
export class CategoryEntity extends defaultClasses.TimeStamps implements Category { | ||
@prop({ required: true, trim: true }) | ||
public name!: string; | ||
} | ||
|
||
export const CategoryModel = getModelForClass(CategoryEntity); |
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,49 @@ | ||
import { inject, injectable } from 'inversify'; | ||
import { DocumentType, types } from '@typegoose/typegoose'; | ||
|
||
import { CategoryService } from './category-service.interface.js'; | ||
import { Component } from '../../types/index.js'; | ||
import { Logger } from '../../libs/logger/index.js'; | ||
import { CategoryEntity } from './category.entity.js'; | ||
import { CreateCategoryDto } from './dto/create-category.dto.js'; | ||
|
||
@injectable() | ||
export class DefaultCategoryService implements CategoryService { | ||
constructor( | ||
@inject(Component.Logger) private readonly logger: Logger, | ||
@inject(Component.CategoryModel) private readonly categoryModel: types.ModelType<CategoryEntity> | ||
) {} | ||
|
||
public async create(dto: CreateCategoryDto): Promise<DocumentType<CategoryEntity>> { | ||
const result = await this.categoryModel.create(dto); | ||
this.logger.info(`Новая категория создана: ${dto.name}`); | ||
return result; | ||
} | ||
|
||
public async findByCategoryId(categoryId: string): Promise<DocumentType<CategoryEntity> | null> { | ||
return this.categoryModel.findById(categoryId).exec(); | ||
} | ||
|
||
public async findByCategoryName( | ||
categoryName: string | ||
): Promise<DocumentType<CategoryEntity> | null> { | ||
return this.categoryModel.findOne({ name: categoryName }).exec(); | ||
} | ||
|
||
public async findByCategoryNameOrCreate( | ||
categoryName: string, | ||
dto: CreateCategoryDto | ||
): Promise<DocumentType<CategoryEntity>> { | ||
const existedCategory = await this.findByCategoryName(categoryName); | ||
|
||
if (existedCategory) { | ||
return existedCategory; | ||
} | ||
|
||
return this.create(dto); | ||
} | ||
|
||
public async find(): Promise<DocumentType<CategoryEntity>[]> { | ||
return this.categoryModel.find(); | ||
} | ||
} |
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,3 @@ | ||
export class CreateCategoryDto { | ||
public name: 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,5 @@ | ||
export * from './category.entity.js'; | ||
export * from './dto/create-category.dto.js'; | ||
export * from './category-service.interface.js'; | ||
export * from './default-category.service.js'; | ||
export * from './category.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { DocumentType } from '@typegoose/typegoose'; | ||
import { CreateCommentDto } from './dto/create-comment.dto.js'; | ||
import { CommentEntity } from './comment.entity.js'; | ||
|
||
export interface CommentService { | ||
create(dto: CreateCommentDto): Promise<DocumentType<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,18 @@ | ||
import { Container } from 'inversify'; | ||
import { types } from '@typegoose/typegoose'; | ||
|
||
import { Component } from '../../types/index.js'; | ||
import { CommentService } from './comment-service.interface.js'; | ||
import { DefaultCommentService } from './default-comment.service.js'; | ||
import { CommentEntity, CommentModel } from './comment.entity.js'; | ||
|
||
export function createCommentContainer() { | ||
const commentContainer = new Container(); | ||
|
||
commentContainer.bind<CommentService>(Component.CommentService).to(DefaultCommentService); | ||
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,23 @@ | ||
import { defaultClasses, getModelForClass, modelOptions, prop } from '@typegoose/typegoose'; | ||
import { Comment } from '../../types/index.js'; | ||
|
||
export interface CommentEntity extends defaultClasses.Base {} | ||
|
||
@modelOptions({ | ||
schemaOptions: { collection: 'comment', timestamps: true }, | ||
}) | ||
export class CommentEntity extends defaultClasses.TimeStamps implements Comment { | ||
@prop({ required: true, trim: true }) | ||
public text!: string; | ||
|
||
@prop({ required: true }) | ||
public publishedAt!: string; | ||
|
||
@prop({ required: true }) | ||
public rating!: number; | ||
|
||
@prop({ required: true }) | ||
public authorId!: string; | ||
} | ||
|
||
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,23 @@ | ||
import { inject, injectable } from 'inversify'; | ||
import { DocumentType, types } from '@typegoose/typegoose'; | ||
|
||
import { CommentService } from './comment-service.interface.js'; | ||
import { Component } from '../../types/index.js'; | ||
import { Logger } from '../../libs/logger/index.js'; | ||
import { CommentEntity } from './comment.entity.js'; | ||
import { CreateCommentDto } from './dto/create-comment.dto.js'; | ||
|
||
@injectable() | ||
export class DefaultCommentService implements CommentService { | ||
constructor( | ||
@inject(Component.Logger) private readonly logger: Logger, | ||
@inject(Component.CommentModel) private readonly CommentModel: types.ModelType<CommentEntity> | ||
) {} | ||
|
||
public async create(dto: CreateCommentDto): Promise<DocumentType<CommentEntity>> { | ||
const result = await this.CommentModel.create(dto); | ||
|
||
this.logger.info(`Новый комментарий создан: ${dto.text}`); | ||
return result; | ||
} | ||
} |
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,5 @@ | ||
export class CreateCommentDto { | ||
public text: string; | ||
public offerId: string; | ||
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,5 @@ | ||
export * from './comment.entity.js'; | ||
export * from './dto/create-comment.dto.js'; | ||
export * from './comment-service.interface.js'; | ||
export * from './default-comment.service.js'; | ||
export * 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,3 @@ | ||
export type Category = { | ||
name: 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,11 +1,15 @@ | ||
export const Component = { | ||
RestApplication: Symbol.for('RestApplication'), | ||
DatabaseClient: Symbol.for('DatabaseClient'), | ||
Logger: Symbol.for('Logger'), | ||
Config: Symbol.for('Config'), | ||
|
||
UserModel: Symbol.for('UserModel'), | ||
UserService: Symbol.for('UserService'), | ||
OfferModel: Symbol.for('OfferModel'), | ||
OfferService: Symbol.for('OfferService'), | ||
DatabaseClient: Symbol.for('DatabaseClient'), | ||
CommentModel: Symbol.for('CommentModel'), | ||
CommentService: Symbol.for('CommentService'), | ||
CategoryModel: Symbol.for('CategoryModel'), | ||
CategoryService: Symbol.for('CategoryService'), | ||
} as const; |
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,10 @@ | ||
export * from './category.type.js'; | ||
export * from './comment.type.js'; | ||
export * from './common.type.js'; | ||
export * from './offer.type.js'; | ||
export * from './user.type.js'; | ||
|
||
export * from './component.enum.js'; | ||
export * from './sort-type.enum.js'; | ||
|
||
export * from './mock-server-data.type.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum SortType { | ||
Down = -1, | ||
Up = 1, | ||
} |