-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/cachaca #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ViniciusTei
wants to merge
10
commits into
main
Choose a base branch
from
feature/cachaca
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c7c281b
cachaca introducao e modificacao em morador
LTaka b64aa53
Merge remote-tracking branch 'origin/feat/crud-moradores' into featur…
LTaka cd48137
add morador ao rank. modificação no service morador.
LTaka ca892eb
change controller cachca
LTaka ca4b563
change morador e cachaca
LTaka ccd43f6
fix find all oficial moradodores
49f886b
feat add cachaca dto
298dccb
arrumando
LTaka e5a1e2a
Merge branch 'feature/cachaca' of github.com:AbatCaverna/abatcaverna-…
4ce0839
add, show e updatede criados em cachaca e moradores
LTaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,87 @@ | ||
| import { Request, Response } from 'express' | ||
| import { z } from 'zod' | ||
|
|
||
| import cachacaService from '../service/cachacaService' | ||
| import { ObjectId } from 'mongodb' | ||
|
|
||
| const CachacaController = { | ||
| async createRank(req: Request, res: Response) { | ||
| try { | ||
| const body: ChachacaDTO = AnoSchema.parse(req.body) | ||
|
|
||
| const response = await cachacaService.addMoradorAoRank(body.ano) | ||
|
|
||
| return res.send({ | ||
| message: 'Sucesso', | ||
| rank: response | ||
| }) | ||
|
|
||
| } catch (error) { | ||
|
|
||
| console.error(`Error[SERVER](${new Date().toDateString()}): Server error!`, error) | ||
|
|
||
| return res.status(500).json({ message: 'Something went wrong with server', error }) | ||
| } | ||
| }, | ||
| async index( req: Request, res: Response){ | ||
| try { | ||
| const body: ChachacaDTO = AnoSchema.parse(req.body) | ||
| const response = await cachacaService.showRank(body.ano) | ||
|
|
||
| return res.send({ | ||
| message: 'Sucesso', | ||
| rank: response | ||
| }) | ||
|
|
||
| } catch (error) { | ||
| console.error(`Error[SERVER](${new Date().toDateString()}): Server error!`, error) | ||
| return res.status(500).json({ message: 'Something went wrong with server', error }) | ||
| } | ||
| }, | ||
| async addCachaca( req: Request, res: Response){ | ||
| try { | ||
| const { moradorId } = req.query | ||
| const morador = MoradorIdSchema.parse(moradorId) | ||
| const id = new ObjectId(morador) | ||
| const response = await cachacaService.addCachacaMorador(id) | ||
|
|
||
| return res.send({ | ||
| message: 'Sucesso', | ||
| rank: response | ||
| }) | ||
|
|
||
| } catch (error) { | ||
| console.error(`Error[SERVER](${new Date().toDateString()}): Server error!`, error) | ||
| return res.status(500).json({ message: 'Something went wrong with server', error }) | ||
| } | ||
| }, | ||
|
|
||
| } | ||
|
|
||
| const MoradorIdSchema = z.string().refine((data) => { | ||
| // Check if the data is a string of 12 bytes | ||
| if (/^[0-9a-fA-F]{24}$/.test(data)) { | ||
| return true | ||
| } | ||
|
|
||
| // Check if the data is a string of 24 hex characters | ||
| if (/^[0-9a-fA-F]{24}$/.test(data)) { | ||
| return true | ||
| } | ||
|
|
||
| // Check if the data is an integer | ||
| if (!isNaN(+data) && Number.isInteger(+data)) { | ||
| return true | ||
| } | ||
|
|
||
| return false | ||
| }, { | ||
| message: 'Must be a ObjectId. A string of 12 bytes, a string of 24 hex characters, or an integer', | ||
| }).optional() | ||
|
|
||
| const AnoSchema = z.object({ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aqui é só uma bilbioteca pra validar a entrada do usuário. Assim a gente traz erros melhores quando for obrigatório algum tipo de dado. Aqui acho que vai precisar só tirar o .object e deixar o number |
||
| ano: z.number().min(2015) | ||
| }) | ||
| type ChachacaDTO = z.infer<typeof AnoSchema> | ||
|
|
||
| export default CachacaController | ||
This file contains hidden or 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 hidden or 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,13 @@ | ||
| import { ObjectId } from 'mongodb' | ||
|
|
||
| class Cachaca { | ||
| constructor( | ||
| public morador_id : ObjectId, | ||
| public cachaca_para_tomar: number, | ||
| public cachaca_ja_tomada: number, | ||
| public ano_do_rank:number, | ||
| public _id?: ObjectId, | ||
| ) {} | ||
| } | ||
|
|
||
| export default Cachaca | ||
|
LTaka marked this conversation as resolved.
|
||
This file contains hidden or 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 hidden or 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,83 @@ | ||
| import { ObjectId } from 'mongodb' | ||
| import Cachaca from '../models/cachaca' | ||
| import getDatabase from '../util/database' | ||
|
|
||
| const CachacaRepository = { | ||
| async verificaRegistroExistente(morador_id:(ObjectId | undefined), ano:number){ | ||
| try{ | ||
| const database = await getDatabase() | ||
| const verify = await database | ||
| .collection('cachaca') | ||
| .findOne({ | ||
| $and:[ | ||
| { morador_id: morador_id}, | ||
| { ano_do_rank: ano }, | ||
| ] | ||
| }) as Cachaca | ||
| return verify | ||
| }catch(error){ | ||
| throw new Error(`Something wrong with server, ${error}`) | ||
| } | ||
| }, | ||
|
|
||
| async showRankMoradores(ano: number) { | ||
| try { | ||
| const _database = await getDatabase() | ||
| const verify = (await _database | ||
| .collection('cachaca') | ||
| .find({ ano_do_rank: ano}) | ||
| .sort({ cachaca_ja_tomada: -1 }) | ||
| .toArray()) as Cachaca[] | ||
| return verify | ||
| } catch (error) { | ||
| throw new Error(`Algo deu errado com o servidor, ${error}`) | ||
| } | ||
| }, | ||
|
|
||
| async addCachaca(morador_id: ObjectId): Promise<void> { | ||
| try { | ||
| const _database = await getDatabase() | ||
| // define uma nova cachaca para um morador | ||
| await _database.collection('cachaca').updateOne( | ||
| { | ||
| _id: new ObjectId(morador_id), | ||
| }, | ||
| { $inc: { cachaca_para_tomar: 1 } } | ||
| ) | ||
| } catch (error) { | ||
| throw new Error(`Something wrong with server, ${error}`) | ||
| } | ||
|
|
||
| }, | ||
|
|
||
| async updateCachaca(morador_id: ObjectId, cachaca_ja_tomada: number, cachaca_para_tomar?: number) { | ||
| try { | ||
| const _database = await getDatabase() | ||
| await _database.collection('cachaca').updateOne( | ||
| { _id: new ObjectId(morador_id) }, | ||
| { $inc: cachaca_para_tomar ? { | ||
| cachaca_para_tomar: cachaca_para_tomar, | ||
| cachaca_ja_tomada: cachaca_ja_tomada | ||
| } : { | ||
| cachaca_ja_tomada: cachaca_ja_tomada | ||
| }} | ||
| ) | ||
| } catch (error) { | ||
| throw new Error(`Something wrong with server, ${error}`) | ||
| } | ||
| }, | ||
|
|
||
| async addMoradorAoRankCachaca(morador: Omit<Cachaca, '_id'>) { | ||
| try { | ||
| const _database = await getDatabase() | ||
| return await _database | ||
| .collection('cachaca') | ||
| .insertOne(morador) | ||
| } catch (error) { | ||
| throw new Error(`Something wrong with server, ${error}`) | ||
| } | ||
| }, | ||
|
|
||
| } | ||
|
|
||
| export default CachacaRepository |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Mudar para receber de req.query