1 00:00:00,490 --> 00:00:04,980 We've made great progress on the FitnessFrog API entries endpoints. 2 00:00:04,980 --> 00:00:09,660 We just have validations left to implement, which is what we'll do next. 3 00:00:09,660 --> 00:00:14,420 But before we do that, let's review our API's design requirements. 4 00:00:14,420 --> 00:00:18,420 When we designed our API, we identified the need for the client to get a list of 5 00:00:18,420 --> 00:00:23,200 the available activities and intensities, in order to support adding a new entry. 6 00:00:27,208 --> 00:00:32,720 Our plan was to add endpoints for those resources that will support GET requests. 7 00:00:32,720 --> 00:00:36,930 So far in this course, we've collaborated on building out the entries endpoint. 8 00:00:36,930 --> 00:00:40,090 But now, it's your turn to take what you've learned and 9 00:00:40,090 --> 00:00:43,742 build out the activities and intensities endpoints. 10 00:00:43,742 --> 00:00:47,560 Let's walkthrough the requirements and high level implementation details for 11 00:00:47,560 --> 00:00:48,360 each endpoint. 12 00:00:49,360 --> 00:00:50,880 For the activities endpoint, 13 00:00:50,880 --> 00:00:54,420 you'll add an API controller that'll accept GET requests. 14 00:00:54,420 --> 00:00:56,970 Which should return a collection of resources 15 00:00:56,970 --> 00:00:59,520 representing the available activities. 16 00:00:59,520 --> 00:01:03,640 Each returned activity should have id and name properties. 17 00:01:10,664 --> 00:01:13,472 To get a list of the available activities, 18 00:01:13,472 --> 00:01:17,220 you can call the activity repositories GetList method. 19 00:01:18,874 --> 00:01:23,700 The intensities endpoint is similar to the activities endpoint. 20 00:01:23,700 --> 00:01:27,610 You'll add an API controller that'll accept GET request. 21 00:01:27,610 --> 00:01:32,379 Which should return a collection of resources representing the available 22 00:01:32,379 --> 00:01:33,940 intensities. 23 00:01:33,940 --> 00:01:38,190 Each returned intensity should have id and name properties. 24 00:01:43,420 --> 00:01:48,720 Unfortunately, the listed available intensities isn't defined in the database. 25 00:01:48,720 --> 00:01:53,487 Instead, intensities are defined as an enum IntensityLevel. 26 00:01:54,960 --> 00:02:01,170 You can use the enum GetValue static method to enumerate the values of an enum. 27 00:02:01,170 --> 00:02:04,660 If you get stuck on how to use the GetValues method to return the appropriate 28 00:02:04,660 --> 00:02:08,580 data from the API controller, see the teachers' notes for an additional hint. 29 00:02:11,775 --> 00:02:14,140 All right, now it's your turn. 30 00:02:14,140 --> 00:02:17,840 Go ahead and pause the video, and give this exercise a try. 31 00:02:17,840 --> 00:02:21,640 You can find a copy of these requirements in the teacher's notes. 32 00:02:21,640 --> 00:02:24,460 Good luck, and we'll see you in a bit to review the solution. 33 00:02:26,045 --> 00:02:28,470 Welcome back, how did you do? 34 00:02:28,470 --> 00:02:32,350 Were you able to add the activities and intensities endpoints? 35 00:02:32,350 --> 00:02:36,140 Don't worry if you weren't able to finish every task in this exercise. 36 00:02:36,140 --> 00:02:40,380 No matter how far you were able to get, just the act of trying alone will improve 37 00:02:40,380 --> 00:02:44,050 your ability to retain what you've learned so far in this course. 38 00:02:44,050 --> 00:02:46,050 Let's take a look at my solution. 39 00:02:46,050 --> 00:02:50,280 I added two classes to the Controllers folder, ActivitiesController and 40 00:02:50,280 --> 00:02:51,831 IntensitiesController. 41 00:02:54,300 --> 00:02:56,947 Let's take a closer look at the ActivitiesController class. 42 00:02:59,850 --> 00:03:04,160 I updated it to inherit from the ApiController base class. 43 00:03:05,350 --> 00:03:08,530 I then added a private field for the activitiesRepository. 44 00:03:10,670 --> 00:03:14,570 To get an instance of the repository, I used constructor injection. 45 00:03:15,995 --> 00:03:20,976 A defined an ActivitiesRepository parameter in order to declare 46 00:03:20,976 --> 00:03:24,400 that we have a dependency on that type. 47 00:03:24,400 --> 00:03:29,110 And then I stored the ActivitiesRepository parameter reference in our private field. 48 00:03:30,390 --> 00:03:33,120 I then added a Get action method. 49 00:03:33,120 --> 00:03:36,160 But how is a return type of IHttpActionResult? 50 00:03:38,513 --> 00:03:41,987 And returns a call to the Ok method. 51 00:03:41,987 --> 00:03:47,950 Passing in for its content a call to the _activitiesRepository.GetList method. 52 00:03:55,670 --> 00:03:59,890 Each activity resource needed to have an Id and name property. 53 00:03:59,890 --> 00:04:02,710 But as it turns out, the activity model or 54 00:04:02,710 --> 00:04:07,190 entity already defines an Id property and a name property. 55 00:04:07,190 --> 00:04:11,050 So, I just went ahead and returned the entities or models as they were. 56 00:04:12,360 --> 00:04:14,520 That's it for the ActivitiesController. 57 00:04:14,520 --> 00:04:16,055 Let's take a look at the IntensitiesController. 58 00:04:18,500 --> 00:04:20,829 Just like with the other controller, 59 00:04:20,829 --> 00:04:25,000 I updated this class to inherit from the ApiController base class. 60 00:04:25,000 --> 00:04:31,680 And, I added a Get action method that has return type of IHttpActionResult. 61 00:04:31,680 --> 00:04:35,490 Notice I don't have a constructor because we didn't need to bring in a dependency to 62 00:04:35,490 --> 00:04:38,560 a repository or anything else. 63 00:04:38,560 --> 00:04:41,270 As mentioned, when we were reviewing the requirements, 64 00:04:41,270 --> 00:04:45,500 we don't have a repository method to call to get the list of intensities. 65 00:04:45,500 --> 00:04:50,560 The intensities are defined as an enumeration named IntensityLevel. 66 00:04:50,560 --> 00:04:54,770 We can get the values of that enumeration by using the .NET Framework, 67 00:04:54,770 --> 00:04:57,270 enum get values static method. 68 00:04:57,270 --> 00:05:01,030 We need to pass into that method call the type information for 69 00:05:01,030 --> 00:05:03,247 the Entry IntensityLevel enumeration. 70 00:05:04,470 --> 00:05:08,655 Here, I'm using the typeof operator to get the type information for the enumeration. 71 00:05:10,680 --> 00:05:15,600 The enum good value static method returns an array of objects. 72 00:05:15,600 --> 00:05:19,970 So I'm using a Cast operator here, to cast all those objects 73 00:05:19,970 --> 00:05:24,529 to the type Entry IntensityLevel And then, 74 00:05:24,529 --> 00:05:30,380 to structure the data as the client app expects it, I'm using a Select operator. 75 00:05:30,380 --> 00:05:34,752 The Select operator takes each enumeration value, intensity level. 76 00:05:34,752 --> 00:05:42,250 And creates or instantiates an anonymous object with id and name properties. 77 00:05:43,773 --> 00:05:44,980 For the id property, 78 00:05:44,980 --> 00:05:48,810 I'm just explicitly casting the numeration to its integer value. 79 00:05:50,131 --> 00:05:54,311 For the name property I'm calling ToString on each numeration, 80 00:05:54,311 --> 00:05:57,280 which returns the numeration value's name. 81 00:05:58,554 --> 00:06:00,415 I then call ToList to execute the query. 82 00:06:00,415 --> 00:06:03,740 And finally, I return from the action method, 83 00:06:03,740 --> 00:06:08,390 I called to the Ok method, passing in my results variable. 84 00:06:08,390 --> 00:06:12,484 Don't worry if you didn't figure out on your own how to get the enumeration values 85 00:06:12,484 --> 00:06:16,480 into the data structure that the API needs to return for the client app. 86 00:06:16,480 --> 00:06:20,280 This approach is a little unusual, it's not something that you see every day. 87 00:06:20,280 --> 00:06:23,890 Next up, adding validations to the entries endpoint.