-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #676 from filipedeschamps/parent-root-content
Mudança de comportamento no `/root` e exposição do `/parent`
- Loading branch information
Showing
8 changed files
with
762 additions
and
61 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
70 changes: 70 additions & 0 deletions
70
pages/api/v1/contents/[username]/[slug]/parent/index.public.js
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,70 @@ | ||
import nextConnect from 'next-connect'; | ||
import controller from 'models/controller.js'; | ||
import user from 'models/user.js'; | ||
import authorization from 'models/authorization.js'; | ||
import validator from 'models/validator.js'; | ||
import content from 'models/content.js'; | ||
import { NotFoundError } from 'errors/index.js'; | ||
|
||
export default nextConnect({ | ||
attachParams: true, | ||
onNoMatch: controller.onNoMatchHandler, | ||
onError: controller.onErrorHandler, | ||
}) | ||
.use(controller.injectRequestMetadata) | ||
.use(controller.logRequest) | ||
.get(getValidationHandler, getHandler); | ||
|
||
function getValidationHandler(request, response, next) { | ||
const cleanValues = validator(request.query, { | ||
username: 'required', | ||
slug: 'required', | ||
}); | ||
|
||
request.query = cleanValues; | ||
|
||
next(); | ||
} | ||
|
||
async function getHandler(request, response) { | ||
const userTryingToGet = user.createAnonymous(); | ||
|
||
const contentFound = await content.findOne({ | ||
where: { | ||
owner_username: request.query.username, | ||
slug: request.query.slug, | ||
status: 'published', | ||
}, | ||
}); | ||
|
||
if (!contentFound) { | ||
throw new NotFoundError({ | ||
message: `O conteúdo informado não foi encontrado no sistema.`, | ||
action: 'Verifique se o "slug" está digitado corretamente.', | ||
stack: new Error().stack, | ||
errorLocationCode: 'CONTROLLER:CONTENT:PARENT:GET_HANDLER:SLUG_NOT_FOUND', | ||
key: 'slug', | ||
}); | ||
} | ||
|
||
if (contentFound && !contentFound.parent_id) { | ||
throw new NotFoundError({ | ||
message: `O conteúdo requisitado é um conteúdo raiz.`, | ||
action: | ||
'Busque apenas por conteúdos com "parent_id", pois este conteúdo não possui níveis superiores na árvore de conteúdos.', | ||
stack: new Error().stack, | ||
errorLocationCode: 'CONTROLLER:CONTENT:PARENT:GET_HANDLER:ALREADY_ROOT', | ||
key: 'parent_id', | ||
}); | ||
} | ||
|
||
const parentContentFound = await content.findOne({ | ||
where: { | ||
id: contentFound.parent_id, | ||
}, | ||
}); | ||
|
||
const secureParentContent = authorization.filterOutput(userTryingToGet, 'read:content', parentContentFound); | ||
|
||
return response.status(200).json(secureParentContent); | ||
} |
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
Oops, something went wrong.
47dc3d8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
tabnews – ./
tabnews-git-main-tabnews.vercel.app
www.tabnews.com.br
tabnews-tabnews.vercel.app
tabnews.com.br