Skip to content

Commit

Permalink
[Feat] post creators route (#196)
Browse files Browse the repository at this point in the history
* Implement createCreator and  creator post route

* Lint

* Lint

* fixes

* lint fix

Co-authored-by: Shrinjay <[email protected]>
  • Loading branch information
helenellyx and Shrinjay authored Dec 8, 2022
1 parent 37a4899 commit 00f06d1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const creatorDtoValidator = async (
if (!validatePrimitive(req.body.genre, "string")) {
return res.status(400).send(getApiValidationError("genre", "string"));
}
if (!validatePrimitive(req.body.rate, "float")) {
if (!validatePrimitive(req.body.rate, "number")) {
return res.status(400).send(getApiValidationError("rate", "float"));
}
if (!validatePrimitive(req.body.ageRange, "string")) {
Expand Down Expand Up @@ -55,7 +55,7 @@ export const creatorCreateUpdateDtoValidator = async (
return res.status(400).send(getApiValidationError("genre", "string"));
}
if (!validatePrimitive(req.body.rate, "float")) {
return res.status(400).send(getApiValidationError("rate", "float"));
return res.status(400).send(getApiValidationError("rate", "number"));
}
if (!validatePrimitive(req.body.ageRange, "string")) {
return res.status(400).send(getApiValidationError("ageRange", "string"));
Expand Down
5 changes: 4 additions & 1 deletion backend/typescript/middlewares/validators/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Type = "string" | "integer" | "boolean" | "Role" | "float";
type Type = "string" | "integer" | "boolean" | "Role" | "float" | "number";

const allowableContentTypes = new Set([
"text/plain",
Expand All @@ -18,6 +18,9 @@ export const validatePrimitive = (value: unknown, type: Type): boolean => {
case "boolean": {
return typeof value === "boolean";
}
case "number": {
return typeof value === "number";
}
case "integer": {
return typeof value === "number" && Number.isInteger(value);
}
Expand Down
28 changes: 26 additions & 2 deletions backend/typescript/rest/creatorRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CreatorService from "../services/implementations/creatorService";
import ICreatorService from "../services/interfaces/creatorService";

const creatorRouter: Router = Router();
const creatorService: ICreatorService = new CreatorService();
creatorRouter.get("/");

interface CreatorReqQuery {
Expand All @@ -16,8 +17,6 @@ interface CreatorReqQuery {
status?: string;
}

const creatorService: ICreatorService = new CreatorService();

creatorRouter.get(
"/",
isAuthorizedByRole(new Set(["Admin", "Subscriber", "Author"])),
Expand Down Expand Up @@ -71,4 +70,29 @@ creatorRouter.put(
},
);

creatorRouter.post(
"/",
isAuthorizedByRole(new Set(["Admin", "Subscriber", "Author"])),
creatorDtoValidator,
async (req, res) => {
try {
if (req.body.isApproved) throw new Error("invalid creator");
await creatorService.createCreator({
id: req.body.id,
userId: req.body.userId,
location: req.body.location,
rate: req.body.rate,
genre: req.body.genre,
ageRange: req.body.ageRange,
timezone: req.body.timezone,
bio: req.body.bio,
});

res.status(200).json({ message: "Created creator!" });
} catch (e: unknown) {
sendErrorResponse(e, res);
}
},
);

export default creatorRouter;
21 changes: 20 additions & 1 deletion backend/typescript/services/implementations/creatorService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CreatorDTO } from "../../types";
import { CreatorCreateUpdateDTO, CreatorDTO } from "../../types";
import logger from "../../utilities/logger";
import { getErrorMessage } from "../../utilities/errorResponse";
import ICreatorService from "../interfaces/creatorService";
Expand Down Expand Up @@ -119,7 +119,26 @@ class CreatorService implements ICreatorService {
Logger.error(
`Failed to approve user. Reason = ${getErrorMessage(error)}`,
);
throw error;
}
}

async createCreator(creator: CreatorCreateUpdateDTO): Promise<void> {
let newCreator: Creator;
try {
newCreator = await Creator.create({
user_id: creator.userId,
location: creator.location,
rate: creator.rate,
genre: creator.genre,
ageRange: creator.ageRange,
timezone: creator.timezone,
bio: creator.bio,
});
} catch (error) {
Logger.error(
`Failed to create creator. Reason = ${getErrorMessage(error)}`,
);
throw error;
}
}
Expand Down
10 changes: 9 additions & 1 deletion backend/typescript/services/interfaces/creatorService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CreatorDTO } from "../../types";
import { CreatorDTO, CreatorCreateUpdateDTO } from "../../types";

interface ICreatorService {
/**
Expand Down Expand Up @@ -30,6 +30,14 @@ interface ICreatorService {
* @param userId The id of the user we want to make an approved creator.
*/
approveCreator(userId: string): Promise<void>;

/**
* Create new creator in databse
* @param CreatorCreateUpdateDTO
* @returns void
* @throws Error if creating a creator fails
*/
createCreator(creator: CreatorCreateUpdateDTO): Promise<void>;
}

export default ICreatorService;

0 comments on commit 00f06d1

Please sign in to comment.