diff --git a/server/graphql/resolvers/listing.js b/server/graphql/resolvers/listing.js index b630f22..41289b4 100644 --- a/server/graphql/resolvers/listing.js +++ b/server/graphql/resolvers/listing.js @@ -1,7 +1,20 @@ const { AuthenticationError } = require('apollo-server'); - const Listing = require('../../models/Listing'); +// Checks if str is in a valid US currency format +function isValid_Currency(str) { + // regex to check valid US currency + let regex = new RegExp(/^\$(\d{1, 3}(\, \d{3})*|(\d+))(\.\d{2})?$/); + + // returns true if the str matched the ReGex + if (regex.test(str) == true) { + return true; + } + else { + return false; + } +} + module.exports = { Query: { async getListings() { @@ -31,7 +44,7 @@ module.exports = { Mutation: { createListing: async (_, { title, description, price, active, pictures, pickup, category }, context) => { - // Check for empty strings + // Check for empty strings if (title.trim() === '') { throw new Error('Title must not b empty'); } @@ -52,19 +65,42 @@ module.exports = { return listing; }, - }, -}; - -// Checks if str is in a valid US currency format -function isValid_Currency(str) { - // regex to check valid US currency - let regex = new RegExp(/^\$(\d{1, 3}(\, \d{3})*|(\d+))(\.\d{2})?$/); - - // returns true if the str matched the ReGex - if (regex.test(str) == true) { - return true; - } - else { - return false; + updateListing: async (_, { listingId, title, description, price, active, pictures, pickup, category }, context) => { + // Create an empty updates object + let updates = {}; + + // Only add fields to the updates object if they are not undefined (i.e., provided in the mutation arguments) + if (title !== undefined) updates.title = title; + if (description !== undefined) updates.description = description; + if (price !== undefined) { + if (!isValid_Currency(price)) { + throw new Error("Price is not in a valid format, don't forget the $!"); + } + updates.price = price; + } + if (active !== undefined) updates.active = active; + if (pictures !== undefined) updates.pictures = pictures; + if (pickup !== undefined) updates.pickup = pickup; + if (category !== undefined) updates.category = category; + + try { + // Use the updates object to update the MongoDB document. + // Fields not included in the updates object will not be affected. + const updatedListing = await Listing.findByIdAndUpdate( + listingId, + updates, + { new: true } + ); + + if (!updatedListing) { + throw new Error('Listing not found'); + } + + return updatedListing; + } catch (err) { + throw new Error(err); + } } -} + } + } + diff --git a/server/graphql/typedefs.js b/server/graphql/typedefs.js index 3b57c12..0ca0cef 100644 --- a/server/graphql/typedefs.js +++ b/server/graphql/typedefs.js @@ -38,8 +38,8 @@ module.exports = gql` createDog(name: String!): Dog! deleteDog(dogId: ID!): String! createListing(title: String!, description: String!, price: String!, active: Boolean!, pictures: [ID], pickup: Boolean!, category: String): Listing! + updateListing(listingId: ID!, title: String, description: String, price: String, active: Boolean, pictures: [ID], pickup: Boolean, category: String): Listing! createUser(netID: String!, firstName: String!, middleInitial: String, lastName: String!, password: String!, email: String!, payment: String!, college: String!): User! deleteUser(userId: ID!): String! } - `;