Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe domain #29

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UserModule } from '@app/user/user.module';
import { AuthModule } from '@app/auth/auth.module';
import { ImageModule } from '@app/image/image.module';
import { IngredientModule } from '@app/ingredient/ingredient.module';
import { RecipeModule } from '@app/recipe/recipe.module';

@Module({
imports: [
Expand All @@ -23,6 +24,7 @@ import { IngredientModule } from '@app/ingredient/ingredient.module';
AuthModule,
ImageModule,
IngredientModule,
RecipeModule,
],
})
export class AppModule {}
2 changes: 2 additions & 0 deletions api/apps/mono_lambda/src/mono_lambda.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UserModule } from '@app/user/user.module';
import { AuthModule } from '@app/auth/auth.module';
import { ImageModule } from '@app/image/image.module';
import { IngredientModule } from '@app/ingredient/ingredient.module';
import { RecipeModule } from '@app/recipe/recipe.module';

@Module({
imports: [
Expand All @@ -23,6 +24,7 @@ import { IngredientModule } from '@app/ingredient/ingredient.module';
AuthModule,
ImageModule,
IngredientModule,
RecipeModule,
],
})
export class MonoLambdaModule {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { TestBed } from '@automock/jest';
import { IngredientController } from './ingredient.controller';
import { UserIngredientController } from './user-ingredient.controller';

describe('IngredientController', () => {
let controller: IngredientController;
let controller: UserIngredientController;

beforeEach(async () => {
const { unit, unitRef } = TestBed.create(IngredientController).compile();
const { unit, unitRef } = TestBed.create(
UserIngredientController,
).compile();

controller = unit;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,38 @@ import {
} from '@nestjs/common';
import { ApiQuery, ApiTags } from '@nestjs/swagger';
import {
CreateIngredientResponseDto,
FindAllIngredientResponseDto,
FindOneIngredientResponseDto,
UpdateIngredientResponseDto,
CreateUserIngredientResponseDto,
FindAllUserIngredientResponseDto,
FindOneUserIngredientResponseDto,
UpdateUserIngredientResponseDto,
} from '../dto/ingredient-response.dto';
import {
CreateIngredientDto,
UpdateIngredientDto,
CreateUserIngredientDto,
UpdateUserIngredientDto,
} from '../dto/modify-ingredient.dto';
import { IngredientService } from '../services/ingredient.service';
import { UserIngredientService } from '../services/user-ingredient.service';
import { FoodType } from '../types/food-type.enum';
import { StoreMethod } from '../types/store-method.enum';
import { FilterIngredientDto } from '../dto/filter-ingredient.dto';
import { FilterUserIngredientDto } from '../dto/filter-ingredient.dto';
import { queryBuilder } from '@app/common/utils/query-builder';
import { ReqUser } from '@app/common/decorators/req-user.decorator';
import { User } from '@app/user/entities/user.entity';

@ApiTags('Ingredient')
@Controller('ingredient')
export class IngredientController {
constructor(private readonly ingredientService: IngredientService) {}
export class UserIngredientController {
constructor(private readonly ingredientService: UserIngredientService) {}

/**
* ## Create Ingredient
*
* Create a new ingredient with given dto.
*/
@Auth()
@ApiPostCreated(CreateIngredientResponseDto)
@ApiPostCreated(CreateUserIngredientResponseDto)
@Post()
async create(
@Body() createIngredientDto: CreateIngredientDto,
@Body() createIngredientDto: CreateUserIngredientDto,
@ReqUser() user: User,
) {
createIngredientDto.user_id = user.id.toString();
Expand All @@ -79,7 +79,7 @@ export class IngredientController {
required: false,
})
@Auth()
@ApiGet(FindAllIngredientResponseDto)
@ApiGet(FindAllUserIngredientResponseDto)
@Get()
async findAll(
@Query(
Expand All @@ -96,7 +96,7 @@ export class IngredientController {
store_method: StoreMethod,
@ReqUser() user: User,
) {
const filterIngredientDto: FilterIngredientDto = queryBuilder({
const filterIngredientDto: FilterUserIngredientDto = queryBuilder({
food_type,
store_method,
user_id: user.id.toString(),
Expand All @@ -110,7 +110,7 @@ export class IngredientController {
* Find one ingredient with given id.
*/
@Auth()
@ApiGet(FindOneIngredientResponseDto)
@ApiGet(FindOneUserIngredientResponseDto)
@Get(':id')
async findOne(@Param('id') id: string) {
return this.ingredientService.findOne(id);
Expand All @@ -122,11 +122,11 @@ export class IngredientController {
* Update ingredient with given id and dto.
*/
@Auth()
@ApiPatch(UpdateIngredientResponseDto)
@ApiPatch(UpdateUserIngredientResponseDto)
@Patch(':id')
async update(
@Param('id') id: string,
@Body() updateIngredientDto: UpdateIngredientDto,
@Body() updateIngredientDto: UpdateUserIngredientDto,
) {
return this.ingredientService.update(id, updateIngredientDto);
}
Expand Down
2 changes: 1 addition & 1 deletion api/libs/ingredient/src/dto/filter-ingredient.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { FoodType } from '../types/food-type.enum';
import { StoreMethod } from '../types/store-method.enum';

export class FilterIngredientDto {
export class FilterUserIngredientDto {
@IsString()
@IsNotEmpty()
@IsOptional()
Expand Down
18 changes: 9 additions & 9 deletions api/libs/ingredient/src/dto/ingredient-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import {
CreatedResponse,
OkResponse,
} from '@app/common/dto/success-response.dto';
import { Ingredient } from '../entities/ingredient.entity';
import { UserIngredient } from '../entities/user-ingredient.entity';

export class CreateIngredientResponseDto extends CreatedResponse {
data: Ingredient;
export class CreateUserIngredientResponseDto extends CreatedResponse {
data: UserIngredient;
}

export class FindAllIngredientResponseDto extends OkResponse {
data: Ingredient[];
export class FindAllUserIngredientResponseDto extends OkResponse {
data: UserIngredient[];
}

export class FindOneIngredientResponseDto extends OkResponse {
data: Ingredient;
export class FindOneUserIngredientResponseDto extends OkResponse {
data: UserIngredient;
}

export class UpdateIngredientResponseDto extends OkResponse {
data: Ingredient;
export class UpdateUserIngredientResponseDto extends OkResponse {
data: UserIngredient;
}
11 changes: 9 additions & 2 deletions api/libs/ingredient/src/dto/modify-ingredient.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import {
import { FoodType } from '../types/food-type.enum';
import { StoreMethod } from '../types/store-method.enum';

export class CreateIngredientDto {
export class CreateUserIngredientDto {
@IsString()
@IsNotEmpty()
name: string;

@IsMongoId()
@IsNotEmpty()
@IsOptional()
ingredient_id?: string;

@IsMongoId()
@IsNotEmpty()
@IsOptional()
Expand All @@ -40,4 +45,6 @@ export class CreateIngredientDto {
days_before_expiration: number;
}

export class UpdateIngredientDto extends PartialType(CreateIngredientDto) {}
export class UpdateUserIngredientDto extends PartialType(
CreateUserIngredientDto,
) {}
62 changes: 14 additions & 48 deletions api/libs/ingredient/src/entities/ingredient.entity.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Schema as MongooseSchema } from 'mongoose';
import { FoodType } from '../types/food-type.enum';
import { StoreMethod } from '../types/store-method.enum';
import { schemaOptions } from '@app/common/utils/schema-option';
import { User } from '@app/user/entities/user.entity';
import { ConfigService } from '@nestjs/config';
import { Prop, Schema } from '@nestjs/mongoose';
import { HydratedDocument, Schema as MongooseSchema } from 'mongoose';

export type IngredientDocument = HydratedDocument<Ingredient>;

@Schema(schemaOptions)
export class Ingredient {
@Prop({ required: true, unique: true, auto: true })
id: MongooseSchema.Types.ObjectId;

@Prop({ required: true })
name: string;

@Prop({ required: true, type: MongooseSchema.Types.ObjectId })
user_id: MongooseSchema.Types.ObjectId;

@Prop({ required: true, type: String, enum: FoodType, default: FoodType.ETC })
food_type: FoodType;

@Prop({
required: true,
type: String,
enum: StoreMethod,
default: StoreMethod.REFRIGERATE,
unique: true,
auto: true,
type: MongooseSchema.Types.ObjectId,
})
store_method: StoreMethod;
id: MongooseSchema.Types.ObjectId;

@Prop({ required: true, type: Number, default: 0 })
count: number;
@Prop({ required: true, unique: true })
name: string;

@Prop({ required: true, type: Number, default: 0 })
days_before_expiration: number;
@Prop({ required: true })
description: string;

@Prop({ required: true })
thumbnail: string;

@Prop({ required: true, type: String })
@Prop({ required: true })
icon: string;

@Prop({ required: false })
Expand All @@ -45,24 +32,3 @@ export class Ingredient {
@Prop({ required: false })
updated_at: Date;
}

export const IngredientSchema = SchemaFactory.createForClass(Ingredient);

IngredientSchema.virtual('user', {
ref: User.name,
localField: 'user_id',
foreignField: 'id',
justOne: true,
});

export const IngredientSchemaFactory = (configService: ConfigService) => {
const schema = IngredientSchema;
schema.path('icon', {
require: true,
type: String,
default: `https://${configService.get(
'AWS_S3_IMAGE_MAIN_BUCKET',
)}.s3.ap-northeast-2.amazonaws.com/ingredient-default-icon.svg`,
});
return schema;
};
72 changes: 72 additions & 0 deletions api/libs/ingredient/src/entities/user-ingredient.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Schema as MongooseSchema } from 'mongoose';
import { FoodType } from '../types/food-type.enum';
import { StoreMethod } from '../types/store-method.enum';
import { schemaOptions } from '@app/common/utils/schema-option';
import { User } from '@app/user/entities/user.entity';
import { ConfigService } from '@nestjs/config';

export type UserIngredientDocument = HydratedDocument<UserIngredient>;

@Schema(schemaOptions)
export class UserIngredient {
@Prop({ required: true, unique: true, auto: true })
id: MongooseSchema.Types.ObjectId;

@Prop({ required: true })
name: string;

@Prop({ required: true, type: MongooseSchema.Types.ObjectId })
ingredient_id: MongooseSchema.Types.ObjectId;

@Prop({ required: true, type: MongooseSchema.Types.ObjectId })
user_id: MongooseSchema.Types.ObjectId;

@Prop({ required: true, type: String, enum: FoodType, default: FoodType.ETC })
food_type: FoodType;

@Prop({
required: true,
type: String,
enum: StoreMethod,
default: StoreMethod.REFRIGERATE,
})
store_method: StoreMethod;

@Prop({ required: true, type: Number, default: 0 })
count: number;

@Prop({ required: true, type: Number, default: 0 })
days_before_expiration: number;

@Prop({ required: true, type: String })
icon: string;

@Prop({ required: false })
created_at: Date;

@Prop({ required: false })
updated_at: Date;
}

export const UserIngredientSchema =
SchemaFactory.createForClass(UserIngredient);

UserIngredientSchema.virtual('user', {
ref: User.name,
localField: 'user_id',
foreignField: 'id',
justOne: true,
});

export const UserIngredientSchemaFactory = (configService: ConfigService) => {
const schema = UserIngredientSchema;
schema.path('icon', {
require: true,
type: String,
default: `https://${configService.get(
'AWS_S3_IMAGE_MAIN_BUCKET',
)}.s3.ap-northeast-2.amazonaws.com/ingredient-default-icon.svg`,
});
return schema;
};
22 changes: 11 additions & 11 deletions api/libs/ingredient/src/ingredient.module.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { IngredientController } from './controllers/ingredient.controller';
import { UserIngredientController } from './controllers/user-ingredient.controller';
import {
Ingredient,
IngredientSchemaFactory,
} from './entities/ingredient.entity';
import { IngredientRepository } from './repositories/ingredient.repository';
import { IngredientService } from './services/ingredient.service';
UserIngredient,
UserIngredientSchemaFactory,
} from './entities/user-ingredient.entity';
import { UserIngredientRepository } from './repositories/user-ingredient.repository';
import { UserIngredientService } from './services/user-ingredient.service';
import { ConfigService } from '@nestjs/config';

@Module({
imports: [
MongooseModule.forFeatureAsync([
{
name: Ingredient.name,
useFactory: IngredientSchemaFactory,
name: UserIngredient.name,
useFactory: UserIngredientSchemaFactory,
inject: [ConfigService],
},
]),
],
controllers: [IngredientController],
providers: [IngredientService, IngredientRepository],
exports: [IngredientService, IngredientRepository],
controllers: [UserIngredientController],
providers: [UserIngredientService, UserIngredientRepository],
exports: [UserIngredientService, UserIngredientRepository],
})
export class IngredientModule {}
Loading