Skip to content

Commit

Permalink
Merge pull request #23 from caions/refactor/habits-routes-params
Browse files Browse the repository at this point in the history
Refactor/habits routes params
  • Loading branch information
caions committed Apr 17, 2024
2 parents 1ed8be1 + 5a26884 commit 27356ae
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 66 deletions.
46 changes: 23 additions & 23 deletions habits-document.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,21 @@
"description": ""
}
}
},
"put": {
}
},
"/habits/{habitId}": {
"get": {
"tags": [
"Habits"
],
"description": "",
"parameters": [
{
"name": "id",
"description": "habit id",
"in": "query",
"type": "string"
},
{
"name": "obj",
"in": "body",
"description": "update a habit",
"schema": {
"$ref": "#/definitions/HabitBodyName"
}
"name": "habitId",
"in": "path",
"required": true,
"type": "string",
"description": "habit habitId"
}
],
"responses": {
Expand All @@ -71,18 +66,25 @@
}
}
},
"delete": {
"put": {
"tags": [
"Habits"
],
"description": "",
"parameters": [
{
"name": "habitId",
"in": "path",
"required": true,
"type": "string",
"description": "habit habitId"
},
{
"name": "obj",
"in": "body",
"description": "habit uuid",
"description": "update a habit",
"schema": {
"$ref": "#/definitions/HabitBodyId"
"$ref": "#/definitions/HabitBodyName"
}
}
],
Expand All @@ -91,21 +93,19 @@
"description": ""
}
}
}
},
"/habits/{id}": {
"get": {
},
"delete": {
"tags": [
"Habits"
],
"description": "",
"parameters": [
{
"name": "id",
"name": "habitId",
"in": "path",
"required": true,
"type": "string",
"description": "habit id"
"description": "habit habitId"
}
],
"responses": {
Expand Down
4 changes: 2 additions & 2 deletions src/application/HabitCompletionDateUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HabitRepositoryProtocol } from '../domain/repositories/HabitRepositoryP
import { AppError } from '../shared/errors/AppError';
import { HabitCompletionDate } from '../domain/entities/HabitCompletionDate';
import { isSameDate } from '../shared/utils/isSameDate';
import { validUtcDate } from '../shared/utils/utcDateValidation';
import { validISODate } from '../shared/utils/validISODate';

export class HabitCompletionDateUseCase {
constructor(
Expand All @@ -20,7 +20,7 @@ export class HabitCompletionDateUseCase {
completedDate,
};

if (!validUtcDate(completedDate)) {
if (!validISODate(completedDate)) {
throw new AppError('completedDate invalid utc format');
}

Expand Down
4 changes: 1 addition & 3 deletions src/application/tests/HabitCompletionDateUseCase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('Complete a Habit', () => {
let habitCompletionDateUseCase: HabitCompletionDateUseCase;
let listHabitCompletionDateUseCase: ListHabitCompletionDateUseCase;
const completedDate = new Date().toISOString();
jest.useFakeTimers().setSystemTime(new Date('2020-01-01'));

beforeEach(() => {
memoryHabitRepository = new MemoryHabitRepository();
Expand Down Expand Up @@ -81,8 +80,7 @@ describe('Complete a Habit', () => {
});

it('should not be possible to complete with an invalid date format.', async () => {
const createdHabit = await createHabitUseCase.execute('verr');
console.log(createdHabit.id);
const createdHabit = await createHabitUseCase.execute('ver');
await expect(
habitCompletionDateUseCase.execute(
createdHabit.id,
Expand Down
12 changes: 6 additions & 6 deletions src/infra/web/controllers/HabitController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export class HabitController {
};

show = async (request: Request, response: Response): Promise<void> => {
const { id } = request.params;
const { habitId } = request.params;
const findHabitUseCase = new FindHabitUseCase(this.habitRepository);
const result = await findHabitUseCase.execute(id);
const result = await findHabitUseCase.execute(habitId);
response.json(result);
};

Expand All @@ -30,10 +30,10 @@ export class HabitController {
};

update = async (request: Request, response: Response): Promise<void> => {
const { id } = request.query;
const { habitId } = request.params;
const { name } = request.body;
const habit = {
id: String(id),
id: String(habitId),
name,
};
const updateHabitUseCase = new UpdateHabitUseCase(this.habitRepository);
Expand All @@ -42,9 +42,9 @@ export class HabitController {
};

destroy = async (request: Request, response: Response): Promise<void> => {
const { id } = request.body;
const { habitId } = request.params;
const deleteHabitUseCase = new DeleteHabitUseCase(this.habitRepository);
await deleteHabitUseCase.execute(id);
await deleteHabitUseCase.execute(habitId);
response.json();
};
}
32 changes: 11 additions & 21 deletions src/infra/web/routes/e2e/habit.routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const memoryHabitRepository = new MemoryHabitRepository();
const habitController = new HabitController(memoryHabitRepository);

habitRouter.get('/', habitController.index);
habitRouter.get('/:id', habitController.show);
habitRouter.get('/:habitId', habitController.show);
habitRouter.post('/', habitController.create);
habitRouter.put('/', habitController.update);
habitRouter.delete('/', habitController.destroy);
habitRouter.put('/:habitId', habitController.update);
habitRouter.delete('/:habitId', habitController.destroy);
describe('Habits Controllers', () => {
beforeAll(() => {
app.use(express.json());
Expand Down Expand Up @@ -44,24 +44,17 @@ describe('Habits Controllers', () => {
const habit = await request(app).post('/').send({
name: 'jumping',
});
const response = await request(app)
.put('/')
.query({
id: habit.body.id,
})
.send({
name: 'thinking',
});
const response = await request(app).put(`/${habit.body.id}`).send({
name: 'thinking',
});
expect(response.status).toBe(201);
});

it('DELETE', async () => {
const habit = await request(app).post('/').send({
name: 'hiking',
});
const response = await request(app).delete('/').send({
id: habit.body.id,
});
const response = await request(app).delete(`/${habit.body.id}`);
expect(response.status).toBe(200);
});

Expand Down Expand Up @@ -99,10 +92,7 @@ describe('Habits Controllers', () => {

it('should NOT be able to update a habit that not exist', async () => {
const response = await request(app)
.put('/')
.query({
id: '672c2ae2-c4fd-443d-8c2a-4bb2cdb179d8',
})
.put(`/672c2ae2-c4fd-443d-8c2a-4bb2cdb179d8`)
.send({
name: 'thinking',
});
Expand All @@ -111,9 +101,9 @@ describe('Habits Controllers', () => {
});

it('should NOT be able to delete a habit that not exist', async () => {
const response = await request(app)
.delete('/')
.send({ id: '672c2ae2-c4fd-443d-8c2a-4bb2cdb179d8' });
const response = await request(app).delete(
`/672c2ae2-c4fd-443d-8c2a-4bb2cdb179d8`,
);
expect(response.status).toBe(400);
expect(response.body).toBe('habit not found');
});
Expand Down
9 changes: 9 additions & 0 deletions src/infra/web/routes/e2e/habitCompDate.routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,13 @@ describe('Habit Complete Date Controllers', () => {
expect(completedHabits).toHaveLength(1);
expect(response.status).toBe(200);
});

it('POST should NOT be able to complete a habit with a invalid date time', async () => {
const response = await request(app).post('/').send({
habitId: '4567',
completedDate: '2000-02-03',
});
expect(response.status).toBe(400);
expect(response.body).toBe('completedDate invalid utc format');
});
});
16 changes: 6 additions & 10 deletions src/infra/web/routes/habit.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ habitRouter.get(
habitController.index,
);
habitRouter.get(
'/:id',
'/:habitId',
// #swagger.tags = ['Habits']
// #swagger.parameters['id'] = { description: 'habit id' }
// #swagger.parameters['habitId'] = { description: 'habit habitId' }
habitController.show,
);
habitRouter.post(
Expand All @@ -28,9 +28,9 @@ habitRouter.post(
habitController.create,
);
habitRouter.put(
'/',
'/:habitId',
// #swagger.tags = ['Habits']
// #swagger.parameters['id'] = { description: 'habit id' }
// #swagger.parameters['habitId'] = { description: 'habit habitId' }
/* #swagger.parameters['obj'] = {
in: 'body',
description: 'update a habit',
Expand All @@ -39,13 +39,9 @@ habitRouter.put(
habitController.update,
);
habitRouter.delete(
'/',
'/:habitId',
// #swagger.tags = ['Habits']
/* #swagger.parameters['obj'] = {
in: 'body',
description: 'habit uuid',
schema: { $ref: '#/definitions/HabitBodyId' }
} */
// #swagger.parameters['habitId'] = { description: 'habit habitId' }
habitController.destroy,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const validUtcDate = (dateString: string) => {
export const validISODate = (dateString: string) => {
const date = new Date(dateString);
return date.toISOString() === dateString;
};

0 comments on commit 27356ae

Please sign in to comment.