1 00:00:00,600 --> 00:00:04,380 In the last video we spoke about some of the different route handlers that we're going to put together 2 00:00:04,380 --> 00:00:08,450 to facilitate the ability to process all these different surveys. 3 00:00:08,460 --> 00:00:13,360 Now I think that the best place to get started will be with the POST request to create a new survey. 4 00:00:13,620 --> 00:00:17,850 Obviously we don't really want to show a user a list of surveys if they don't have the ability to create 5 00:00:17,850 --> 00:00:19,370 a new one in the first place. 6 00:00:19,440 --> 00:00:24,300 So the easiest place to start is probably going to be by putting together the route to create a new 7 00:00:24,300 --> 00:00:25,770 survey. 8 00:00:25,770 --> 00:00:30,810 Now whenever we think about putting together a brand new feature like this we wanted to ask ourselves 9 00:00:31,080 --> 00:00:35,150 what parts of the stack like what parts of this application do we have to touch. 10 00:00:35,460 --> 00:00:37,940 Well obviously we have to put together a route handler. 11 00:00:38,010 --> 00:00:38,250 Right. 12 00:00:38,250 --> 00:00:39,780 So there's our express server. 13 00:00:39,900 --> 00:00:42,800 We probably need some front end aspect to the application. 14 00:00:42,810 --> 00:00:48,030 So probably something on the reac side and then the real question we want to ask ourselves especially 15 00:00:48,030 --> 00:00:52,940 in this case is whether or not we have to do any work at the database level. 16 00:00:53,130 --> 00:00:57,990 So I'm going to ask you right now if we want the ability to create a new survey do you think that's 17 00:00:57,990 --> 00:01:01,090 going to be related to our Mangu database at all. 18 00:01:01,430 --> 00:01:07,590 All probably so right we probably want to persist a survey after it's been created so that at some point 19 00:01:07,590 --> 00:01:13,260 in the future someone can come back get the list of all the different surveys or add feedback to a very 20 00:01:13,260 --> 00:01:15,280 particular one. 21 00:01:15,420 --> 00:01:21,260 So remember how we create new records or new collections of data inside of our database. 22 00:01:21,330 --> 00:01:27,360 We always create a mongoose model class when we create that model class it automatically creates a new 23 00:01:27,360 --> 00:01:30,770 collection of records inside of our database. 24 00:01:30,830 --> 00:01:36,750 And so thinking about this particular feature I'm thinking that will create a new model class called 25 00:01:36,750 --> 00:01:37,980 a survey. 26 00:01:38,760 --> 00:01:44,400 Every survey will have at least a title a body a subject and some number of recipients or the people 27 00:01:44,400 --> 00:01:46,110 who are receiving the survey. 28 00:01:46,470 --> 00:01:51,750 And then because we want to make sure that we know which user created which survey we'll make sure that 29 00:01:51,750 --> 00:01:58,410 there is some type of link between the user model class and the survey model class as well so that we 30 00:01:58,410 --> 00:02:03,680 always know okay this user right here created these three particular surveys. 31 00:02:04,290 --> 00:02:05,430 So let's get to it. 32 00:02:05,430 --> 00:02:12,130 We're going to start off by creating a new model class file where we are going to define a new survey. 33 00:02:12,650 --> 00:02:17,430 I'm going to change on over to my code editor and then inside of my models directory I'll make a new 34 00:02:17,430 --> 00:02:19,590 file called survey. 35 00:02:19,750 --> 00:02:25,750 J.S. before we start writing any code inside of here let's go reference back inside of our user dot 36 00:02:25,920 --> 00:02:28,670 file just to get a reminder of what we're supposed to do. 37 00:02:29,040 --> 00:02:36,600 So we require in mongoose we get the schema object off of it then we create a new schema that contains 38 00:02:36,690 --> 00:02:40,480 all the different properties that we expect our model class to have. 39 00:02:40,770 --> 00:02:45,700 So in the case of a user We had a Google ID and a number of credits. 40 00:02:45,750 --> 00:02:48,730 So let's take the same approach with the survey. 41 00:02:49,230 --> 00:02:54,830 We will require in mongoose at the top. 42 00:02:55,110 --> 00:03:00,210 We will then pull off this schema object from mongoose. 43 00:03:00,210 --> 00:03:01,240 That's a typo. 44 00:03:01,860 --> 00:03:04,320 And then we'll create our survey schema. 45 00:03:04,320 --> 00:03:09,490 So we'll say survey schema is new schema. 46 00:03:10,080 --> 00:03:10,340 Okay. 47 00:03:10,350 --> 00:03:14,790 So on this property right here or in the subject will list out all the different properties that we 48 00:03:14,790 --> 00:03:16,320 expect the schema to have. 49 00:03:16,560 --> 00:03:18,900 So we've spoken about this a couple different times now. 50 00:03:19,110 --> 00:03:25,020 We think that we need a title a body a subject and then a recipient list. 51 00:03:25,020 --> 00:03:26,970 So no recipient list. 52 00:03:26,970 --> 00:03:30,770 This one might be a little bit challenging but let's take care of these easy ones. 53 00:03:30,760 --> 00:03:33,160 First title body and subject. 54 00:03:33,300 --> 00:03:37,330 Remember this is the title of the email the body of the actual surveyor. 55 00:03:37,410 --> 00:03:41,960 Some Not title the e-mail but the title of the campaign or the title of the survey. 56 00:03:41,970 --> 00:03:47,280 The body is the text that is displayed to an end user and then the subject is the actual subject line 57 00:03:47,280 --> 00:03:48,210 of the e-mail. 58 00:03:48,240 --> 00:03:54,000 And so in all three cases I'm really thinking that these are definitely going to be strings of data 59 00:03:54,090 --> 00:03:57,140 so they're not numbers they're not objects nothing else. 60 00:03:57,210 --> 00:03:59,620 They're really just strings of data. 61 00:03:59,730 --> 00:04:03,040 So I'm going to make a title property of type string. 62 00:04:03,210 --> 00:04:11,620 I mean to say that we have a body of type string and a subject of string as well. 63 00:04:12,270 --> 00:04:15,390 OK so now I'm down to the recipient's down here. 64 00:04:15,480 --> 00:04:20,700 So when we think about recipients we had previously said we were looking at the diagram right here that 65 00:04:20,700 --> 00:04:24,940 this would be a comma separated list of email addresses. 66 00:04:25,260 --> 00:04:30,620 Well when it's sent to our endpoint right here it's definitely still going to be a string. 67 00:04:30,700 --> 00:04:36,240 Because I'm saying specifically comma separated list of email addresses so you can definitely make the 68 00:04:36,240 --> 00:04:41,460 case here that maybe this should be like an array of strings maybe something like that if we want this 69 00:04:41,460 --> 00:04:42,430 to be an array of strings. 70 00:04:42,430 --> 00:04:47,710 Let's let's settle with that for right now let's say that when the front end posts us a list of email 71 00:04:47,720 --> 00:04:48,530 addresses. 72 00:04:48,660 --> 00:04:54,770 It will be an array of strings and every string is an individual email list. 73 00:04:56,590 --> 00:05:03,150 So back over inside of our news schema you're going to add on recipients. 74 00:05:03,400 --> 00:05:05,590 And please triple check your spelling and recipients. 75 00:05:05,590 --> 00:05:08,660 This is a very easy one to make a typo on. 76 00:05:08,740 --> 00:05:14,890 So I want to say here that this is going to be an array of strings where every single string represents 77 00:05:14,980 --> 00:05:17,080 a single e-mail address. 78 00:05:17,110 --> 00:05:22,780 So I'm not going to say that it's a string because I have specifically and array of strings well to 79 00:05:22,870 --> 00:05:27,040 specify that recipients will be an array containing some discrete type. 80 00:05:27,040 --> 00:05:30,620 We can just write out an array and then inside there put string. 81 00:05:30,970 --> 00:05:36,550 So this right here communicates to mongoose that the recipient's property will be an array containing 82 00:05:36,610 --> 00:05:37,760 a list of strings. 83 00:05:39,770 --> 00:05:42,490 All right this is definitely to get us started with our schema. 84 00:05:42,560 --> 00:05:48,140 So the very last thing we have to do is make sure that we load this up into our mongoose library. 85 00:05:48,140 --> 00:05:51,170 Remember we called mongoose model. 86 00:05:51,350 --> 00:05:56,900 We pass in the name of the collection or the name of the model class which will be surveys and then 87 00:05:56,930 --> 00:05:59,750 the name of the schema which is sort of a schema 88 00:06:02,970 --> 00:06:04,610 OK that looks pretty good. 89 00:06:05,070 --> 00:06:06,770 Now one last thing to do with the survey. 90 00:06:06,800 --> 00:06:12,780 J.S. file remember that whenever we create a new file inside of our express project that doesn't just 91 00:06:12,840 --> 00:06:15,850 automatically include it or execute the code inside of here. 92 00:06:16,050 --> 00:06:21,240 So we need to make sure that we actually require this file in at least one location in our project. 93 00:06:21,240 --> 00:06:26,880 So I can to go back to the index js file and inside of here right underneath the user makes make sure 94 00:06:26,940 --> 00:06:35,910 that I actually require in that model file that we just create it will say models slash survey group 95 00:06:36,000 --> 00:06:37,720 survey. 96 00:06:38,160 --> 00:06:41,290 So OK this is looking pretty good. 97 00:06:41,550 --> 00:06:46,000 So we now have the idea of a survey inside of our application. 98 00:06:46,020 --> 00:06:50,470 Right now we've got the idea of our symbionts list as being an array of strings right here. 99 00:06:50,640 --> 00:06:55,440 I want to take a break and then in the next section I want to revisit this property and decide whether 100 00:06:55,440 --> 00:07:01,150 or not this is going to really work out for what we are trying to accomplish inside of our application. 101 00:07:01,170 --> 00:07:06,820 So let's take a quick break and decide whether or not this is going to do what we expect it to.