Skip to content

Commit

Permalink
write logic, handler and tests in api for edit resource b00tc4mp#484
Browse files Browse the repository at this point in the history
  • Loading branch information
juditta99 committed May 22, 2024
1 parent 1baba0e commit 3b16a29
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 0 deletions.
29 changes: 29 additions & 0 deletions staff/judy-grayland/project/api/handlers/editResourceHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logic from '../logic/index.js'
import { errors } from 'shared'
const { NotFoundError, ContentError } = errors

export default (req, res) => {
const { id, newData } = req.body
try {
logic
.editResource(id, newData)
.then(() => res.status(200).send())
.catch((error) => {
let status = 500
if (error instanceof NotFoundError) {
status = 404
}
res
.status(status)
.json({ error: error.constructor.name, message: error.message })
})
} catch (error) {
let status = 500
if (error instanceof ContentError || error instanceof TypeError) {
status = 406
res
.status(status)
.json({ error: error.constructor.name, message: error.message })
}
}
}
2 changes: 2 additions & 0 deletions staff/judy-grayland/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import authenticateUserHandler from './authenticateUserHandler.js'
import createResourceHandler from './createResourceHandler.js'
import retrieveResourcesHandler from './retrieveResourcesHandler.js'
import deleteResourceHandler from './deleteResourceHandler.js'
import editResourceHandler from './editResourceHandler.js'

export {
registerUserHandler,
authenticateUserHandler,
createResourceHandler,
retrieveResourcesHandler,
deleteResourceHandler,
editResourceHandler,
}
3 changes: 3 additions & 0 deletions staff/judy-grayland/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createResourceHandler,
retrieveResourcesHandler,
deleteResourceHandler,
editResourceHandler,
} from './handlers/index.js'

dotenv.config()
Expand Down Expand Up @@ -38,6 +39,8 @@ mongoose
// the :id is dynamic. we pass the id as params to specify which resource needs to be deleted
server.delete('/resources/:id', deleteResourceHandler)

server.patch('/resources/:id', jsonBodyParser, editResourceHandler)

server.listen(process.env.PORT, () => {
console.log(`Server is running on port ${process.env.PORT}`)
})
Expand Down
25 changes: 25 additions & 0 deletions staff/judy-grayland/project/api/logic/editResource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { errors } from 'shared'
import { Resource } from '../data/models.js'
const { SystemError, NotFoundError } = errors

function editResource(resourceId, newData) {
return Resource.findOne({ _id: resourceId })
.catch((error) => {
throw new SystemError(error.message)
})
.then((resource) => {
if (!resource) {
throw new NotFoundError(`Resource with id ${resourceId} not found`)
}

//Object.assign is a JS method used to update an object with new values. In the context of updating a resource (resource), it helps merge the new data (newData) into the existing resource object, ensuring only the provided fields are updated while keeping the rest of the resource intact.
Object.assign(resource, newData)

// .save() is a method provided by Mongoose to save a document to the db.
return resource.save().catch((error) => {
throw new SystemError(error.message)
})
})
}

export default editResource
147 changes: 147 additions & 0 deletions staff/judy-grayland/project/api/logic/editResource.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import dotenv from 'dotenv'
import mongoose from 'mongoose'
import { expect } from 'chai'
import random from './helpers/random.js'
import { errors } from 'shared'
const { NotFoundError, ContentError } = errors

import { Resource } from '../data/models.js'
import editResource from './editResource.js'

dotenv.config()

describe('edit resource', () => {
before(() => mongoose.connect(process.env.MONGODB_URL_TEST))
// beforeEach(() => Resource.deleteMany())

// HAPPY path - book
it('succeeds on book being correctly edited', () => {
const initialData = {
title: random.title(),
description: random.description(),
resourceType: 'book',
topic: ['lgbt+', 'diversidad funcional'],
image: random.image(),
author: 'Roald Dahl',
}

const newData = {
title: 'Bee Bop Bops',
author: 'Billy Bee Boo',
}
return Resource.create(initialData).then((resource) => {
return editResource(resource._id, newData)
.then(() => Resource.findOne(resource._id))
.then((updatedResource) => {
expect(updatedResource.title).to.equal(newData.title)
expect(updatedResource.description).to.equal(initialData.description)
expect(updatedResource.resourceType).to.equal(
initialData.resourceType
)
expect(updatedResource.topic).to.deep.equal(initialData.topic)
expect(updatedResource.image).to.equal(initialData.image)
expect(updatedResource.author).to.equal(newData.author)
})
})
})

// // HAPPY path - activity
// it('succeeds on new activity being correctly created', () => {
// const title = random.title()
// const description = random.description()
// const resourceType = 'activity'
// const topic = ['igualdad de genero']
// const link = random.link()
// const image = random.image()
// const author = ''
// const ageRange = ''

// return createResource({
// title,
// description,
// resourceType,
// topic,
// link,
// image,
// author,
// ageRange,
// }).then((value) => {
// expect(value).to.be.undefined
// return Resource.findOne({ title: title }).then((resource) => {
// expect(resource.description).to.equal(description)
// expect(resource.link).to.equal(link)
// expect(resource.resourceType).to.equal(resourceType)
// expect(resource.topic).to.deep.equal(topic)
// expect(resource.image).to.equal(image)
// expect(resource.author).to.equal('')
// expect(resource.ageRange).to.deep.equal([''])
// })
// })
// })
// })

// // UNHAPPY path - activity on missing required field for all resources
// it('fails on missing resourceType', async () => {
// const title = random.title()
// const description = random.description()
// const resourceType = ''
// const topic = ['bullying']
// const link = random.link()
// const image = random.image()
// const author = ''
// const ageRange = ''

// await expect(() => {
// createResource({
// title,
// description,
// resourceType,
// topic,
// link,
// image,
// author,
// ageRange,
// }).to.throw(ContentError, 'resourceType is empty')
// })
// })

// // UNHAPPY path - book on already existing resources
// it('fails on already existing resource', () => {
// const title = random.title()
// const description = random.description()
// const resourceType = 'activity'
// const topic = ['diversidad cultural', 'diversidad funcional']
// const link = random.link()
// const image = random.image()
// const author = ''
// const ageRange = ''

// return Resource.create({
// title,
// description,
// resourceType,
// topic,
// link,
// image,
// author,
// ageRange,
// }).then((resource) => {
// return createResource({
// title,
// description,
// resourceType,
// topic,
// link,
// image,
// author,
// ageRange,
// })
// .then(() => {
// throw new Error('should not reach this point')
// })
// .catch((error) => {
// expect(error).to.be.instanceOf(DuplicityError)
// expect(error.message).to.equal('resource already exists')
// })
// })
})
25 changes: 25 additions & 0 deletions staff/judy-grayland/project/api/logic/editResource.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import dotenv from 'dotenv'
import mongoose from 'mongoose'
import editResource from './editResource.js'

dotenv.config()

mongoose
.connect(process.env.MONGODB_URL_TEST)
.then(() => {
try {
editResource('6645f479c04e170db038d1bc', {
title: 'Libro 4000',
description: 'Libro 2000',
})
.then(() => {
console.log('resource edited successfully')
})
.catch((error) => console.error(error))
} catch (error) {
console.error(error)
}
})
.catch((error) => {
console.error(error)
})
2 changes: 2 additions & 0 deletions staff/judy-grayland/project/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import authenticateUser from './authenticateUser.js'
import createResource from './createResource.js'
import retrieveResources from './retrieveResources.js'
import deleteResource from './deleteResource.js'
import editResource from './editResource.js'

const logic = {
registerUser,
authenticateUser,
createResource,
retrieveResources,
deleteResource,
editResource,
}

export default logic
15 changes: 15 additions & 0 deletions staff/judy-grayland/project/api/test/edit-resource.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
TEST 'edit-resource'

CASE 'passes for resource edited'

curl 'http://localhost:9000/resources/6645f479c04e170db038d1bc' \
-H 'Content-Type: application/json' \
-X PATCH \
-d '{ "id":"6645f479c04e170db038d1bc",
"newData": {
"title": "Libro 6000",
"description": "Libro description"
}
}' \


0 comments on commit 3b16a29

Please sign in to comment.