generated from TritonSE/onboarding
-
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.
* Implement Part 2.2 * Implement Part 2.2 * Minor change * Implement 2.3
- Loading branch information
1 parent
ceb8338
commit 2920549
Showing
22 changed files
with
463 additions
and
51 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
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
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
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,31 @@ | ||
import { RequestHandler } from "express"; | ||
import createHttpError from "http-errors"; | ||
import UserModel from "src/models/user"; | ||
|
||
export const createUser: RequestHandler = async (req, res, next) => { | ||
const { name, profilePictureURL } = req.body; | ||
|
||
try { | ||
const user = await UserModel.create({ | ||
name: name, | ||
profilePictureURL: profilePictureURL, | ||
}); | ||
res.status(201).json(user); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export const getUser: RequestHandler = async (req, res, next) => { | ||
const { id } = req.params; | ||
|
||
try { | ||
const user = await UserModel.findById(id); | ||
if (user === null) { | ||
throw createHttpError(404, "User not found."); | ||
} | ||
res.status(200).json(user); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
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
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,9 @@ | ||
import { InferSchemaType, Schema, model } from "mongoose"; | ||
|
||
const userSchema = new Schema({ | ||
name: { type: String, required: true }, | ||
profilePictureURL: { type: String, required: false }, | ||
}); | ||
|
||
type User = InferSchemaType<typeof userSchema>; | ||
export default model<User>("User", userSchema); |
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,11 @@ | ||
import express from "express"; | ||
|
||
import * as UserController from "src/controllers/user"; | ||
import * as UserValidator from "src/validators/user"; | ||
|
||
const router = express.Router(); | ||
|
||
router.post("/", UserValidator.createUser, UserController.createUser); | ||
router.get("/:id", UserController.getUser); | ||
|
||
export default router; |
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,17 @@ | ||
import { body } from "express-validator"; | ||
|
||
const makeNameValidator = () => | ||
body("title") | ||
.exists() | ||
.withMessage("title is required") | ||
.bail() | ||
.isString() | ||
.withMessage("title must be a string") | ||
.bail() | ||
.notEmpty() | ||
.withMessage("title cannot be empty"); | ||
|
||
const makeURLValidator = () => | ||
body("profilePictureURL").optional().isString().withMessage("description must be a string"); | ||
|
||
export const createUser = [makeNameValidator(), makeURLValidator()]; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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,34 @@ | ||
import { get, handleAPIError } from "src/api/requests"; | ||
|
||
import type { APIResult } from "src/api/requests"; | ||
|
||
// function parseUser(user: UserJSON): User { | ||
// return { | ||
// _id: user._id, | ||
// name: user.name, | ||
// profilePictureURL: user.profilePictureURL, | ||
// }; | ||
// } | ||
|
||
export interface User { | ||
_id: string; | ||
name: string; | ||
profilePictureURL: string; | ||
} | ||
|
||
// interface UserJSON { | ||
// _id: string; | ||
// name: string, | ||
// profilePictureURL: string, | ||
// } | ||
|
||
export async function getUser(id: string): Promise<APIResult<User>> { | ||
try { | ||
const response = await get(`/api/user/${id}`); | ||
const json = await response.json(); | ||
|
||
return { success: true, data: json }; | ||
} catch (error) { | ||
return handleAPIError(error); | ||
} | ||
} |
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
Oops, something went wrong.