Skip to content

1995parham-teaching/students-fall-2022

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Students

Based on a Fall-2022 Internet Engineering Course Project at Amirkabir University of Tech.

GitHub Workflow Status GitHub go.mod Go version

Introduction

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.

SQLite is not enough?

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.

Up and Running

Build and run the students' server:

go build
./students

Student creation request:

curl 127.0.0.1:1373/v1/students -X POST -H 'Content-Type: application/json' -d '{ "name": "Parham Alvani" }'
{ "name": "Parham Alvani", "id": "89846857", "courses": null }

Student list request:

curl 127.0.0.1:1373/v1/students
[{ "name": "Parham Alvani", "id": "89846857", "courses": [] }]

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" }
  ]
}

About

Example ReSTFul Server in Golang - CE261-Web

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published