-
Notifications
You must be signed in to change notification settings - Fork 0
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
47 changed files
with
14,306 additions
and
11,472 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
SECRET = 'STSWENGGGG' | ||
|
||
|
||
|
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 +1,3 @@ | ||
node_modules/ | ||
node_modules/ | ||
coverage | ||
.env |
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,6 +1,7 @@ | ||
module.exports = { | ||
presets: [ | ||
['@babel/preset-env', {targets: {node: 'current'}}], | ||
'@babel/preset-typescript', | ||
['@babel/preset-env', {targets: {node: 'current'}}], | ||
'@babel/preset-typescript', | ||
], | ||
|
||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 }) | ||
} | ||
} |
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,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 |
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,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 |
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
Oops, something went wrong.