1
00:00:00,050 --> 00:00:00,380
All right.

2
00:00:00,380 --> 00:00:06,350
And up next, let's fix the lead job as well as update job controller.

3
00:00:06,350 --> 00:00:07,190
And you know what?

4
00:00:07,190 --> 00:00:13,180
Let me start with delete, since I think it's going to be easier to grasp.

5
00:00:13,190 --> 00:00:16,250
So first, let's navigate back to job controller.

6
00:00:16,280 --> 00:00:18,050
Leave these ones for now.

7
00:00:18,050 --> 00:00:21,560
So I will remove them at the very end of this video.

8
00:00:22,300 --> 00:00:22,810
Effectively.

9
00:00:22,810 --> 00:00:25,120
We're looking for delete job again.

10
00:00:25,150 --> 00:00:26,230
Same deal.

11
00:00:26,260 --> 00:00:34,210
We're not working anymore with the local array, so pretty much remove all of the code that in some

12
00:00:34,210 --> 00:00:37,060
shape or form is working with that local array.

13
00:00:37,090 --> 00:00:41,590
So I'm talking about this one over here and also these two lines of code.

14
00:00:41,860 --> 00:00:44,680
And first, let's create a variable.

15
00:00:44,680 --> 00:00:48,460
And in my case, I'm going to go with remove job.

16
00:00:48,610 --> 00:00:55,420
We want to go with Await again the model and we're looking for find one and delete.

17
00:00:55,420 --> 00:00:59,890
So that's the one we want to use as far as the difference between delete and remove.

18
00:01:00,470 --> 00:01:04,610
The remove is being deprecated right now, so stick with delete.

19
00:01:04,910 --> 00:01:11,090
And you want to go over here and pass in the ID, which again is coming from params and same deal.

20
00:01:11,090 --> 00:01:13,640
If Mongoose cannot find the specific job.

21
00:01:13,640 --> 00:01:16,010
Well, we want to send back the 404.

22
00:01:16,040 --> 00:01:19,490
Now in this case since the variable name is removed job.

23
00:01:20,450 --> 00:01:23,300
I'm going to change it here to remove job.

24
00:01:23,300 --> 00:01:24,190
And you know what?

25
00:01:24,200 --> 00:01:26,660
I will send back job deleted.

26
00:01:26,660 --> 00:01:31,340
But I'll also add the job just so you can see what's happening.

27
00:01:31,340 --> 00:01:37,160
So we're going to go here with Job and we're going to go with removed job.

28
00:01:37,160 --> 00:01:38,420
Let's save it.

29
00:01:38,570 --> 00:01:41,570
We're going to navigate to all jobs again.

30
00:01:42,270 --> 00:01:43,230
Let's make a request.

31
00:01:43,230 --> 00:01:44,250
Because why not?

32
00:01:44,440 --> 00:01:47,270
And I just want to delete the first one over here.

33
00:01:47,280 --> 00:01:49,620
So we're looking for delete job.

34
00:01:50,520 --> 00:01:52,740
You want to pass here the correct ID?

35
00:01:53,400 --> 00:01:57,610
If everything is correct, this is the job we just removed.

36
00:01:57,630 --> 00:02:02,710
So if I go back to all jobs now, I have only three of them.

37
00:02:02,730 --> 00:02:07,320
Again, this is going to depend what are you doing on the front end?

38
00:02:07,560 --> 00:02:11,700
So most likely you'll just send back message.

39
00:02:11,700 --> 00:02:14,560
You'll say, Hey, you were successful.

40
00:02:14,580 --> 00:02:15,420
That's it.

41
00:02:15,450 --> 00:02:16,350
We're good to go.

42
00:02:16,380 --> 00:02:17,980
Move on to the next task.

43
00:02:18,000 --> 00:02:25,770
If for some reason you need the info about the job you just removed, well, you have access to it and

44
00:02:25,770 --> 00:02:27,030
just to showcase.

45
00:02:27,040 --> 00:02:32,420
And I don't think I'm going to do that with the updated job, but this one, I'll still do it.

46
00:02:32,430 --> 00:02:40,020
So I'm going to go to remove job and I just want to showcase what happens if again, the ID doesn't

47
00:02:40,020 --> 00:02:40,410
exist.

48
00:02:40,410 --> 00:02:43,920
On a side note, you can actually use the existing value.

49
00:02:43,950 --> 00:02:44,370
Why?

50
00:02:44,370 --> 00:02:46,920
Well, because we just removed that job, correct.

51
00:02:46,920 --> 00:02:50,050
So of course there is no more job.

52
00:02:50,050 --> 00:02:51,340
And again, same deal.

53
00:02:51,340 --> 00:02:52,870
We get back this null.

54
00:02:53,110 --> 00:02:55,150
So hopefully that is clear.

55
00:02:55,240 --> 00:03:04,150
And now let's move up and we want to handle the update one which usually contains more logic.

56
00:03:04,780 --> 00:03:09,940
And actually, since we'll have the validation layer, we can remove all of these lines of code.

57
00:03:09,940 --> 00:03:16,200
So we will check for empty or wrong values in a different way.

58
00:03:16,210 --> 00:03:22,360
So pretty much everything starting from the company and then the 400 and all that, just remove it.

59
00:03:22,360 --> 00:03:25,030
So we're still looking for the params.

60
00:03:25,210 --> 00:03:31,150
That doesn't change, but of course we're not going to be looking here in the local one and we're also

61
00:03:31,150 --> 00:03:34,360
not going to update in such a way.

62
00:03:34,510 --> 00:03:36,190
So what are we going to do?

63
00:03:36,190 --> 00:03:38,680
Well, we're going to set up a variable.

64
00:03:38,890 --> 00:03:42,490
I'll call this updated job in here.

65
00:03:42,490 --> 00:03:49,780
Again, we go with Await, then Job Dot and find one and update.

66
00:03:49,840 --> 00:03:51,100
That's the method.

67
00:03:51,100 --> 00:03:54,010
And in here, of course, we need to provide more value.

68
00:03:54,010 --> 00:03:57,910
So the first one is going to be ID, which is coming from params.

69
00:03:58,450 --> 00:04:01,660
And yes, I probably have said this quite a few times.

70
00:04:01,660 --> 00:04:06,890
I fully understand that it might be annoying for somebody, but I just want to make it clear.

71
00:04:06,890 --> 00:04:09,050
So it's in the params that's our ID.

72
00:04:09,470 --> 00:04:13,640
And second argument in this case is going to be well what do we want to update?

73
00:04:13,760 --> 00:04:18,740
And again, since we'll have the validation layer, what's really cool, we can basically pass in the

74
00:04:18,740 --> 00:04:19,640
entire thing.

75
00:04:19,640 --> 00:04:21,260
So again, you have multiple options.

76
00:04:21,260 --> 00:04:26,600
If you want to go here manually, set up the object and let's say pass in the property with some kind

77
00:04:26,600 --> 00:04:28,520
of value, you can definitely do so.

78
00:04:28,520 --> 00:04:34,520
But in our case, since we know that Req.body is already an object, I can simply do it over here.

79
00:04:34,520 --> 00:04:38,480
And again, yes, we will be checking for those empty values.

80
00:04:38,950 --> 00:04:40,930
And we'll set it up a little bit later.

81
00:04:41,170 --> 00:04:44,500
And the last thing over here is going to be the options object.

82
00:04:44,860 --> 00:04:47,870
And we want to go with new equals to true.

83
00:04:47,890 --> 00:04:51,990
So by default, it's not going to send you the updated job.

84
00:04:52,000 --> 00:04:55,950
It basically is going to send you the old job before the update.

85
00:04:55,960 --> 00:04:57,370
But we don't want that.

86
00:04:57,370 --> 00:04:58,330
So we want a new one.

87
00:04:58,330 --> 00:04:59,770
That's why we go here with new.

88
00:04:59,770 --> 00:05:00,310
True.

89
00:05:00,340 --> 00:05:01,420
Same deal.

90
00:05:01,420 --> 00:05:02,140
If.

91
00:05:02,700 --> 00:05:09,000
There is no job which matches the ID, we'll get back the null and therefore, of course we can set

92
00:05:09,000 --> 00:05:13,170
up this check as well with updated job over here.

93
00:05:13,170 --> 00:05:17,610
And if we're successful, we'll send back the message job modified.

94
00:05:17,850 --> 00:05:23,910
And when it comes to my job property, I'll just set it equal to updated job.

95
00:05:24,060 --> 00:05:27,210
Let me save this and let's try it out.

96
00:05:27,240 --> 00:05:31,140
So I'm going to go to edit Job.

97
00:05:32,070 --> 00:05:33,300
And you know what?

98
00:05:33,300 --> 00:05:35,250
First, let me get all the jobs.

99
00:05:35,250 --> 00:05:36,090
Let me see what I have.

100
00:05:36,120 --> 00:05:37,200
So I have.

101
00:05:38,100 --> 00:05:39,390
These three jobs.

102
00:05:39,630 --> 00:05:40,350
And you know what?

103
00:05:40,350 --> 00:05:42,630
I'm going to change this one.

104
00:05:42,630 --> 00:05:46,770
The second one, and I'm going to go from frontend to DevOps.

105
00:05:46,830 --> 00:05:51,060
So let me navigate right now to edit job.

106
00:05:51,180 --> 00:05:52,740
First, let me set up.

107
00:05:54,050 --> 00:05:55,370
The over here.

108
00:05:55,700 --> 00:05:57,530
And as far as the company.

109
00:05:57,530 --> 00:06:03,080
Well, since I'm not updating, I mean, we don't really need to use it, so I can just remove it.

110
00:06:03,080 --> 00:06:07,100
And as far as the position, I'm going to go with DevOps and check it out.

111
00:06:07,130 --> 00:06:11,540
This is going to be the new updated job.

112
00:06:11,630 --> 00:06:18,080
And if I take a look at all the jobs, of course the value is going to be DevOps in the second one as

113
00:06:18,080 --> 00:06:18,620
well.

114
00:06:18,620 --> 00:06:23,750
And just to reiterate, of course we can provide other properties and values as well.

115
00:06:24,170 --> 00:06:30,830
So this was just to demonstrate that of course our added functionality in the database works and at

116
00:06:30,830 --> 00:06:37,250
the very end let me navigate to job controller and notice we're not using the local jobs anymore.

117
00:06:37,250 --> 00:06:42,740
I can also remove the nano ID and up next we can talk about the status code.

