-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
133 lines (115 loc) · 3.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import mongoose, { Schema, Document } from "mongoose";
// TypeScript Interfaces for MongoDB Models
interface IHost extends Document {
id: number;
city: string;
state: string;
address: string;
zip: string;
lat: string;
long: string;
short_name: string;
full_name: string;
phone: string;
}
interface IReview extends Document {
id: number;
name: string;
host: number;
review: string;
visit: boolean;
visit_date: string;
room: string;
amenities: string;
}
// MongoDB URI from environment
const MONGO_URI: string = process.env.MONGO_URI || "";
// ✅ Define Schemas with TypeScript
const hostSchema: Schema = new Schema<IHost>({
id: Number,
city: String,
state: String,
address: String,
zip: String,
lat: String,
long: String,
short_name: String,
full_name: String,
phone: String,
});
const reviewSchema: Schema = new Schema<IReview>({
id: Number,
name: String,
host: Number,
review: String,
visit: Boolean,
visit_date: String,
room: String,
amenities: String,
});
// Create Models
const Hosts = mongoose.model<IHost>("Hosts", hostSchema);
const Reviews = mongoose.model<IReview>("Reviews", reviewSchema);
// Connect to MongoDB
async function connectDB(): Promise<void> {
if (mongoose.connection.readyState === 0) {
await mongoose.connect(MONGO_URI, {
serverSelectionTimeoutMS: 30000,
});
console.log("✅ MongoDB Connected Successfully");
}
}
// Define Type for API Responses
interface APIGatewayEvent {
path: string;
httpMethod: string;
body?: string;
}
// Lambda Handler Function
export const handler = async (event: APIGatewayEvent) => {
console.log("🚀 Lambda function triggered", event);
await connectDB();
try {
if (event.path === "/") {
return { statusCode: 200, body: JSON.stringify({ message: "Welcome to Mongoose API! 🔥" }) };
}
if (event.path === "/fetchHosts") {
const hosts = await Hosts.find();
return { statusCode: 200, body: JSON.stringify(hosts) };
}
if (event.path.startsWith("/fetchHosts/") && event.httpMethod === "GET") {
const state = event.path.split("/").pop();
const hosts = await Hosts.find({ state });
return { statusCode: 200, body: JSON.stringify(hosts) };
}
if (event.path.startsWith("/fetchHost/") && event.httpMethod === "GET") {
const id = parseInt(event.path.split("/").pop()!, 10);
const host = await Hosts.findOne({ id });
if (!host) {
return { statusCode: 404, body: JSON.stringify({ error: "Host not found!" }) };
}
return { statusCode: 200, body: JSON.stringify(host) };
}
if (event.path === "/insert_review" && event.httpMethod === "POST") {
const data = JSON.parse(event.body!);
const lastReview = await Reviews.find().sort({ id: -1 }).limit(1);
const new_id = lastReview.length ? lastReview[0].id + 1 : 1;
const review = new Reviews({
id: new_id,
name: data.name,
host: data.host,
review: data.review,
visit: true,
visit_date: data.visit_date,
room: data.room,
amenities: data.amenities,
});
const savedReview = await review.save();
return { statusCode: 201, body: JSON.stringify(savedReview) };
}
return { statusCode: 404, body: JSON.stringify({ error: "Route not found" }) };
} catch (error) {
console.error("❌ Error:", error);
return { statusCode: 500, body: JSON.stringify({ error: "Internal Server Error" }) };
}
};