mirror of
https://github.com/jlengrand/simple-food-diary.git
synced 2026-03-10 08:41:19 +00:00
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { AzureFunction, Context } from "@azure/functions"
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
mongoose.connect(
|
|
process.env.CONNECTION_STRING,
|
|
{
|
|
useNewUrlParser: true,
|
|
useUnifiedTopology: true,
|
|
}
|
|
);
|
|
|
|
const entrySchema = new mongoose.Schema({
|
|
portionSize: String,
|
|
mealTypes: [String],
|
|
ts: Date,
|
|
});
|
|
|
|
const userSchema = new mongoose.Schema({
|
|
userId: { type: String, required: true, unique: true },
|
|
identityProvider: { type: String, required: true },
|
|
userDetails: { type: String, required: true },
|
|
entries: [entrySchema],
|
|
});
|
|
|
|
const UserModel = mongoose.model('user', userSchema);
|
|
|
|
const httpTrigger: AzureFunction = async function (context: Context): Promise<void> {
|
|
|
|
const { body } = context.req;
|
|
const user = await UserModel.create(body);
|
|
context.res = {
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
};
|
|
context.res.status = 201;
|
|
context.res.body = user;
|
|
|
|
};
|
|
|
|
export default httpTrigger; |