-
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.
- Loading branch information
1 parent
3f15d46
commit abe7716
Showing
9 changed files
with
207 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Schema } from 'mongoose' | ||
import Mongo from '../mongo' | ||
|
||
const schema = new Schema( | ||
{ | ||
name: { | ||
type: String, | ||
index: true, | ||
required: true, | ||
}, | ||
}, | ||
{ | ||
timestamps: { | ||
createdAt: 'created_at', | ||
updatedAt: 'updated_at', | ||
}, | ||
versionKey: false, | ||
} | ||
) | ||
|
||
export default Mongo.Model('categories', schema) |
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,32 @@ | ||
import Http from '../../transport/http/http' | ||
import Logger from '../../pkg/logger' | ||
import Usecase from './usecase/usecase' | ||
import Handler from './delivery/http/handler' | ||
import Repository from './repository/mongo/repository' | ||
|
||
class Category { | ||
private usecase: Usecase | ||
constructor( | ||
private logger: Logger, | ||
) { | ||
const repository = new Repository(logger) | ||
const usecase = new Usecase(logger, repository) | ||
this.usecase = usecase | ||
} | ||
|
||
public loadHttp(http: Http) { | ||
const handler = new Handler(this.logger, http, this.usecase) | ||
this.httpPrivate(http, handler) | ||
} | ||
|
||
private httpPrivate(http: Http, handler: Handler) { | ||
const Router = http.Router() | ||
|
||
Router.post('/', handler.Store()) | ||
Router.get('/', handler.Fetch()) | ||
|
||
http.SetRouter('/v1/categories', Router) | ||
} | ||
} | ||
|
||
export default Category |
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,58 @@ | ||
import Http from '../../../../transport/http/http' | ||
import Logger from '../../../../pkg/logger' | ||
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' | ||
|
||
class Handler { | ||
constructor( | ||
private logger: Logger, | ||
private http: Http, | ||
private usecase: Usecase | ||
) {} | ||
|
||
public Fetch() { | ||
return async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const request = GetRequestParams(req.query) | ||
const { data, count } = await this.usecase.Fetch(request) | ||
this.logger.Info(statusCode[statusCode.OK], { | ||
additional_info: this.http.AdditionalInfo( | ||
req, | ||
statusCode.OK | ||
), | ||
}) | ||
|
||
return res.json({ data, meta: GetMeta(request, count) }) | ||
} catch (error) { | ||
return next(error) | ||
} | ||
} | ||
} | ||
|
||
public Store() { | ||
return async (req: any, res: Response, next: NextFunction) => { | ||
try { | ||
const value = ValidateFormRequest(Store, req.body) | ||
const result = await this.usecase.Store(value) | ||
this.logger.Info(statusCode[statusCode.CREATED], { | ||
additional_info: this.http.AdditionalInfo( | ||
req, | ||
statusCode.CREATED | ||
), | ||
}) | ||
|
||
return res | ||
.status(statusCode.CREATED) | ||
.json({ data: result, message: 'CREATED' }) | ||
} catch (error) { | ||
return next(error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default Handler |
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,3 @@ | ||
export interface Store { | ||
name: string | ||
} |
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,6 @@ | ||
import Joi from 'joi' | ||
import { RegexSubdomain } from '../../../helpers/regex' | ||
|
||
export const Store = Joi.object({ | ||
name: Joi.string().regex(RegexSubdomain).required(), | ||
}) |
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,60 @@ | ||
import Logger from '../../../../pkg/logger' | ||
import { RequestParams } from '../../../../helpers/requestParams' | ||
import categorySchema from '../../../../database/mongo/schemas/category.schema' | ||
import { Store } from '../../entity/interface' | ||
|
||
class Repository { | ||
constructor(private logger: Logger) {} | ||
|
||
public async Fetch({ | ||
offset, | ||
limit, | ||
keyword, | ||
sort_order, | ||
sort_by, | ||
}: RequestParams) { | ||
const filter = {} | ||
const sort = {} | ||
|
||
if (keyword) | ||
Object.assign(filter, { | ||
name: { | ||
$regex: new RegExp(keyword, 'i'), | ||
}, | ||
}) | ||
|
||
if (['created_at', 'name'].includes(sort_by)) { | ||
Object.assign(sort, { | ||
[sort_by]: sort_order, | ||
}) | ||
} | ||
|
||
const data = await categorySchema | ||
.find(filter) | ||
.sort(sort) | ||
.skip(offset) | ||
.limit(limit) | ||
const count = await categorySchema.find(filter).count() | ||
|
||
return { | ||
data, | ||
count, | ||
} | ||
} | ||
|
||
public async Store(body: Store) { | ||
const schemaNew = new categorySchema(body) | ||
|
||
return schemaNew.save() | ||
} | ||
|
||
public async FindUnique(name: string) { | ||
const result = await categorySchema.findOne({ | ||
name, | ||
}) | ||
|
||
return result | ||
} | ||
} | ||
|
||
export default Repository |
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,25 @@ | ||
import { RequestParams } from '../../../helpers/requestParams' | ||
import error from '../../../pkg/error' | ||
import Logger from '../../../pkg/logger' | ||
import statusCode from '../../../pkg/statusCode' | ||
import { Store } from '../entity/interface' | ||
import Repository from '../repository/mongo/repository' | ||
|
||
class Usecase { | ||
constructor(private logger: Logger, private repository: Repository) {} | ||
|
||
public async Fetch(request: RequestParams) { | ||
return this.repository.Fetch(request) | ||
} | ||
|
||
public async Store(body: Store) { | ||
const item = await this.repository.FindUnique(body.name) | ||
if (item) | ||
throw new error(statusCode.BAD_REQUEST, 'Category Already Exist') | ||
const result = await this.repository.Store(body) | ||
|
||
return result | ||
} | ||
} | ||
|
||
export default Usecase |
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