Skip to content
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
671 changes: 0 additions & 671 deletions materials/mock.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
"preview": "vite preview",
"start-server": "node simple_api/server.js"
},
"dependencies": {
"@reduxjs/toolkit": "^2.8.2",
Expand Down
22 changes: 22 additions & 0 deletions simple_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# API
npm run start-server

## Ресторан

1. GET /api/restaurants/ - все рестораны;
2. GET /api/restaurant/:restaurantId - ресторан по айдишки (/api/restaurant/d32n32d8huasj );

## Блюда

1. GET /api/dishes?restaurantId=:restaurantId - получить блюда по айди ресторана (/api/dishes?restaurantId=d32n32d8huasj)
2. GET /api/dish/:dishId - блюдо по айдишки (/api/dish/djshfusdhfi29 )

## Отзывы

1. GET /api/reviews?restaurantId=:restaurantId - получить отзывы по айди ресторана (/api/reviews?restaurantId=d32n32d8huasj)
2. POST /api/review/:restaurantId - создать отзыв по айди ресторана (/api/review/d32n32d8huasj, а в бади сам отзыв без айдишки)
3. PATCH /api/review/:reviewId - изменить отзыв по айди ресторана (/api/review/d32n32d8huasj, а в бади сам отзыв без айдишки)

## Пользователи

1. GET /api/users/ - все пользователи;
97 changes: 97 additions & 0 deletions simple_api/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const router = require("express").Router();
const { nanoid } = require("nanoid");
const { restaurants, products, reviews, users } = require("./mock");
const { reply, getById, updateById } = require("./utils");

router.get("/restaurants", (req, res, next) => {
console.log("request");
reply(res, restaurants);
});

router.get("/restaurant/:restaurantId", (req, res, next) => {
const restaurantId = req.params?.restaurantId;
let restaurant;

if (restaurantId) {
restaurant = getById(restaurants)(restaurantId);
}

reply(res, restaurant);
});

router.get("/dishes", (req, res, next) => {
const { restaurantId, dishId } = req.query;
let result = products;

if (restaurantId) {
const restaurant = getById(restaurants)(restaurantId);
if (restaurant) {
result = restaurant.menu.map(getById(result));
}
}

if (!restaurantId && dishId) {
result = getById(result)(dishId);
}
reply(res, result);
});

router.get("/dish/:dishId", (req, res, next) => {
const dishId = req.params?.dishId;
let product;

if (dishId) {
product = getById(products)(dishId);
}
reply(res, product);
});

router.get("/reviews", (req, res, next) => {
const { restaurantId } = req.query;
let result = reviews;
if (restaurantId) {
const restaurant = getById(restaurants)(restaurantId);
if (restaurant) {
result = restaurant.reviews.map(getById(result));
}
}
reply(res, result);
});

router.post("/review/:restaurantId", (req, res, next) => {
const body = req.body;
const restaurantId = req.params?.restaurantId;
const restaurant = restaurantId && getById(restaurants)(restaurantId);
let newReview = {};

if (restaurant && body) {
const newReviewId = nanoid();

newReview = {
...body,
id: newReviewId,
};
restaurant.reviews.push(newReviewId);
reviews.push(newReview);
}

reply(res, newReview);
});

router.patch("/review/:reviewId", (req, res, next) => {
const body = req.body;
const reviewId = req.params?.reviewId;
let updatedReview;

if (reviewId) {
updatedReview = updateById(reviews)(reviewId, body);
}

reply(res, updatedReview);
});

router.get("/users", (req, res, next) => {
reply(res, users);
});

module.exports = router;
25 changes: 20 additions & 5 deletions materials/normalized-mock.js → simple_api/api/mock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export const normalizedRestaurants = [
const normalizedRestaurants = [
{
id: "a757a0e9-03c1-4a2a-b384-8ac21dbe2fb2",
name: "Dishoom",
name: "Вкусно и точка",
description: "Японская кухня",
img: "https://images.unsplash.com/photo-1504674900247-0877df9cc836?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2340&q=80",
menu: [
"d75f762a-eadd-49be-8918-ed0daa8dd024",
"c3cb8f92-a2ed-4716-92a1-b6ea813e9049",
Expand All @@ -15,6 +17,8 @@ export const normalizedRestaurants = [
{
id: "bb8afbec-2fec-491f-93e9-7f13950dd80b",
name: "Homeslice",
description: "Итальянская кухня",
img: "https://images.unsplash.com/photo-1455619452474-d2be8b1e70cd?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2370&q=80",
menu: [
"25402233-0095-49ea-9939-1e67ed89ffb9",
"90902233-0095-49ea-9939-1e67ed89ffb9",
Expand All @@ -28,6 +32,8 @@ export const normalizedRestaurants = [
{
id: "982bfbce-c5e0-41a0-9f99-d5c20ecee49d",
name: "Fabrique",
description: "Русская кухня",
img: "https://images.unsplash.com/photo-1562565652-a0d8f0c59eb4?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3264&q=80",
menu: [
"08c9ffa0-d003-4310-9e15-20978743296e",
"64a4967c-2080-4a99-9074-4655a4569a95",
Expand All @@ -38,6 +44,8 @@ export const normalizedRestaurants = [
{
id: "d9241927-09e1-44f3-8986-a76346869037",
name: "Flat Iron",
description: "Грузинская кухня",
img: "https://images.unsplash.com/photo-1559314809-0d155014e29e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2370&q=80",
menu: [
"6c02c2ce-b868-4191-b4a7-8686429f4bac",
"99bb6fbb-e53b-4b7e-b9c2-23b63b77385d",
Expand All @@ -49,7 +57,7 @@ export const normalizedRestaurants = [
},
];

export const normalizedDishes = [
const normalizedProducts = [
{
id: "d75f762a-eadd-49be-8918-ed0daa8dd024",
name: "Chicken tikka masala",
Expand Down Expand Up @@ -112,7 +120,7 @@ export const normalizedDishes = [
},
];

export const normalizedReviews = [
const normalizedReviews = [
{
id: "5909796d-5030-4e36-adec-68b8f9ec2d96",
userId: "a304959a-76c0-4b34-954a-b38dbf310360",
Expand Down Expand Up @@ -163,7 +171,7 @@ export const normalizedReviews = [
},
];

export const normalizedUsers = [
const normalizedUsers = [
{
id: "a304959a-76c0-4b34-954a-b38dbf310360",
name: "Antony",
Expand All @@ -189,3 +197,10 @@ export const normalizedUsers = [
name: "Sam",
},
];

module.exports = {
products: normalizedProducts,
restaurants: normalizedRestaurants,
reviews: normalizedReviews,
users: normalizedUsers,
};
16 changes: 16 additions & 0 deletions simple_api/api/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const reply = (res, body, timeout = 1000, status = 200) =>
setTimeout(() => {
res.status(status).json(body);
}, timeout);

const getById = (entities) => (id) =>
entities.find((entity) => entity.id === id);

const updateById = (entities) => (id, data) => {
const index = entities.findIndex((entity) => entity.id === id);
entities[index] = { ...entities[index], ...data };

return entities[index];
};

module.exports = { reply, getById, updateById };
Loading