hngprojects / hng_boilerplate_golang_web

Apache License 2.0
77 stars 49 forks source link

[FEAT] Database Setup - Models, and Seeding #42

Closed Micah-Shallom closed 2 months ago

Micah-Shallom commented 2 months ago

Description

Set up a database management system for our golang project using GORM. The task includes configuring the database connection, creating entity classes, and seeding the database with initial data. This setup will help streamline data operations and ensure application can interact with the database efficiently.

Tasks

Database Configuration

Entity Classes

Create table structs for User, Profile, Organisation, and Product.

Define relationships between entities:

A user has one profile (https://github.com/OnetoOne). A user has many products (@OneToMany). A user belongs to many organisations (@manytomany). An organisation has many users (@manytomany).

Seeding the Database

Validation Steps

Api Endoint

{
    "status": "success",
    "code": 200,
    "data": {
        "id": "0190ceb8-c4d8-7843-8ab3-916dde3633b3",
        "name": "John Doe",
        "email": "john@example.com",
        "profile": {
            "profile_id": "0190ceb8-c4d8-7844-bd6f-45cb8e60eae8",
            "first_name": "John",
            "last_name": "Doe",
            "phone": "1234567890",
            "avatar_url": "http://example.com/avatar.jpg",
            "user_id": "0190ceb8-c4d8-7843-8ab3-916dde3633b3",
            "created_at": "2024-07-20T06:58:53.921069+01:00",
            "updated_at": "2024-07-20T06:58:53.921069+01:00"
        },
        "organisations": [
            {
                "id": "0190ceb8-c4d8-784b-83d0-8cb4787eaf49",
                "name": "Org1",
                "description": "Description1",
                "users": null,
                "created_at": "2024-07-20T06:58:53.932198+01:00",
                "updated_at": "2024-07-20T06:58:53.932198+01:00"
            },
            {
                "id": "0190ceb8-c4d8-784c-a831-461d6be50ba0",
                "name": "Org2",
                "description": "Description2",
                "users": null,
                "created_at": "2024-07-20T06:58:53.934652+01:00",
                "updated_at": "2024-07-20T06:58:53.934652+01:00"
            }
        ],
        "products": [
            {
                "product_id": "0190ceb8-c4d8-7845-af93-0c2b71af59da",
                "name": "Product1",
                "description": "Description1",
                "owner_id": "0190ceb8-c4d8-7843-8ab3-916dde3633b3",
                "created_at": "2024-07-20T06:58:53.924903+01:00",
                "updated_at": "2024-07-20T06:58:53.924903+01:00"
            },
            {
                "product_id": "0190ceb8-c4d8-7846-862f-aafc4aed3cd4",
                "name": "Product2",
                "description": "Description2",
                "owner_id": "0190ceb8-c4d8-7843-8ab3-916dde3633b3",
                "created_at": "2024-07-20T06:58:53.924903+01:00",
                "updated_at": "2024-07-20T06:58:53.924903+01:00"
            }
        ],
        "created_at": "2024-07-20T06:58:53.919147+01:00",
        "updated_at": "2024-07-20T06:58:53.936264+01:00"
    }
}

Testing

Micah-Shallom commented 2 months ago

@NwokoyeChigozie

Micah-Shallom commented 2 months ago

@NwokoyeChigozie modifications made to database setup