mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
UC38 - gestion images backend fin
This commit is contained in:
parent
ee5e096984
commit
e7c3c84b80
4 changed files with 45 additions and 24 deletions
|
|
@ -77,7 +77,8 @@ describe('Images', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockImagesCollection = {
|
mockImagesCollection = {
|
||||||
insertOne: jest.fn().mockResolvedValue({ insertedId: 'image123' }),
|
insertOne: jest.fn().mockResolvedValue({ insertedId: 'image123' }),
|
||||||
findOne: jest.fn()
|
findOne: jest.fn(),
|
||||||
|
find: jest.fn().mockReturnValue({ sort: jest.fn().mockReturnValue([]) })
|
||||||
};
|
};
|
||||||
|
|
||||||
dbConn = {
|
dbConn = {
|
||||||
|
|
@ -145,5 +146,36 @@ describe('Images', () => {
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getAll', () => {
|
||||||
|
it('should retrieve a paginated list of images', async () => {
|
||||||
|
const mockImages = [
|
||||||
|
{ id: '1', file_name: 'image1.png', file_content: Buffer.from('data1').toString('base64'), mime_type: 'image/png' },
|
||||||
|
{ id: '2', file_name: 'image2.png', file_content: Buffer.from('data2').toString('base64'), mime_type: 'image/png' }
|
||||||
|
];
|
||||||
|
mockImagesCollection.find.mockReturnValue({ sort: jest.fn().mockReturnValue(mockImages) });
|
||||||
|
|
||||||
|
const result = await images.getAll(1, 10);
|
||||||
|
|
||||||
|
expect(db.connect).toHaveBeenCalled();
|
||||||
|
expect(db.getConnection).toHaveBeenCalled();
|
||||||
|
expect(dbConn.collection).toHaveBeenCalledWith('images');
|
||||||
|
expect(mockImagesCollection.find).toHaveBeenCalledWith({});
|
||||||
|
expect(result.length).toEqual(mockImages.length);
|
||||||
|
expect(result).toEqual([
|
||||||
|
{ id: '1', file_name: 'image1.png', file_content: Buffer.from('data1'), mime_type: 'image/png' },
|
||||||
|
{ id: '2', file_name: 'image2.png', file_content: Buffer.from('data2'), mime_type: 'image/png' }
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return null if not images is not found', async () => {
|
||||||
|
mockImagesCollection.find.mockReturnValue({ sort: jest.fn().mockReturnValue(undefined) });
|
||||||
|
const result = await images.getAll(1, 10);
|
||||||
|
expect(db.connect).toHaveBeenCalled();
|
||||||
|
expect(db.getConnection).toHaveBeenCalled();
|
||||||
|
expect(dbConn.collection).toHaveBeenCalledWith('images');
|
||||||
|
expect(mockImagesCollection.find).toHaveBeenCalledWith({});
|
||||||
|
expect(result).toEqual(null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -50,18 +50,19 @@ class ImagesController {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//TODO TEST
|
getImages = async (req, res, next) => {
|
||||||
getAll = async (req, res, next) => {
|
|
||||||
try {
|
try {
|
||||||
|
const page = parseInt(req.query.page) || 1;
|
||||||
|
const limit = parseInt(req.query.limit) || 10;
|
||||||
|
|
||||||
const images = await this.images.getAll();
|
const imagesBit = await this.images.getImages(page, limit);
|
||||||
|
|
||||||
if (!images || images.length === 0) {
|
if (!imagesBit || imagesBit.length === 0) {
|
||||||
throw new AppError(IMAGE_NOT_FOUND);
|
throw new AppError(IMAGE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'application/json');
|
res.setHeader('Content-Type', 'application/json');
|
||||||
return res.status(200).json(imagesName);
|
return res.status(200).json(imagesBit);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return next(error);
|
return next(error);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,38 +42,25 @@ class Images {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO TEST
|
async getImages(page, limit) {
|
||||||
async getAll() {
|
|
||||||
await this.db.connect()
|
await this.db.connect()
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
const imagesCollection = conn.collection('images');
|
const imagesCollection = conn.collection('images');
|
||||||
|
|
||||||
const result = await imagesCollection.find({});
|
const result = await imagesCollection.find({}).sort({created_at: 1});
|
||||||
|
|
||||||
if (!result) return null;
|
if (!result) return null;
|
||||||
|
|
||||||
//TODO latency issues -> images > 20
|
const objImages = result.slice((page - 1) * limit, page * limit).map(image => ({
|
||||||
// USE pagination
|
|
||||||
/*
|
|
||||||
app.get('/images', (req, res) => {
|
|
||||||
const page = parseInt(req.query.page) || 1;
|
|
||||||
const limit = parseInt(req.query.limit) || 10;
|
|
||||||
|
|
||||||
const images = getImagesFromDatabase(page, limit);
|
|
||||||
res.json(images);
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
const imagesName = result.map(image => ({
|
|
||||||
id: image.id,
|
id: image.id,
|
||||||
file_name: image.file_name,
|
file_name: image.file_name,
|
||||||
file_content: Buffer.from(image.file_content, 'base64'),
|
file_content: Buffer.from(image.file_content, 'base64'),
|
||||||
mime_type: image.mime_type
|
mime_type: image.mime_type
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return imagesName;
|
return objImages;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Images;
|
module.exports = Images;
|
||||||
|
|
|
||||||
|
|
@ -12,5 +12,6 @@ const upload = multer({ storage: storage });
|
||||||
|
|
||||||
router.post("/upload", jwt.authenticate, upload.single('image'), asyncHandler(images.upload));
|
router.post("/upload", jwt.authenticate, upload.single('image'), asyncHandler(images.upload));
|
||||||
router.get("/get/:id", asyncHandler(images.get));
|
router.get("/get/:id", asyncHandler(images.get));
|
||||||
|
router.get("/getImages", asyncHandler(images.getImages));
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue