Files
greenlight/cmd/api/users.go
Maxime Delporte 0262be65ef
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 2m54s
Sending background emails.
2025-12-04 14:12:55 +01:00

79 lines
2.8 KiB
Go

package main
import (
"errors"
"net/http"
"greenlight.craftr.fr/internal/data"
"greenlight.craftr.fr/internal/validator"
)
func (app *application) registerUserHandler(w http.ResponseWriter, r *http.Request) {
// Create an anonymous struct to hold the expected data from the request body
var input struct {
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
}
// Parse the request body into the anonymous struct
err := app.readJSON(w, r, &input)
if err != nil {
app.badRequestResponse(w, r, err)
return
}
// Copy the data from the request body into a new User struct. Notice also that we set the Activated field to false, which isn't strictly necessary because the Activated field will have the zero-value of false by default. But setting this explicitly helps to male our intentions clear to anyone reading the code.
user := &data.User{
Name: input.Name,
Email: input.Email,
Activated: false,
}
// Use the Password.Set() method to generate and store the hashed and plaintext passwords
err = user.Password.Set(input.Password)
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
v := validator.New()
// Validate the user struct and return the error messages to the client if any of the checks fail
if data.ValidateUser(v, user); !v.Valid() {
app.failedValidationResponse(w, r, v.Errors)
return
}
// Insert the user data into the database
err = app.models.Users.Insert(user)
if err != nil {
switch {
// If we get a ErrDuplicateEmail error, use the v.AddError() method to manually add a message to the validator instance, and then cal our failedValidationResponse() helper
case errors.Is(err, data.ErrDuplicateEmail):
v.AddError("email", "a user with this email address already exists")
app.failedValidationResponse(w, r, v.Errors)
default:
app.serverErrorResponse(w, r, err)
}
return
}
// Use the background helper to execute an anonymous function that send the welcome email
app.background(func() {
// Call the Send() method on our Mail, passing in the user's email address, name of the template file, and the User struct containing the new user's data.
err = app.mailer.Send(user.Email, "user_welcome.tmpl", user)
if err != nil {
// Importantly, if there is an error sending the email, then we use the app.logger.Error() helper to manage it, instead of the app.serverErrorResponse() helper like before.
app.logger.Error(err.Error())
}
})
// Not that we also change this to send the client a 202 Accepted status code. This status code indicates that the request has been accepted for processing, but the processing has not been completed.
err = app.writeJSON(w, http.StatusAccepted, envelope{"user": user}, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}