Skip to content

Commit 2402563

Browse files
authored
Merge pull request #2 from actionanand/features/1-http-feature
Features/1 http feature
2 parents 12ddb26 + 2f3dd5d commit 2402563

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1091
-14
lines changed

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 18.2.3.
44

5+
## How to run backend (Express App)
6+
7+
1. Navigate to **backend** folder by `cd backend/`
8+
2. Install dependencies `yarn install`
9+
3. Run the express server `yarn start`
10+
4. Open the URL `http://localhost:3000/places` in your browser
11+
512
## Cloning Guide
613

714
1. Clone only the remote primary HEAD (default: origin/master)

Diff for: backend/.gitignore

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files.
2+
3+
# Compiled output
4+
/dist
5+
/tmp
6+
/out-tsc
7+
/bazel-out
8+
9+
# Node
10+
/node_modules
11+
npm-debug.log
12+
yarn-error.log
13+
14+
# IDEs and editors
15+
.idea/
16+
.project
17+
.classpath
18+
.c9/
19+
*.launch
20+
.settings/
21+
*.sublime-workspace
22+
23+
# Visual Studio Code
24+
# .vscode/*
25+
# !.vscode/settings.json
26+
!.vscode/tasks.json
27+
!.vscode/launch.json
28+
# !.vscode/extensions.json
29+
.history/*
30+
31+
# Miscellaneous
32+
/.angular/cache
33+
.sass-cache/
34+
/connect.lock
35+
/coverage
36+
/libpeerconnection.log
37+
testem.log
38+
/typings
39+
40+
# System files
41+
.DS_Store
42+
Thumbs.db

Diff for: backend/app.js

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import fs from "node:fs/promises";
2+
3+
import bodyParser from "body-parser";
4+
import express from "express";
5+
6+
const app = express();
7+
8+
app.use(express.static("images"));
9+
app.use(bodyParser.json());
10+
11+
// CORS
12+
13+
app.use((req, res, next) => {
14+
res.setHeader("Access-Control-Allow-Origin", "*"); // allow all domains
15+
res.setHeader("Access-Control-Allow-Methods", "GET, PUT, DELETE");
16+
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
17+
18+
next();
19+
});
20+
21+
app.get("/places", async (req, res) => {
22+
await new Promise((resolve) => setTimeout(resolve, 3000));
23+
24+
const fileContent = await fs.readFile("./data/places.json");
25+
26+
const placesData = JSON.parse(fileContent);
27+
28+
res.status(200).json({ places: placesData });
29+
});
30+
31+
app.get("/user-places", async (req, res) => {
32+
const fileContent = await fs.readFile("./data/user-places.json");
33+
34+
const places = JSON.parse(fileContent);
35+
36+
res.status(200).json({ places });
37+
});
38+
39+
app.put("/user-places", async (req, res) => {
40+
const placeId = req.body.placeId;
41+
42+
const fileContent = await fs.readFile("./data/places.json");
43+
const placesData = JSON.parse(fileContent);
44+
45+
const place = placesData.find((place) => place.id === placeId);
46+
47+
const userPlacesFileContent = await fs.readFile("./data/user-places.json");
48+
const userPlacesData = JSON.parse(userPlacesFileContent);
49+
50+
let updatedUserPlaces = userPlacesData;
51+
52+
if (!userPlacesData.some((p) => p.id === place.id)) {
53+
updatedUserPlaces = [...userPlacesData, place];
54+
}
55+
56+
await fs.writeFile(
57+
"./data/user-places.json",
58+
JSON.stringify(updatedUserPlaces)
59+
);
60+
61+
res.status(200).json({ userPlaces: updatedUserPlaces });
62+
});
63+
64+
app.delete("/user-places/:id", async (req, res) => {
65+
const placeId = req.params.id;
66+
67+
const userPlacesFileContent = await fs.readFile("./data/user-places.json");
68+
const userPlacesData = JSON.parse(userPlacesFileContent);
69+
70+
const placeIndex = userPlacesData.findIndex((place) => place.id === placeId);
71+
72+
let updatedUserPlaces = userPlacesData;
73+
74+
if (placeIndex >= 0) {
75+
updatedUserPlaces.splice(placeIndex, 1);
76+
}
77+
78+
await fs.writeFile(
79+
"./data/user-places.json",
80+
JSON.stringify(updatedUserPlaces)
81+
);
82+
83+
res.status(200).json({ userPlaces: updatedUserPlaces });
84+
});
85+
86+
// 404
87+
app.use((req, res, next) => {
88+
if (req.method === "OPTIONS") {
89+
return next();
90+
}
91+
res.status(404).json({ message: "404 - Not Found" });
92+
});
93+
94+
app.listen(3000);

Diff for: backend/data/places.json

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
[
2+
{
3+
"id": "p1",
4+
"title": "Forest Waterfall",
5+
"image": {
6+
"src": "forest-waterfall.jpg",
7+
"alt": "A tranquil forest with a cascading waterfall amidst greenery."
8+
},
9+
"lat": 44.5588,
10+
"lon": -80.344
11+
},
12+
{
13+
"id": "p2",
14+
"title": "Sahara Desert Dunes",
15+
"image": {
16+
"src": "desert-dunes.jpg",
17+
"alt": "Golden dunes stretching to the horizon in the Sahara Desert."
18+
},
19+
"lat": 25.0,
20+
"lon": 0.0
21+
},
22+
{
23+
"id": "p3",
24+
"title": "Himalayan Peaks",
25+
"image": {
26+
"src": "majestic-mountains.jpg",
27+
"alt": "The sun setting behind snow-capped peaks of majestic mountains."
28+
},
29+
"lat": 27.9881,
30+
"lon": 86.925
31+
},
32+
{
33+
"id": "p4",
34+
"title": "Caribbean Beach",
35+
"image": {
36+
"src": "caribbean-beach.jpg",
37+
"alt": "Pristine white sand and turquoise waters of a Caribbean beach."
38+
},
39+
"lat": 18.2208,
40+
"lon": -66.5901
41+
},
42+
{
43+
"id": "p5",
44+
"title": "Ancient Grecian Ruins",
45+
"image": {
46+
"src": "ruins.jpg",
47+
"alt": "Historic ruins standing tall against the backdrop of the Grecian sky."
48+
},
49+
"lat": 37.9715,
50+
"lon": 23.7257
51+
},
52+
{
53+
"id": "p6",
54+
"title": "Amazon Rainforest Canopy",
55+
"image": {
56+
"src": "rainforest.jpg",
57+
"alt": "Lush canopy of a rainforest, teeming with life."
58+
},
59+
"lat": -3.4653,
60+
"lon": -62.2159
61+
},
62+
{
63+
"id": "p7",
64+
"title": "Northern Lights",
65+
"image": {
66+
"src": "northern-lights.jpg",
67+
"alt": "Dazzling display of the Northern Lights in a starry sky."
68+
},
69+
"lat": 64.9631,
70+
"lon": -19.0208
71+
},
72+
{
73+
"id": "p8",
74+
"title": "Japanese Temple",
75+
"image": {
76+
"src": "japanese-temple.jpg",
77+
"alt": "Ancient Japanese temple surrounded by autumn foliage."
78+
},
79+
"lat": 34.9949,
80+
"lon": 135.785
81+
},
82+
{
83+
"id": "p9",
84+
"title": "Great Barrier Reef",
85+
"image": {
86+
"src": "great-barrier-reef.jpg",
87+
"alt": "Vibrant coral formations of the Great Barrier Reef underwater."
88+
},
89+
"lat": -18.2871,
90+
"lon": 147.6992
91+
},
92+
{
93+
"id": "p10",
94+
"title": "Parisian Streets",
95+
"image": {
96+
"src": "parisian-streets.jpg",
97+
"alt": "Charming streets of Paris with historic buildings and cafes."
98+
},
99+
"lat": 48.8566,
100+
"lon": 2.3522
101+
},
102+
{
103+
"id": "p11",
104+
"title": "Grand Canyon",
105+
"image": {
106+
"src": "grand-canyon.jpg",
107+
"alt": "Expansive view of the deep gorges and ridges of the Grand Canyon."
108+
},
109+
"lat": 36.1069,
110+
"lon": -112.1129
111+
},
112+
{
113+
"id": "p12",
114+
"title": "Venetian Canals",
115+
"image": {
116+
"src": "venetian-canals.jpg",
117+
"alt": "Glistening waters of the Venetian canals as gondolas glide by at sunset."
118+
},
119+
"lat": 45.4408,
120+
"lon": 12.3155
121+
},
122+
{
123+
"id": "p13",
124+
"title": "Taj Mahal",
125+
"image": {
126+
"src": "taj-mahal.jpg",
127+
"alt": "The iconic Taj Mahal reflecting in its surrounding waters during sunrise."
128+
},
129+
"lat": 27.1751,
130+
"lon": 78.0421
131+
},
132+
{
133+
"id": "p14",
134+
"title": "Kerala Backwaters",
135+
"image": {
136+
"src": "kerala-backwaters.jpg",
137+
"alt": "Tranquil waters and lush greenery of the Kerala backwaters."
138+
},
139+
"lat": 9.4981,
140+
"lon": 76.3388
141+
},
142+
{
143+
"id": "p15",
144+
"title": "African Savanna",
145+
"image": {
146+
"src": "african-savanna.jpg",
147+
"alt": "Wild animals roaming freely in the vast landscapes of the African savanna."
148+
},
149+
"lat": -2.153,
150+
"lon": 34.6857
151+
},
152+
{
153+
"id": "p16",
154+
"title": "Victoria Falls",
155+
"image": {
156+
"src": "victoria-falls.jpg",
157+
"alt": "The powerful cascade of Victoria Falls, a natural wonder between Zambia and Zimbabwe."
158+
},
159+
"lat": -17.9243,
160+
"lon": 25.8572
161+
},
162+
{
163+
"id": "p17",
164+
"title": "Machu Picchu",
165+
"image": {
166+
"src": "machu-picchu.jpg",
167+
"alt": "The historic Incan citadel of Machu Picchu illuminated by the morning sun."
168+
},
169+
"lat": -13.1631,
170+
"lon": -72.545
171+
},
172+
{
173+
"id": "p18",
174+
"title": "Amazon River",
175+
"image": {
176+
"src": "amazon-river.jpg",
177+
"alt": "Navigating the waters of the Amazon River, surrounded by dense rainforest."
178+
},
179+
"lat": -3.4653,
180+
"lon": -58.38
181+
}
182+
]

Diff for: backend/data/user-places.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

Diff for: backend/images/african-savanna.jpg

220 KB
Loading

Diff for: backend/images/amazon-river.jpg

287 KB
Loading

Diff for: backend/images/caribbean-beach.jpg

258 KB
Loading

Diff for: backend/images/desert-dunes.jpg

158 KB
Loading

Diff for: backend/images/forest-waterfall.jpg

253 KB
Loading

Diff for: backend/images/grand-canyon.jpg

212 KB
Loading

Diff for: backend/images/great-barrier-reef.jpg

254 KB
Loading

Diff for: backend/images/japanese-temple.jpg

173 KB
Loading

Diff for: backend/images/kerala-backwaters.jpg

244 KB
Loading

Diff for: backend/images/machu-picchu.jpg

222 KB
Loading

Diff for: backend/images/majestic-mountains.jpg

232 KB
Loading

Diff for: backend/images/northern-lights.jpg

208 KB
Loading

Diff for: backend/images/parisian-streets.jpg

287 KB
Loading

Diff for: backend/images/rainforest.jpg

290 KB
Loading

Diff for: backend/images/ruins.jpg

269 KB
Loading

Diff for: backend/images/taj-mahal.jpg

221 KB
Loading

Diff for: backend/images/venetian-canals.jpg

262 KB
Loading

Diff for: backend/images/victoria-falls.jpg

231 KB
Loading

Diff for: backend/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "backend",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"start": "node app.js"
9+
},
10+
"keywords": [],
11+
"author": "Anand Raja",
12+
"license": "ISC",
13+
"dependencies": {
14+
"body-parser": "^1.20.2",
15+
"express": "^4.18.2"
16+
},
17+
"engines": {
18+
"node": "20"
19+
}
20+
}

0 commit comments

Comments
 (0)