diff --git a/backend/user-service/src/models/user.repository.ts b/backend/user-service/src/models/user.repository.ts new file mode 100644 index 0000000000..c9d00c3cc8 --- /dev/null +++ b/backend/user-service/src/models/user.repository.ts @@ -0,0 +1,32 @@ +import { CreateUserDto } from '../types/CreateUserDto' +import { IUser } from '../types/IUser' +import { UserDto } from '../types/UserDto' +import userModel from './user.model' + +export async function findAllUsers(): Promise { + return userModel.find() +} + +export async function findOneUserById(id: string): Promise { + return userModel.findById(id) +} + +export async function findOneUserByUsername(username: string): Promise { + return userModel.findOne({ username }) +} + +export async function findOneUserByEmail(email: string): Promise { + return userModel.findOne({ email }) +} + +export async function createUser(dto: CreateUserDto): Promise { + return userModel.create(dto) +} + +export async function updateUser(id: string, dto: UserDto): Promise { + return userModel.findByIdAndUpdate(id, dto, { new: true }) +} + +export async function deleteUser(id: string): Promise { + await userModel.findByIdAndDelete(id) +} diff --git a/backend/user-service/src/server.ts b/backend/user-service/src/server.ts index 1414a41af8..9630448bd8 100644 --- a/backend/user-service/src/server.ts +++ b/backend/user-service/src/server.ts @@ -1,5 +1,6 @@ import 'dotenv/config' import http, { Server } from 'http' +import { connect } from 'mongoose' import Winston, { Logger } from 'winston' import index from './index' @@ -16,9 +17,22 @@ export const logger: Logger = Winston.createLogger({ }) const port: string = process.env.PORT ?? '3000' +const dbUrl: string | undefined = process.env.DB_URL +if (!dbUrl) { + logger.error(`[Init] DB_URL is not set`) + process.exit(1) +} const server: Server = http.createServer(index) +connect(dbUrl) + .then(() => { + logger.info(`[Init] Connected to MongoDB`) + }) + .catch((error: Error) => { + logger.error(`[Init] ${error.message}`) + }) + server.listen(port, async () => { logger.info(`[Init] Server is listening on port ${port}`) })