11import express from "express" ;
22import cors from "cors" ;
33import mongoose from "mongoose" ;
4+ import dotenv from "dotenv" ;
45import fs from "fs" ;
56
7+ // Ladda miljövariabler
8+ dotenv . config ( ) ;
9+
10+ // Läs in JSON-fil
611const rawData = fs . readFileSync ( "./data/netflix-titles.json" ) ;
712const netflixData = JSON . parse ( rawData ) ;
813
14+ // Miljövariabler
915const mongoUrl = process . env . MONGO_URL || "mongodb://localhost/netflix_titles" ;
16+ const port = process . env . PORT || 8080 ;
17+
18+ // Anslut till MongoDB
1019mongoose . connect ( mongoUrl , { useNewUrlParser : true , useUnifiedTopology : true } ) ;
1120mongoose . Promise = Promise ;
1221
13- const port = process . env . PORT || 8080 ;
22+ // Kontrollera anslutning
23+ mongoose . connection . on ( "connected" , ( ) => console . log ( "Connected to MongoDB!" ) ) ;
24+ mongoose . connection . on ( "error" , ( err ) => console . error ( "MongoDB connection error:" , err ) ) ;
25+
26+ // Skapa Express-app
1427const app = express ( ) ;
1528
1629// Middleware
1730app . use ( cors ( ) ) ;
1831app . use ( express . json ( ) ) ;
1932
20- // Define the model
33+ // Definiera Mongoose-modell
2134const NetflixTitle = mongoose . model ( "NetflixTitle" , {
2235 show_id : Number ,
2336 title : String ,
@@ -27,10 +40,10 @@ const NetflixTitle = mongoose.model("NetflixTitle", {
2740 description : String ,
2841} ) ;
2942
30- // Seed the database
31- if ( process . env . RESET_DB ) {
43+ // Seed-databasen
44+ if ( process . env . RESET_DB === "true" ) {
3245 const seedDatabase = async ( ) => {
33- await NetflixTitle . deleteMany ( ) ; // Clear existing data
46+ await NetflixTitle . deleteMany ( ) ;
3447 await NetflixTitle . insertMany ( netflixData . map ( ( item ) => ( {
3548 show_id : item . show_id ,
3649 title : item . title ,
@@ -44,73 +57,17 @@ if (process.env.RESET_DB) {
4457 seedDatabase ( ) ;
4558}
4659
47- // Routes
48- import listEndpoints from "express-list-endpoints" ;
49-
50- // API Documentation Route
51- app . get ( "/" , ( req , res ) => {
52- const documentation = {
53- welcome : "Welcome to the Netflix Titles API!" ,
54- description : "This API provides access to a collection of Netflix titles." ,
55- endpoints : listEndpoints ( app ) . map ( ( endpoint ) => ( {
56- path : endpoint . path ,
57- methods : endpoint . methods ,
58- } ) ) ,
59- queryParameters : {
60- "/netflix_titles" : {
61- sorted : "Sort titles by rating (true/false)" ,
62- country : "Filter by country (case-insensitive)" ,
63- release_year : "Filter by release year" ,
64- } ,
65- } ,
66- } ;
67- res . json ( documentation ) ;
68- } ) ;
69-
70- // Get all Netflix titles with optional filters and pretty-print
60+ // API-rutter
7161app . get ( "/netflix_titles" , async ( req , res ) => {
72- const { sorted, country, release_year } = req . query ;
73-
74- const query = { } ;
75- if ( country ) {
76- query . country = { $regex : country , $options : "i" } ; // Case-insensitive search
77- }
78- if ( release_year ) {
79- query . release_year = Number ( release_year ) ;
80- }
81-
82- try {
83- let titles = await NetflixTitle . find ( query ) ;
84- if ( sorted ) {
85- titles = titles . sort ( ( a , b ) => b . rating - a . rating ) ; // Sort by rating
86- }
87-
88- // Set header and send indented JSON
89- res . setHeader ( "Content-Type" , "application/json" ) ;
90- res . send ( JSON . stringify ( titles , null , 2 ) ) ;
91- } catch ( error ) {
92- res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
93- }
94- } ) ;
95-
96-
97- // Get a single Netflix title by show_id
98- app . get ( "/netflix_titles/:id" , async ( req , res ) => {
99- const { id } = req . params ;
100-
10162 try {
102- const title = await NetflixTitle . findOne ( { show_id : Number ( id ) } ) ;
103- if ( title ) {
104- res . json ( title ) ;
105- } else {
106- res . status ( 404 ) . json ( { error : "Title not found" } ) ;
107- }
108- } catch ( error ) {
63+ const titles = await NetflixTitle . find ( ) ;
64+ res . json ( titles ) ;
65+ } catch ( err ) {
10966 res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
11067 }
11168} ) ;
11269
113- // Start server
70+ // Starta servern
11471app . listen ( port , ( ) => {
11572 console . log ( `Server running on http://localhost:${ port } ` ) ;
11673} ) ;
0 commit comments