Skip to content

Commit

Permalink
added: merged with development
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethea2 committed Apr 4, 2024
2 parents 64287f7 + df5b732 commit a783145
Show file tree
Hide file tree
Showing 47 changed files with 14,306 additions and 11,472 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SECRET = 'STSWENGGGG'



4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
node_modules/
coverage
.env
5 changes: 3 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],

};
51 changes: 51 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"cors": "^2.8.5",
"dotenv": "^16.4.1",
"express": "^4.18.2",
"express-fileupload": "^1.5.0",
"jsonwebtoken": "^9.0.2",
"mongodb": "^6.3.0",
"mongoose": "^8.1.1",
Expand All @@ -28,6 +29,7 @@
"@types/bcrypt": "^5.0.2",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/express-fileupload": "^1.5.0",
"@types/jsonwebtoken": "^9.0.5",
"@types/morgan": "^1.9.9",
"@types/node": "^20.11.10",
Expand Down
5 changes: 4 additions & 1 deletion backend/src/api/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export const login = async (req: Request, res: Response) => {
const user = await User.login(username, password)
const token = createToken(user._id)
return res.status(200).json({
token
token,
id: user._id,
username: user.username,
picture: user.picture
})
} catch (error) {
const result = error as Error
Expand Down
133 changes: 133 additions & 0 deletions backend/src/api/controllers/pet.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Request, Response } from "express"
import Pet from "../../models/pet.model"
import jwt, { JwtPayload } from "jsonwebtoken"
import { UploadedFile } from "express-fileupload"
import fs from "fs"

export const createPet = async (req: Request, res: Response) => {
try {
const { name, bday, sex, breed, age } = req.body
const { authorization } = req.headers
if (!authorization) {
return res.status(401).json({ message: "Token required!" })
}
const token = authorization.split(" ")[1]
const { _id } = jwt.verify(token, process.env.SECRET!) as JwtPayload

const pet = await Pet.addPets(name, breed, bday, _id, sex, age)
if (!pet) {
return res.status(401).json({ message: "Pet creation failed" })
}
return res.status(200).json({ message: "success", data: pet })
} catch (e) {
const result = e as Error
return res.status(500).json({ message: result.message })
}
}

export const getUserPets = async (req: Request, res: Response) => {
try {
const { id } = req.params
const pets = await Pet.getUserPets(id)
return res.status(200).json({ message: "success", data: pets })
} catch (e) {
const result = e as Error
return res.status(500).json({ message: result.message })
}
}

export const getPet = async (req: Request, res: Response) => {
try {
const { id } = req.params
const pet = await Pet.getPet(id)
return res.status(200).json({ message: "success", data: pet })
} catch (e) {
const result = e as Error
return res.status(500).json({ message: result.message })
}
}

export const addMedicalRecord = async (req: Request, res: Response) => {
try {
const { date, doctor, notes, reason, medication } = req.body
const { id } = req.params
const pet = await Pet.addMedications(
date,
doctor,
notes,
reason,
medication,
id
)
return res
.status(200)
.json({ message: "successfully added", data: pet })
} catch (e) {
const result = e as Error
return res.status(500).json({ message: result.message })
}
}

export const getMedicalRecords = async (req: Request, res: Response) => {
try {
const { id } = req.params
const medicalRecords = await Pet.getMedications(id)
return res
.status(200)
.json({ message: "success", data: medicalRecords })
} catch (e) {
const result = e as Error
return res.status(500).json({ message: result.message })
}
}

export const addVisit = async (req: Request, res: Response) => {
try {
const { date, reason, location, doctor } = req.body
const { id } = req.params
const visit = await Pet.addVisits(date, location, doctor, reason, id)
return res.status(200).json({ message: "success", data: visit })
} catch (e) {
const result = e as Error
return res.status(500).json({ message: result.message })
}
}

export const addPetWithPic = async (req: Request, res: Response) => {
try {
const files = req.files
const { name, bday, sex, breed, age } = req.body
const { authorization } = req.headers
if (!authorization) {
return res.status(401).json({ message: "Token required!" })
}
const token = authorization.split(" ")[1]
const { _id } = jwt.verify(token, process.env.SECRET!) as JwtPayload

if (!files) {
return res.status(500).json({ message: "no file received" })
}

const img = files.img as UploadedFile
const picturePath = img.tempFilePath
const pet = await Pet.addPetsWithPic(
name,
breed,
age,
bday,
picturePath,
_id
)
fs.unlink(img.tempFilePath, (err) => {
if (err) {
console.error("Error deleting the temporary file:", err)
} else {
console.log("Temporary file deleted.")
}
})
return res.status(200).json({ message: "success", data: pet })
} catch (e) {
const result = e as Error
return res.status(500).json({ message: result.message })
}
}
18 changes: 18 additions & 0 deletions backend/src/api/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import User from "../../models/user.model"
import mongoose from "mongoose"
import { Request, Response } from "express"
import jwt, { JwtPayload } from "jsonwebtoken"

export const getSingleUser = async (req: Request, res: Response) => {
try {
Expand All @@ -22,3 +23,20 @@ export const getUsers = async (req: Request, res: Response) => {
return res.status(500).json({ message: result.message })
}
}

export const editUser = async (req: Request, res: Response) => {
try {
const { username, bio } = req.body
const { authorization } = req.headers
if (!authorization) {
return res.status(401).json({ message: "Token required!" })
}
const token = authorization.split(" ")[1]
const { _id } = jwt.verify(token, process.env.SECRET!) as JwtPayload
const user = await User.editUser(username, bio, _id)
return res.status(200).json({ message: "success", data: user })
} catch (error) {
const result = error as Error
return res.status(500).json({ message: result.message })
}
}
29 changes: 29 additions & 0 deletions backend/src/api/routes/pet.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import express from "express"
import requireAuth from "../middleware/auth.middleware"
import {
addMedicalRecord,
addPetWithPic,
addVisit,
createPet,
getMedicalRecords,
getPet,
getUserPets
} from "../controllers/pet.controller"

const petRoutes = express.Router()

petRoutes.post("/", createPet)

petRoutes.get("/user/:id", getUserPets)

petRoutes.get("/single/:id", getPet)

petRoutes.post("/medical/:id", addMedicalRecord)

petRoutes.get("/medical/:id", getMedicalRecords)

petRoutes.post("/visit/:id", addVisit)

petRoutes.post("/upload", addPetWithPic)

export default petRoutes
10 changes: 7 additions & 3 deletions backend/src/api/routes/user.routes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import express from "express"
import requireAuth from "../middleware/auth.middleware"
import { getSingleUser, getUsers } from "../controllers/user.controller"
import {
editUser,
getSingleUser,
getUsers
} from "../controllers/user.controller"

const userRouter = express.Router()

userRouter.use(requireAuth)

userRouter.get("/", getUsers)

userRouter.get("/:id", getSingleUser)

userRouter.post("/edit", editUser)

export default userRouter
10 changes: 9 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import authRouter from "./api/routes/auth.routes"
import bcrypt from "bcrypt"
import postRouter from "./api/routes/post.routes"
import morgan from "morgan"
import petRoutes from "./api/routes/pet.routes"
import fileUpload from "express-fileupload"
import Pet from "./models/pet.model"

dotenv.config()

Expand All @@ -25,14 +28,19 @@ var corsOptions = function (req: Request, res: Response, next: NextFunction) {
}

app.use(corsOptions)

app.use(
fileUpload({
useTempFiles: true
})
)
app.use(express.json())
app.use(morgan("dev"))

//router setup
app.use("/api/user", userRouter)
app.use("/api/auth", authRouter)
app.use("/api/post", postRouter)
app.use("/api/pet", petRoutes)

//server initialization
mongoose.connect(process.env.MONGODB_URI!).then(() => {
Expand Down
Loading

0 comments on commit a783145

Please sign in to comment.