Skip to content

Commit 0dcf686

Browse files
committed
feat: add endpoint for search existing
1 parent 31a3ed1 commit 0dcf686

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

src/modules/images/delivery/http/handler.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import Usecase from '../../usecase/usecase'
44
import { NextFunction, Request, Response } from 'express'
55
import statusCode from '../../../../pkg/statusCode'
66
import { GetMeta, GetRequestParams } from '../../../../helpers/requestParams'
7-
import { ValidateFormRequest } from '../../../../helpers/validate'
8-
import { Store } from '../../entity/schema'
7+
import {
8+
ValidateFormRequest,
9+
ValidateParams,
10+
} from '../../../../helpers/validate'
11+
import { Search, Store } from '../../entity/schema'
912
import { unlinkSync } from 'fs'
13+
import error from '../../../../pkg/error'
1014

1115
class Handler {
1216
constructor(
@@ -70,6 +74,25 @@ class Handler {
7074
}
7175
}
7276
}
77+
78+
public Search() {
79+
return async (req: any, res: Response, next: NextFunction) => {
80+
try {
81+
const value = ValidateFormRequest(Search, req.query)
82+
const isExist = await this.usecase.Search(value)
83+
this.logger.Info(statusCode[statusCode.OK], {
84+
additional_info: this.http.AdditionalInfo(
85+
req,
86+
statusCode.OK
87+
),
88+
})
89+
const message = isExist ? 'Available' : 'Not Available'
90+
return res.status(statusCode.OK).json({ message })
91+
} catch (error) {
92+
return next(error)
93+
}
94+
}
95+
}
7396
}
7497

7598
export default Handler

src/modules/images/entity/interface.ts

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ export interface File {
1515
filename: string
1616
uri?: string
1717
}
18+
19+
export type Search = {
20+
category: string
21+
filename: string
22+
}

src/modules/images/entity/schema.ts

+5
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ export const Store = Joi.object({
2929
}),
3030
file,
3131
})
32+
33+
export const Search = Joi.object({
34+
category: Joi.string().alphanum().required(),
35+
filename: Joi.string().regex(RegexSanitize).required(),
36+
})

src/modules/images/images.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Images {
3838
const Router = this.http.Router()
3939

4040
Router.post('/', this.http.Upload('file'), handler.Store())
41+
Router.get('/', handler.Search())
4142

4243
this.http.SetRouter('/v1/images', Router)
4344
}

src/modules/images/usecase/usecase.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readFileSync } from 'fs'
22
import { RequestParams } from '../../../helpers/requestParams'
33
import Logger from '../../../pkg/logger'
4-
import { File, Store } from '../entity/interface'
4+
import { File, Search, Store } from '../entity/interface'
55
import Repository from '../repository/mongo/repository'
66
import S3 from '../../../external/s3'
77
import { CustomPathFile } from '../../../helpers/file'
@@ -11,6 +11,8 @@ import {
1111
RegexExtensionImage,
1212
} from '../../../helpers/regex'
1313
import FileGenerator from '../../../external/fileGenerator'
14+
import statusCode from '../../../pkg/statusCode'
15+
import error from '../../../pkg/error'
1416

1517
class Usecase {
1618
constructor(
@@ -93,6 +95,13 @@ class Usecase {
9395
this.logger.Error(error.message)
9496
}
9597
}
98+
99+
public async Search({ filename, category }: Search) {
100+
const path = `${category}/${filename}`
101+
const result = await this.repository.FindByPath(path)
102+
103+
return !!result
104+
}
96105
}
97106

98107
export default Usecase

0 commit comments

Comments
 (0)