Skip to content

Commit

Permalink
Merge pull request #58 from #12-changes-requested
Browse files Browse the repository at this point in the history
Mudanças feitas em cerradogo#12
  • Loading branch information
CPeluti authored Aug 21, 2022
2 parents 0cdd1c7 + f1e1026 commit 8ed8e52
Show file tree
Hide file tree
Showing 15 changed files with 1,703 additions and 247 deletions.
6 changes: 2 additions & 4 deletions backend/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ const dotenv_1 = __importDefault(require("dotenv"));
const mongoose_1 = __importDefault(require("mongoose"));
//import routes
const User_1 = __importDefault(require("./routes/User"));
const Question_1 = __importDefault(require("./routes/Question"));
dotenv_1.default.config();
const app = (0, express_1.default)();
app.use(express_1.default.json());
const port = process.env.PORT;
const dbUser = process.env.DB_USERNAME;
const dbPasswd = process.env.DB_PASSWORD;
console.log(dbUser);
console.log(dbPasswd);
// mongoose.connect(`mongodb://cerradogo:cerradogo@localhost:27017`)
mongoose_1.default.connect(`mongodb+srv://${dbUser}:${dbPasswd}@cluster0.ub24ryp.mongodb.net/?retryWrites=true&w=majority`);
//mongoose.connect(`mongodb://cerradogo:cerradogo@localhost:27017`)
app.use('/user', User_1.default);
app.use('/question', Question_1.default);
app.listen(port, () => {
console.log(`server rodando na porta ${port}`);
});
63 changes: 60 additions & 3 deletions backend/src/controllers/User.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Request, Response} from 'express'
import UserModel, {User} from '../models/User'
import { HydratedDocument } from 'mongoose'
const create = async (req: Request, res: Response) => {
console.log(req.body)
export const create = async (req: Request, res: Response) => {
const user: User = req.body
const userModel: HydratedDocument<User> = new UserModel(user)
try{
Expand All @@ -13,4 +12,62 @@ const create = async (req: Request, res: Response) => {
}
}

export default create
export const findAll = async (req: Request, res: Response) => {
try {
const user = await UserModel.find({})
res.send(user)
} catch (e){
console.error(e)
res.status(404).send("Nao foi possivel achar o usuario")
}
}

export const findById = async (req: Request, res: Response) => {
const id = req.params.id
try {
const user = await UserModel.findById(id)
if(user===null){
throw(new Error("Usuario nao encontrado"))
}
res.send(user)
} catch (e){
console.error(e)
res.status(404).send("Nao foi possivel achar o usuario")
}
}

export const updateById = async (req: Request, res: Response) => {
const id = req.params.id
const data = req.body
try {
var keyArray = Object.keys(data)
let flag = false // false significa que a key enviada no body não está presente no user
for (let _i = 0; _i < keyArray.length; _i++){
if (UserModel.hasOwnProperty(keyArray[_i])){
flag = true
break
}
}
if (flag){
const user = await UserModel.findByIdAndUpdate(id, req.body, {new: true})
res.send("Usuario atualizado com sucesso")
}
else{
res.send("Nao foi possivel atualizar usuario, verifique os atributos")
}
} catch (e) {
console.error(e)
res.status(500).send("Falha ao atualizar o usuario")
}
}

export const deleteById = async (req: Request, res: Response) => {
const id = req.params.id
try {
const user = await UserModel.deleteOne({_id:id})
res.send("usuario deletado com sucesso")
} catch (e) {
console.error(e)
res.send("Falha ao deletar o ID")
}
}
8 changes: 2 additions & 6 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import mongoose from 'mongoose'

//import routes
import User from './routes/User'
import Question from './routes/Question'

dotenv.config()

Expand All @@ -15,14 +14,11 @@ const port = process.env.PORT
const dbUser = process.env.DB_USERNAME
const dbPasswd = process.env.DB_PASSWORD

console.log(dbUser)
console.log(dbPasswd)

// mongoose.connect(`mongodb://cerradogo:cerradogo@localhost:27017`)
mongoose.connect(`mongodb+srv://${dbUser}:${dbPasswd}@cluster0.ub24ryp.mongodb.net/?retryWrites=true&w=majority`)

//mongoose.connect(`mongodb://cerradogo:cerradogo@localhost:27017`)

app.use('/user', User)
app.use('/question', Question)

app.listen(port, () => {
console.log(`server rodando na porta ${port}`)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ const userSchema = new Schema<User>({

const UserModel = model<User>('User', userSchema)

export default UserModel
export default UserModel
9 changes: 7 additions & 2 deletions backend/src/routes/User.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import express, {Router} from 'express'
import User from '../controllers/User'
import {create, deleteById, findById, findAll, updateById} from '../controllers/User'
const router: Router = express.Router()
router.post('/', User)
router.post('/', create)
router.get('/:id', findById)
router.get('/', findAll)
router.patch('/:id', updateById)
router.delete('/:id',deleteById)

//router.post('/validate', validate)

export default router
Loading

0 comments on commit 8ed8e52

Please sign in to comment.