Skip to content

Commit

Permalink
update a pub
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelsalemdev committed Feb 19, 2023
1 parent 1f1a455 commit 290503b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const protect = catchAsync(
// 2) Verification token
const decoded: any = await jwtVerifyPromisified(token, config.JWT_SECRET);
//const decoded = await promisify(jwt.verify)(token, process.env.JWT_SECRET);
console.log(decoded);

// 3) Check if user still exists
const currentUser = await UserModel.findById(decoded.id);
if (!currentUser) {
Expand Down
44 changes: 43 additions & 1 deletion src/controllers/pub.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { NextFunction, Request, Response } from "express";
import PubModel from "../models/pub.model";
import { createPubInput } from "../schema/pub.schema";
import { createPub, deletePub, getAllPubs } from "../services/pub.service";
import {
createPub,
deletePub,
getAllPubs,
getPub,
updatePub,
} from "../services/pub.service";
import AppError from "../utils/appError";
import catchAsync from "../utils/catchAsync";

Expand Down Expand Up @@ -48,3 +55,38 @@ export const getAllPubsHandler = catchAsync(
});
}
);

export const getPubHandler = catchAsync(
async (req: Request, res: Response, next: NextFunction) => {
const pub: object = await getPub(req.params.pubId);
if (!pub) {
return next(new AppError("No pub found with that ID", 404));
}
res.status(200).json({
status: "success",
data: {
pub,
},
});
}
);

export const updatePubHandler = catchAsync(
async (req: Request, res: Response, next: NextFunction) => {
const pub: object = await updatePub(
req.params.pubId,
req.body,
res.locals.user
);

if (!pub) {
return next(new AppError("No pub found with that ID", 404));
}
res.status(200).json({
status: "success",
data: {
pub,
},
});
}
);
7 changes: 7 additions & 0 deletions src/models/pub.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ pubSchema.pre("save", async function (next) {
this.user = userId;
next();
}); */
/* pubSchema.pre(/^findOneAnd/, async function (next) {
const pub = await this.findOne();
this.title = pub.title;
this.description = pub.description;
this.status = pub.status;
next();
}); */

const PubModel = mongoose.model<PubDocument>("Pub", pubSchema);

Expand Down
5 changes: 4 additions & 1 deletion src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
createPubHandler,
deletePubHandler,
getAllPubsHandler,
getPubHandler,
updatePubHandler,
} from "./controllers/pub.controller";
import { createPubSchema } from "./schema/pub.schema";
import { setPubUserId } from "./services/pub.service";
Expand All @@ -28,7 +30,8 @@ function routes(app: Express) {
setPubUserId,
createPubHandler
);
app.patch("/api/pubs/:pubId");
app.get("/api/pubs/:pubId", protect, getPubHandler);
app.patch("/api/pubs/:pubId", protect, updatePubHandler);
app.delete("/api/pubs/:pubId", protect, deletePubHandler);
app.get("/api/users");
app.get("/api/users/:userId");
Expand Down
32 changes: 32 additions & 0 deletions src/services/pub.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,35 @@ export async function getAllPubs() {
return new AppError(error, 404);
}
}

export async function getPub(id: any) {
try {
const pub = await PubModel.findById(id);
return pub;
} catch (error) {
return new AppError(error, 404);
}
}

export async function updatePub(id: any, body: object, user: any) {
try {
const pubToUpdate = await PubModel.findById(id);
const pubToUpdateUserId = pubToUpdate?.user.toString();
const userId = user._id.toString();

//return pub;
if (user.role === "user") {
if (pubToUpdateUserId === userId) {
await PubModel.findByIdAndUpdate(id, body);
const pubUpdated = await PubModel.findById(id);
return pubUpdated;
}
} else if (user.role === "admin") {
await PubModel.findByIdAndUpdate(id, body);
const pubUpdated = await PubModel.findById(id);
return pubUpdated;
}
} catch (error) {
return new AppError(error, 404);
}
}
1 change: 0 additions & 1 deletion src/utils/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dotenv.config({ path: "../../config.env" });

async function connect() {
const dbUrl = config.DATABASE;
console.log(dbUrl);

try {
await mongoose.connect(dbUrl);
Expand Down

0 comments on commit 290503b

Please sign in to comment.