Skip to content

Commit 1c3ada2

Browse files
authored
Merge pull request #10 from pranav514/layered_architecture
refactored [listings] code improvement
2 parents bc51273 + 93d37ea commit 1c3ada2

File tree

4 files changed

+61
-37
lines changed

4 files changed

+61
-37
lines changed

dist/routes/listingRoute.js

+9-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1313
};
1414
Object.defineProperty(exports, "__esModule", { value: true });
1515
const express_1 = __importDefault(require("express"));
16-
const db_1 = require("../db");
1716
const authMiddleware_1 = require("../middleware/authMiddleware");
1817
const listing_1 = require("../repositories/listing");
1918
const lisiting_1 = require("../services/lisiting");
@@ -114,24 +113,16 @@ router.get("/getlisting/:id", authMiddleware_1.authMiddleware, (req, res) => __a
114113
}
115114
}));
116115
router.get("/userlisting", authMiddleware_1.authMiddleware, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
117-
try {
118-
const userId = req.userId;
119-
console.log("userId", userId);
120-
const listing = yield db_1.prisma.listing.findMany({
121-
where: {
122-
userId: userId,
123-
},
124-
});
125-
console.log(listing);
126-
return res.status(200).json({
127-
message: `fetched the blog of the user ${userId}`,
128-
listing,
129-
});
130-
}
131-
catch (error) {
132-
res.status(411).json({
133-
message: "cannot fetched the blog",
116+
const userId = req.userId;
117+
const specificListing = yield (0, lisiting_1.GetUserSpecific)(userId);
118+
if (specificListing.status === 411) {
119+
return res.status(specificListing.status).json({
120+
message: specificListing.message
134121
});
135122
}
123+
return res.status(200).json({
124+
message: specificListing.message,
125+
listing: specificListing.listing
126+
});
136127
}));
137128
exports.default = router;

dist/services/lisiting.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
99
});
1010
};
1111
Object.defineProperty(exports, "__esModule", { value: true });
12-
exports.GetAll = exports.DeleteListing = exports.UpdateListings = exports.CreateListing = void 0;
12+
exports.GetUserSpecific = exports.GetAll = exports.DeleteListing = exports.UpdateListings = exports.CreateListing = void 0;
1313
const listing_1 = require("../repositories/listing");
1414
const CreateListing = ({ title, description, images, rent, prefered_gender, address, location_city, userId }) => __awaiter(void 0, void 0, void 0, function* () {
1515
try {
@@ -104,3 +104,22 @@ const GetAll = ({ skip, limit, page }) => __awaiter(void 0, void 0, void 0, func
104104
}
105105
});
106106
exports.GetAll = GetAll;
107+
const GetUserSpecific = (userId) => __awaiter(void 0, void 0, void 0, function* () {
108+
try {
109+
console.log("userId", userId);
110+
const listing = yield (0, listing_1.findMany)(userId);
111+
console.log(listing);
112+
return {
113+
message: `fetched the blog of the user ${userId}`,
114+
status: 200,
115+
listing
116+
};
117+
}
118+
catch (error) {
119+
return {
120+
message: "cannot fetched the blog",
121+
status: 411,
122+
};
123+
}
124+
});
125+
exports.GetUserSpecific = GetUserSpecific;

src/routes/listingRoute.ts

+13-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
getAll,
1010
Update,
1111
} from "../repositories/listing";
12-
import { CreateListing, DeleteListing, GetAll, UpdateListings } from "../services/lisiting";
12+
import { CreateListing, DeleteListing, GetAll, GetUserSpecific, UpdateListings } from "../services/lisiting";
1313
const router = express.Router();
1414

1515
router.post(
@@ -144,24 +144,20 @@ router.get(
144144
);
145145

146146
router.get("/userlisting", authMiddleware, async (req, res): Promise<any> => {
147-
try {
147+
148148
const userId = req.userId;
149-
console.log("userId", userId);
150-
const listing = await prisma.listing.findMany({
151-
where: {
152-
userId: userId,
153-
},
154-
});
155-
console.log(listing);
149+
const specificListing = await GetUserSpecific(userId);
150+
if(specificListing.status === 411){
151+
return res.status(specificListing.status).json({
152+
message : specificListing.message
153+
})
154+
}
156155
return res.status(200).json({
157-
message: `fetched the blog of the user ${userId}`,
158-
listing,
159-
});
160-
} catch (error) {
161-
res.status(411).json({
162-
message: "cannot fetched the blog",
163-
});
164-
}
156+
message : specificListing.message,
157+
listing : specificListing.listing
158+
})
159+
160+
165161
});
166162

167163
export default router;

src/services/lisiting.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { any } from "vitest-mock-extended";
22
import { CreateListing as CreateListingInterface, Pagination, UpdateListing } from "../interface/listingInterface";
3-
import { Count, Create, deleteListing, getAll, Update } from "../repositories/listing";
3+
import { Count, Create, deleteListing, findMany, getAll, Update } from "../repositories/listing";
44

55
export const CreateListing = async ({title , description, images , rent , prefered_gender , address , location_city , userId} : CreateListingInterface) => {
66
try {
@@ -105,4 +105,22 @@ export const GetAll = async({skip , limit , page} :any ) => {
105105
status : 411
106106
}
107107
}
108+
}
109+
110+
export const GetUserSpecific = async(userId : string) => {
111+
try {
112+
console.log("userId", userId);
113+
const listing = await findMany(userId);
114+
console.log(listing);
115+
return {
116+
message : `fetched the blog of the user ${userId}`,
117+
status :200,
118+
listing
119+
}
120+
} catch (error) {
121+
return {
122+
message : "cannot fetched the blog",
123+
status : 411,
124+
}
125+
}
108126
}

0 commit comments

Comments
 (0)