-
Notifications
You must be signed in to change notification settings - Fork 4
Implemented Scheme Manager microservice #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aerospace-prog
wants to merge
1
commit into
ONEST-Network:main
Choose a base branch
from
Aerospace-prog:scheme-manager-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| DB_HOST=db | ||
| DB_USER=kushagrapandey | ||
| DB_PASSWORD=postgres | ||
| DB_NAME=postgres | ||
| DB_PORT=5432 |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # syntax=docker/dockerfile:1 | ||
| FROM golang:1.24.2 | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| COPY go.mod ./ | ||
| COPY go.sum ./ | ||
| RUN go mod download | ||
|
|
||
| COPY . . | ||
|
|
||
| RUN go build -o scheme-manager . | ||
|
|
||
| EXPOSE 8080 | ||
|
|
||
| CMD ["./scheme-manager"] |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
|
|
||
| "github.com/joho/godotenv" | ||
| "gorm.io/driver/postgres" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // Global DB instance | ||
| var DB *gorm.DB | ||
|
|
||
| func ConnectDatabase() { | ||
| // Load environment variables from .env file | ||
| err := godotenv.Load() | ||
| if err != nil { | ||
| log.Fatal("β Error loading .env file:", err) | ||
| } | ||
|
|
||
| // Build DSN from environment variables | ||
| dsn := fmt.Sprintf( | ||
| "host=%s user=%s password=%s dbname=%s port=%s sslmode=disable", | ||
| os.Getenv("DB_HOST"), | ||
| os.Getenv("DB_USER"), | ||
| os.Getenv("DB_PASSWORD"), | ||
| os.Getenv("DB_NAME"), | ||
| os.Getenv("DB_PORT"), | ||
| ) | ||
|
|
||
| // Connect to PostgreSQL using GORM | ||
| db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
| if err != nil { | ||
| log.Fatal("β Failed to connect to database:", err) | ||
| } | ||
|
|
||
| // Assign db instance to global variable | ||
| DB = db | ||
| fmt.Println("β Connected to PostgreSQL database!") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/Aerospace-prog/scheme-manager/database" | ||
| "github.com/Aerospace-prog/scheme-manager/models" | ||
| ) | ||
|
|
||
| func CreateApplication(c *gin.Context) { | ||
| var application models.Application | ||
|
|
||
| // Bind the JSON payload to the struct | ||
| if err := c.ShouldBindJSON(&application); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request payload", "details": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| // Check if the referenced scheme exists | ||
| var scheme models.Scheme | ||
| if err := database.DB.First(&scheme, application.SchemeID).Error; err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Scheme not found"}) | ||
| return | ||
| } | ||
|
|
||
| // Default status | ||
| if application.Status == "" { | ||
| application.Status = "Pending" | ||
| } | ||
|
|
||
| // Save the application to DB | ||
| if err := database.DB.Create(&application).Error; err != nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create application", "details": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusCreated, application) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/Aerospace-prog/scheme-manager/config" | ||
| "github.com/Aerospace-prog/scheme-manager/models" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // =================== 1. Create Scheme =================== | ||
| func PushScheme(c *gin.Context) { | ||
| var scheme models.Scheme | ||
|
|
||
| if err := c.ShouldBindJSON(&scheme); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON", "details": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| if scheme.Name == "" || scheme.Description == "" || scheme.Eligibility == "" { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Name, Description and Eligibility are required"}) | ||
| return | ||
| } | ||
|
|
||
| if err := config.DB.Create(&scheme).Error; err != nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create scheme"}) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusCreated, gin.H{"message": "Scheme created", "scheme": scheme}) | ||
| } | ||
|
|
||
| // =================== 2. Get All Applications =================== | ||
| func GetApplications(c *gin.Context) { | ||
| status := c.Query("status") | ||
| var applications []models.Application | ||
| query := config.DB | ||
|
|
||
| if status != "" { | ||
| query = query.Where("status = ?", status) | ||
| } | ||
|
|
||
| if err := query.Find(&applications).Error; err != nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve applications"}) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, applications) | ||
| } | ||
|
|
||
| // =================== 3. Update Application Status =================== | ||
| func UpdateApplicationStatus(c *gin.Context) { | ||
| id := c.Param("id") | ||
| var payload struct { | ||
| Status string `json:"status"` | ||
| } | ||
|
|
||
| if err := c.ShouldBindJSON(&payload); err != nil || payload.Status == "" { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Status field is required"}) | ||
| return | ||
| } | ||
|
|
||
| validStatuses := map[string]bool{ | ||
| "pending": true, | ||
| "approved": true, | ||
| "rejected": true, | ||
| } | ||
|
|
||
| if !validStatuses[payload.Status] { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid status value"}) | ||
| return | ||
| } | ||
|
|
||
| var app models.Application | ||
| if err := config.DB.First(&app, id).Error; err != nil { | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "Application not found"}) | ||
| return | ||
| } | ||
|
|
||
| app.Status = payload.Status | ||
| if err := config.DB.Save(&app).Error; err != nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update status"}) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{"message": "Status updated", "application": app}) | ||
| } | ||
|
|
||
| // =================== 4. Get Application Status By its ID=================== | ||
|
|
||
| func GetApplicationByID(c *gin.Context) { | ||
| id := c.Param("id") | ||
| var application models.Application | ||
|
|
||
| // Find application by ID | ||
| if err := config.DB.First(&application, id).Error; err != nil { | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "Application not found"}) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "application": application, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package database | ||
|
|
||
| import ( | ||
| "gorm.io/driver/sqlite" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| var DB *gorm.DB | ||
|
|
||
| func InitDatabase() { | ||
| var err error | ||
| DB, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) | ||
Aerospace-prog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| panic("failed to connect database") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| version: "3.8" | ||
|
|
||
| services: | ||
| postgres-db: | ||
| image: postgres:15 | ||
| container_name: postgres-db | ||
| environment: | ||
| POSTGRES_USER: kushagrapandey | ||
| POSTGRES_PASSWORD: password | ||
| POSTGRES_DB: postgres | ||
| ports: | ||
| - "5432:5432" | ||
| healthcheck: | ||
| test: ["CMD-SHELL", "pg_isready -U kushagrapandey"] | ||
| interval: 5s | ||
| timeout: 5s | ||
| retries: 5 | ||
|
|
||
| scheme-manager-app: | ||
| build: . | ||
| container_name: scheme-manager-app | ||
| depends_on: | ||
| postgres-db: | ||
| condition: service_healthy | ||
| ports: | ||
| - "8080:8080" | ||
| environment: | ||
| DB_USER: kushagrapandey | ||
| DB_PASSWORD: password | ||
| DB_NAME: postgres | ||
| DB_HOST: postgres-db |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| module github.com/Aerospace-prog/scheme-manager | ||
|
|
||
| go 1.24.2 | ||
|
|
||
| require ( | ||
| github.com/gin-gonic/gin v1.10.0 | ||
| github.com/joho/godotenv v1.5.1 | ||
| gorm.io/driver/postgres v1.5.11 | ||
| gorm.io/gorm v1.25.12 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/bytedance/sonic v1.11.6 // indirect | ||
| github.com/bytedance/sonic/loader v0.1.1 // indirect | ||
| github.com/cloudwego/base64x v0.1.4 // indirect | ||
| github.com/cloudwego/iasm v0.2.0 // indirect | ||
| github.com/gabriel-vasile/mimetype v1.4.3 // indirect | ||
| github.com/gin-contrib/sse v0.1.0 // indirect | ||
| github.com/go-playground/locales v0.14.1 // indirect | ||
| github.com/go-playground/universal-translator v0.18.1 // indirect | ||
| github.com/go-playground/validator/v10 v10.20.0 // indirect | ||
| github.com/goccy/go-json v0.10.2 // indirect | ||
| github.com/jackc/pgpassfile v1.0.0 // indirect | ||
| github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect | ||
| github.com/jackc/pgx/v5 v5.7.4 // indirect | ||
| github.com/jackc/puddle/v2 v2.2.2 // indirect | ||
| github.com/jinzhu/inflection v1.0.0 // indirect | ||
| github.com/jinzhu/now v1.1.5 // indirect | ||
| github.com/json-iterator/go v1.1.12 // indirect | ||
| github.com/klauspost/cpuid/v2 v2.2.7 // indirect | ||
| github.com/kr/text v0.2.0 // indirect | ||
| github.com/leodido/go-urn v1.4.0 // indirect | ||
| github.com/mattn/go-isatty v0.0.20 // indirect | ||
| github.com/mattn/go-sqlite3 v1.14.22 // indirect | ||
| github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
| github.com/modern-go/reflect2 v1.0.2 // indirect | ||
| github.com/pelletier/go-toml/v2 v2.2.2 // indirect | ||
| github.com/rogpeppe/go-internal v1.14.1 // indirect | ||
| github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
| github.com/ugorji/go/codec v1.2.12 // indirect | ||
| golang.org/x/arch v0.8.0 // indirect | ||
| golang.org/x/crypto v0.36.0 // indirect | ||
| golang.org/x/net v0.25.0 // indirect | ||
| golang.org/x/sync v0.13.0 // indirect | ||
| golang.org/x/sys v0.31.0 // indirect | ||
| golang.org/x/text v0.24.0 // indirect | ||
| google.golang.org/protobuf v1.34.1 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| gorm.io/driver/sqlite v1.5.7 // indirect | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.