-
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.
feat: species information popup (#29)
- Loading branch information
1 parent
e298a80
commit cf1d61b
Showing
23 changed files
with
667 additions
and
97 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 was deleted.
Oops, something went wrong.
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,68 @@ | ||
jest.mock('../middlewares/authMiddleware', () => ({ | ||
isAuthenticated: ( | ||
req: express.Request, | ||
res: express.Response, | ||
next: express.NextFunction, | ||
) => { | ||
return next(); | ||
}, | ||
})); | ||
import { Species } from '../models/species'; | ||
import request from 'supertest'; | ||
import express from 'express'; | ||
import species from '../routes/species'; | ||
import mongoose from 'mongoose'; | ||
|
||
jest.mock('../models/species'); | ||
|
||
const app = express(); | ||
const router = express.Router(); | ||
species(router); | ||
|
||
app.use(router); | ||
const speciesMock = Species.findById as jest.Mock; | ||
const speciesFindMock = Species.findOne as jest.Mock; | ||
|
||
describe('GET /species/id', () => { | ||
beforeEach(() => { | ||
speciesMock.mockReset(); | ||
}); | ||
|
||
it('No species with this id', async () => { | ||
const randomObjectId = new mongoose.Types.ObjectId().toString(); | ||
const query = `/species/${randomObjectId}`; | ||
const res = await request(app).get(query); | ||
expect(res.status).toBe(404); | ||
}); | ||
|
||
it('Gets a specific species id', async () => { | ||
const id = new mongoose.Types.ObjectId().toString(); | ||
const species = { _id: id, scientificName: 'Scorpaena brachion' }; | ||
speciesMock.mockResolvedValue(species); | ||
const res = await request(app).get(`/species/id/${id}`); | ||
expect(res.status).toBe(200); | ||
expect(res.body).toEqual(species); | ||
}); | ||
}); | ||
|
||
describe('GET /species/scientific/id', () => { | ||
beforeEach(() => { | ||
speciesFindMock.mockReset(); | ||
}); | ||
|
||
it('No species with this scientific name', async () => { | ||
const query = `/species/scientific/doesntexist"`; | ||
const res = await request(app).get(query); | ||
expect(res.status).toBe(404); | ||
}); | ||
|
||
it('Gets a specific species by scientific name', async () => { | ||
const id = new mongoose.Types.ObjectId().toString(); | ||
const scientificName = 'Scorpaena brachion'; | ||
const species = { _id: id, scientificName: scientificName }; | ||
speciesFindMock.mockResolvedValue(species); | ||
const res = await request(app).get(`/species/scientific/${scientificName}`); | ||
expect(res.status).toBe(200); | ||
expect(res.body).toEqual(species); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,19 @@ | ||
import express from 'express'; | ||
import { Species } from '../../models/species'; | ||
|
||
export const getById = async (req: express.Request, res: express.Response) => { | ||
const id = req.params.id; | ||
const species = await Species.findById(id); | ||
if (!species) return res.status(404).json({ message: 'Species not found' }); | ||
return res.status(200).json(species); | ||
}; | ||
|
||
export const getByScientificName = async ( | ||
req: express.Request, | ||
res: express.Response, | ||
) => { | ||
const scientificName = req.params.scientificName; | ||
const species = await Species.findOne({ scientificName: scientificName }); | ||
if (!species) return res.status(404).json({ message: 'Species not found' }); | ||
return res.status(200).json(species); | ||
}; |
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
const SpeciesSchema = new mongoose.Schema({ | ||
aphiaId: { type: String, required: true }, | ||
articleUrl: { type: String }, | ||
articleTitle: { type: String }, | ||
commonNames: [String], | ||
scientificName: { type: String }, | ||
introduction: { type: String }, | ||
imageUrls: [String], | ||
}); | ||
|
||
export const Species = mongoose.model('Species', SpeciesSchema); |
This file was deleted.
Oops, something went wrong.
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,51 @@ | ||
import express from 'express'; | ||
import { isAuthenticated } from '../middlewares/authMiddleware'; | ||
import { getById, getByScientificName } from '../controllers/species/get'; | ||
|
||
/** | ||
* @swagger | ||
* /species/id/{id}: | ||
* get: | ||
* summary: Get species by ID | ||
* description: Retrieve a species by its unique ID. | ||
* parameters: | ||
* - in: path | ||
* name: id | ||
* required: true | ||
* description: ID of the species to retrieve | ||
* schema: | ||
* type: string | ||
* responses: | ||
* 200: | ||
* description: Successfully retrieved species | ||
* 401: | ||
* description: Unauthorized | ||
* 404: | ||
* description: species not found | ||
* /species/scientifc/{scientificName}: | ||
* get: | ||
* summary: Get species by scientificName | ||
* description: Retrieve a species by its unique scientificName. | ||
* parameters: | ||
* - in: path | ||
* name: scientificName | ||
* required: true | ||
* description: scientificName of the species to retrieve | ||
* schema: | ||
* type: string | ||
* responses: | ||
* 200: | ||
* description: Successfully retrieved species | ||
* 401: | ||
* description: Unauthorized | ||
* 404: | ||
* description: species not found | ||
*/ | ||
export default (router: express.Router) => { | ||
router.get('/species/id/:id', isAuthenticated, getById); | ||
router.get( | ||
'/species/scientific/:scientificName', | ||
isAuthenticated, | ||
getByScientificName, | ||
); | ||
}; |
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
Oops, something went wrong.