CSCI 2006 - Spring 2024 - Server-Side ProgrammingLab #6 - jsNodeSolution

Solution

app.js

const express = require('express') 
const app = express() 

dataset = {
	106020: {
		'name':'Girl with a Pearl Earring',
		'artist':'Vermeer',
		'date':1665,
		'desc':'Girl with a Pearl Earring is an oil painting by Dutch Golden Age painter Johannes Vermeer, dated c. 1665. Going by various names over the centuries, it became known by its present title towards the end of the 20th century after the earring worn by the girl portrayed there.',
		'variations': [
			{
				'name':'4x5 Reprint',
				'price': 40,
			},
			{
				'name':'8x10 Reprint',
				'price': 80,
			},
			{
				'name':'12x15 Reprint',
				'price': 120,
			}
		]
	},
	116010: {
		'name':'Artist Holding a Thistle',
		'artist':'Dürer',
		'date':1493,
		'desc':'Portrait of the Artist Holding a Thistle is an oil painting on parchment pasted on canvas by German artist Albrecht Dürer. Painted in 1493, it is the earliest of Dürer\'s painted self-portraits and has been identified as one of the first self-portraits painted by a Northern artist.',
		'variations': [
			{
				'name':'4x5 Reprint',
				'price': 65,
			},
			{
				'name':'8x10 Reprint',
				'price': 125,
			},
			{
				'name':'12x15 Reprint',
				'price': 180,
			}
		]
	},
	120010: {
		'name':'Portrait of Eleanor of Toledo',
		'artist':'Cosimo',
		'date':1545,
		'desc':'The Portrait of Eleanor of Toledo and Her Son is a painting by the Italian artist Agnolo di Cosimo, known as Bronzino, finished ca. 1545. One of his most famous works, it is housed in the Uffizi Gallery of Florence, Italy and is considered one of the preeminent examples of Mannerist portraiture',
		'variations': [
			{
				'name':'4x5 Reprint',
				'price': 50,
			},
			{
				'name':'8x10 Reprint',
				'price': 75,
			},
			{
				'name':'12x15 Reprint',
				'price': 125,
			}
		]
	}	
}

app.get("/matt/artists", (req, res) => { 
	artists = {};
	for (const [key,value] of Object.entries(dataset)) {
		if (value.artist in artists) {
			artists[value.artist].push(key);
		} else {
			artists[value.artist] = [key];
		}
	}
	res.send(JSON.stringify(artists));
}); 

app.get("/matt/artworks/?:artist?", (req, res) => {
	artworks = {};
	for (const [key,value] of Object.entries(dataset)) {
		if (req.params.artist == undefined || req.params.artist==value.artist) {
			artworks[key] = value.name;
		}
	}
	res.send(JSON.stringify(artworks));
});

app.get("/matt/artwork/:artwork", (req, res) => {
	if (dataset[req.params.artwork] == undefined) {
		res.send('{}');
	} else {
		res.send(JSON.stringify(dataset[req.params.artwork]));
	}
});

app.get("/matt/products/?:artist?", (req, res) => {
	products = [];
	for (const [key,value] of Object.entries(dataset)) {
		if (req.params.artist == undefined || req.params.artist==value.artist) {
			for (vid in value.variations) {
				variation = value.variations[vid];
				products.push({'id':key,'name':value.name,'var':variation.name,'price':variation.price});
			}
		}
	}
	res.send(JSON.stringify(products));
});

app.get("/", (req, res) => { 
	    res.send("404 URL NOT FOUND"); 
}); 
  
app.listen(8080, () => { 
	    console.log("listening on http://localhost:8080"); 
});