Skip to content

Commit

Permalink
[#7] feat: add user repository
Browse files Browse the repository at this point in the history
  • Loading branch information
glemenneo committed Sep 18, 2024
1 parent c575df8 commit 139d23c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
32 changes: 32 additions & 0 deletions backend/user-service/src/models/user.repository.ts
Original file line number Diff line number Diff line change
@@ -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<IUser[]> {
return userModel.find()
}

export async function findOneUserById(id: string): Promise<IUser | null> {
return userModel.findById(id)
}

export async function findOneUserByUsername(username: string): Promise<IUser | null> {
return userModel.findOne({ username })
}

export async function findOneUserByEmail(email: string): Promise<IUser | null> {
return userModel.findOne({ email })
}

export async function createUser(dto: CreateUserDto): Promise<IUser> {
return userModel.create(dto)
}

export async function updateUser(id: string, dto: UserDto): Promise<IUser | null> {
return userModel.findByIdAndUpdate(id, dto, { new: true })
}

export async function deleteUser(id: string): Promise<void> {
await userModel.findByIdAndDelete(id)
}
14 changes: 14 additions & 0 deletions backend/user-service/src/server.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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}`)
})

0 comments on commit 139d23c

Please sign in to comment.