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

Feature/recipes #35

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions controllers/recipes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Request, Response, NextFunction } from "express";
import { recipeService } from './../service/recipeService';


export class RecipeController{

private service = recipeService;
createRecipe = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const newRecipe = await this.service.addRecipe(req.body);
res.status(200).json(newRecipe)
} catch (error) {
next(error);
}
}

getRecipeById = async(req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const recipe = await this.service.getRecipe(req.params.id);
res.status(200).json(recipe)
} catch (error) {
next(error)
}
}

getAllRecipes = async (
req: Request,
res: Response,
next: NextFunction
): Promise<void> => {
try {
const users = await this.service.getAllRecipes();
res.status(200).json(users);
} catch (error) {
next(error);
}
};



}

19 changes: 19 additions & 0 deletions dtos/recipes.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose from "mongoose";
import { Dto } from "./dto";

type Ingredients = {
quantity: number;
denomination: string
}


export interface RecipesDto extends Dto{
_id: mongoose.Schema.Types.ObjectId;
title: string;
times: number;
difficulty: string
ingredients: Ingredients[]
instruction: string[]
datePublication: Date
}

3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { roomsRouter } from "./routes/rooms";
import { conversationRouter } from "./routes/conversations";
import { messageRouter } from "./routes/messages";
import { statsRouter } from "./routes/stats";
import { recipeRouter } from "./routes/recipes";
import { reportRouter } from "./routes/report";
import { searchRouter } from './routes/search';


const app = express();

// loads .env file contents into process.env
Expand Down Expand Up @@ -42,6 +42,7 @@ app.use("/api/auth", authRouter);
app.use("/api/conversations", conversationRouter);
app.use("/api/messages", messageRouter);
app.use("/api/stats", statsRouter);
app.use("/api/recipes", recipeRouter);
app.use("/api/report", reportRouter);
app.use('/api/search', searchRouter)

Expand Down
19 changes: 19 additions & 0 deletions models/RecipesModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose, { Schema } from "mongoose";
import { RecipesDto } from "../dtos/recipes.dto";

const RecipesSchema = new mongoose.Schema<RecipesDto>(
{
title: { type: String, required: true },
times: { type: Number, required: true },
difficulty: { type: String, required: true },
ingredients: {
quantity: { type: Number },
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu as juste oublié de mettre en required la qauntité des ingrédients. si cela est facultatif ignore mon commentaire

denomination: { type: String, required: true },
},
instruction: { type: [String], required: true },
},
{ timestamps: true }
);

const RecipesModel = mongoose.model<RecipesDto>("Recipes", RecipesSchema);
export { RecipesModel };
11 changes: 11 additions & 0 deletions routes/recipes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from "express";import { RecipeController } from "../controllers/recipes";
;

const recipeController = new RecipeController();
const recipeRouter = express.Router();

recipeRouter.post('/createrecipe', recipeController.createRecipe)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pas besoin de la route 'createrecipe', juste '/' devrait suffire étant donné que le terme 'post' parle de lui -même

recipeRouter.get('/:id', recipeController.getRecipeById)
recipeRouter.get('/', recipeController.getAllRecipes)

export {recipeRouter}
27 changes: 27 additions & 0 deletions service/recipeService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { RecipesModel } from "../models/RecipesModel";
import { RecipesDto } from "../dtos/recipes.dto";

export class RecipeService {
addRecipe = async (recipe: RecipesDto): Promise<RecipesDto> => {
const newRecipe = new RecipesModel(recipe);
await newRecipe.save();
return newRecipe;
};

getAllRecipes = async (): Promise<RecipesDto[]> => {
const recipes = RecipesModel.find();
return recipes;
};

getRecipe = async (id: string): Promise<RecipesDto> => {
const recipe = await RecipesModel.findById(id).orFail();
return recipe;
};

deleteRecipeById = async (recipeId: string): Promise<void> => {
StudioAzur marked this conversation as resolved.
Show resolved Hide resolved
await RecipesModel.deleteOne({
recipeId: recipeId,
});
};
}
export const recipeService = Object.freeze(new RecipeService());