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

Tratamento de Erros Backend #117

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb' }));
setupWebsocket(server);
const databaseConnect = require('./src/config/database');
const { errorHandler } = require('./src/utils/errorHandler');

app.use(cors());
app.use(bodyParser.json());
app.use(errorHandler);

databaseConnect();
dailySchedule();
Expand Down
124 changes: 40 additions & 84 deletions src/controllers/CampaignController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const CampaignService = require('../services/CampaignService');
const { UnauthorizedError, BadRequestError } = require('../utils/errorHandler');
const saveError = require('../utils/ErrorHistory');

class CampaignController {
Expand All @@ -7,70 +8,46 @@ class CampaignController {
}

async createCampaign(req, res) {
try {
const newCampaign = await this.CampaignService.createNewCampaign(
req.body,
);
return res.json(newCampaign);
} catch (error) {
return res.status(400).json({ error: error.message });
}
const newCampaign = await this.CampaignService.createNewCampaign(
req.body,
);
return res.json(newCampaign);
}

async listCampaign(req, res) {
try {
const campaign = await this.CampaignService.listCampaign();
return res.json(campaign);
} catch (error) {
return res.status(400).json(error);
}
const campaign = await this.CampaignService.listCampaign();
return res.json(campaign);
}

async getCampaignListByStatus(req, res, next) {
async getCampaignListByStatus(req, res) {
const { userId } = req.params;

const statusList = req.query.statusList.split(',');

try {
const result = await this.CampaignService.getCampaignListByStatus({
userId,
statusList,
});
res.status(200).json(result);
next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
next();
}
const result = await this.CampaignService.getCampaignListByStatus({
userId,
statusList,
});
return res.status(200).json(result);
}

async listCampaignByOwnerId(req, res) {
const { ownerId } = req.params;
try {
const campaign = await this.CampaignService.listCampaignByOwnerId(
ownerId,
);
return res.json(campaign);
} catch (error) {
return res.status(400).json(error);
}
if(!ownerId) throw new UnauthorizedError('No ownerId provided');
const campaign = await this.CampaignService.listCampaignByOwnerId(
ownerId,
);
return res.json(campaign);
}

async deleteCampaignLogic(req, res, next) {
async deleteCampaignLogic(req, res) {
const { id } = req.params;
try {
const result = await this.CampaignService.deleteCampaign(id);
res.status(200).json(result);
next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
next();
}
if(!id) throw new BadRequestError('No id provided');
const result = await this.CampaignService.deleteCampaign(id);
res.status(200).json(result);
}

async listCampaignNear(req, res, next) {
async listCampaignNear(req, res) {
const except = !!req.query['id.except'];
const helper = !!req.query['id.helper'];
let temp = null;
Expand All @@ -90,51 +67,30 @@ class CampaignController {
? req.query.coords.split(',').map((coord) => Number(coord))
: null;

try {
let result;
if (near) {
result = await this.CampaignService.getNearCampaignList(
coords,
except,
id,
categoryArray,
);
}
res.status(200);
res.json(result);
next();
} catch (err) {
console.log(err);
saveError(err);
res.status(400).json({ error: err.message });
next();
let result;
if (near) {
result = await this.CampaignService.getNearCampaignList(
coords,
except,
id,
categoryArray,
);
}
return res.status(200).json(result);
}

async finishCampaign(req, res, next) {
async finishCampaign(req, res) {
const { id } = req.params;
try {
const result = await this.CampaignService.finishCampaign(id);
res.status(200).json(result);
next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
next();
}
if(!id) throw new BadRequestError('No id provided');
thiagohdaqw marked this conversation as resolved.
Show resolved Hide resolved
const result = await this.CampaignService.finishCampaign(id);
return res.status(200).json(result);
}

async getCampaignById(req, res, next) {
async getCampaignById(req, res) {
const { id } = req.params;
try {
const result = await this.CampaignService.getCampaignById(id);
res.status(200).json(result);
next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
next();
}
if(!id) throw new BadRequestError('No id provided')
const result = await this.CampaignService.getCampaignById(id);
return res.status(200).json(result);
}
}

Expand Down
30 changes: 8 additions & 22 deletions src/controllers/CategoryController.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
const CategoryService = require('../services/CategoryService');
const { BadRequestError } = require('../utils/errorHandler');
const saveError = require('../utils/ErrorHistory');

class CategoryController {
constructor() {
this.CategoryService = new CategoryService();
}

async getCategoryById(req, res, next) {
async getCategoryById(req, res) {
const { id } = req.params;

try {
const result = await this.CategoryService.getCategoryByid(id);
res.status(200).json(result);
next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
next();
}
if(!id) throw new BadRequestError('No id provided');
const result = await this.CategoryService.getCategoryByid(id);
return res.status(200).json(result);
}

async getCategoryList(req, res, next) {
async getCategoryList(req, res) {
const id = req.query.id || null;

try {
const result = await this.CategoryService.getCategoryList(id);
res.status(200).json(result);
next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
next();
}
const result = await this.CategoryService.getCategoryList(id);
return res.status(200).json(result);
}
}

Expand Down
81 changes: 16 additions & 65 deletions src/controllers/EntityController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class EntityController {
this.entityService = new EntityService();
}

async createEntity(req, res, next) {
async createEntity(req, res) {
const { latitude, longitude } = req.body;

const location = {
Expand All @@ -20,18 +20,11 @@ class EntityController {
hasEntity: req.query.hasEntity === 'true',
};

try {
const result = await this.entityService.createEntity(data);
res.status(201).json(result);
next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
next();
}
const result = await this.entityService.createEntity(data);
return res.status(201).json(result);
}

async editEntityById(req, res, next) {
async editEntityById(req, res) {
const data = {
email: req.decodedToken.email,
photo: req.body.photo,
Expand All @@ -40,15 +33,8 @@ class EntityController {
notificationToken: req.body.notificationToken,
deviceId: req.body.deviceId,
};
try {
const result = await this.entityService.editEntityById(data);
res.status(200).json(result);
return next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
return next();
}
const result = await this.entityService.editEntityById(data);
return res.status(200).json(result);
}

async editEntityAddressById(req, res, next) {
Expand All @@ -61,29 +47,15 @@ class EntityController {
complement: req.body.complement,
};

try {
const result = await this.entityService.editEntityAddressById(data);
res.status(200).json(result);
return next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
return next();
}
const result = await this.entityService.editEntityAddressById(data);
return res.status(200).json(result);
}

async deleteEntityLogic(req, res, next) {
const { email } = req.decodedToken;

try {
const result = await this.entityService.deleteEntityLogically(email);
res.status(200).json(result);
return next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
return next();
}
const result = await this.entityService.deleteEntityLogically(email);
return res.status(200).json(result);
}

async getEntityById(req, res, next) {
Expand All @@ -92,15 +64,8 @@ class EntityController {
email: req.decodedToken.email,
};

try {
const result = await this.entityService.getEntity(data);
res.status(200).json(result);
next();
} catch (err) {
saveError(err);
res.status(404).json({ error: err.message });
next();
}
const result = await this.entityService.getEntity(data);
return res.status(200).json(result);
}

async updateEntityLocationById(req, res, next) {
Expand All @@ -110,15 +75,8 @@ class EntityController {
longitude: req.body.longitude,
};

try {
const result = await this.entityService.updateEntityLocationById(data);
res.status(200).json(result);
next();
} catch (err) {
saveError(err);
res.status(400).json({ error: err.message });
next();
}
const result = await this.entityService.updateEntityLocationById(data);
return res.status(200).json(result);
}

async checkEntityExistence(req, res, next) {
Expand All @@ -128,15 +86,8 @@ class EntityController {
entityIdentifier = req.decodedToken.email;
}

try {
const result = await this.entityService.checkEntityExistence(entityIdentifier);
res.status(200).json(result);
next();
} catch (err) {
saveError(err);
res.status(404).json({ error: err.message });
next();
}
const result = await this.entityService.checkEntityExistence(entityIdentifier);
return res.status(200).json(result);
}
}

Expand Down
Loading