-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: base de code minimal * feat: shared models and types * fix: missing await * feat: watch shared on server * feat: wip naming types and schemas * fix: path * chore: improve typings --------- Co-authored-by: David Dela Cruz <david.dela.cruz@beta.gouv.fr> Co-authored-by: Antoine Bigard <bigard.antoine@gmail.com>
- Loading branch information
1 parent
d4d7702
commit fb6ff23
Showing
12 changed files
with
200 additions
and
49 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,9 @@ | ||
import usersModelDescriptor from "shared/models/user.model"; | ||
|
||
export interface IModelDescriptor { | ||
schema: unknown; | ||
indexes: unknown; | ||
collectionName: string; | ||
} | ||
|
||
export const modelDescriptors: IModelDescriptor[] = [usersModelDescriptor]; |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { Server } from "../../server"; | ||
import { coreRoutes } from "./routes/core.routes"; | ||
import { userRoutes } from "./routes/user.routes"; | ||
|
||
export const registerCoreModule = ({ server }: { server: Server }) => { | ||
coreRoutes({ server }); | ||
userRoutes({ server }); | ||
}; |
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,39 @@ | ||
import { IUser } from "shared/models/user.model"; | ||
import { SReqPostUser, SResPostUser } from "shared/routes/user.routes"; | ||
|
||
import { getDbCollection } from "../../../db/mongodb"; | ||
import { Server } from "../../../server"; | ||
|
||
export const userRoutes = ({ server }: { server: Server }) => { | ||
/** | ||
* Créer un utilisateur | ||
*/ | ||
server.post( | ||
"/user", | ||
{ | ||
schema: { | ||
body: SReqPostUser, | ||
response: { 200: SResPostUser }, | ||
} as const, | ||
}, | ||
async (request, response) => { | ||
try { | ||
const { insertedId: userId } = await getDbCollection("users").insertOne( | ||
request.body | ||
); | ||
|
||
const user = await getDbCollection("users").findOne<IUser>({ | ||
_id: userId, | ||
}); | ||
|
||
if (!user) { | ||
throw new Error("User not created"); | ||
} | ||
|
||
return response.status(200).send(user); | ||
} catch (error) { | ||
console.error(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
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,21 @@ | ||
import { FromSchema } from "json-schema-to-ts"; | ||
|
||
const collectionName = "users"; | ||
|
||
const indexes = () => { | ||
return [[{ name: 1 }, { name: "name" }]]; | ||
}; | ||
|
||
export const SUser = { | ||
type: "object", | ||
properties: { | ||
_id: { type: "string" }, | ||
name: { type: "string" }, | ||
email: { type: "string" }, | ||
}, | ||
required: ["name"], | ||
} as const; | ||
|
||
export type IUser = FromSchema<typeof SUser>; | ||
|
||
export default { schema: SUser, indexes, collectionName }; |
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,18 @@ | ||
import { FromSchema } from "json-schema-to-ts"; | ||
|
||
import { IUser, SUser } from "../models/user.model"; | ||
|
||
export const SReqPostUser = { | ||
type: "object", | ||
properties: { | ||
name: { type: "string" }, | ||
email: { type: "string", format: "email" }, | ||
}, | ||
required: ["name"], | ||
} as const; | ||
|
||
export type IReqPostUser = FromSchema<typeof SReqPostUser>; | ||
|
||
|
||
export const SResPostUser = SUser; | ||
export interface IResPostUser extends IUser {} |
Oops, something went wrong.