Note Deletion

Learn to implement a route to delete a note from the database using Beego.

Deleting a note

To delete a note, we will have to implement a new route.

First, we will create a new route with the HTTP DELETE method, followed by the implementation of the handler function.

Let’s start by defining a new route in the routers/route.go file:

Press + to interact
package routers
import (
"beego_notes/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/notes", &controllers.NotesController{}, "get:NotesIndex")
beego.Router("/notes/new", &controllers.NotesController{}, "get:NotesNewForm")
beego.Router("/notes", &controllers.NotesController{}, "post:NotesCreate")
beego.Router("/notes/:id([0-9]+)", &controllers.NotesController{}, "get:NotesShow")
beego.Router("/notes/edit/:id([0-9]+)", &controllers.NotesController{}, "get:NotesEditPage")
beego.Router("/notes/:id", &controllers.NotesController{}, "post:NotesUpdate")
beego.Router("/notes/:id", &controllers.NotesController{}, "delete:NotesDelete")
}

A new DELETE route, /notes/:id, is added at line 17 to trigger the NotesDelete() action in the NotesController. This action removes the note with the specified ID from the database when an HTTP DELETE request is made to this path.

Handler to delete a note

Now, let’s implement the handler functions for the route we defined:

Press + to interact
package controllers
import (
"beego_notes/models"
"fmt"
"net/http"
"strconv"
beego "github.com/beego/beego/v2/server/web"
)
// NotesController operations for Notes
type NotesController struct {
beego.Controller
}
func (c *NotesController) NotesIndex() {
// Get all notes
notes := models.NotesGetAll()
c.Data["notes"] = notes
c.TplName = "notes/index.tpl"
}
func (c *NotesController) NotesNewForm() {
c.TplName = "notes/new.tpl"
}
func (c *NotesController) NotesCreate() {
name := c.GetString("name")
content := c.GetString("content")
models.NotesCreate(name, content)
c.Redirect("/notes", http.StatusFound)
}
func (c *NotesController) NotesShow() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
fmt.Printf("Error: %v", err)
}
note := models.NotesFind(id)
c.Data["note"] = note
c.TplName = "notes/show.tpl"
}
func (c *NotesController) NotesEditPage() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
fmt.Printf("Error: %v", err)
}
note := models.NotesFind(id)
c.Data["note"] = note
c.TplName = "notes/edit.tpl"
}
func (c *NotesController) NotesUpdate() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
fmt.Printf("Error: %v", err)
}
note := models.NotesFind(id)
name := c.GetString("name")
content := c.GetString("content")
note.Update(name, content)
c.Redirect("/notes/"+idStr, http.StatusFound)
}
func (c *NotesController) NotesDelete() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
fmt.Printf("Error: %v", err)
}
models.NotesMarkDelete(id)
c.Ctx.ResponseWriter.WriteHeader(http.StatusOK)
}
...