From 183cfb33432aac001f4705ab1765fe7e40c85407 Mon Sep 17 00:00:00 2001 From: Javier Godoy Date: Sat, 25 May 2024 20:25:36 +0200 Subject: [PATCH] solved lab --- app.js | 68 +++++++++++++++++++++++++++++---- package.json | 10 ++++- views/albums.hbs | 19 +++++++++ views/artist-search-results.hbs | 13 +++++++ views/index.hbs | 10 +++++ views/layout.hbs | 13 +++++++ views/tracks.hbs | 17 +++++++++ 7 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 views/albums.hbs create mode 100644 views/artist-search-results.hbs create mode 100644 views/index.hbs create mode 100644 views/layout.hbs create mode 100644 views/tracks.hbs diff --git a/app.js b/app.js index 5ea8eb4db..52b19b397 100644 --- a/app.js +++ b/app.js @@ -1,18 +1,70 @@ -require('dotenv').config(); - -const express = require('express'); -const hbs = require('hbs'); +require("dotenv").config(); +const express = require("express"); +const hbs = require("hbs"); +const SpotifyWebApi = require("spotify-web-api-node"); // require spotify-web-api-node package here: const app = express(); -app.set('view engine', 'hbs'); -app.set('views', __dirname + '/views'); -app.use(express.static(__dirname + '/public')); +app.set("view engine", "hbs", "ejs"); +app.set("views", __dirname + "/views"); +app.use(express.static(__dirname + "/public")); +hbs.registerPartials(__dirname + "/views/partials"); +hbs.registerHelper("eq", function (a, b) { + return a === b; +}); // setting the spotify-api goes here: +const spotifyApi = new SpotifyWebApi({ + clientId: process.env.CLIENT_ID, + clientSecret: process.env.CLIENT_SECRET, +}); + +// Retrieve an access token +spotifyApi + .clientCredentialsGrant() + .then((data) => spotifyApi.setAccessToken(data.body["access_token"])) + .catch((error) => + console.log("Something went wrong when retrieving an access token", error) + ); // Our routes go here: +app.get(`/`, (req, res) => { + res.render(`index`); +}); + +app.get(`/artist-search`, (req, res) => { + spotifyApi + .searchArtists(req.query.artist) + .then((data) => { + console.log(data.body.artist.items); + res.render(`artist-search-results`, { artists: data.body.artists.items }); + }) + .catch((err) => res.send(`something went wrong`) + ); +}); + +app.get("/albums/:artistId", (req, res) => { + spotifyApi + .getArtistAlbums(req.params.artistId) + .then((data) => { + res.render("albums", { albums: data.body.items }); + }) + .catch((err) => res.send(`something went wrong`) + ); +}); + +app.get("/tracks/:albumId", (req, res) => { + spotifyApi + .getAlbumTracks(req.params.albumId) + .then((data) => { + res.render("traks", { traks: data.body.items }); + }) + .catch((err) => res.send(`something went wrong`) + ); +}); -app.listen(3000, () => console.log('My Spotify project running on port 3000 🎧 🥁 🎸 🔊')); +app.listen(3000, () => + console.log("My Spotify project running on port 3000 🎧 🥁 🎸 🔊") +); diff --git a/package.json b/package.json index c9f4085ba..892236e4f 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "starter-code", "version": "1.0.0", - "description": "", + "description": "![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png)", "main": "app.js", "scripts": { - "dev": "nodemon app.js", + "dev": "nodemon app.js -e hbs,js,css", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], @@ -12,5 +12,11 @@ "license": "ISC", "devDependencies": { "nodemon": "^2.0.2" + }, + "dependencies": { + "dotenv": "^16.4.5", + "express": "^4.19.2", + "hbs": "^4.2.0", + "spotify-web-api-node": "^5.0.2" } } diff --git a/views/albums.hbs b/views/albums.hbs new file mode 100644 index 000000000..7b191177e --- /dev/null +++ b/views/albums.hbs @@ -0,0 +1,19 @@ +
+

+ Albums for: + {{albums.0.artist.0.name}} +

+
+ {{#each albums}} +
+ {{#if this.images.length}} + image + {{/if}} +

{{this.name}}

+ +
+ {{/each}} +
+
\ No newline at end of file diff --git a/views/artist-search-results.hbs b/views/artist-search-results.hbs new file mode 100644 index 000000000..0edfa634d --- /dev/null +++ b/views/artist-search-results.hbs @@ -0,0 +1,13 @@ +
+ {{#each artist}} +
+ {{#if this.images.length}} + image + {{/if}} +

{{this.name}}

+ +
+ {{/each}} +
\ No newline at end of file diff --git a/views/index.hbs b/views/index.hbs new file mode 100644 index 000000000..9c8bd00e6 --- /dev/null +++ b/views/index.hbs @@ -0,0 +1,10 @@ +
+ +
+ + + +
+
\ No newline at end of file diff --git a/views/layout.hbs b/views/layout.hbs new file mode 100644 index 000000000..ffb5a9c01 --- /dev/null +++ b/views/layout.hbs @@ -0,0 +1,13 @@ + + + + + + + + Document + + + {{{body}}} + + \ No newline at end of file diff --git a/views/tracks.hbs b/views/tracks.hbs new file mode 100644 index 000000000..36cf615bf --- /dev/null +++ b/views/tracks.hbs @@ -0,0 +1,17 @@ +
+ {{#each tracks}} +
+

{{this.name}}

+ {{#each this.artist}} +

{{this.name}}

+ {{/each}} + {{#if this.preview_url}} + + {{else}} +

No preview available

+ {{/if}} +
+ {{/each}} +
\ No newline at end of file