Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Session = require("../models/Session");
  2.  
  3. module.exports = {
  4.   async index(req, res) {
  5.     const sessions = await Session.find();
  6.  
  7.     return res.json(sessions);
  8.   },
  9.  
  10.   async store(req, res) {
  11.     const { movie, desc, day, hour } = req.body;
  12.  
  13.     const session = await Session.create({
  14.       movie,
  15.       desc,
  16.       day,
  17.       hour
  18.     });
  19.  
  20.     return res.json(session);
  21.   }
  22. };
  23.  
  24. const mongoose = require("mongoose");
  25.  
  26. const SessionSchema = new mongoose.Schema({
  27.   movie: String,
  28.   desc: String,
  29.   day: String,
  30.   hour: String
  31. });
  32.  
  33. module.exports = mongoose.model("Session", SessionSchema);
  34.  
  35.  
  36. const Theater = require("../models/Theater");
  37. const Session = require("../models/Session");
  38.  
  39. module.exports = {
  40.   async index(req, res) {
  41.     const theaters = await Theater.find();
  42.  
  43.     return res.json(theaters);
  44.   },
  45.  
  46.   async store(req, res) {
  47.     const { session_id } = req.params;
  48.     const { name, city } = req.body;
  49.  
  50.     const theater = await Theater.create({
  51.       session: session_id,
  52.       name,
  53.       city
  54.     });
  55.  
  56.     await theater.populate("session").execPopulate();
  57.  
  58.     return res.json(theater);
  59.   }
  60. };
  61.  
  62.  
  63. const mongoose = require("mongoose");
  64.  
  65. const TheaterSchema = new mongoose.Schema(
  66.   {
  67.     name: String,
  68.     city: String,
  69.     session: {
  70.       type: mongoose.Schema.Types.ObjectId,
  71.       ref: "Session"
  72.     }
  73.   },
  74.   {
  75.     toJSON: {
  76.       virtuals: true
  77.     }
  78.   }
  79. );
  80.  
  81. module.exports = mongoose.model("Theater", TheaterSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement