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

10 naming #25

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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: 1 addition & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { AppConfig, DatabaseConfig } from './config';
import { EventModule } from './resource/event/event.module';
import { NoteModule } from './resource/notes/notes.module';
import { NoteModule } from './resource/note/note.module';
import { SearchModule } from './resource/search/search.module';
import { TaskModule } from './resource/task/task.module';
import { UserModule } from './resource/user/user.module';
Expand Down
2 changes: 1 addition & 1 deletion src/decorators/OpenAPI/event.decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function GetEventExceptionByIdApi() {
);
}

export function CreateEventByCurrentUserApi() {
export function AddEventApi() {
return applyDecorators(
ApiCreatedResponse({ type: ReturnEventDTO }),
ApiNotFoundResponse({ description: 'Event not found' }),
Expand Down
2 changes: 1 addition & 1 deletion src/decorators/OpenAPI/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './auth.decorators';
export * from './event.decorators';
export * from './notes.decorators';
export * from './note.decorators';
export * from './search.decorators';
export * from './task.decorators';
export * from './user.decorators';
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
ApiNotFoundResponse,
ApiOkResponse,
} from '@nestjs/swagger';
import { editNoteDTO, NoteDTO } from '../../resource/notes/dto';
import { editNoteDTO, NoteDTO } from '../../resource/note/dto';

export function FetchUserNotesApi() {
export function GetUserNotesApi() {
return applyDecorators(ApiOkResponse({ type: [NoteDTO] }));
}

Expand All @@ -21,7 +21,7 @@ export function AddNoteApi() {
);
}

export function EditNoteByIdApi() {
export function EditNoteApi() {
return applyDecorators(
ApiOkResponse(),
ApiBadRequestResponse({ description: 'Invalid body' }),
Expand Down
4 changes: 2 additions & 2 deletions src/decorators/OpenAPI/task.decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {
ReturnTaskDTO,
} from '../../resource/task/dto';

export function GetAllUserTasksApi() {
export function GetUserTasksApi() {
return applyDecorators(ApiOkResponse({ type: [ReturnTaskDTO] }));
}

export function GetUserTaskApi() {
export function GetTaskByIdApi() {
return applyDecorators(
ApiOkResponse({ type: ReturnTaskDTO }),
ApiNotFoundResponse({ description: 'Task not found' }),
Expand Down
22 changes: 11 additions & 11 deletions src/resource/event/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { JwtGuard } from 'src/auth/guard';
import { GetUser } from 'src/decorators';
import {
AddEventExceptionApi,
CreateEventByCurrentUserApi,
AddEventApi,
DeleteEventApi,
DeleteExceptionApi,
EditEventApi,
Expand Down Expand Up @@ -54,7 +54,7 @@ export class EventController {
async getUserEvents(
@GetUser('id') userId: number,
): Promise<ReturnEventDTO[]> {
return await this.eventService.fetchUserEvents(userId);
return await this.eventService.fetchUser(userId);
}

@Get('dates')
Expand All @@ -74,7 +74,7 @@ export class EventController {
@Get(':id')
@GetEventByIdApi()
async getEventById(@Param('id') eventId: number): Promise<ReturnEventDTO> {
return await this.eventService.fetchById(eventId);
return await this.eventService.fetch(eventId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fetchById because we don't know if we fetch all or not.
You could use [serviceName].fetchAll or fetchOne and albo fetchById

}

@Get('except/:id')
Expand All @@ -86,12 +86,12 @@ export class EventController {
}

@Post()
@CreateEventByCurrentUserApi()
async createEventByCurrentUser(
@AddEventApi()
async addEvent(
@GetUser('id') userId: number,
@Body() eventDto: CreateEventDTO,
): Promise<ReturnEventDTO> {
return await this.eventService.createEvent(userId, eventDto);
return await this.eventService.add(userId, eventDto);
}

@Post('exceptions/:id')
Expand All @@ -100,7 +100,7 @@ export class EventController {
@Param('id') eventId: number,
@Body() exceptionDto: CreateEventExceptionDTO,
): Promise<ReturnEventExceptionDTO> {
return await this.eventService.createException(eventId, exceptionDto);
return await this.eventService.addException(eventId, exceptionDto);
}

@Patch(':id')
Expand All @@ -109,7 +109,7 @@ export class EventController {
@Param('id') eventId: number,
@Body() editEventDto: EditEventDTO,
): Promise<ReturnEventDTO> {
return await this.eventService.editById(eventId, editEventDto);
return await this.eventService.edit(eventId, editEventDto);
}

@Patch('exceptions/:id')
Expand All @@ -118,7 +118,7 @@ export class EventController {
@Param('id') exceptionId: number,
@Body() editExceptionDto: EditEventExceptionDTO,
): Promise<ReturnEventExceptionDTO> {
return await this.eventService.editExceptionById(
return await this.eventService.editException(
exceptionId,
editExceptionDto,
);
Expand All @@ -128,13 +128,13 @@ export class EventController {
@Delete(':id')
@DeleteEventApi()
async deleteEvent(@Param('id') eventId: number): Promise<void> {
await this.eventService.deleteById(eventId);
await this.eventService.delete(eventId);
}

@HttpCode(HttpStatus.NO_CONTENT)
@Delete('exceptions/:id')
@DeleteExceptionApi()
async deleteException(@Param('id') exceptionId: number): Promise<void> {
await this.eventService.removeExceptionById(exceptionId);
await this.eventService.deleteException(exceptionId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleteException in this case is clear, because we know that we want to delete one so it is obvious that we need for example id

}
}
16 changes: 8 additions & 8 deletions src/resource/event/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class EventService {
private readonly exceptionRepository: Repository<EventExceptionEntity>,
) {}

async fetchById(eventId: number): Promise<ReturnEventDTO> {
async fetch(eventId: number): Promise<ReturnEventDTO> {
const event = await this.eventRepository.findOne({
where: { id: eventId },
relations: { recurrencePattern: true },
Expand All @@ -42,7 +42,7 @@ export class EventService {
return event;
}

async fetchUserEvents(userId: number): Promise<ReturnEventDTO[]> {
async fetchUser(userId: number): Promise<ReturnEventDTO[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong naming convention in my opinion, service method returns Events, not a User, I think something like "fetchByUser" would be more descriptive

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

return this.eventRepository.find({
where: { createdById: userId },
relations: { recurrencePattern: true, eventExceptions: true },
Expand Down Expand Up @@ -182,7 +182,7 @@ export class EventService {
return exception;
}

async createEvent(
async add(
userId: number,
eventDto: CreateEventDTO,
): Promise<ReturnEventDTO> {
Expand All @@ -194,7 +194,7 @@ export class EventService {
return event;
}

async createException(
async addException(
eventId: number,
exceptionDto: CreateEventExceptionDTO,
): Promise<ReturnEventExceptionDTO> {
Expand All @@ -206,7 +206,7 @@ export class EventService {
return exception;
}

async editById(
async edit(
eventId: number,
editEventDto: EditEventDTO,
): Promise<ReturnEventDTO> {
Expand All @@ -228,7 +228,7 @@ export class EventService {
return eventToUpdate;
}

async editExceptionById(
async editException(
exceptionId: number,
editExceptionDto: EditEventExceptionDTO,
): Promise<ReturnEventExceptionDTO> {
Expand All @@ -245,11 +245,11 @@ export class EventService {
return exceptionToUpdate;
}

async deleteById(eventId: number): Promise<void> {
async delete(eventId: number): Promise<void> {
await this.eventRepository.delete({ id: eventId });
}

async removeExceptionById(exceptionId: number): Promise<void> {
async deleteException(exceptionId: number): Promise<void> {
await this.exceptionRepository.delete({ id: exceptionId });
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './notes.dto';
export * from './note.dto';
export * from './editNote.dto';
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import {
Post,
UseGuards,
} from '@nestjs/common';
import { NoteService } from './notes.service';
import { NoteService } from './note.service';
import { editNoteDTO, NoteDTO } from './dto';
import { JwtGuard } from '../../auth/guard';
import { GetUser } from '../../decorators';
import { UserEntity } from 'src/database/entities/user.entity';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import {
AddNoteApi,
EditNoteByIdApi,
FetchUserNotesApi,
EditNoteApi as EditNoteApi,
GetUserNotesApi as GetUserNotesApi,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, why there is 'as'?

} from '../../decorators/OpenAPI';

@UseGuards(JwtGuard)
Expand All @@ -27,20 +27,20 @@ export class NoteController {
constructor(private notesService: NoteService) {}

@Get()
@FetchUserNotesApi()
fetchUserNotes(@GetUser() user: UserEntity) {
return this.notesService.fetchUserNotes(user);
@GetUserNotesApi()
getUserNotes(@GetUser() user: UserEntity): Promise<NoteDTO[]> {
return this.notesService.fetchUser(user);
}

@Post(':id')
@Post()
@AddNoteApi()
addNote(@GetUser() user: UserEntity, @Body() noteDto: NoteDTO) {
return this.notesService.addNote(user, noteDto);
addNote(@GetUser() user: UserEntity, @Body() noteDto: NoteDTO): Promise<NoteDTO> {
return this.notesService.add(user, noteDto);
}

@Patch(':id')
@EditNoteByIdApi()
editNoteById(@Param('id') id: number, @Body() noteDto: editNoteDTO) {
return this.notesService.editNoteById(id, noteDto);
@EditNoteApi()
editNote(@Param('id') id: number, @Body() noteDto: editNoteDTO): Promise<void> {
return this.notesService.edit(id, noteDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { NoteEntity } from 'src/database/entities/notes.entity';
import { NoteService } from './notes.service';
import { NoteController } from './notes.controller';
import { NoteService } from './note.service';
import { NoteController } from './note.controller';
import { UserEntity } from 'src/database/entities/user.entity';

@Module({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ export class NoteService {
private noteRepository: Repository<NoteEntity>,
) {}

async fetchUserNotes(user: UserEntity) {
async fetchUser(user: UserEntity): Promise<NoteDTO[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

return this.noteRepository.find({
relations: ['user'],
where: { user },
});
}

async addNote(user: UserEntity, noteDto: NoteDTO) {
async add(user: UserEntity, noteDto: NoteDTO): Promise<NoteDTO> {
if (!user)
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
const newNote = this.noteRepository.create({ ...noteDto, user });

return this.noteRepository.save(newNote);
}

async editNoteById(id: number, noteDto: editNoteDTO) {
return this.noteRepository.update({ id }, { ...noteDto });
async edit(id: number, noteDto: editNoteDTO): Promise<void> {
this.noteRepository.update({ id }, { ...noteDto });
}
}
2 changes: 1 addition & 1 deletion src/resource/search/dto/SearchResult.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReturnEventDTO } from 'src/resource/event/dto';
import { NoteDTO } from 'src/resource/notes/dto';
import { NoteDTO } from 'src/resource/note/dto';
import { ReturnTaskDTO } from 'src/resource/task/dto';
import { ApiProperty } from '@nestjs/swagger';

Expand Down
26 changes: 13 additions & 13 deletions src/resource/task/task.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
AddTaskApi,
DeleteTaskApi,
EditTaskApi,
GetAllUserTasksApi,
GetUserTaskApi,
GetUserTasksApi as GetUserTasksApi,
GetTaskByIdApi as GetTaskbyIdApi,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again unnecessary 'as'

} from '../../decorators/OpenAPI';

@UseGuards(JwtGuard)
Expand All @@ -34,21 +34,21 @@ import {
export class TaskController {
constructor(private readonly taskService: TaskService) {}

@Get('/user/all')
@GetAllUserTasksApi()
async getAllUserTasks(
@Get()
@GetUserTasksApi()
async getUserTasks(
@GetUser() user: UserEntity,
): Promise<ReturnTaskDTO[]> {
return await this.taskService.getAllUserTasks(user);
return await this.taskService.fetchUser(user);
}

@Get('/user/:id')
@GetUserTaskApi()
async getUserTask(
@Get(':id')
@GetTaskbyIdApi()
async getTaskById(
@GetUser() user: UserEntity,
@Param('id', ParseIntPipe) taskId: number,
): Promise<ReturnTaskDTO> {
const task = await this.taskService.getUserTask(user, taskId);
const task = await this.taskService.fetch(user, taskId);
if (!task) {
throw new NotFoundException('Task not found');
}
Expand All @@ -62,7 +62,7 @@ export class TaskController {
@GetUser() user: UserEntity,
@Body() createTaskDTO: CreateTaskDTO,
): Promise<ReturnTaskDTO> {
return await this.taskService.addTask(user, createTaskDTO);
return await this.taskService.add(user, createTaskDTO);
}

@Patch(':id')
Expand All @@ -76,7 +76,7 @@ export class TaskController {
throw new BadRequestException('ID is not valid in URL and body');
}

const task = await this.taskService.editTask(user, editTask);
const task = await this.taskService.edit(user, editTask);
if (!task) {
throw new NotFoundException('Task not found');
}
Expand All @@ -90,7 +90,7 @@ export class TaskController {
@GetUser() user: UserEntity,
@Param('id', ParseIntPipe) taskId: number,
): Promise<void> {
const isTaskRemoved = await this.taskService.deleteTask(user, taskId);
const isTaskRemoved = await this.taskService.delete(user, taskId);
if (!isTaskRemoved) throw new NotFoundException('Task not found');
return;
}
Expand Down
Loading