Skip to content
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

close issue #33 #110

Merged
merged 2 commits into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions backend/src/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Request, Response } from 'express'
import { UserService } from '../services/UserService';

class UserController {

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

const user = await userService.getUser(user_id);

console.log(user);
return response.status(200).json({ user });
} catch (error) {
return response.status(400).json({ message: error.message });
}
}

}

export { UserController }
21 changes: 12 additions & 9 deletions backend/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import {Router} from 'express';
import { Router } from 'express';
import { ClientController } from './controllers/ClientController';
import { EmotionalReactionController } from './controllers/EmotionalRecordController';
import { ProfessionalController } from './controllers/ProfessionalController';
import { UserController } from './controllers/UserController';

const routes = Router();
const routes = Router();

const emotionalReactionController = new EmotionalReactionController();
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.post("/clients", clientController.create);
routes.post("/clients/update", clientController.update);

routes.post("/clients",clientController.create);
routes.post("/clients/update",clientController.update);
routes.get("/reactions/:client_id", emotionalReactionController.index);

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

routes.delete("/reactions/:client_id/:emotional_reaction_id",emotionalReactionController.destroy);
routes.post("/reactions/create/:client_id", emotionalReactionController.create);

routes.post("/reactions/create/:client_id",emotionalReactionController.create);
routes.post("/reactions/update/:reaction_id", emotionalReactionController.update);

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

export {routes}
export { routes }
58 changes: 35 additions & 23 deletions backend/src/services/ClientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { Client } from "../entities/Client";
import { UserService } from './UserService';


interface ClientInterface{
name:string;
phone:string;
user_id:string;
interface ClientInterface {
name: string;
phone: string;
user_id: string;
}

interface UpdateClientInterface{
name?:string;
phone?:string;
email?:string;
password?:string;
professional_id?:string;
id:string;
interface UpdateClientInterface {
name?: string;
phone?: string;
email?: string;
password?: string;
professional_id?: string;
id: string;
}


Expand All @@ -26,34 +26,46 @@ class ClientService {
this.clientRepository = getRepository(Client);
}

async createClient({name,user_id,phone}:ClientInterface) {
const newClient = this.clientRepository.create({name,phone,user_id});
async createClient({ name, user_id, phone }: ClientInterface) {
const newClient = this.clientRepository.create({ name, phone, user_id });

await this.clientRepository.save(newClient);
}

async update({id,email,password,name,professional_id,phone}:UpdateClientInterface){
async update({ id, email, password, name, professional_id, phone }: UpdateClientInterface) {

const client = await this.clientRepository.findOne({ where: [{ id }], relations: ['user'] })
const client_new_values = { name, professional_id, phone }

const client = await this.clientRepository.findOne({where:[{id}],relations:['user']})
const client_new_values = {name,professional_id,phone}

if(client){
if (client) {
await this.clientRepository.save({
...client
,...client_new_values});
, ...client_new_values
});

const userService = new UserService();

await userService.updateUser({
id:client.user_id,
id: client.user_id,
email,
password
})
}else
} else
throw new Error(`Cliente não encontrado`)

}

async getClient(user_id) {
const clientRegistered = await this.clientRepository.findOne({ where: { user_id } });

if (clientRegistered) {
return clientRegistered;
}
else {
throw new Error("Cliente não encontrado!")
}
}

}

export { ClientService }
Expand Down
67 changes: 40 additions & 27 deletions backend/src/services/ProfessionalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import { Professional } from "../entities/Professional";
import { Client } from '../entities/Client';
import { UserService } from './UserService';

interface ProfessionalInterface{
name:string;
crm_crp:string;
speciality:string;
association_code?:string;
user_id:string;
interface ProfessionalInterface {
name: string;
crm_crp: string;
speciality: string;
association_code?: string;
user_id: string;
}

interface UpdateClientInterface{
name?:string;
crm_crp?:string;
speciality?:string;
association_code?:string;
email?:string;
password?:string;
id:string;
interface UpdateClientInterface {
name?: string;
crm_crp?: string;
speciality?: string;
association_code?: string;
email?: string;
password?: string;
id: string;
}

class ProfessionalService {
Expand All @@ -30,46 +30,59 @@ class ProfessionalService {
this.clientRepository = getRepository(Client);
}


async getClients(professional_id: string): Promise<Client[]>{

const clients = await this.clientRepository.find({where: { professional_id: professional_id }, relations:['user']});

return clients;
}

async createProfessional({name,crm_crp,speciality,user_id,association_code}:ProfessionalInterface) {
async createProfessional({ name, crm_crp, speciality, user_id, association_code }: ProfessionalInterface) {
const professionalRegistered = await this.professionalRepository.findOne({ where: { crm_crp } });

if(professionalRegistered){
if (professionalRegistered) {
throw new Error("Crm ou Crp já cadastrado!")
}

const newProfessional = this.professionalRepository.create({name,crm_crp,speciality,user_id,association_code});
const newProfessional = this.professionalRepository.create({ name, crm_crp, speciality, user_id, association_code });
await this.professionalRepository.save(newProfessional);

}

async update({name,crm_crp,speciality,association_code,email,password,id}:UpdateClientInterface){
async update({ name, crm_crp, speciality, association_code, email, password, id }: UpdateClientInterface) {

const professional = await this.professionalRepository.findOne({ where: [{ id }], relations: ['user'] })
const professional_new_values = { name, crm_crp, speciality, association_code }

const professional = await this.professionalRepository.findOne({where:[{id}],relations:['user']})
const professional_new_values = {name,crm_crp,speciality,association_code}

if(professional){
if (professional) {
await this.professionalRepository.save({
...professional
,...professional_new_values});
, ...professional_new_values
});

const userService = new UserService();

await userService.updateUser({
id:professional.user_id,
id: professional.user_id,
email,
password
})
}else
} else
throw new Error(`Profissional não encontrado`)

}

async getProfessional(user_id) {
const professionalRegistered = await this.professionalRepository.findOne({ where: { user_id } });

if (professionalRegistered) {
return professionalRegistered;
}
else {
throw new Error("Profissional não encontrado!")
}
}
}

export { ProfessionalService }
Loading