DS : code correction

Architecture

  • Back-end (API): The API lives in the API-QCM repository. It serves the QCM endpoints (questions, categories, submit). By default the frontend expects the API at http://localhost:3000.

https://github.com/dupontdenis/API-QCM.git


  • Front-end : Static files in this repository (index.htmljs/css/) implement the UI that calls the API.

https://github.com/dupontdenis/publicQcm-cat.git

Virtual en action !

 

How to : virtual

API examples

馃崟Projet API pizza

馃摎Projet API Books

馃馃徑‍馃彨Projet API Teacher


 


Projet final : Librairy

 

express-locallibrary-tutorial

Tutorial "Local Library" website written in Node/Express.

 

馃帀code → https://github.com/dupontdenis/library.git


http://localhost:3000/catalog

http://localhost:3000/catalog/books


http://localhost:3000/catalog/author/create





Mongoose !

Let's start with mongoose !

Example : 

  1. // Include virtuals when converting documents to objects/JSON
  2. pizzaSchema.set("toObject", { virtuals: true });
  3. pizzaSchema.set("toJSON", { virtuals: true });

  4. // Virtual populate: connect Pizza -> Topping via Topping.pizzas (inverse relation)
  5. pizzaSchema.virtual("toppings", {
  6.   ref: "Topping",
  7.   localField: "_id",
  8.   foreignField: "pizzas",
  9.   justOne: false,
  10. });

  11. // Virtual total price (cents) includes toppings when `toppings` is populated
  12. pizzaSchema.virtual("totalPriceCents").get(function () {
  13.   // Compute total price from populated `toppings` only (no base pizza price)
  14.   if (
  15.     !this.toppings ||
  16.     !Array.isArray(this.toppings) ||
  17.     this.toppings.length === 0
  18.   )
  19.     return 0;
  20.   return this.toppings.reduce((acc, t) => acc + (t.priceCents || 0), 0);
  21. });

  22. pizzaSchema.virtual("totalPriceEur").get(function () {
  23.   return (this.totalPriceCents || 0) / 100;
  24. });

  25. export const Pizza = mongoose.model("Pizza", pizzaSchema);