Skip to content

Commit

Permalink
Update and Delete methods added in DevController + routes.js and a ne…
Browse files Browse the repository at this point in the history
…w component (UpdateTechs) was created in web.
  • Loading branch information
pgThiago committed Jul 25, 2020
1 parent 991d536 commit fa30a9f
Show file tree
Hide file tree
Showing 17 changed files with 583 additions and 118 deletions.
54 changes: 54 additions & 0 deletions backend/src/controllers/DevController.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,60 @@ module.exports = {

return response.json(dev);

},

// Desafio do Di:
// Criar os métodos update() techs e destroy() user
// Não permitir q o user atualize o username pois não faz sentido NTC

async update(request, response){

const github_username = request.params;

const { techs } = request.body;

const techsArray = parseStringAsArray(techs);

const dev = await Dev.findOneAndUpdate(github_username, {
techs: techsArray,
},{
returnOriginal: false
});

if(!dev)
return response.status(202).json({ message: 'User not found' });


await dev.save();

return response.json(dev);

/* const { id } = request.params;
const { techs } = request.body;
const techsArray = parseStringAsArray(techs);
const dev = await Dev.findIdAndUpdate(id, {
techs: techsArray
});
console.log(id);
return response.json(dev); */
},

async destroy(request, response){

const { id } = request.params;

const dev = await Dev.findByIdAndDelete(id, { returnOriginal: false });

if(!dev)
return response.status(202).json({ message: 'User not found' });


return response.json(dev);
}

};
Expand Down
14 changes: 1 addition & 13 deletions backend/src/controllers/SearchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,5 @@ module.exports = {

return response.json({ devs });

},

// Desafio do Di:
// Criar os métodos update() e destroy()
// Não permitir q o user atualize o username pois não faz sentido NTC

/* async update(){
},
async destroy(){
} */
}
}
5 changes: 3 additions & 2 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ setupWebsocket(server);
mongoose.connect('mongodb+srv://omnistack:[email protected]/omnistack10',
{
useNewUrlParser: true,
useUnifiedTopology: true
useUnifiedTopology: true,
useFindAndModify: false
}
);

Expand All @@ -28,7 +29,7 @@ app.use(routes);

// Query Params: request.query (Filtros, odenação, paginação, ...)
// Route Params: request.params (Identificar um recurso na alteração ou remoção)
// Body Params: request.body (Dados para criação ou alteração de um registro)
// Body Params: request.body (Dados para criação ou alteração de um registro)


server.listen(3333);
3 changes: 2 additions & 1 deletion backend/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ routes.get('/devs', DevController.index);
routes.get('/search', SearchController.index);

routes.post('/devs', DevController.store);
routes.post('/')
routes.put('/update/:github_username', DevController.update)
routes.delete('/delete/:id', DevController.destroy);

module.exports = routes;
108 changes: 108 additions & 0 deletions web/debug.log
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,111 @@
[0719/231427.523:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0719/231427.523:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0719/231427.523:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.018:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.087:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.087:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.087:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.087:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.087:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.087:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.088:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.088:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.088:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.088:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.088:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.088:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.088:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.088:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.089:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.089:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.089:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.089:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.089:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.089:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.089:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.089:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.090:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.090:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.090:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/175438.090:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.890:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.937:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.937:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.937:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.937:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.937:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.937:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.937:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.938:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.938:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.938:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.938:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.938:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.938:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.938:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.938:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.939:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.939:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.939:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.939:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.939:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.939:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.939:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.940:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.940:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.940:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0723/210143.940:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204440.965:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.068:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.069:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.069:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.069:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.069:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.070:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.070:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.070:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.070:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.070:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.070:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.071:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.071:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.071:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.071:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.071:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.071:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.072:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.072:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.072:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.072:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.072:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.072:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.072:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.073:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/204441.073:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.013:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.099:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.099:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.099:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.099:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.099:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.099:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.100:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.100:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.100:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.100:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.100:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.101:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.101:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.101:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.101:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.102:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.102:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.102:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.102:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.102:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.103:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.103:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.103:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.103:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.103:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0724/213135.103:ERROR:crash_report_database_win.cc(469)] failed to stat report
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"axios": "^0.19.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.1"
},
"scripts": {
Expand Down
14 changes: 2 additions & 12 deletions web/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,10 @@
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />

<title>React App</title>
<title>DevRadar</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<noscript>No JavaScript no app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
26 changes: 0 additions & 26 deletions web/src/App.css

This file was deleted.

60 changes: 7 additions & 53 deletions web/src/App.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
import React, { useState, useEffect } from 'react';
import React from 'react';

import './App.css';
import './global.css';
import './Sidebar.css';
import './Main.css';

import api from './services/api';

import DevItem from './components/DevItem';
import DevForm from './components/DevForm';

function App() {

const [ devs, setDevs ] = useState([]);

useEffect(() => {
async function loadDevs(){
const response = await api.get('/devs')
setDevs(response.data);
}
loadDevs();
}, []);

async function handleAddDev(data){

const response = await api.post('/devs', data);

setDevs([...devs, response.data]);
}


return (
<div id="app">
<aside>
<strong>Cadastrar</strong>
<DevForm onSubmit={handleAddDev}/>
</aside>

<main>

<ul>
{
devs.map(dev => (
<DevItem key={dev._id} dev={dev} />
))
}
</ul>

</main>

</div>
);
import Routes from './components/routes';

function App(){
return (
<Routes />
)
}

export default App;
export default App;
34 changes: 31 additions & 3 deletions web/src/components/DevItem/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import React from 'react';

import { useHistory } from "react-router-dom";

import api from '../../services/api';

import './styles.css';

function DevItem({ dev }){
function DevItem({ dev }){

const history = useHistory();

function goToUpdatePage(){
history.push(`/update/${dev.github_username}`, {
dev
});
}

async function handleDelete(){
try{
await api.delete(`/delete/${dev._id}`);
window.location.reload() ;
}

catch(error){
console.log(error);
}
}

return(
<li className="dev-item">
<header>
Expand All @@ -12,8 +36,12 @@ function DevItem({ dev }){
<span>{dev.bio}</span>
</div>
</header>
<p>{dev.description}</p>
<a target="_blank" href={`https://github.com/${dev.github_username}`}>Acessar perfil</a>
<div className="buttons">
<p>{dev.description}</p>
<button className="link-button"><a target="_blank" rel="noopener noreferrer" href={`https://github.com/${dev.github_username}`}>Acessar perfil</a></button>
<button onClick={goToUpdatePage} className="updateButton">Atualizar perfil</button>
<button onClick={handleDelete} className="deleteButton">Excluir perfil</button>
</div>
</li>
)
}
Expand Down
Loading

0 comments on commit fa30a9f

Please sign in to comment.