Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scheme-manager/.env
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 added scheme-manager/.gitignore
Empty file.
16 changes: 16 additions & 0 deletions scheme-manager/Dockerfile
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 added scheme-manager/README.md
Empty file.
42 changes: 42 additions & 0 deletions scheme-manager/config/db.go
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!")
}
39 changes: 39 additions & 0 deletions scheme-manager/controllers/applicationsController.go
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)
}
105 changes: 105 additions & 0 deletions scheme-manager/controllers/schemeController.go
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,
})
}
16 changes: 16 additions & 0 deletions scheme-manager/database/database.go
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{})
if err != nil {
panic("failed to connect database")
}
}
31 changes: 31 additions & 0 deletions scheme-manager/docker-compose.yml
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
50 changes: 50 additions & 0 deletions scheme-manager/go.mod
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
)
Loading