diff --git a/docs/openapi.yaml b/docs/openapi.yaml index d9af5fb..2e2c715 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -1,11 +1,12 @@ -openapi: 3.0.0 +# yaml-language-server: $schema=https://json-schema.org/draft/2020-12/schema +openapi: 3.0.3 info: title: Snapi (Sneakers API) version: 1.0.0 description: API for managing sneakers, reviews, users, providers and stores servers: - - url: http://localhost:8080/api - description: V1 + - url: http://localhost:8080/api/ + description: Production server tags: - name: sneakers @@ -216,7 +217,17 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Review' + $ref: '#/components/schemas/ReviewsResponse' + '400': + description: Invalid input data + content: + application/json: + schema: { $ref: '#/components/schemas/Error' } + '409': + description: User has already reviewed this sneaker + content: + application/json: + schema: { $ref: '#/components/schemas/Error' } '404': description: Sneaker not found content: @@ -240,7 +251,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Review' + $ref: '#/components/schemas/ReviewsResponse' '404': description: Review not found content: @@ -269,12 +280,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Review' - '403': - description: Not authorized to update this review - content: - application/json: - schema: { $ref: '#/components/schemas/Error' } + $ref: '#/components/schemas/ReviewsResponse' '404': description: Review not found content: @@ -294,11 +300,6 @@ paths: responses: '204': description: Review deleted - '403': - description: Not authorized to delete this review - content: - application/json: - schema: { $ref: '#/components/schemas/Error' } '404': description: Review not found content: @@ -331,6 +332,12 @@ paths: content: application/json: schema: { $ref: '#/components/schemas/Error' } + '409': + description: Email already exists + content: + application/json: + schema: + $ref: '#/components/schemas/Error' /users/{id}: get: @@ -349,7 +356,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/User' + $ref: '#/components/schemas/UserResponse' '404': description: User not found content: @@ -371,14 +378,14 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/User' + $ref: '#/components/schemas/UserInput' responses: '200': description: User updated content: application/json: schema: - $ref: '#/components/schemas/User' + $ref: '#/components/schemas/UserResponse' '400': description: Invalid input data content: @@ -460,9 +467,7 @@ paths: content: application/json: schema: - type: array - items: - $ref: '#/components/schemas/Sneaker' + $ref: '#/components/schemas/FavoritesResponse' '404': description: User or sneaker not found content: @@ -507,9 +512,7 @@ paths: content: application/json: schema: - type: array - items: - $ref: '#/components/schemas/Provider' + $ref: '#/components/schemas/Provider' '404': description: No providers found content: @@ -532,7 +535,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Provider' + $ref: '#/components/schemas/ProvidersResponse' '400': description: Invalid input data content: @@ -556,7 +559,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Provider' + $ref: '#/components/schemas/ProvidersResponse' '404': description: Provider not found content: @@ -585,7 +588,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Provider' + $ref: '#/components/schemas/ProvidersResponse' '400': description: Invalid input data content: @@ -788,15 +791,31 @@ components: _id: type: string readOnly: true + sneakerId: + type: string + readOnly: true # The saved sneakerId is the one in the param + userId: + type: string rating: type: integer minimum: 1 maximum: 5 comment: type: string - userId: + date: type: string - required: [rating] + format: date-time + required: [sneakerId, rating, userId] + + UserInput: + type: object + properties: + name: + type: string + email: + type: string + format: email + required: [name, email] User: type: object @@ -809,8 +828,51 @@ components: email: type: string format: email + favorites: + type: array + items: + type: string required: [name, email] + UserResponse: + type: object + properties: + items: + $ref: '#/components/schemas/User' + message: + type: string + status: + type: string + enum: [success] + required: [items, message, status] + + FavoritesResponse: + type: object + properties: + items: + type: array + items: + type: string + message: + type: string + status: + type: string + enum: [success] + required: [items, message, status] + + ReviewsResponse: + type: object + properties: + items: + $ref: '#/components/schemas/Review' + message: + type: string + status: + type: string + enum: [success] + required: [items, message, status] + + Provider: type: object properties: @@ -824,6 +886,18 @@ components: format: email required: [name, contact_email] + ProvidersResponse: + type: object + properties: + items: + $ref: '#/components/schemas/Provider' + message: + type: string + status: + type: string + enum: [success] + required: [items, message, status] + Store: type: object properties: @@ -840,6 +914,9 @@ components: Error: type: object properties: - code: { type: string } - message: { type: string } - required: [error] \ No newline at end of file + message: + type: string + status: + type: string + enum: [failure] + required: [message, status] \ No newline at end of file diff --git a/docs/roadmap.md b/docs/roadmap.md index c327f69..3b6c85b 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -5,16 +5,16 @@ - [x] The API offers a REST interface and allows CRUD operations on the DB - [x] The database is a MongoDB database. - [x] The database is **automatically seeded** on launch if it's empty. - - [ ] At least one message is in XML format and has an associated schema. + - [ ] At least one message is in XML format and has an associated schema. (**optional**) - [x] At least one response is in JSON format - - [ ] There are at least 3 resources and they are related to each other. + - [x] There are at least 3 resources and they are related to each other (sneakers, users, reviews). - [x] One of the collections has at least **1000 documents** - [x] There is a **dataset** to seed this collection in the repository ( -> this dataset is in **JSON format**) - [ ] At least one route allows pagination - [x] At least one route allows filtering data to search inside this collection - [x] Uses an external API - - [ ] At least one consumed message is in XML format + - [ ] At least one consumed message is in XML format (**optional**) - [x] At least one consumed message is in JSON format - [x] The consumed information is integrated with our API and data is saved in our DB. - [x] The API keeps working even if the external API is down. diff --git a/package-lock.json b/package-lock.json index e5d295b..17633f3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,16 +12,17 @@ "axios": "^1.9.0", "body-parser": "^1.20.3", "cors": "^2.8.5", - "dotenv": "^16.4.7", - "express": "^4.17.1", - "mongoose": "^8.14.1", + "dotenv": "^16.5.0", + "express": "^4.21.2", + "mongoose": "^8.15.0", "ts-node": "^10.9.2" }, "devDependencies": { - "@types/express": "5.0.0", + "@types/express": "^5.0.0", "@types/mongoose": "^5.11.96", + "@types/node": "^22.15.18", "ts-node-dev": "^2.0.0", - "typescript": "^5.7.3" + "typescript": "^5.8.3" } }, "node_modules/@cspotcode/source-map-support": { @@ -166,12 +167,12 @@ } }, "node_modules/@types/node": { - "version": "22.13.5", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.5.tgz", - "integrity": "sha512-+lTU0PxZXn0Dr1NBtC7Y8cR21AJr87dLLU953CWA6pMxxv/UDc7jYAY90upcrie1nRcD6XNG5HOYEDtgW5TxAg==", + "version": "22.15.18", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.15.18.tgz", + "integrity": "sha512-v1DKRfUdyW+jJhZNEI1PYy29S2YRxMV5AOO/x/SjKmW0acCIOqmbj6Haf9eHAhsPmrhlHSxEhv/1WszcLWV4cg==", "license": "MIT", "dependencies": { - "undici-types": "~6.20.0" + "undici-types": "~6.21.0" } }, "node_modules/@types/qs": { @@ -588,9 +589,9 @@ } }, "node_modules/dotenv": { - "version": "16.4.7", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz", - "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==", + "version": "16.5.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.5.0.tgz", + "integrity": "sha512-m/C+AwOAr9/W1UOIZUo232ejMNnJAJtYQjUbHoNTBNTJSvqzzDh7vnrei3o3r3m9blf6ZoDkvcw0VmozNRFJxg==", "license": "BSD-2-Clause", "engines": { "node": ">=12" @@ -1282,9 +1283,9 @@ } }, "node_modules/mongoose": { - "version": "8.14.1", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.14.1.tgz", - "integrity": "sha512-ijd12vjqUBr5Btqqflu0c/o8Oed5JpdaE0AKO9TjGxCgywYwnzt6ynR1ySjhgxGxrYVeXC0t1P11f1zlRiE93Q==", + "version": "8.15.0", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.15.0.tgz", + "integrity": "sha512-WFKsY1q12ScGabnZWUB9c/QzZmz/ESorrV27OembB7Gz6rrh9m3GA4Srsv1uvW1s9AHO5DeZ6DdUTyF9zyNERQ==", "license": "MIT", "dependencies": { "bson": "^6.10.3", @@ -1966,9 +1967,9 @@ } }, "node_modules/typescript": { - "version": "5.7.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.3.tgz", - "integrity": "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==", + "version": "5.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.8.3.tgz", + "integrity": "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==", "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", @@ -1979,9 +1980,9 @@ } }, "node_modules/undici-types": { - "version": "6.20.0", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.20.0.tgz", - "integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==", + "version": "6.21.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", "license": "MIT" }, "node_modules/unpipe": { diff --git a/package.json b/package.json index 62e8118..2d120c6 100644 --- a/package.json +++ b/package.json @@ -13,18 +13,19 @@ "license": "ISC", "description": "", "devDependencies": { - "@types/express": "5.0.0", + "@types/express": "^5.0.0", "@types/mongoose": "^5.11.96", + "@types/node": "^22.15.18", "ts-node-dev": "^2.0.0", - "typescript": "^5.7.3" + "typescript": "^5.8.3" }, "dependencies": { "axios": "^1.9.0", "body-parser": "^1.20.3", "cors": "^2.8.5", - "dotenv": "^16.4.7", - "express": "^4.17.1", - "mongoose": "^8.14.1", + "dotenv": "^16.5.0", + "express": "^4.21.2", + "mongoose": "^8.15.0", "ts-node": "^10.9.2" } } diff --git a/seeds/providers/providers.json b/seeds/providers/providers.json new file mode 100644 index 0000000..835ed7e --- /dev/null +++ b/seeds/providers/providers.json @@ -0,0 +1,402 @@ +[ + { + "name": "Nike", + "contact_email": "support@nike.com" + }, + { + "name": "Adidas", + "contact_email": "support@adidas.com" + }, + { + "name": "Puma", + "contact_email": "support@puma.com" + }, + { + "name": "Reebok", + "contact_email": "support@reebok.com" + }, + { + "name": "New Balance", + "contact_email": "support@newbalance.com" + }, + { + "name": "Under Armour", + "contact_email": "support@underarmour.com" + }, + { + "name": "Asics", + "contact_email": "support@asics.com" + }, + { + "name": "Converse", + "contact_email": "support@converse.com" + }, + { + "name": "Vans", + "contact_email": "support@vans.com" + }, + { + "name": "Fila", + "contact_email": "support@fila.com" + }, + { + "name": "Brooks", + "contact_email": "support@brooks.com" + }, + { + "name": "Saucony", + "contact_email": "support@saucony.com" + }, + { + "name": "Skechers", + "contact_email": "support@skechers.com" + }, + { + "name": "Mizuno", + "contact_email": "support@mizuno.com" + }, + { + "name": "Jordan", + "contact_email": "support@jordan.com" + }, + { + "name": "New Era", + "contact_email": "support@newera.com" + }, + { + "name": "DC Shoes", + "contact_email": "support@dcshoes.com" + }, + { + "name": "Burton", + "contact_email": "support@burton.com" + }, + { + "name": "The North Face", + "contact_email": "support@thenorthface.com" + }, + { + "name": "Columbia", + "contact_email": "support@columbia.com" + }, + { + "name": "Salomon", + "contact_email": "support@salomon.com" + }, + { + "name": "Merrell", + "contact_email": "support@merrell.com" + }, + { + "name": "Patagonia", + "contact_email": "support@patagonia.com" + }, + { + "name": "Arc'teryx", + "contact_email": "support@arcteryx.com" + }, + { + "name": "Hoka One One", + "contact_email": "support@hokaoneone.com" + }, + { + "name": "Altra", + "contact_email": "support@altra.com" + }, + { + "name": "On Running", + "contact_email": "support@onrunning.com" + }, + { + "name": "Vibram", + "contact_email": "support@vibram.com" + }, + { + "name": "K-Swiss", + "contact_email": "support@k-swiss.com" + }, + { + "name": "Lululemon", + "contact_email": "support@lululemon.com" + }, + { + "name": "Karhu", + "contact_email": "support@karhu.com" + }, + { + "name": "Diadora", + "contact_email": "support@diadora.com" + }, + { + "name": "Ecco", + "contact_email": "support@ecco.com" + }, + { + "name": "Gola", + "contact_email": "support@gola.com" + }, + { + "name": "KangaROOS", + "contact_email": "support@kangaroos.com" + }, + { + "name": "Le Coq Sportif", + "contact_email": "support@lecoqsportif.com" + }, + { + "name": "Veja", + "contact_email": "support@veja.com" + }, + { + "name": "Allbirds", + "contact_email": "support@allbirds.com" + }, + { + "name": "Koio", + "contact_email": "support@koio.com" + }, + { + "name": "Cariuma", + "contact_email": "support@cariuma.com" + }, + { + "name": "Novesta", + "contact_email": "support@novesta.com" + }, + { + "name": "Danner", + "contact_email": "support@danner.com" + }, + { + "name": "Clarks", + "contact_email": "support@clarks.com" + }, + { + "name": "TOMS", + "contact_email": "support@toms.com" + }, + { + "name": "Supra", + "contact_email": "support@supra.com" + }, + { + "name": "Golden Goose", + "contact_email": "support@goldengoose.com" + }, + { + "name": "Common Projects", + "contact_email": "support@commonprojects.com" + }, + { + "name": "BAPE", + "contact_email": "support@bape.com" + }, + { + "name": "Yeezy", + "contact_email": "support@yeezy.com" + }, + { + "name": "Li-Ning", + "contact_email": "support@li-ning.com" + }, + { + "name": "Peak", + "contact_email": "support@peak.com" + }, + { + "name": "Anta", + "contact_email": "support@anta.com" + }, + { + "name": "361 Degrees", + "contact_email": "support@361degrees.com" + }, + { + "name": "Air Jordan", + "contact_email": "support@airjordan.com" + }, + { + "name": "Y-3", + "contact_email": "support@y-3.com" + }, + { + "name": "Off-White", + "contact_email": "support@off-white.com" + }, + { + "name": "Balenciaga", + "contact_email": "support@balenciaga.com" + }, + { + "name": "Gucci", + "contact_email": "support@gucci.com" + }, + { + "name": "Dior", + "contact_email": "support@dior.com" + }, + { + "name": "Dolce & Gabbana", + "contact_email": "support@dolcegabbana.com" + }, + { + "name": "Moschino", + "contact_email": "support@moschino.com" + }, + { + "name": "Alexander McQueen", + "contact_email": "support@alexandermcqueen.com" + }, + { + "name": "Prada", + "contact_email": "support@prada.com" + }, + { + "name": "Versace", + "contact_email": "support@versace.com" + }, + { + "name": "Saint Laurent", + "contact_email": "support@saintlaurent.com" + }, + { + "name": "Givenchy", + "contact_email": "support@givenchy.com" + }, + { + "name": "Fendi", + "contact_email": "support@fendi.com" + }, + { + "name": "Valentino", + "contact_email": "support@valentino.com" + }, + { + "name": "Chanel", + "contact_email": "support@chanel.com" + }, + { + "name": "Balmain", + "contact_email": "support@balmain.com" + }, + { + "name": "Undercover", + "contact_email": "support@undercover.com" + }, + { + "name": "Maison Margiela", + "contact_email": "support@maisonmargiela.com" + }, + { + "name": "Rick Owens", + "contact_email": "support@rickowens.com" + }, + { + "name": "Veja x Rick Owens", + "contact_email": "support@vejaxrickowens.com" + }, + { + "name": "Nike SB", + "contact_email": "support@nikesb.com" + }, + { + "name": "Adidas Originals", + "contact_email": "support@adidasoriginals.com" + }, + { + "name": "Puma Select", + "contact_email": "support@pumaselect.com" + }, + { + "name": "New Balance Numeric", + "contact_email": "support@newbalancenumeric.com" + }, + { + "name": "Saucony Originals", + "contact_email": "support@sauconyoriginals.com" + }, + { + "name": "Converse Chuck", + "contact_email": "support@conversechuck.com" + }, + { + "name": "Vans Vault", + "contact_email": "support@vansvault.com" + }, + { + "name": "Reebok Classics", + "contact_email": "support@reebokclassics.com" + }, + { + "name": "Asics Tiger", + "contact_email": "support@asicstiger.com" + }, + { + "name": "Onitsuka Tiger", + "contact_email": "support@onitsukatiger.com" + }, + { + "name": "Under Armour HOVR", + "contact_email": "support@underarmourhovr.com" + }, + { + "name": "Skechers Sport", + "contact_email": "support@skecherssport.com" + }, + { + "name": "Merrell Moab", + "contact_email": "support@merrellmoab.com" + }, + { + "name": "Salomon Speedcross", + "contact_email": "support@salomonspeedcross.com" + }, + { + "name": "Patagonia Footwear", + "contact_email": "support@patagoniafootwear.com" + }, + { + "name": "Arc'teryx Veilance", + "contact_email": "support@arcteryxveilance.com" + }, + { + "name": "Hoka EVO", + "contact_email": "support@hokaevo.com" + }, + { + "name": "Altra Escalante", + "contact_email": "support@altraescalante.com" + }, + { + "name": "On Cloud", + "contact_email": "support@oncloud.com" + }, + { + "name": "Vibram FiveFingers", + "contact_email": "support@vibramfivefingers.com" + }, + { + "name": "K-Swiss Classic", + "contact_email": "support@k-swissclassic.com" + }, + { + "name": "Lululemon Surge", + "contact_email": "support@lululemonsurge.com" + }, + { + "name": "Karhu Fusion", + "contact_email": "support@karhufusion.com" + }, + { + "name": "Diadora Heritage", + "contact_email": "support@diadoraheritage.com" + }, + { + "name": "Ecco Biom", + "contact_email": "support@eccobiom.com" + }, + { + "name": "Gola Classics", + "contact_email": "support@golaclassics.com" + } + ] \ No newline at end of file diff --git a/src/config/database.ts b/src/config/database.ts index 884bf83..f1a5e34 100644 --- a/src/config/database.ts +++ b/src/config/database.ts @@ -4,10 +4,12 @@ import dotenv from 'dotenv'; import { Sneaker } from "../schemas/sneaker"; import { Currency } from "../schemas/currency"; import { Store } from "../schemas/store"; +import { Provider } from "../schemas/provider"; import sneakerSeedData from "../../seeds/sneakers/sneakers.json"; import currencySeedData from "../../seeds/currencies.json"; import storeSeedData from "../../seeds/stores/stores.json"; +import providerSeedData from "../../seeds/providers/providers.json"; dotenv.config({ path: '../../.env' }); @@ -28,7 +30,8 @@ export async function seedDatabase() { await Promise.all([ seedSneakers(), seedCurrencies(), - seedStores() + seedStores(), + seedProviders() ]); } catch (error) { console.error("Seeding failed:", error); @@ -71,3 +74,15 @@ async function seedStores() { await Store.insertMany(storeSeedData); console.log(`Seeded ${storeSeedData.length} documents in 'stores'`); } + +async function seedProviders() { + const providersCount = await Provider.countDocuments(); + + if (providersCount > 0) { + console.log(`Found ${providersCount} documents in 'providers' - skipping seed`); + return; + } + + await Provider.insertMany(providerSeedData); + console.log(`Seeded ${providerSeedData.length} documents in 'providers'`); +} diff --git a/src/controllers/provider.ts b/src/controllers/provider.ts new file mode 100644 index 0000000..bbd3283 --- /dev/null +++ b/src/controllers/provider.ts @@ -0,0 +1,139 @@ +import { Provider } from "../schemas/provider"; + +export const getProviders = async (req, res) => { + try { + const providers = await Provider.find(); + + if (!providers || providers.length === 0) { + return res.status(404).json({ + message: 'No providers found', + status: 'failure' + }); + } + + return res.status(200).json({ + items: providers, + count: providers.length, + message: 'Providers data fetched successfully', + status: 'success' + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error fetching providers', + status: 'failure' + }); + } +}; + +export const getProviderById = async (req, res) => { + const { id } = req.params; + + try { + const provider = await Provider.findById(id); + + if (!provider) { + return res.status(404).json({ + message: 'Provider not found', + status: 'failure' + }); + } + + return res.status(200).json({ + items: provider, + message: 'Provider data fetched successfully', + status: 'success' + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error fetching provider', + status: 'failure' + }); + } +}; + +export const createProvider = async (req, res) => { + try { + const providerData = req.body; + + if (!providerData.name || !providerData.contact_email) { + return res.status(400).json({ + message: 'Invalid input data', + status: 'failure' + }); + } + + const provider = await Provider.insertOne(providerData); + + return res.status(201).json({ + items: provider, + message: 'Provider created successfully', + status: 'success' + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error creating provider', + status: 'failure' + }); + } +}; + +export const updateProviderById = async (req, res) => { + const { id } = req.params; + const updateData = req.body; + + try { + const provider = await Provider.findByIdAndUpdate(id, updateData, { new: true }); + + if (!provider) { + return res.status(404).json({ + message: 'Provider not found', + status: 'failure' + }); + } + + if (!updateData.name || !updateData.contact_email) { + return res.status(400).json({ + message: 'Invalid input data', + status: 'failure' + }); + } + + return res.status(200).json({ + items: provider, + message: 'Provider updated successfully', + status: 'success' + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error updating provider', + status: 'failure' + }); + } +}; + +export const deleteProviderById = async (req, res) => { + const { id } = req.params; + + try { + const provider = await Provider.findByIdAndDelete(id); + + if (!provider) { + return res.status(404).json({ + message: 'Provider not found', + status: 'failure' + }); + } + + return res.status(204).send(); + + } catch (error) { + return res.status(500).json({ + message: 'Error deleting provider', + status: 'failure' + }); + } +}; diff --git a/src/controllers/reviews.ts b/src/controllers/reviews.ts new file mode 100644 index 0000000..708eeeb --- /dev/null +++ b/src/controllers/reviews.ts @@ -0,0 +1,75 @@ +import { Review } from "../schemas/review"; + +export const getReviewById = async (req, res) => { + try { + const { reviewId } = req.params; + const review = await Review.findById(reviewId); + + if (!review) { + return res.status(404).json({ + message: 'Review not found', status: "failure" + }); + } + + return res.status(200).json({ + items: review, + message: "Review successfully updated", + status: "success" + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error getting review', + status: 'failure' + }); + } +} + +export const updateReviewById = async (req, res) => { + try { + const { reviewId } = req.params; + const updatedReview = await Review.findByIdAndUpdate( + reviewId, + req.body, + { new: true } + ); + + if (!updatedReview) { + return res.status(404).json({ message: 'Review not found' }); + } + + return res.status(200).json({ + items: updatedReview, + message: "Store successfully updated", + status: "success" + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error updating review', + status: 'failure' + }); + } +} + +export const deleteReviewById = async (req, res) => { + try { + const { reviewId } = req.params; + const deletedReview = await Review.findByIdAndDelete(reviewId); + + if (!deletedReview) { + return res.status(404).json({ + message: 'Review not found', + status: 'failure' + }); + } + + return res.status(204).send(); + + } catch (error) { + return res.status(500).json({ + message: 'Error deleting review', + status: 'failure' + }); + } +} diff --git a/src/controllers/sneakers.ts b/src/controllers/sneakers.ts index a7d25c1..211926d 100644 --- a/src/controllers/sneakers.ts +++ b/src/controllers/sneakers.ts @@ -1,3 +1,4 @@ +import { Review } from "../schemas/review"; import { Sneaker } from "../schemas/sneaker"; import { getCurrencyRate } from "./utils/currency"; @@ -74,26 +75,36 @@ export const getSneakers = async (req, res) => { export const getSneakerById = async (req, res) => { - const { sneakerId } = req.params; + try { + const { sneakerId } = req.params; - const sneaker = await Sneaker.findOne({ _id: sneakerId }); + const sneaker = await Sneaker.findOne({ _id: sneakerId }); + + if (!sneaker) { + return res + .status(404) + .json({ + message: 'Sneaker not found', + status: 'failure' + }); + } - if (!sneaker) { return res - .status(404) + .status(200) .json({ - message: 'Sneaker not found', + items: sneaker, + message: 'Sneaker data fetched succesfully.', + status: 'success' + }) + + } catch (error) { + return res + .status(500) + .json({ + message: 'Error getting sneaker', status: 'failure' }); } - - return res - .status(200) - .json({ - items: sneaker, - message: 'Sneaker data fetched succesfully.', - status: 'success' - }) }; export const createSneaker = async (req, res) => { @@ -188,4 +199,98 @@ export const deleteSneakerById = async (req, res) => { status: 'failure' }); } -}; \ No newline at end of file +}; + + +export const getSneakerReviews = async (req, res) => { + const { sneakerId } = req.params; + + try { + + const existingSneaker = await Sneaker.findOne({ _id: sneakerId }); + + if (!existingSneaker) { + return res.status(404).json({ + message: 'Sneaker not found', + status: 'failure' + }); + } + + const reviews = await Review.find({ sneakerId }).sort({ date: -1 }); + + if (!reviews || reviews.length === 0) { + return res.status(404).json({ + message: 'No reviews available for this sneaker', + status: 'failure' + }); + } + + return res.status(200).json(reviews); + + } catch (error) { + + return res.status(500).json({ + message: 'Error fetching reviews', + status: 'failure' + }); + } +}; + + + +export const createSneakerReview = async (req, res) => { + const { sneakerId } = req.params; + const { rating, comment, userId } = req.body; + + try { + const existingSneaker = await Sneaker.findOne({ _id: sneakerId }); + + if (!existingSneaker) { + return res.status(404).json({ + message: 'Sneaker not found', + status: 'failure' + }); + } + + if (!rating || rating < 1 || rating > 5) { + return res.status(400).json({ + message: 'Rating must be between 1 and 5', + status: 'failure' + }); + } + + const existingReview = await Review.findOne({ sneakerId, userId }); + + if (existingReview) { + return res.status(409).json({ + message: 'You have already reviewed this sneaker', + status: 'failure' + }); + } + + const newReview = new Review({ + sneakerId, + rating, + userId, + comment, + date: new Date() + }) + + await newReview.save(); + + + return res.status(201).json({ + items: newReview, + message: 'Review created successfully', + status: 'success' + }); + + } catch (error) { + + return res.status(500).json({ + message: 'Error creating review', + status: 'failure' + }); + } +} + diff --git a/src/controllers/stores.ts b/src/controllers/stores.ts index 5d2e25a..1732bfe 100644 --- a/src/controllers/stores.ts +++ b/src/controllers/stores.ts @@ -42,7 +42,7 @@ export const getStoreById = async (req, res) => { } catch (error) { return res.status(500).json({ - message: 'Error fetching stores', + message: 'Error getting store', status: 'failure' }); } diff --git a/src/controllers/users.ts b/src/controllers/users.ts new file mode 100644 index 0000000..aa7a696 --- /dev/null +++ b/src/controllers/users.ts @@ -0,0 +1,224 @@ +import { User } from "../schemas/users"; +import { Sneaker } from "../schemas/sneaker"; + + +export const getUserById = async (req, res) => { + try { + const { id } = req.params; + + const user = await User.findById(id); + + if (!user) { + return res.status(404).json({ + message: 'User not found', + status: 'failure' + }); + } + + return res.status(200).json({ + items: user, + message: 'User data fetched succesfully.', + status: 'success' + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error getting user', + status: 'failure' + }); + } +}; + + +export const createUser = async (req, res) => { + try { + const { name, email } = req.body; + + if (!name || !email) { + return res.status(400).json({ + message: 'Invalid input data', + status: 'failure' + }); + } + + const existingUser = await User.findOne({ email }); + + if (existingUser) { + return res.status(409).json({ + message: 'Email already exists', + status: 'failure' + }); + } + + const newUser = await User.insertOne({ name, email }); + + return res.status(201).json({ + items: newUser, + message: 'User created successfully', + status: 'success' + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error creating user', + status: 'failure' + }); + } +}; + +export const updateUser = async (req, res) => { + try { + const { id } = req.params; + const updateData = req.body; + + if (!updateData.name || !updateData.email) { + return res.status(400).json({ + message: 'Invalid input data', + status: 'failure' + }); + } + + const updatedUser = await User.findByIdAndUpdate(id, updateData, { new: true }); + + if (!updatedUser) { + return res.status(404).json({ + message: "User not found", + status: "failure" + }); + } + + return res.status(200).json({ + items: updatedUser, + message: "User successfully updated", + status: "success" + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error updating user', + status: 'failure' + }); + } +}; + + +export const deleteUserById = async (req, res) => { + try { + const { id } = req.params; + + const user = await User.findByIdAndDelete(id); + + if (!user) { + return res.status(404).json({ + message: "User not found", + status: "failure" + }); + } + + return res.status(204).send(); + + } catch (error) { + return res.status(500).json({ + message: 'Error deleting user', + status: 'failure' + }); + } +}; + +export const getFavorites = async (req, res) => { + try { + const { id } = req.params; + const user = await User.findById(id).populate("favorites"); + + if (!user) { + return res.status(404).json({ + message: "User not found", + status: "failure" + }); + } + + return res.status(200).json({ + items: user.favorites || [], + status: "success" + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error fetching favorites', + status: 'failure' + }); + } +}; + + +export const addFavorite = async (req, res) => { + try { + const { id } = req.params; + const { sneakerId } = req.body; + + const user = await User.findById(id); + const sneaker = await Sneaker.findById(sneakerId); + + if (!user || !sneaker) { + return res.status(404).json({ + message: "User or sneaker not found", + status: "failure" + }); + } + + if (!user.favorites.includes(sneakerId)) { + user.favorites.push(sneakerId); + await user.save(); + } + + return res.status(201).json({ + items: user.favorites, + message: "Sneaker added to favorites", + status: "success" + }); + + } catch (error) { + return res.status(500).json({ + message: 'Error adding favorite', + status: 'failure' + }); + } +}; + + +export const removeFavorite = async (req, res) => { + try { + const { id, sneakerId } = req.params; + + const user = await User.findById(id); + + if (!user) { + return res.status(404).json({ + message: "User not found", + status: "failure" + }); + } + + const index = user.favorites.findIndex( + (fav) => fav.toString() === sneakerId + ); + + if (index === -1) { + return res.status(404).json({ + message: "Sneaker not found in favorites", + status: "failure" + }); + } + + user.favorites.splice(index, 1); + await user.save(); + + return res.status(204).send(); + + } catch (error) { + return res.status(500).json({ + message: 'Error removing favorite', + status: 'failure' + }); + } +}; diff --git a/src/router/index.ts b/src/router/index.ts index cb013e4..eff765b 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,11 +1,16 @@ import express from 'express'; import sneakersRouter from './sneakers'; import storesRouter from './stores'; +import providersRouter from './provider'; +import usersRouter from './users'; +import reviewsRouter from './reviews'; const router = express.Router() router.use('/sneakers', sneakersRouter); router.use('/stores', storesRouter); - +router.use('/providers', providersRouter); +router.use('/users', usersRouter); +router.use('/reviews', reviewsRouter); export default router; \ No newline at end of file diff --git a/src/router/provider.ts b/src/router/provider.ts new file mode 100644 index 0000000..4fd1f75 --- /dev/null +++ b/src/router/provider.ts @@ -0,0 +1,25 @@ +import express from 'express'; +import { + getProviders, + getProviderById, + createProvider, + updateProviderById, + deleteProviderById +} from '../controllers/provider'; + +const providersRouter = express.Router(); + +// GET +providersRouter.get('/', getProviders); +providersRouter.get('/:id', getProviderById); + +// POST +providersRouter.post('/', createProvider); + +// PUT +providersRouter.put('/:id', updateProviderById); + +// DELETE +providersRouter.delete('/:id', deleteProviderById); + +export default providersRouter; diff --git a/src/router/reviews.ts b/src/router/reviews.ts new file mode 100644 index 0000000..8482509 --- /dev/null +++ b/src/router/reviews.ts @@ -0,0 +1,16 @@ +import express from 'express'; +import { + getReviewById, + updateReviewById, + deleteReviewById +} from '../controllers/reviews'; + +const reviewsRouter = express.Router(); + +reviewsRouter.get('/:reviewId', getReviewById); + +reviewsRouter.put('/:reviewId', updateReviewById); + +reviewsRouter.delete('/:reviewId', deleteReviewById); + +export default reviewsRouter; \ No newline at end of file diff --git a/src/router/sneakers.ts b/src/router/sneakers.ts index 3a1967b..cbc5022 100644 --- a/src/router/sneakers.ts +++ b/src/router/sneakers.ts @@ -4,7 +4,9 @@ import { getSneakerById, createSneaker, updateSneakerById, - deleteSneakerById + deleteSneakerById, + getSneakerReviews, + createSneakerReview } from '../controllers/sneakers'; const sneakersRouter = express.Router(); @@ -13,8 +15,11 @@ const sneakersRouter = express.Router(); sneakersRouter.get('/', getSneakers); sneakersRouter.get('/:sneakerId', getSneakerById); +sneakersRouter.get('/:sneakerId/reviews', getSneakerReviews); + // POST sneakersRouter.post('/', createSneaker); +sneakersRouter.post('/:sneakerId/reviews', createSneakerReview); // PUT sneakersRouter.put('/:sneakerId', updateSneakerById); diff --git a/src/router/users.ts b/src/router/users.ts new file mode 100644 index 0000000..99c4f37 --- /dev/null +++ b/src/router/users.ts @@ -0,0 +1,29 @@ +import express from 'express'; +import { + createUser, + getUserById, + updateUser, + deleteUserById, + getFavorites, + addFavorite, + removeFavorite +} from '../controllers/users'; + +const usersRouter = express.Router(); + +// GET +usersRouter.get('/:id', getUserById); +usersRouter.get('/:id/favorites', getFavorites); + +// POST +usersRouter.post('/', createUser); +usersRouter.post('/:id/favorites', addFavorite); + +// PUT +usersRouter.put('/:id', updateUser); + +// DELETE +usersRouter.delete('/:id', deleteUserById); +usersRouter.delete('/:id/favorites/:sneakerId', removeFavorite); + +export default usersRouter; diff --git a/src/schemas/provider.ts b/src/schemas/provider.ts new file mode 100644 index 0000000..162f80a --- /dev/null +++ b/src/schemas/provider.ts @@ -0,0 +1,8 @@ +import mongoose from "mongoose"; + +const providerSchema = new mongoose.Schema({ + name: { type: String, required: true }, + contact_email: { type: String, required: true} +}); + +export const Provider = mongoose.model('Provider', providerSchema); diff --git a/src/schemas/review.ts b/src/schemas/review.ts new file mode 100644 index 0000000..5c93218 --- /dev/null +++ b/src/schemas/review.ts @@ -0,0 +1,11 @@ +import mongoose from "mongoose" + +const reviewSchema = new mongoose.Schema({ + sneakerId: { type: mongoose.Schema.Types.ObjectId, ref: 'Sneaker', required: true }, + userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, + rating: { type: Number, min: 0, max: 5, required: true}, + comment: { type: String, minLength: 0, maxLength: 80 }, + date: { type: Date, default: Date.now, required: true } +}) + +export const Review = mongoose.model('Review', reviewSchema); \ No newline at end of file diff --git a/src/schemas/users.ts b/src/schemas/users.ts new file mode 100644 index 0000000..e900b2c --- /dev/null +++ b/src/schemas/users.ts @@ -0,0 +1,9 @@ +import mongoose from "mongoose"; + +const userSchema = new mongoose.Schema({ + name: { type: String, required: true }, + email: { type: String, required: true, unique: true }, + favorites: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Sneaker' }] +}); + +export const User = mongoose.model("User", userSchema);