...

/

Add a CRUD Feature for Item Data

Add a CRUD Feature for Item Data

Learn how to add the CRUD functionality to the REST API application with MySQL Database.

Add CRUD functionality to the service component

In the previous lesson, the service component uses local storage with a slice in Go. Because persistent storage will be used, we change the local storage to persistent storage using the MySQL database for all functionalities.

Get all items

Inside the services.go file, we change the implementation of the function used for getting all items.

func GetAllItems() []models.Item {
// create a variable to store items data
var items []models.Item = []models.Item{}
// get all data from the database order by created_at
database.DB.Order("created_at desc").Find(&items)
// return all items from the database
return items
}
Changed implementation for getting all items

In the code above, the variable called items is created to store the item data from the database. The Find() method is used ...