This commit is contained in:
Julien Lengrand-Lambert
2021-08-15 21:58:08 +02:00
parent 06b63fb122
commit d676b8e30e
3 changed files with 35 additions and 16 deletions

View File

@@ -2,32 +2,43 @@ import { AzureFunction, Context } from '@azure/functions';
const mongoose = require('mongoose');
mongoose.connect(
process.env.CONNECTION_STRING,
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
mongoose.connect(process.env.CONNECTION_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const entrySchema = new mongoose.Schema({
portionSize: String,
mealTypes: [String],
ts: Date,
});
const EntryModel = mongoose.model('entry', entrySchema);
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);
// eslint-disable-next-line func-names
const httpTrigger: AzureFunction = async function (context: Context): Promise<void> {
const httpTrigger: AzureFunction = async function (
context: Context
): Promise<void> {
const { body } = context.req;
const task = await EntryModel.create(body);
const user = await UserModel.findOne({ userId: body.user.userId });
user.entries.push(body.meal);
const newUser = user.save();
context.res = {
header: {
'Content-Type': 'application/json',
},
};
context.res.status = 201;
context.res.body = task;
context.res.body = newUser;
};
export default httpTrigger;

View File

@@ -1,10 +1,13 @@
import { LitElement, html, css } from 'lit';
import { customElement } from 'lit/decorators.js';
import { customElement, property } from 'lit/decorators.js';
import 'fa-icons';
@customElement('food-log-form')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class FoodLogForm extends LitElement {
@property()
me?: Object;
static styles = css`
.portion-size {
display: flex;
@@ -48,14 +51,19 @@ class FoodLogForm extends LitElement {
ts: new Date(),
};
const payload = {
meal,
user: this.me,
};
// eslint-disable-next-line no-console
console.log(JSON.stringify(meal));
console.log(JSON.stringify(payload));
fetch(
'/api/addDiaryEntry', // API location
{
method: 'POST',
body: JSON.stringify(meal),
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
},

View File

@@ -50,7 +50,7 @@ export class SimpleFoodDiary extends LitElement {
: html`<a href="/.auth/login/twitter">Login</a>`}
</header>
<main>
<food-log-form></food-log-form>
<food-log-form .me=${this.me}></food-log-form>
</main>
<footer></footer>