
Lesson Description
The "Register User API" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:
Melkey codes the HandleRegisterUser function. The JSON data in the incoming request body is decoded and validated.
Transcript from the "Register User API" Lesson
[00:00:00]
>> Melkey: So with that, we're gonna create a function that allows us to register users, okay? So the function is gonna be pretty easy, I'm just gonna go and make some space. We can do funk, I'm gonna do h, I'm gonna make it a pointer to our user handler, like so.
[00:00:17]
And then here we'll do handle register user, it's gonna take WH, TTP response writer and then a pointer to HTP request, like so. All right, in this two-step process, we should be fairly familiar with, we can do a var request, which you can be of type registerUserRequest. And then we're gonna decode the values we get from the R body into this particular variable here.
[00:00:46]
So we can do err is gonna be json.NewDecoder, pass it r.Body, okay? I'm gonna decode this into the address of that request like so, if error does not equal to nil, then we can do a few things. We do h.logger.Printf for ourselves, say, ERROR: decoding register request, okay?
[00:01:29]
>> Melkey: Oops.
>> Melkey: And we could also bring in the utils.WriteJSON library or function that we created earlier. And here we can do w, http.StatusBadRequest to bring in utils, that envelope we can say error, can say invalid pay request payload.
>> Melkey: And then make sure you return.
>> Speaker 2: So we only created that type at the top, basically just encoded?
[00:02:03]
>> Melkey: We created type-
>> Speaker 2: On type at the top of the register.
>> Melkey: Yeah, this one?
>> Speaker 2: One requires just to basically have a dump.
>> Melkey: Yep, yeah, so we have like this structs gonna hold that data, right? Which is kind of like the definition of structs, it just holds data, and you can use it across your application.
[00:02:21]
So it's just an easier way for us to extract the value from our do body into this not permanent data structure.
>> Speaker 3: Would you on a project have some sort of maybe globally defined errors across the project, like they did with, I can't remember, we were looking at the SQL package, and they had-
[00:02:44]
>> Melkey: Var error, this one actually var SQL error, yeah, absolutely, there's many different ways you can do it. I for one, you can have even a file called constants in your internal whatever. And that constants package could just export a bunch of things for it to use, like errors, different print statements, you can have that.
[00:03:10]
Depends on how big the project gets with the scope of it, you can reuse the variables like definitely use the same error. And you can declare there, but yeah, that's a pretty common practice have like a constant.go and you declare everything there and it's exported. Okay, so we have this handle, our registered user, so at this point, we can assume we've decoded everything safely and securely into the register user request into that dump struck.
[00:03:34]
Now we need to validate it, and how can we validate? Well, we actually have validate register user request, so put it there, and this has to be addressed like that, right? So now we can decode it, but that's not validating yet, right? Now we have the valid register request there, we can handle this error, but if error does not equal to nil, we can do utils.WriteJSON(w, http.statusBadRequest).
[00:04:04]
And in here we can do utils.Envelope, you put error, and this time it's gonna be err.Error like so. Because we've created that error message using the errors built in package, and then don't forget the return, okay? Next, we can go ahead and create store.User, so this is the actual user, we are okay to persist into our database.
[00:04:28]
We can make the username as req.Username, and we can also make the email as request.Email.
>> Melkey: Because bio is an optional field, we can do if req.Bio does not equal to this empty string, we can then also insert user.Bio = req.Bio. This could also be in its own function in the validate request function, if you want.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops