In the last video we spoke about some of the different route handlers that we're going to put together
to facilitate the ability to process all these different surveys.
Now I think that the best place to get started will be with the POST request to create a new survey.
Obviously we don't really want to show a user a list of surveys if they don't have the ability to create
a new one in the first place.
So the easiest place to start is probably going to be by putting together the route to create a new
survey.
Now whenever we think about putting together a brand new feature like this we wanted to ask ourselves
what parts of the stack like what parts of this application do we have to touch.
Well obviously we have to put together a route handler.
Right.
So there's our express server.
We probably need some front end aspect to the application.
So probably something on the reac side and then the real question we want to ask ourselves especially
in this case is whether or not we have to do any work at the database level.
So I'm going to ask you right now if we want the ability to create a new survey do you think that's
going to be related to our Mangu database at all.
All probably so right we probably want to persist a survey after it's been created so that at some point
in the future someone can come back get the list of all the different surveys or add feedback to a very
particular one.
So remember how we create new records or new collections of data inside of our database.
We always create a mongoose model class when we create that model class it automatically creates a new
collection of records inside of our database.
And so thinking about this particular feature I'm thinking that will create a new model class called
a survey.
Every survey will have at least a title a body a subject and some number of recipients or the people
who are receiving the survey.
And then because we want to make sure that we know which user created which survey we'll make sure that
there is some type of link between the user model class and the survey model class as well so that we
always know okay this user right here created these three particular surveys.
So let's get to it.
We're going to start off by creating a new model class file where we are going to define a new survey.
I'm going to change on over to my code editor and then inside of my models directory I'll make a new
file called survey.
J.S. before we start writing any code inside of here let's go reference back inside of our user dot
file just to get a reminder of what we're supposed to do.
So we require in mongoose we get the schema object off of it then we create a new schema that contains
all the different properties that we expect our model class to have.
So in the case of a user We had a Google ID and a number of credits.
So let's take the same approach with the survey.
We will require in mongoose at the top.
We will then pull off this schema object from mongoose.
That's a typo.
And then we'll create our survey schema.
So we'll say survey schema is new schema.
Okay.
So on this property right here or in the subject will list out all the different properties that we
expect the schema to have.
So we've spoken about this a couple different times now.
We think that we need a title a body a subject and then a recipient list.
So no recipient list.
This one might be a little bit challenging but let's take care of these easy ones.
First title body and subject.
Remember this is the title of the email the body of the actual surveyor.
Some Not title the e-mail but the title of the campaign or the title of the survey.
The body is the text that is displayed to an end user and then the subject is the actual subject line
of the e-mail.
And so in all three cases I'm really thinking that these are definitely going to be strings of data
so they're not numbers they're not objects nothing else.
They're really just strings of data.
So I'm going to make a title property of type string.
I mean to say that we have a body of type string and a subject of string as well.
OK so now I'm down to the recipient's down here.
So when we think about recipients we had previously said we were looking at the diagram right here that
this would be a comma separated list of email addresses.
Well when it's sent to our endpoint right here it's definitely still going to be a string.
Because I'm saying specifically comma separated list of email addresses so you can definitely make the
case here that maybe this should be like an array of strings maybe something like that if we want this
to be an array of strings.
Let's let's settle with that for right now let's say that when the front end posts us a list of email
addresses.
It will be an array of strings and every string is an individual email list.
So back over inside of our news schema you're going to add on recipients.
And please triple check your spelling and recipients.
This is a very easy one to make a typo on.
So I want to say here that this is going to be an array of strings where every single string represents
a single e-mail address.
So I'm not going to say that it's a string because I have specifically and array of strings well to
specify that recipients will be an array containing some discrete type.
We can just write out an array and then inside there put string.
So this right here communicates to mongoose that the recipient's property will be an array containing
a list of strings.
All right this is definitely to get us started with our schema.
So the very last thing we have to do is make sure that we load this up into our mongoose library.
Remember we called mongoose model.
We pass in the name of the collection or the name of the model class which will be surveys and then
the name of the schema which is sort of a schema
OK that looks pretty good.
Now one last thing to do with the survey.
J.S. file remember that whenever we create a new file inside of our express project that doesn't just
automatically include it or execute the code inside of here.
So we need to make sure that we actually require this file in at least one location in our project.
So I can to go back to the index js file and inside of here right underneath the user makes make sure
that I actually require in that model file that we just create it will say models slash survey group
survey.
So OK this is looking pretty good.
So we now have the idea of a survey inside of our application.
Right now we've got the idea of our symbionts list as being an array of strings right here.
I want to take a break and then in the next section I want to revisit this property and decide whether
or not this is going to really work out for what we are trying to accomplish inside of our application.
So let's take a quick break and decide whether or not this is going to do what we expect it to.