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.
add logic and tests in API for retrieving all posts b00tc4mp#482
- Loading branch information
Showing
11 changed files
with
155 additions
and
15 deletions.
There are no files selected for viewing
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
32 changes: 32 additions & 0 deletions
32
staff/judy-grayland/project/api/handlers/retrieveResourcesHandler.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,32 @@ | ||
import logic from '../logic/index.js' | ||
import { errors } from 'shared' | ||
const { NotFoundError } = errors | ||
|
||
export default (req, res) => { | ||
try { | ||
logic | ||
.retrieveResources() | ||
.then((resources) => res.json(resources)) | ||
.catch((error) => { | ||
let status = 500 | ||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} | ||
res.status(status).json({ | ||
error: error.constructor.name, | ||
message: error.constructor.message, | ||
}) | ||
return | ||
}) | ||
} 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
18 changes: 18 additions & 0 deletions
18
staff/judy-grayland/project/api/logic/retrieveResources.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,18 @@ | ||
import { errors } from 'shared' | ||
const { SystemError } = errors | ||
|
||
import { Resource } from '../data/models.js' | ||
|
||
function retrieveResources() { | ||
return Resource.find() | ||
.select('-__v') | ||
.lean() | ||
.catch((error) => { | ||
throw new SystemError(error.message) | ||
}) | ||
.then((resources) => { | ||
return resources | ||
}) | ||
} | ||
|
||
export default retrieveResources |
17 changes: 17 additions & 0 deletions
17
staff/judy-grayland/project/api/logic/retrieveResources.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,17 @@ | ||
import dotenv from 'dotenv' | ||
import mongoose from 'mongoose' | ||
import { expect } from 'chai' | ||
|
||
import retrieveResources from './retrieveResources.js' | ||
|
||
dotenv.config() | ||
|
||
describe('retrieveResources', () => { | ||
before(() => mongoose.connect(process.env.MONGODB_URL_TEST)) | ||
|
||
it('succeeds', () => { | ||
return retrieveResources().then(() => { | ||
expect() | ||
}) | ||
}) | ||
}) |
17 changes: 17 additions & 0 deletions
17
staff/judy-grayland/project/api/logic/retrieveResources.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,17 @@ | ||
import dotenv from 'dotenv' | ||
import mongoose from 'mongoose' | ||
import retrieveResources from './retrieveResources.js' | ||
|
||
dotenv.config() | ||
|
||
mongoose.connect(process.env.MONGODB_URL_TEST).then(() => { | ||
try { | ||
retrieveResources() | ||
.then(() => { | ||
console.log('resources retrieved correctly') | ||
}) | ||
.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
7 changes: 7 additions & 0 deletions
7
staff/judy-grayland/project/api/test/retrieve-resources.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,7 @@ | ||
TEST 'retrieve-resources' | ||
|
||
CASE 'resources retrieved correctly' | ||
|
||
curl 'http://localhost:9000/resources' \ | ||
|
||
|