Skip to content

Commit

Permalink
add history endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fauzan-radji committed Dec 17, 2023
1 parent 1f8122f commit 92fa8fb
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 23 deletions.
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ This is a dummy backend for [Ingridentify android app](https://github.com/Ingrid

6. The server should be running on `http://localhost:3000`

## User

| Name | Email | Password |
| ------ | ---------------- | -------- |
| Fauzan | fauzan@email.com | 12345678 |
| Hanif | hanif@email.com | 12345678 |

## Endpoints

The default port is `3000`, but can be changed by setting the `PORT` environment variable.
Expand All @@ -61,7 +68,6 @@ POST /auth/login
```json
{
"name": "Fauzan",
"username": "fauzan",
"email": "fauzan@email.com",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
}
Expand All @@ -74,3 +80,54 @@ POST /auth/login
"message": "Invalid email or password"
}
```

### History

#### Get all history

```http
GET /history
```

##### Request

###### Headers

| Name | Parameter | Type | Required | Example |
| ------------- | --------- | ------ | ------------------ | ------------------- |
| Authorization | token | string | :white_check_mark: | bearer eyJhbGciOiJI |

###### Params

| Parameter | Type | Required | Default |
| --------- | ------ | -------- | ------- |
| page | number | :x: | 1 |
| limit | number | :x: | 10 |

##### Response

```json
[
{
"id": 1,
"name": "Nasi Goreng",
"cuisine": "Indonesia",
"recipes": "https://www.masakapahariini.com/resep/resep-nasi-goreng/",
"imageUrl": "https://kurio-img.kurioapps.com/20/10/10/a7e9eaa0-1c22-42b0-a11f-0a5ad1d30126.jpeg"
},
{
"id": 2,
"name": "Nasi Kuning",
"cuisine": "Indonesia",
"recipes": "https://www.masakapahariini.com/resep/resep-nasi-kuning/",
"imageUrl": "https://www.sasa.co.id/medias/page_medias/nasi_kuning_rice_cooker.jpg"
},
{
"id": 3,
"name": "Nasi Uduk",
"cuisine": "Indonesia",
"recipes": "https://www.masakapahariini.com/resep/resep-nasi-uduk/",
"imageUrl": "https://www.blibli.com/friends-backend/wp-content/uploads/2023/04/B300028-Cover-resep-nasi-uduk.jpg"
}
]
```
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import dotenv from "dotenv";
import express from "express";
import User from "./model/User.js";
import { User, History } from "./model/index.js";
import LoginResponse from "./response/LoginResponse.js";
import ErrorResponse from "./response/ErrorResponse.js";
import RecipeResponse from "./response/RecipeResponse.js";

dotenv.config();
const app = express();
Expand Down Expand Up @@ -43,6 +44,20 @@ app.post("/auth/login", (req, res) => {
return res.status(200).json(LoginResponse.create(user));
});

app.get("/histories", (req, res) => {
const token = req.headers.authorization.split(" ")[1];
const user = User.findBy("token", token);
if (!user) {
return res.status(401).json(ErrorResponse.create("Unauthorized"));
}

const histories = History.where("userId", user.id);

return res
.status(200)
.json(RecipeResponse.create(histories.map((history) => history.recipe)));
});

app.listen(port, () => {
console.log(`Example app listening on http://127.0.0.1:${port}`);
});
6 changes: 6 additions & 0 deletions model/History.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Model, Field } from "json-modelizer";

export default class History extends Model {
static _table = "history";
static schema = {};
}
11 changes: 11 additions & 0 deletions model/Recipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Model, Field } from "json-modelizer";

export default class Recipe extends Model {
static _table = "recipes";
static schema = {
name: Field.String,
cuisine: Field.String,
recipes: Field.String,
imageUrl: Field.String,
};
}
1 change: 0 additions & 1 deletion model/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class User extends Model {
static _table = "users";
static schema = {
name: Field.String,
username: Field.String,
email: Field.String,
password: Field.String,
token: Field.String,
Expand Down
11 changes: 11 additions & 0 deletions model/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import User from "./User.js";
import Recipe from "./Recipe.js";
import History from "./History.js";

Recipe.hasMany(History);
History.belongsTo(User).as("user");

User.hasMany(History);
History.belongsTo(Recipe).as("recipe");

export { User, Recipe, History };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"seed": "node seed.js",
"seed": "node seeder/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down
11 changes: 11 additions & 0 deletions response/RecipeResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default class RecipeResponse {
static create(recipes) {
return recipes.map((recipe) => ({
id: recipe.id,
name: recipe.name,
cuisine: recipe.cuisine,
recipes: recipe.recipes,
imageUrl: recipe.imageUrl,
}));
}
}
19 changes: 0 additions & 19 deletions seed.js

This file was deleted.

28 changes: 28 additions & 0 deletions seeder/HistorySeeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { History } from "../model/index.js";

History.clear();

History.create({
recipeId: 1,
userId: 1,
});

History.create({
recipeId: 2,
userId: 1,
});

History.create({
recipeId: 3,
userId: 1,
});

History.create({
recipeId: 2,
userId: 2,
});

History.create({
recipeId: 3,
userId: 2,
});
43 changes: 43 additions & 0 deletions seeder/RecipeSeeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Recipe } from "../model/index.js";

Recipe.clear();

Recipe.create({
name: "Nasi Goreng",
cuisine: "Indonesia",
recipes: "https://www.masakapahariini.com/resep/resep-nasi-goreng/",
imageUrl:
"https://kurio-img.kurioapps.com/20/10/10/a7e9eaa0-1c22-42b0-a11f-0a5ad1d30126.jpeg",
});

Recipe.create({
name: "Nasi Kuning",
cuisine: "Indonesia",
recipes: "https://www.masakapahariini.com/resep/resep-nasi-kuning/",
imageUrl:
"https://www.sasa.co.id/medias/page_medias/nasi_kuning_rice_cooker.jpg",
});

Recipe.create({
name: "Nasi Uduk",
cuisine: "Indonesia",
recipes: "https://www.masakapahariini.com/resep/resep-nasi-uduk/",
imageUrl:
"https://www.blibli.com/friends-backend/wp-content/uploads/2023/04/B300028-Cover-resep-nasi-uduk.jpg",
});

Recipe.create({
name: "Nasi Liwet",
cuisine: "Indonesia",
recipes: "https://www.masakapahariini.com/resep/resep-nasi-liwet/",
imageUrl:
"https://assets.tmecosys.com/image/upload/t_web767x639/img/recipe/ras/Assets/a5786edc81a00587a470c25b13b10924/Derivates/b346197f18531cd806d52165903f8da023e3bf84.jpg",
});

Recipe.create({
name: "Nasi Bakar",
cuisine: "Indonesia",
recipes: "https://www.masakapahariini.com/resep/resep-nasi-bakar/",
imageUrl:
"https://asset.kompas.com/crops/5GT-nmWVwnzqL6JiiAiyevIqSXU=/0x1774:3376x4025/750x500/data/photo/2022/12/06/638ee2b3e1b20.jpg",
});
30 changes: 30 additions & 0 deletions seeder/UserSeeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { User } from "../model/index.js";

User.clear();

User.create({
name: "Fauzan",
email: "fauzan@email.com",
password: "12345678",
token: generateToken(64),
});

User.create({
name: "Hanif",
email: "hanif@email.com",
password: "12345678",
token: generateToken(64),
});

function generateToken(length) {
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;

for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}

return result;
}
11 changes: 11 additions & 0 deletions seeder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
console.log("Seeding database...");
console.log("Seeding users...");
import "./UserSeeder.js";

console.log("Seeding recipes...");
import "./RecipeSeeder.js";

console.log("Seeding histories...");
import "./HistorySeeder.js";

console.log("Done!");

0 comments on commit 92fa8fb

Please sign in to comment.