uAdmin the Golang Web Framework #3 Business Logic

Abdullah Alrasheed
4 min readNov 5, 2018

--

Wouldn’t be cool if you can access your todo items directly from your task? well this is exactly what inlines are for. We will register an inline for task to add a list of related todo to it. Open your main.go and edit it to look like this:

package mainimport (
"github.com/uadmin/uadmin"
"github.com/your_name/todo/models"
)
func main() {
uadmin.Register(
models.Todo{},
models.Task{},
)
uadmin.RegisterInlines(models.Task{}, map[string]string{
"Todo": "TaskID",
})
uadmin.SiteName = "Todo App"
uadmin.StartServer()
}

Notice the uadmin.RegisterInlines function that we added. It receives a model as the first parameter and the second parameter is a map[string]string where the key of the map is the name of the related model and the value is the uint field for that relation. You can add multiple inlines if you have multiple relationships that you want to link to a model.

Now run your app again and you should see this:

Notice the second tab called “TODO”. If you open it you will see a table with all related todo items to this task.

From this table you can add new todo items directly.

Notice that the task is pre-selected for you so you can fill the other fields and save and it will return you to the same task you were in.

Now let’s add some business logic to our app. I would like to calculate the progress of my task based on the progress of the child todo items. Now to do that, I will need to get all the todo items from the database that is linked to my task and get the average progress from all of them and update my task. Since this is going to change when we change a todo we need to add this business logic in todo. To do that, we will override the Save() method of todo. Open /models/todo.go and add this method:

package modelsimport (
"github.com/uadmin/uadmin"
"time"
)
type Todo struct {
uadmin.Model
Name string
Description string `uadmin:"html"`
TargetDate *time.Time
Progress int `uadmin:"progress_bar"`
Task Task
TaskID uint
}
func (t *Todo) Save() {
// Save the model to DB
uadmin.Save(t)
// Get a list of other todo items that share the same
// TaskID. Notice that in the filter we use task_id which
// is the way this is created in the DB
todoList := []Todo{}
uadmin.Filter(&todoList, "task_id = ?", t.TaskID)
progressSum := 0
progressCount := len(todoList)
// Sum up the progress of all tasks
for _, todo := range todoList {
progressSum += todo.Progress
}
// Preload the todo model to get the related task
uadmin.Preload(t)
// Calculate the average progress
t.Task.TotalProgress = progressSum / progressCount
// Finally save the task
uadmin.Save(&t.Task)
}

Now go ahead and add a few todo items and make sure they point to your task.

Notice now that your task is updating its progress automatically

Finally, let’s make this customize our progress bar. I would like to make it show different colors depending on progress:

  • Red: 0–30
  • Yellow: 31–70
  • Green: 71–100

The way we can do that is by customizing the progress_bar tag. We can add these thresholds like this progress_bar:30:red,70:yellow,100:green. Let’s do that to both progress bars in todo and task.

In /models/todo.go, change the field Progress with this line:

Progress int `uadmin:"progress_bar:30:red,70:yellow,100:green"`

In /models/task.go, change the field TotalProgress with this line:

TotalProgress int `uadmin:"progress_bar:30:red,70:yellow,100:green"`

Run your app again and you will get this:

Congrats, you know how do the following:

  • add inlines
  • use uadmin.Filter to get records from your database
  • add business logic by overriding Save() method
  • you have mastered the progress_bar tag.

In part 4, we will talk about customizing your dashboard.

--

--