This review discusses how to write a web application using the Echo HTTP Framework and GORM ORM. The application is designed to store information about students and their courses in a SQLite database. The relationship between the courses and students is many-to-many, meaning that students can take multiple courses, and each course can have multiple students.
To ensure code simplicity and maintainability, best practices were used. The code structure is compatible with the popular project-layout.
The application uses two models, Student
and Course
, for in-application communication.
The models use request/responses to serialize data over HTTP and store structures to serialize data from/to the database.
To generate a student ID, a random number is assigned to each student.
There is no authentication over the APIs, and anyone can use CRUD over students and courses.
However, using SQLite has its limitations. GORM cannot easily switch to PostgreSQL, and implementing this change would require a complete structure overhaul. Changing the connection is not enough, and running the migration on store creation is not recommended.
Build and run the students' server:
go build
./students
Student creation request:
curl 127.0.0.1:1373/students/ -X POST -H 'Content-Type: application/json' -d '{ "name": "Parham", "family": "Alvani" }'
711378
Create request will create a random student number, so you can insert students that have same name and family. Student list request:
curl 127.0.0.1:1373/v1/students/
[
{ "id": 644366, "first_name": "Parham", "last_name": "Alvani" },
{ "id": 917946, "first_name": "Parham", "last_name": "الوانی" },
{ "id": 711378, "first_name": "Parham", "last_name": "الوانی" }
]
Course creation request:
curl 127.0.0.1:1373/v1/courses -X POST -H 'Content-Type: application/json' -d '{ "name": "Internet Engineering" }'
{ "Name": "Internet Engineering", "ID": "00000007" }
Register student into a course:
curl 127.0.0.1:1373/v1/students/89846857/register/00000007
null
And then we have the course into the student course list:
curl 127.0.0.1:1373/v1/students/89846857
{
"name": "Parham Alvani",
"id": "89846857",
"courses": [{ "Name": "Internet Engineering", "ID": "00000007" }]
}
Then we can even add new course and register our student into that course too:
curl 127.0.0.1:1373/v1/courses -X POST -H 'Content-Type: application/json' -d '{ "name": "C Programming" }'
{ "Name": "C Programming", "ID": "00000000" }
curl 127.0.0.1:1373/v1/students/89846857/register/00000000
null
curl 127.0.0.1:1373/v1/students/89846857
{
"name": "Parham Alvani",
"id": "89846857",
"courses": [
{ "Name": "C Programming", "ID": "00000000" },
{ "Name": "Internet Engineering", "ID": "00000007" }
]
}