Search⌘ K
AI Features

Notes Creation API

Explore how to create a Notes Creation API using Golang and the Beego framework. Learn to set up routes, handle form submissions, and use ORM to save new notes in the database. This lesson teaches the practical implementation of creating and storing notes in a CRUD application.

The new note form page

We will start with creating an HTML form to enter the details of a new note.

Route

First, let’s create the route of the /notes/new page. In the routers/router.go file, create a new entry:

Go (1.18.2)
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")
}

We define the route for the new note form in line 12. The route is /notes/new. The action for the route is get:NotesNewForm. This means that the NotesNewForm() method in the NotesController controller will be executed when the route is requested.

The notes form handler

Now, we will implement the handler function for the above route in the file controllers/notes.go.

Go (1.18.2)
package controllers
import (
"beego_notes/models"
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"
}

In this ...