Skip to content

Commit

Permalink
returning decisions
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloendoh committed Jan 7, 2024
1 parent 381d17f commit 3e9f360
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/domains/BigDecisions/DecisionTableController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
BadRequestError,
Body,
CurrentUser,
Delete,
JsonController,
NotFoundError,
Param,
Post,
} from "routing-controllers"
import { DecisionTable } from "../../entities/BigDecisions/DecisionTable"
import { User } from "../../entities/User"
import DecisionRepository from "../../repositories/BigDecisions/DecisionRepository"
import DecisionTableRepository from "../../repositories/BigDecisions/DecisionTableRepository"
import { myConsoleError } from "../../utils/myConsoleError"

const repo = DecisionTableRepository
const decisionRepo = DecisionRepository

@JsonController("/BigDecisions/decisionTable")
export class DecisionTableController {
@Delete("/:id")
async deleteQuestion(
@CurrentUser({ required: true })
user: User,
@Param("id") id: string
) {
const tableId = Number(id)

try {
const table = await repo.findOne({
where: { id: tableId, userId: user.id },
})

if (!table) {
throw new NotFoundError(`Doc doesn't exist or user doesn't own it.`)
}

await repo.remove(table)
return "OK"
} catch (err) {
myConsoleError(err.message)
throw new NotFoundError(err.message)
}
}

@Post("/")
async saveQuestion(
@CurrentUser({ required: true })
user: User,
@Body() sent: DecisionTable
) {
try {
if (sent.id) {
const found = await repo.findOne({
where: { id: sent.id, userId: user.id },
})

if (!found) {
throw new NotFoundError(`Doesn't exist or user doesn't own it.`)
}
}

sent.userId = user.id
const saved = await repo.save(sent)

const fullDecision = await decisionRepo.getFullDecision(saved.decisionId)
return fullDecision
} catch (err) {
myConsoleError(err.message)
throw new BadRequestError(err.message)
}
}

@Post("/sortProblemsByWeight")
async sortProblemsByWeight(
@CurrentUser({ required: true })
user: User,
@Body() body: { tableId: number; order: "asc" | "desc" }
) {
try {
const { tableId: decisionTableId, order } = body

const found = await repo.findOne({
where: { id: decisionTableId, userId: user.id },
})

if (!found) {
throw new BadRequestError(`Doesn't exist or user doesn't own it.`)
}

// sorting by order
if (order !== "asc" && order !== "desc")
throw new BadRequestError(`Invalid order`)

await repo.sortProblemsByWeight(found.id, order)

const fullDecision = await decisionRepo.getFullDecision(found.decisionId)
return fullDecision
} catch (err) {
myConsoleError(err.message)
throw new BadRequestError(err.message)
}
}
}
42 changes: 42 additions & 0 deletions src/domains/BigDecisions/DecisionTableItemController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
Body,
CurrentUser,
JsonController,
NotFoundError,
Post,
} from "routing-controllers"
import { dataSource } from "../../dataSource"
import { DecisionTableItem } from "../../entities/BigDecisions/DecisionTableItem"
import { User } from "../../entities/User"
import { myConsoleError } from "../../utils/myConsoleError"

const itemRepo = dataSource.getRepository(DecisionTableItem)

@JsonController("/BigDecisions/decisionTableItem")
export class DecisionTableItemController {
@Post("/")
async saveItem(
@CurrentUser({ required: true })
user: User,
@Body() sent: DecisionTableItem
) {
try {
if (sent.id) {
const found = await itemRepo.findOne({
where: { id: sent.id, userId: user.id },
})

if (!found) {
throw new NotFoundError(`Doesn't exist or user doesn't own it.`)
}
}

sent.userId = user.id
const saved = await itemRepo.save(sent)
return saved
} catch (err) {
myConsoleError(err.message)
throw new NotFoundError(err.message)
}
}
}
95 changes: 95 additions & 0 deletions src/domains/BigDecisions/DecisionV2Controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
BadRequestError,
Body,
CurrentUser,
Delete,
Get,
JsonController,
Param,
Post,
} from "routing-controllers"
import { dataSource } from "../../dataSource"
import { Decision } from "../../entities/BigDecisions/Decision"
import { DecisionTableItem } from "../../entities/BigDecisions/DecisionTableItem"
import { User } from "../../entities/User"
import DecisionRepository from "../../repositories/BigDecisions/DecisionRepository"
import DecisionTableRepository from "../../repositories/BigDecisions/DecisionTableRepository"
import { myConsoleError } from "../../utils/myConsoleError"

const repo = DecisionTableRepository
const itemRepo = dataSource.getRepository(DecisionTableItem)

const decisionRepo = DecisionRepository

@JsonController("/v2/BigDecisions/decision")
export class DecisionTableItemController {
@Post("/")
async saveItem(
@CurrentUser({ required: true })
user: User,
@Body() sentDecision: Decision
) {
try {
if (sentDecision.id) {
// TODO
// check ownership
const decision = await decisionRepo.findOne({
where: { id: sentDecision.id, userId: user.id },
})

if (!decision) {
throw new BadRequestError(`Doc doesn't exist or user doesn't own it.`)
}
}

sentDecision.userId = user.id
const saved = await decisionRepo.save(sentDecision)
const fullSaved = await decisionRepo.getFullDecision(saved.id)

// const allDecisions = await decisionRepo.getAllFromUser(req.user.id)
return fullSaved
} catch (err) {
myConsoleError(err.message)
throw new BadRequestError(err.message)
}
}

@Get("/")
async getAll(
@CurrentUser({ required: true })
user: User
) {
try {
const allDecisions = await decisionRepo.getAllFromUser(user.id)
return allDecisions
} catch (err) {
myConsoleError(err.message)
throw new BadRequestError(err.message)
}
}

@Delete("/:id")
async deleteDecision(
@CurrentUser({ required: true })
user: User,
@Param("id") id: string
) {
const decisionId = parseFloat(id)

try {
const decision = await decisionRepo.findOne({
where: { id: decisionId, userId: user.id },
})

if (!decision) {
throw new BadRequestError(`Doc doesn't exist or user doesn't own it.`)
}

await decisionRepo.remove(decision)
return "OK"
} catch (err) {
myConsoleError(err.message)
throw new BadRequestError(err.message)
}
}
}

0 comments on commit 3e9f360

Please sign in to comment.