Skip to content

Commit

Permalink
Merge pull request #139 from ES2-UFPI/refactoring
Browse files Browse the repository at this point in the history
Alterando nomes das rotas
  • Loading branch information
giselesousar authored Jul 10, 2021
2 parents 289fcda + 3ba1849 commit 58edaa5
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 47 deletions.
26 changes: 20 additions & 6 deletions backend/src/controllers/EmotionalRecordController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class EmotionalReactionController {

async index(request: Request, response: Response): Promise<Response> {
try {
const { client_id } = request.params;
const { id } = request.params;

const emotionalReactionService = new EmotionalReactionService();

const emotionalReactions = await emotionalReactionService.listByUser(client_id);
const emotionalReactions = await emotionalReactionService.listByUser(id);

return response.json(emotionalReactions)

Expand All @@ -20,14 +20,28 @@ class EmotionalReactionController {

}

async getById(request: Request, response: Response): Promise<Response> {
try {
const { id } = request.params;

const emotionalReactionService = new EmotionalReactionService();

const emotionalReaction = await emotionalReactionService.getById(id);

return response.status(200).json({ emotionalReaction });
} catch (err) {
return response.status(400).json({ erro: err })
}
}


async destroy(request: Request, response: Response): Promise<Response> {
try {
const { client_id, emotional_reaction_id } = request.params;
const { id } = request.params;

const emotionalReactionService = new EmotionalReactionService();

await emotionalReactionService.delete(client_id, emotional_reaction_id);
await emotionalReactionService.delete(id);

return response.json({ message: "Deletada com sucesso!" })

Expand All @@ -39,7 +53,7 @@ class EmotionalReactionController {

async create(request: Request, response: Response): Promise<Response> {
try {
const client_id = request.params['client_id'];
const client_id = request.params['id'];

const emotionalReactionService = new EmotionalReactionService();

Expand All @@ -53,7 +67,7 @@ class EmotionalReactionController {

async update(request: Request, response: Response): Promise<Response> {
try {
const reaction_id = request.params['reaction_id'];
const reaction_id = request.params['id'];

const body: EmotionalReaction = request.body;

Expand Down
4 changes: 2 additions & 2 deletions backend/src/controllers/ProfessionalController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class ProfessionalController {

async getClients(request: Request, response: Response) {
try {
const { professional_id } = request.params;
const { id } = request.params;

const professionalService = new ProfessionalService();

const clients = await professionalService.getClients(professional_id);
const clients = await professionalService.getClients(id);

return response.status(200).json({clients});
} catch(error) {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class UserController {

async getUserById(request: Request, response: Response) {
try {
const { user_id } = request.params;
const { id } = request.params;
const userService = new UserService();

const user = await userService.getUser(user_id);
const user = await userService.getUser(id);

return response.status(200).json({ user });
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class CreateEmotionalReaction1622341164120 implements MigrationInterface
generationStrategy: 'uuid'

},
{name:"title",type:"varchar",length:'150',default:"'(Sem nome)'"},
{name:"title",type:"varchar",length:'150',default:"''"},

{name:"emotions",type:"varchar",length:'200',default:"''"},

Expand Down
22 changes: 10 additions & 12 deletions backend/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ const professionalController = new ProfessionalController();
const clientController = new ClientController();
const userController = new UserController();


routes.post("/professionals",professionalController.create);
routes.post("/professionals/update",professionalController.update);
routes.get("/professionals/clients/:professional_id", professionalController.getClients);
routes.put("/professionals",professionalController.update);
routes.get("/professionals/:id/clients", professionalController.getClients);

routes.post("/clients", clientController.create);
routes.post("/clients/update", clientController.update);

routes.get("/reactions/:client_id", emotionalReactionController.index);

routes.delete("/reactions/:client_id/:emotional_reaction_id", emotionalReactionController.destroy);
routes.put("/clients", clientController.update);
routes.get("/clients/:id/reactions", emotionalReactionController.index);
routes.post("/clients/:id/reactions", emotionalReactionController.create);

routes.post("/reactions/create/:client_id", emotionalReactionController.create);
routes.get("/reactions/:id", emotionalReactionController.getById);
routes.put("/reactions/:id", emotionalReactionController.update);
routes.delete("/reactions/:id", emotionalReactionController.destroy);

routes.post("/reactions/update/:reaction_id", emotionalReactionController.update);
routes.post("/users/authentication", userController.login);
routes.get("/users/:id", userController.getUserById);

routes.get("/users/:user_id", userController.getUserById);

routes.get("/user/login", userController.login);

export { routes }
10 changes: 8 additions & 2 deletions backend/src/services/EmotionalReactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ class EmotionalReactionService {
return emotionalReactions;
}

async getById(id: string): Promise<EmotionalReaction> {
const emotionalReaction = await this.emotionalReactionRepository.findOne(id);

async delete(client_id: string, emotional_reaction_id: string) {
return emotionalReaction;
}


async delete(emotional_reaction_id: string) {

await this.emotionalReactionRepository.delete({ client_id, id: emotional_reaction_id });
await this.emotionalReactionRepository.delete({ id: emotional_reaction_id });
}

async create(client_id: string): Promise<string> {
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@types/jsonwebtoken": "^8.5.4",
"@types/react": "~16.9.35",
"@types/react-native": "~0.63.2",
"@types/react-native-vector-icons": "^6.4.6",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/ClientList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const ClientList = (props: any) => {
try {
setLoading(true);

const response = await api.get(`/professionals/clients/${professional_id}`);
const response = await api.get(`professionals/${professional_id}/clients`);
setClients(response.data.clients);
console.log(response)
setLoading(false);
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/views/RecordsList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Alert2Options from '../../components/Alert2Options';
import Record_card from '../../components/Record_card';
import { Title,Container,NothingFound,ContainerAll,TextNothingFound } from './styles';
import api from '../../services/api'
import { useNavigation } from '@react-navigation/native';

interface Record{
id:string;
Expand All @@ -25,7 +26,7 @@ export default function RecordsList() {

try {
setLoading(true)
const reaponse = await api.get(`/reactions/${client_id}`);
const reaponse = await api.get(`/clients/${client_id}/reactions`);

const data = reaponse.data as [any];

Expand Down Expand Up @@ -69,7 +70,7 @@ export default function RecordsList() {

async function handleDelete(){

await api.delete(`/reactions/${client_id}/${idCurrent}`)
await api.delete(`/reactions/${idCurrent}`)
.then(()=>{
const filteredRecord = records?.filter((record) => record.id != idCurrent );
setRecords(filteredRecord);
Expand All @@ -79,13 +80,15 @@ export default function RecordsList() {

}

const navigate = useNavigation();

function onPressDelete(id:string){
setIdCurrent(id);
setModalIsVisible(true);
}

function onPressCard(){

function onPressCard(id: string){
navigate.navigate('Registration', { id })
}

return (
Expand All @@ -112,7 +115,7 @@ export default function RecordsList() {
date={item.date}
completed={item.completed}
onPressDelete={onPressDelete}
onPress={onPressCard}
onPress={() => onPressCard(item.id)}
/>}
horizontal={false}
contentContainerStyle={{
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/views/Registration/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import StepThree from './views/StepThree';

const StackSettings = createStackNavigator<RoutesList>();

export default function Registration() {
export default function Registration(props: any) {
return (

<StackSettings.Navigator screenOptions={{ headerShown: false }}>
<StackSettings.Screen name="StepZero" component={StepZero} />
<StackSettings.Screen name="StepZero" component={StepZero} initialParams={props.route.params} />
<StackSettings.Screen name="StepOne" component={StepOne} />
<StackSettings.Screen name="StepTwo" component={StepTwo} />
<StackSettings.Screen name="StepThree" component={StepThree} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,26 @@ const CheckBoxGroup = forwardRef((props: any, ref: any) => {
return arr.join(',');
}

const setReceivedValues = (values: string) => {
const arr = values.split(',');
const newArr: boolean[] = new Array(sentimentos.length).fill(false);

arr.map((value: string) => {
for (const sentimento of sentimentos) {
if(value === sentimento.name) {
newArr[sentimento.id] = true;
break;
}
}
})

setValues(newArr);

}

useImperativeHandle(ref, () => ({
getValues: () => makeArrayOfTrueValues(),
setValues: (values: string) => setReceivedValues(values)
}))

const onChangeCheckBoxValue = (newValue: boolean, index: number) => {
Expand Down
27 changes: 23 additions & 4 deletions frontend/src/views/Registration/views/StepOne/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from 'react';
import React, { useRef, useState, useEffect } from 'react';
import {
Container,
ScrollView,
Expand Down Expand Up @@ -32,6 +32,21 @@ const RegistrationStepZero = (props: any) => {

const checkboxGroupRef = useRef<any>();

const loadEmotionalReaction = async () => {
try {
const response = await api.get(`reactions/${id}`);
const emotionalReaction: EmotionalReaction = response?.data['emotionalReaction'];
setFormInput({ ...formInput, ...emotionalReaction });
checkboxGroupRef.current.setValues(emotionalReaction.emotions);
} catch (error) {
console.log(error);
}
}

useEffect(() => {
loadEmotionalReaction();
}, []);

const getEmotions = (): string => {
return checkboxGroupRef?.current?.getValues() || '';
}
Expand All @@ -40,10 +55,14 @@ const RegistrationStepZero = (props: any) => {

const emotions = getEmotions();

const data = { ...formInput, emotions };
let data: EmotionalReaction = { ...formInput, emotions };

if(formInput?.title?.length === 0) {
data = {...formInput, title: '(Sem nome)'};
}

api.post(`reactions/update/${id}`, data)
.then((res: AxiosResponse) => { console.log(res.data.message); navigateToNextStep() })
api.put(`reactions/${id}`, data)
.then((res: AxiosResponse) => navigateToNextStep() )
.catch((err: AxiosError) => console.log(err.message));
}

Expand Down
20 changes: 17 additions & 3 deletions frontend/src/views/Registration/views/StepThree/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from 'react';
import React, { useRef, useState, useEffect } from 'react';
import {
Container,
ScrollView,
Expand All @@ -25,9 +25,23 @@ const RegistrationStepThree = (props: any) => {

const [formInput, setFormInput] = useState<EmotionalReaction>();

const loadEmotionalReaction = async () => {
try {
const response = await api.get(`reactions/${id}`);
const emotionalReaction: EmotionalReaction = response?.data['emotionalReaction'];
setFormInput({ ...formInput, ...emotionalReaction });
} catch (error) {
console.log(error);
}
}

useEffect(() => {
loadEmotionalReaction();
}, []);

const handleConfirmation = () => {
api.post(`reactions/update/${id}`, formInput)
.then((res: AxiosResponse) => {console.log(res.data.message); navigateToHome()})
api.put(`reactions/${id}`, formInput)
.then((res: AxiosResponse) => navigateToHome())
.catch((err: AxiosError) => console.log(err.message));
}

Expand Down
20 changes: 17 additions & 3 deletions frontend/src/views/Registration/views/StepTwo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from 'react';
import React, { useRef, useState, useEffect } from 'react';
import {
Container,
ScrollView,
Expand All @@ -25,9 +25,23 @@ const RegistrationStepTwo = (props: any) => {

const [formInput, setFormInput] = useState<EmotionalReaction>();

const loadEmotionalReaction = async () => {
try {
const response = await api.get(`reactions/${id}`);
const emotionalReaction: EmotionalReaction = response?.data['emotionalReaction'];
setFormInput({ ...formInput, ...emotionalReaction });
} catch (error) {
console.log(error);
}
}

useEffect(() => {
loadEmotionalReaction();
}, []);

const handleConfirmation = () => {
api.post(`reactions/update/${id}`, formInput)
.then((res: AxiosResponse) => {console.log(res.data.message); navigateToNextStep()})
api.put(`reactions/${id}`, formInput)
.then((res: AxiosResponse) => navigateToNextStep())
.catch((err: AxiosError) => console.log(err.message));
}

Expand Down
Loading

0 comments on commit 58edaa5

Please sign in to comment.