Skip to content

Features/1 http feature #2

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

Merged
merged 3 commits into from
Sep 26, 2024
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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

## How to run backend (Express App)

1. Navigate to **backend** folder by `cd backend/`
2. Install dependencies `yarn install`
3. Run the express server `yarn start`
4. Open the URL `http://localhost:3000/places` in your browser

## Cloning Guide

1. Clone only the remote primary HEAD (default: origin/master)
Expand Down
42 changes: 42 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files.

# Compiled output
/dist
/tmp
/out-tsc
/bazel-out

# Node
/node_modules
npm-debug.log
yarn-error.log

# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# Visual Studio Code
# .vscode/*
# !.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
# !.vscode/extensions.json
.history/*

# Miscellaneous
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings

# System files
.DS_Store
Thumbs.db
94 changes: 94 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import fs from "node:fs/promises";

import bodyParser from "body-parser";
import express from "express";

const app = express();

app.use(express.static("images"));
app.use(bodyParser.json());

// CORS

app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*"); // allow all domains
res.setHeader("Access-Control-Allow-Methods", "GET, PUT, DELETE");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");

next();
});

app.get("/places", async (req, res) => {
await new Promise((resolve) => setTimeout(resolve, 3000));

const fileContent = await fs.readFile("./data/places.json");

const placesData = JSON.parse(fileContent);

res.status(200).json({ places: placesData });
});

app.get("/user-places", async (req, res) => {
const fileContent = await fs.readFile("./data/user-places.json");

const places = JSON.parse(fileContent);

res.status(200).json({ places });
});

app.put("/user-places", async (req, res) => {
const placeId = req.body.placeId;

const fileContent = await fs.readFile("./data/places.json");
const placesData = JSON.parse(fileContent);

const place = placesData.find((place) => place.id === placeId);

const userPlacesFileContent = await fs.readFile("./data/user-places.json");
const userPlacesData = JSON.parse(userPlacesFileContent);

let updatedUserPlaces = userPlacesData;

if (!userPlacesData.some((p) => p.id === place.id)) {
updatedUserPlaces = [...userPlacesData, place];
}

await fs.writeFile(
"./data/user-places.json",
JSON.stringify(updatedUserPlaces)
);

res.status(200).json({ userPlaces: updatedUserPlaces });
});

app.delete("/user-places/:id", async (req, res) => {
const placeId = req.params.id;

const userPlacesFileContent = await fs.readFile("./data/user-places.json");
const userPlacesData = JSON.parse(userPlacesFileContent);

const placeIndex = userPlacesData.findIndex((place) => place.id === placeId);

let updatedUserPlaces = userPlacesData;

if (placeIndex >= 0) {
updatedUserPlaces.splice(placeIndex, 1);
}

await fs.writeFile(
"./data/user-places.json",
JSON.stringify(updatedUserPlaces)
);

res.status(200).json({ userPlaces: updatedUserPlaces });
});

// 404
app.use((req, res, next) => {
if (req.method === "OPTIONS") {
return next();
}
res.status(404).json({ message: "404 - Not Found" });
});

app.listen(3000);
182 changes: 182 additions & 0 deletions backend/data/places.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
[
{
"id": "p1",
"title": "Forest Waterfall",
"image": {
"src": "forest-waterfall.jpg",
"alt": "A tranquil forest with a cascading waterfall amidst greenery."
},
"lat": 44.5588,
"lon": -80.344
},
{
"id": "p2",
"title": "Sahara Desert Dunes",
"image": {
"src": "desert-dunes.jpg",
"alt": "Golden dunes stretching to the horizon in the Sahara Desert."
},
"lat": 25.0,
"lon": 0.0
},
{
"id": "p3",
"title": "Himalayan Peaks",
"image": {
"src": "majestic-mountains.jpg",
"alt": "The sun setting behind snow-capped peaks of majestic mountains."
},
"lat": 27.9881,
"lon": 86.925
},
{
"id": "p4",
"title": "Caribbean Beach",
"image": {
"src": "caribbean-beach.jpg",
"alt": "Pristine white sand and turquoise waters of a Caribbean beach."
},
"lat": 18.2208,
"lon": -66.5901
},
{
"id": "p5",
"title": "Ancient Grecian Ruins",
"image": {
"src": "ruins.jpg",
"alt": "Historic ruins standing tall against the backdrop of the Grecian sky."
},
"lat": 37.9715,
"lon": 23.7257
},
{
"id": "p6",
"title": "Amazon Rainforest Canopy",
"image": {
"src": "rainforest.jpg",
"alt": "Lush canopy of a rainforest, teeming with life."
},
"lat": -3.4653,
"lon": -62.2159
},
{
"id": "p7",
"title": "Northern Lights",
"image": {
"src": "northern-lights.jpg",
"alt": "Dazzling display of the Northern Lights in a starry sky."
},
"lat": 64.9631,
"lon": -19.0208
},
{
"id": "p8",
"title": "Japanese Temple",
"image": {
"src": "japanese-temple.jpg",
"alt": "Ancient Japanese temple surrounded by autumn foliage."
},
"lat": 34.9949,
"lon": 135.785
},
{
"id": "p9",
"title": "Great Barrier Reef",
"image": {
"src": "great-barrier-reef.jpg",
"alt": "Vibrant coral formations of the Great Barrier Reef underwater."
},
"lat": -18.2871,
"lon": 147.6992
},
{
"id": "p10",
"title": "Parisian Streets",
"image": {
"src": "parisian-streets.jpg",
"alt": "Charming streets of Paris with historic buildings and cafes."
},
"lat": 48.8566,
"lon": 2.3522
},
{
"id": "p11",
"title": "Grand Canyon",
"image": {
"src": "grand-canyon.jpg",
"alt": "Expansive view of the deep gorges and ridges of the Grand Canyon."
},
"lat": 36.1069,
"lon": -112.1129
},
{
"id": "p12",
"title": "Venetian Canals",
"image": {
"src": "venetian-canals.jpg",
"alt": "Glistening waters of the Venetian canals as gondolas glide by at sunset."
},
"lat": 45.4408,
"lon": 12.3155
},
{
"id": "p13",
"title": "Taj Mahal",
"image": {
"src": "taj-mahal.jpg",
"alt": "The iconic Taj Mahal reflecting in its surrounding waters during sunrise."
},
"lat": 27.1751,
"lon": 78.0421
},
{
"id": "p14",
"title": "Kerala Backwaters",
"image": {
"src": "kerala-backwaters.jpg",
"alt": "Tranquil waters and lush greenery of the Kerala backwaters."
},
"lat": 9.4981,
"lon": 76.3388
},
{
"id": "p15",
"title": "African Savanna",
"image": {
"src": "african-savanna.jpg",
"alt": "Wild animals roaming freely in the vast landscapes of the African savanna."
},
"lat": -2.153,
"lon": 34.6857
},
{
"id": "p16",
"title": "Victoria Falls",
"image": {
"src": "victoria-falls.jpg",
"alt": "The powerful cascade of Victoria Falls, a natural wonder between Zambia and Zimbabwe."
},
"lat": -17.9243,
"lon": 25.8572
},
{
"id": "p17",
"title": "Machu Picchu",
"image": {
"src": "machu-picchu.jpg",
"alt": "The historic Incan citadel of Machu Picchu illuminated by the morning sun."
},
"lat": -13.1631,
"lon": -72.545
},
{
"id": "p18",
"title": "Amazon River",
"image": {
"src": "amazon-river.jpg",
"alt": "Navigating the waters of the Amazon River, surrounded by dense rainforest."
},
"lat": -3.4653,
"lon": -58.38
}
]
1 change: 1 addition & 0 deletions backend/data/user-places.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Binary file added backend/images/african-savanna.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/amazon-river.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/caribbean-beach.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/desert-dunes.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/forest-waterfall.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/grand-canyon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/great-barrier-reef.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/japanese-temple.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/kerala-backwaters.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/machu-picchu.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/majestic-mountains.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/northern-lights.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/parisian-streets.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/rainforest.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/ruins.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/taj-mahal.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/venetian-canals.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/victoria-falls.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node app.js"
},
"keywords": [],
"author": "Anand Raja",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2"
},
"engines": {
"node": "20"
}
}
Loading