Skip to content

Commit

Permalink
feat: add endpoint for search existing
Browse files Browse the repository at this point in the history
  • Loading branch information
ayocodingit committed Aug 23, 2024
1 parent 31a3ed1 commit 0dcf686
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
27 changes: 25 additions & 2 deletions src/modules/images/delivery/http/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import Usecase from '../../usecase/usecase'
import { NextFunction, Request, Response } from 'express'
import statusCode from '../../../../pkg/statusCode'
import { GetMeta, GetRequestParams } from '../../../../helpers/requestParams'
import { ValidateFormRequest } from '../../../../helpers/validate'
import { Store } from '../../entity/schema'
import {
ValidateFormRequest,
ValidateParams,
} from '../../../../helpers/validate'
import { Search, Store } from '../../entity/schema'
import { unlinkSync } from 'fs'
import error from '../../../../pkg/error'

class Handler {
constructor(
Expand Down Expand Up @@ -70,6 +74,25 @@ class Handler {
}
}
}

public Search() {
return async (req: any, res: Response, next: NextFunction) => {
try {
const value = ValidateFormRequest(Search, req.query)
const isExist = await this.usecase.Search(value)
this.logger.Info(statusCode[statusCode.OK], {
additional_info: this.http.AdditionalInfo(
req,
statusCode.OK
),
})
const message = isExist ? 'Available' : 'Not Available'
return res.status(statusCode.OK).json({ message })
} catch (error) {
return next(error)
}
}
}
}

export default Handler
5 changes: 5 additions & 0 deletions src/modules/images/entity/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ export interface File {
filename: string
uri?: string
}

export type Search = {
category: string
filename: string
}
5 changes: 5 additions & 0 deletions src/modules/images/entity/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ export const Store = Joi.object({
}),
file,
})

export const Search = Joi.object({
category: Joi.string().alphanum().required(),
filename: Joi.string().regex(RegexSanitize).required(),
})
1 change: 1 addition & 0 deletions src/modules/images/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Images {
const Router = this.http.Router()

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

this.http.SetRouter('/v1/images', Router)
}
Expand Down
11 changes: 10 additions & 1 deletion src/modules/images/usecase/usecase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'fs'
import { RequestParams } from '../../../helpers/requestParams'
import Logger from '../../../pkg/logger'
import { File, Store } from '../entity/interface'
import { File, Search, Store } from '../entity/interface'
import Repository from '../repository/mongo/repository'
import S3 from '../../../external/s3'
import { CustomPathFile } from '../../../helpers/file'
Expand All @@ -11,6 +11,8 @@ import {
RegexExtensionImage,
} from '../../../helpers/regex'
import FileGenerator from '../../../external/fileGenerator'
import statusCode from '../../../pkg/statusCode'
import error from '../../../pkg/error'

class Usecase {
constructor(
Expand Down Expand Up @@ -93,6 +95,13 @@ class Usecase {
this.logger.Error(error.message)
}
}

public async Search({ filename, category }: Search) {
const path = `${category}/${filename}`
const result = await this.repository.FindByPath(path)

return !!result
}
}

export default Usecase

0 comments on commit 0dcf686

Please sign in to comment.