Skip to content

Commit

Permalink
Rename everything from 'note' to 'question' [cl9fdtpzc98900myzkzgiqxfu]
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloendoh committed Apr 4, 2024
1 parent 3e9f360 commit 0a51576
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 268 deletions.
54 changes: 29 additions & 25 deletions src/domains/define/flashnotes/FlashnotesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import {
Put,
QueryParam,
} from "routing-controllers"
import { Note } from "../../../entities/define/Note"
import { User } from "../../../entities/User"
import { getNoteRepository } from "../../../repositories/define/NoteRepository"
import { Question } from "../../../entities/define/Question"
import { getQuestionRepository } from "../../../repositories/define/QuestionRepository"
import { FlashnotesService } from "./FlashnotesService"
import { CreateManyNotesDto } from "./types/CreateManyNotesDto"
import { CreateManyQuestionsDto } from "./types/CreateManyNotesDto"
import { FlashnotesSearchType } from "./types/FlashnotesSearchType"

@JsonController()
export class FlashnotesController {
constructor(
private flashnotesService = new FlashnotesService(),

private noteRepo = getNoteRepository()
private questionRepo = getQuestionRepository()
) {}

@Get("/flashnotes/search")
Expand All @@ -39,49 +39,53 @@ export class FlashnotesController {
})
}

@Get("/define/note")
async getAllNotes(
@Get("/define/question")
async getAllQuestions(
@CurrentUser({ required: true })
user: User
) {
return this.noteRepo.getAllNotesFromUserId(user.id)
return this.questionRepo.getAllQuestionsFromUserId(user.id)
}

@Post("/define/note")
async saveNote(
@Post("/define/question")
async saveQuestion(
@CurrentUser({ required: true })
user: User,
@Body() sentNote: Note
@Body() sentQuestion: Question
) {
if (sentNote.id)
return this.flashnotesService.updateQuestion(sentNote, user.id)
if (sentQuestion.id)
return this.flashnotesService.updateQuestion(sentQuestion, user.id)

return this.flashnotesService.createQuestion(sentNote, user.id)
return this.flashnotesService.createQuestion(sentQuestion, user.id)
}

@Put("/define/note/many")
async updateManyNotes(
@Put("/define/question/many")
async updateManyQuestions(
@CurrentUser({ required: true })
user: User,
@Body() sentNotes: Note[]
@Body() sentQuestions: Question[]
) {
const isOwner = await getNoteRepository().isOwner(sentNotes, user.id)
if (!isOwner) throw new ForbiddenError("User is not owner of all notes.")
const isOwner = await getQuestionRepository().isOwner(
sentQuestions,
user.id
)
if (!isOwner)
throw new ForbiddenError("User is not owner of all questions.")

await this.noteRepo.save(sentNotes)
return this.noteRepo.getAllNotesFromUserId(user.id)
await this.questionRepo.save(sentQuestions)
return this.questionRepo.getAllQuestionsFromUserId(user.id)
}

@Post("/define/doc/:docId/notes/many")
async createManyNotes(
@Post("/define/doc/:docId/questions/many")
async createManyQuestions(
@CurrentUser({ required: true })
user: User,
@Body() body: CreateManyNotesDto,
@Body() body: CreateManyQuestionsDto,
@Param("docId") docId: number
) {
return this.flashnotesService.createManyNotes(
return this.flashnotesService.createManyQuestions(
docId,
body.notesQuantity,
body.questionsQuantity,
user.id
)
}
Expand Down
63 changes: 34 additions & 29 deletions src/domains/define/flashnotes/FlashnotesService.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
import { NotFoundError } from "routing-controllers"
import { Note } from "../../../entities/define/Note"
import { Question } from "../../../entities/define/Question"
import DocRepository from "../../../repositories/define/DocRepository"
import NoteRepository from "../../../repositories/define/NoteRepository"
import QuestionRepository from "../../../repositories/define/QuestionRepository"
import { FlashnotesSearchType } from "./types/FlashnotesSearchType"

export class FlashnotesService {
constructor(
private noteRepository = NoteRepository,
private questionRepo = QuestionRepository,
private docRepo = DocRepository
) {}

async createQuestion(sentNote: Note, requesterId: number) {
sentNote.userId = requesterId
async createQuestion(sentQuestion: Question, requesterId: number) {
sentQuestion.userId = requesterId

const noteWithHighestIndex =
await this.noteRepository.findNoteWithHighestIndex(sentNote.docId)
const questionWithHighestIndex =
await this.questionRepo.findQuestionWithHighestIndex(sentQuestion.docId)

sentNote.index = noteWithHighestIndex ? noteWithHighestIndex.index + 1 : 0
sentQuestion.index = questionWithHighestIndex
? questionWithHighestIndex.index + 1
: 0

return this.noteRepository.save({
...sentNote,
return this.questionRepo.save({
...sentQuestion,
doc: undefined,
})
}

async updateQuestion(sentNote: Note, requesterId: number) {
const found = await this.noteRepository.findOne({
where: { userId: requesterId, id: sentNote.id },
async updateQuestion(sentQuestion: Question, requesterId: number) {
const found = await this.questionRepo.findOne({
where: { userId: requesterId, id: sentQuestion.id },
})

if (!found)
throw new NotFoundError("Note doesn't exist or user is not owner")
throw new NotFoundError("Question doesn't exist or user is not owner")

return this.noteRepository.save({
...sentNote,
return this.questionRepo.save({
...sentQuestion,
userId: requesterId,
doc: undefined,
})
Expand All @@ -49,25 +51,29 @@ export class FlashnotesService {
if (type === "questions") {
return {
docs: [],
notes: await this.noteRepository.searchNotesByQuestionText(
questions: await this.questionRepo.searchQuestionsByQuestionText(
query,
requesterId
),
}
}

const [docs, notes] = await Promise.all([
const [docs, questions] = await Promise.all([
this.docRepo.searchDocs(query, requesterId),
this.noteRepository.searchNotes(query, requesterId),
this.questionRepo.searchQuestions(query, requesterId),
])

return {
docs,
notes,
questions,
}
}

async createManyNotes(docId: number, quantity: number, requesterId: number) {
async createManyQuestions(
docId: number,
quantity: number,
requesterId: number
) {
const doc = await this.docRepo.findOne({
where: {
id: docId,
Expand All @@ -77,26 +83,25 @@ export class FlashnotesService {

if (!doc) throw new NotFoundError("Doc not found or user is not owner")

const highestIndexNote = await this.noteRepository.findNoteWithHighestIndex(
docId
)
const highestIndexQuestion =
await this.questionRepo.findQuestionWithHighestIndex(docId)

return this.noteRepository.createEmptyNotes({
return this.questionRepo.createEmptyQuestions({
docId,
userId: requesterId,
initialIndex: highestIndexNote ? highestIndexNote.index + 1 : 0,
initialIndex: highestIndexQuestion ? highestIndexQuestion.index + 1 : 0,
quantity: quantity,
})
}

deleteQuestion = async (questionId: number, requesterId: number) => {
const found = await this.noteRepository.findOne({
const found = await this.questionRepo.findOne({
where: { userId: requesterId, id: questionId },
})

if (!found)
throw new NotFoundError("Note doesn't exist or user is not owner")
throw new NotFoundError("Question doesn't exist or user is not owner")

return this.noteRepository.delete(questionId)
return this.questionRepo.delete(questionId)
}
}
4 changes: 2 additions & 2 deletions src/domains/define/flashnotes/types/CreateManyNotesDto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IsNumber, Max } from "class-validator"

export class CreateManyNotesDto {
export class CreateManyQuestionsDto {
@IsNumber()
@Max(25)
notesQuantity: number
questionsQuantity: number
}
14 changes: 7 additions & 7 deletions src/domains/doc/DocController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
Req,
UseBefore,
} from "routing-controllers"
import { Doc } from "../../entities/define/Doc"
import { User } from "../../entities/User"
import { Doc } from "../../entities/define/Doc"
import { MyAuthMiddleware } from "../../middlewares/MyAuthMiddleware"
import { getDocRepository } from "../../repositories/define/DocRepository"
import { getNoteRepository } from "../../repositories/define/NoteRepository"
import { getQuestionRepository } from "../../repositories/define/QuestionRepository"
import { MyAuthRequest } from "../../utils/MyAuthRequest"
import { DocService } from "./DocService"

Expand All @@ -24,7 +24,7 @@ export class DocController {
constructor(
private docService = new DocService(),
private docRepository = getDocRepository(),
private noteRepo = getNoteRepository()
private questionRepo = getQuestionRepository()
) {}

@Delete("/docs/:docId")
Expand Down Expand Up @@ -83,13 +83,13 @@ export class DocController {
return saved
}

@Delete("/define/doc/:id/clear-empty-notes")
async clearEmptyNotes(
@Delete("/define/doc/:id/clear-empty-questions")
async clearEmptyQuestions(
@CurrentUser({ required: true }) user: User,
@Param("id") docId: number
) {
await this.noteRepo.removeEmptyNotesFromDocId(docId, user.id)
await this.questionRepo.removeEmptyQuestionsFromDocId(docId, user.id)

return this.noteRepo.getAllNotesFromUserId(user.id)
return this.questionRepo.getAllQuestionsFromUserId(user.id)
}
}
6 changes: 5 additions & 1 deletion src/domains/doc/DocService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export class DocService {

const savedDoc = await this.docRepository.save(newDoc)

await this.flashnotesService.createManyNotes(savedDoc.id, 10, requesterId)
await this.flashnotesService.createManyQuestions(
savedDoc.id,
10,
requesterId
)
return savedDoc
}
}
6 changes: 3 additions & 3 deletions src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Test } from "./Test"
import { UserGotIt } from "./UserGotIt"
import { UserPreference } from "./UserPreference"
import { Doc } from "./define/Doc"
import { Note } from "./define/Note"
import { Question } from "./define/Question"
import { Follow } from "./feed/Follow"
import { FollowingTag } from "./feed/FollowingTag"
import { LastSeenResource } from "./feed/LastSeenResource"
Expand Down Expand Up @@ -165,8 +165,8 @@ export class User {
@OneToMany((_) => Doc, (doc) => doc.user)
docs: Doc[]

@OneToMany((_) => Note, (note) => note.user)
notes: Note[]
@OneToMany((_) => Question, (question) => question.user)
questions: Question[]

// BigDecisions
@OneToMany((_) => Decision, (decision) => decision.user)
Expand Down
30 changes: 15 additions & 15 deletions src/entities/define/Doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,42 @@ import {
OneToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from "typeorm";
import { Folder } from "../playground/file-system/Folder";
import { User } from "../User";
import { Note } from "./Note";
} from "typeorm"
import { User } from "../User"
import { Folder } from "../playground/file-system/Folder"
import { Question } from "./Question"

@Entity()
export class Doc {
@PrimaryGeneratedColumn()
id: number;
id: number

@ManyToOne((type) => User, (user) => user.docs, {
onDelete: "CASCADE",
})
user: User;
user: User
@Column()
userId: number;
userId: number

@ManyToOne(() => Folder, (folder) => folder.docs, { nullable: true })
folder: Folder | null;
folder: Folder | null

@Column({ nullable: true })
folderId: number | null;
folderId: number | null

@OneToMany(() => Note, (note) => note.doc)
notes: Note[];
@OneToMany(() => Question, (question) => question.doc)
questions: Question[]

// END OF RELATIONS
@Column()
title: string;
title: string

@CreateDateColumn()
createdAt: string;
createdAt: string

@UpdateDateColumn()
updatedAt: string;
updatedAt: string

@Column({ type: "timestamp without time zone", nullable: true })
lastOpenedAt: string;
lastOpenedAt: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
import { User } from "../User"
import { Doc } from "./Doc"

@Entity()
export class Note {
@Entity({
name: "question",
})
export class Question {
@PrimaryGeneratedColumn()
id: number

Expand All @@ -21,7 +23,7 @@ export class Note {
@Column()
userId: number

@ManyToOne(() => Doc, (doc) => doc.notes, {
@ManyToOne(() => Doc, (doc) => doc.questions, {
onDelete: "CASCADE",
})
doc: Doc
Expand Down
Loading

0 comments on commit 0a51576

Please sign in to comment.