forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
write logic, handler and tests in api for edit resource b00tc4mp#484
- Loading branch information
Showing
8 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
staff/judy-grayland/project/api/handlers/editResourceHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
147
staff/judy-grayland/project/api/logic/editResource.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
staff/judy-grayland/project/api/logic/editResource.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
staff/judy-grayland/project/api/test/edit-resource.test.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
}' \ | ||
|
||
|