Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/22 #58

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b23d93a
added listing mutation updates
duongaaron Aug 1, 2023
1854f48
added updateListing to typdef
duongaaron Aug 1, 2023
7e5c9db
removed uncessary import
duongaaron Aug 1, 2023
8987f99
Created getUser and getUsers query methods
alelucena Aug 1, 2023
de8235b
Add user query imports
DanielCufino Aug 2, 2023
4a322f4
transferred all components into components folder
duongaaron Aug 1, 2023
98cd834
removed card.js and its import in app.js
duongaaron Aug 1, 2023
b44d88c
removed test.png
duongaaron Aug 1, 2023
87ab41f
fixed import error favoritesIcon card component
duongaaron Aug 1, 2023
6d23468
renamed file names
duongaaron Aug 1, 2023
0b3cc62
renamed files better and removed components
duongaaron Aug 2, 2023
422d297
slight file name change
duongaaron Aug 2, 2023
dd77bd4
added listing mutation updates
duongaaron Aug 1, 2023
cabb908
fixed bug
duongaaron Aug 2, 2023
3680aed
Implemeted getListing and getListings
isauros1000 Aug 2, 2023
0774e8a
added listing mutation updates
duongaaron Aug 1, 2023
2a42043
added listing mutation updates
duongaaron Aug 1, 2023
0a7be7d
merge conflicts fix
duongaaron Aug 2, 2023
a81a4db
merge conflicts
duongaaron Aug 2, 2023
7ab7844
Implemeted getListing and getListings
isauros1000 Aug 2, 2023
035af2f
Typedef conflict resolution
alelucena Aug 1, 2023
9af76e7
Added missing bracket
alelucena Aug 1, 2023
f3c6534
fixed bug backend
duongaaron Aug 1, 2023
14afb39
added listing mutation updates
duongaaron Aug 1, 2023
2a95a91
added listing mutation updates
duongaaron Aug 1, 2023
2e81b31
removed duplicate functions
duongaaron Aug 2, 2023
ec8815a
fix bug
duongaaron Aug 2, 2023
4decac5
Implemeted getListing and getListings
isauros1000 Aug 2, 2023
5072af8
Typedef conflict resolution
alelucena Aug 1, 2023
ccb0467
Added missing bracket
alelucena Aug 1, 2023
708641a
added listing mutation updates
duongaaron Aug 1, 2023
8e29f10
added listing mutation updates
duongaaron Aug 1, 2023
6b40785
fix bug
duongaaron Aug 2, 2023
0138337
Merge branch 'main' into feature/22
duongaaron Aug 2, 2023
7645003
cleaned code
duongaaron Aug 2, 2023
cc35277
deleted comment
duongaaron Aug 2, 2023
d16e6fe
deleted duplicate
duongaaron Aug 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions server/graphql/resolvers/listing.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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');
}
Expand All @@ -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);
}
}
}
}
}

2 changes: 1 addition & 1 deletion server/graphql/typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}

`;