-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
14 changed files
with
87 additions
and
30 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
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,11 @@ | ||
class MongoServerError extends Error { | ||
public code: number; | ||
|
||
constructor(message: string, code: number) { | ||
super(message); | ||
this.name = 'MongoServerError'; | ||
this.code = code; | ||
} | ||
} | ||
|
||
export default MongoServerError; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { Elysia } from 'elysia'; | ||
import cors from '@elysiajs/cors'; | ||
import { Elysia } from 'elysia'; | ||
|
||
export default (app: Elysia) => app.use(cors()); |
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,12 +1,13 @@ | ||
import { Elysia, t } from 'elysia'; | ||
|
||
import * as userController from '../controllers/user'; | ||
|
||
export default new Elysia() | ||
.post('/users', userController.create, { | ||
body: t.Object({ | ||
name: t.String(), | ||
email: t.String(), | ||
password: t.String() | ||
name: t.String({ minLength: 1, maxLength: 256 }), | ||
email: t.String({ format: 'email', maxLength: 256 }), | ||
password: t.String({ minLength: 8, maxLength: 256 }) | ||
}) | ||
}) | ||
.get('/users', userController.fetchAll); |
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,20 +1,36 @@ | ||
import { User } from '../models/User'; | ||
import ConflictError from '../domain/exceptions/ConflictError'; | ||
import MongoServerError from '../domain/exceptions/MongoServerError'; | ||
import { User } from '../models/User'; | ||
|
||
export const create = (payload: User) => { | ||
/** | ||
* Creates a new user. | ||
* | ||
* @param payload - The user data to be created. | ||
* @returns {Promise<User>} A promise that resolves to the created user. | ||
* @throws {ConflictError} If a user with the same data already exists. | ||
* @throws {Error} If an error occurs while creating the user. | ||
*/ | ||
export async function create(payload: User) { | ||
try { | ||
const user = new User(payload); | ||
|
||
return user.save(); | ||
} catch (e: any) { | ||
if (e.name === 'MongoServerError' && e.code === 11000) { | ||
return await user.save(); | ||
} catch (e) { | ||
const error = e as MongoServerError; | ||
|
||
if (error.name === 'MongoServerError' && error.code === 11000) { | ||
throw new ConflictError('User exists.'); | ||
} | ||
|
||
throw e; | ||
throw error; | ||
} | ||
}; | ||
} | ||
|
||
export const fetchAll = () => { | ||
/** | ||
* Fetches all users from the database. | ||
* | ||
* @returns {Promise<User[]>} A promise that resolves to an array of User objects. | ||
*/ | ||
export function fetchAll() { | ||
return User.find(); | ||
}; | ||
} |
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