Updating MovieModel's Delete method from internal/data/movies.go file. Create the deleteMovieHandler function inside cmd/api/movies.go. Adding the route into routes.go to delete a movie.
This commit is contained in:
@@ -150,3 +150,30 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques
|
||||
app.serverErrorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) deleteMovieHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Extract the movie ID from the URL
|
||||
id, err := app.readIDParam(r)
|
||||
if err != nil {
|
||||
app.notFoundResponse(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete the movie from the database, sending a 404 Not Found response to the client if there isn't a matching record
|
||||
err = app.models.Movies.Delete(id)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, data.ErrRecordNotFound):
|
||||
app.notFoundResponse(w, r)
|
||||
default:
|
||||
app.serverErrorResponse(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Return a 200 OK status code along with a success message
|
||||
err = app.writeJSON(w, http.StatusOK, envelope{"message": "movie successfully deleted"}, nil)
|
||||
if err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user