-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Showing
6 changed files
with
84 additions
and
15 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 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 |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import { QueryService } from "../../Services"; | ||
import { IContext } from "../../Interfaces"; | ||
import { Knex } from "knex"; | ||
import { addSoftDeleteQuery } from "../../Handlers/Helpers"; | ||
|
||
export default async (context: IContext) => { | ||
const { req, model, version, database } = context; | ||
|
||
context.queryParser = new QueryService( | ||
context.model, | ||
context.version.modelList.get(), | ||
context.version.config, | ||
model, | ||
version.modelList.get(), | ||
version.config, | ||
); | ||
|
||
// We should parse URL query string to use as condition in Lucid query | ||
context.conditions = context.queryParser.get(context.req.query); | ||
context.conditions = context.queryParser.get(req.query); | ||
|
||
// Fetching item | ||
context.query = (context.database as Knex).from(context.model.instance.table); | ||
context.query = database.from(model.instance.table); | ||
|
||
// If there is a deletedAtColumn, it means that this table support soft-delete | ||
addSoftDeleteQuery(context.model, context.conditions, context.query); | ||
addSoftDeleteQuery(model, context.conditions, context.query); | ||
}; |
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,33 @@ | ||
import { IContext, IModelService } from "../Interfaces"; | ||
import URLService from "../Services/URLService"; | ||
import { StatusCodes } from "../Enums"; | ||
|
||
const queryByPrimaryKey = async ( | ||
content: IContext, | ||
queryModel: IModelService, | ||
value: any, | ||
) => { | ||
const { database } = content; | ||
return database | ||
.from(queryModel.instance.table) | ||
.where(queryModel.instance.primaryKey, value) | ||
.first(); | ||
}; | ||
|
||
export default async (context: IContext) => { | ||
const { req, res } = context; | ||
|
||
const match = URLService.match(req); | ||
if (match) { | ||
const promises = match.parentPairs.map((pair) => | ||
queryByPrimaryKey(context, pair.model, match.params[pair.paramName]), | ||
); | ||
const results = await Promise.all(promises); | ||
const isMissing = results.some((item) => !item); | ||
if (isMissing) { | ||
res | ||
.status(StatusCodes.NOT_FOUND) | ||
.json({ error: "The resource is not found" }); | ||
} | ||
} | ||
}; |
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