Skip to content

Commit

Permalink
Merge pull request #4 from hariscs/develop
Browse files Browse the repository at this point in the history
Feat: Add Starter Routes, Controllers, and Models
  • Loading branch information
hariscs authored Feb 11, 2024
2 parents 5b7518f + 8dbf16b commit a0f1bfe
Show file tree
Hide file tree
Showing 14 changed files with 406 additions and 3,353 deletions.
3 changes: 2 additions & 1 deletion apps/api/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
NODE_ENV=development
PORT=
PORT=5000
MONGO_URI=YOUR_MONGO_URI
12 changes: 12 additions & 0 deletions apps/api/config/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import mongoose from 'mongoose'
import { log } from '@repo/logger'

export const connect_db = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI as string)
log(`MongoDB Connected here: ${conn.connection.name}`)
} catch (error) {
log(`Error: ${error}`)
process.exit(1)
}
}
9 changes: 5 additions & 4 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@
"preset": "@repo/jest-presets/jest/node"
},
"dependencies": {
"@repo/logger": "*",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"@repo/logger": "*",
"mongoose": "^8.1.1",
"morgan": "^1.10.0"
},
"devDependencies": {
"@repo/eslint-config": "*",
"@repo/jest-presets": "*",
"@repo/typescript-config": "*",
"@types/body-parser": "^1.19.5",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.11",
"@types/morgan": "^1.9.9",
"@types/node": "^20.10.6",
"@types/supertest": "^6.0.2",
"@repo/eslint-config": "*",
"@repo/jest-presets": "*",
"supertest": "^6.3.3",
"@repo/typescript-config": "*",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
}
Expand Down
29 changes: 29 additions & 0 deletions apps/api/src/controllers/job_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Request, Response } from 'express'
import { JOB_SCHEMA } from '../models/job_model'

//* @desc Get all jobs
//* route GET /api/jobs
//* @access Public
export async function get_jobs(req: Request, res: Response): Promise<void> {
try {
const jobs = await JOB_SCHEMA.find()
res.status(200).json({ jobs })
} catch (error) {
console.error('Error fetching jobs:', error)
res.status(500).json({ error: 'Internal server error' })
}
}

//* @desc Post a job
//* route POST /api/jobs/job
//* @access Private
export async function post_job(req: Request, res: Response): Promise<void> {
try {
const jobData = req.body
const newJob = await JOB_SCHEMA.create(jobData)
res.status(201).json({ message: 'Job posted', newJob })
} catch (error) {
console.error('Error fetching jobs:', error)
res.status(500).json({ error: 'Internal server error' })
}
}
4 changes: 4 additions & 0 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { log } from '@repo/logger'
import { createServer } from './server'
import job_route from './routes/job_route'

const port = process.env.PORT || 5001
const server = createServer()

server.use('/api/v1/jobs', job_route)

server.listen(port, () => {
log(`api running on ${port}`)
})
20 changes: 20 additions & 0 deletions apps/api/src/models/job_model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import mongoose, { Schema } from 'mongoose'

const job_schema = new Schema(
{
company: { type: String, required: true },
logo: { type: String },
position: { type: String, required: true },
role: { type: String, required: true },
level: { type: String, required: true },
contract: { type: String, required: true },
location: { type: String, required: true },
languages: { type: [String], required: true },
tools: { type: [String], default: [] },
},
{
timestamps: true,
}
)

export const JOB_SCHEMA = mongoose.model('Job', job_schema)
9 changes: 9 additions & 0 deletions apps/api/src/routes/job_route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from 'express'
import { get_jobs, post_job } from '../controllers/job_controller'

const router = express.Router()

router.get('/', get_jobs)
router.post('/job', post_job)

export default router
7 changes: 3 additions & 4 deletions apps/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import express, { type Express } from 'express'
import morgan from 'morgan'
import cors from 'cors'
import { config } from 'dotenv'
import { connect_db } from '../config/db'

config()
connect_db()

export const createServer = (): Express => {
const app = express()
Expand All @@ -14,10 +16,7 @@ export const createServer = (): Express => {
.use(urlencoded({ extended: true }))
.use(json())
.use(cors())
.get('/message/:name', (req, res) => {
return res.json({ message: `hello ${req.params.name}` })
})
.get('/status', (_, res) => {
.get('/health', (_, res) => {
return res.json({ ok: true })
})

Expand Down
12 changes: 12 additions & 0 deletions apps/api/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type Job = {
company: string
logo?: string
featured?: boolean
position: string
role: string
level: string
contract: string
location: string
languages: string[]
tools: string[]
}
16 changes: 16 additions & 0 deletions apps/api/types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@repo/types",
"version": "1.0.0",
"description": "",
"main": "./src/index.ts",
"types": "./src/index.ts",
"exports": {
".": "./src/index.ts"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT"
}
Loading

0 comments on commit a0f1bfe

Please sign in to comment.