From d676b8e30e13d6c73a6b10358413c7a83519bea4 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Sun, 15 Aug 2021 21:58:08 +0200 Subject: [PATCH] Testing --- api/addDiaryEntry/index.ts | 35 +++++++++++++++++++++++------------ src/food-log-form.ts | 14 +++++++++++--- src/simple-food-diary.ts | 2 +- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/api/addDiaryEntry/index.ts b/api/addDiaryEntry/index.ts index 64b643e..dc8610d 100644 --- a/api/addDiaryEntry/index.ts +++ b/api/addDiaryEntry/index.ts @@ -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 { - +const httpTrigger: AzureFunction = async function ( + context: Context +): Promise { 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; diff --git a/src/food-log-form.ts b/src/food-log-form.ts index 6d57a67..4135cf7 100644 --- a/src/food-log-form.ts +++ b/src/food-log-form.ts @@ -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', }, diff --git a/src/simple-food-diary.ts b/src/simple-food-diary.ts index ea9f95c..08517e3 100644 --- a/src/simple-food-diary.ts +++ b/src/simple-food-diary.ts @@ -50,7 +50,7 @@ export class SimpleFoodDiary extends LitElement { : html`Login`}
- +