-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
381d17f
commit 3e9f360
Showing
3 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |