Initial commit
This commit is contained in:
31
app/internal/models/db.go
Normal file
31
app/internal/models/db.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var DB *sql.DB
|
||||
|
||||
func InitDB() {
|
||||
var err error
|
||||
|
||||
// Example DSN format for PostgreSQL:
|
||||
// "postgres://username:password@host:port/dbname?sslmode=disable"
|
||||
dsn := "postgres://mannpatel:Admin@localhost:5432/poll_database?sslmode=disable"
|
||||
|
||||
DB, err = sql.Open("postgres", dsn)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to DB: %v", err)
|
||||
}
|
||||
|
||||
err = DB.Ping()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to ping DB: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("Database connection successful")
|
||||
}
|
||||
176
app/internal/models/structs.go
Normal file
176
app/internal/models/structs.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
|
||||
type Claims struct {
|
||||
UserID int
|
||||
Role int
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
Token string
|
||||
User User
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error string
|
||||
Details []string
|
||||
}
|
||||
|
||||
type Role struct {
|
||||
RoleID int
|
||||
Name string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type User struct {
|
||||
UserID int
|
||||
FirstName string
|
||||
LastName string
|
||||
Email string
|
||||
Phone string
|
||||
Password string
|
||||
RoleID int
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type UserAddress struct {
|
||||
UserID int
|
||||
AddressLine1 string
|
||||
AddressLine2 string
|
||||
City string
|
||||
Province string
|
||||
Country string
|
||||
PostalCode string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Address Database
|
||||
// =====================
|
||||
|
||||
type AddressDatabase struct {
|
||||
AddressID int
|
||||
Address string
|
||||
StreetName string
|
||||
StreetType string
|
||||
StreetQuadrant string
|
||||
HouseNumber string
|
||||
HouseAlpha *string
|
||||
Longitude float64
|
||||
Latitude float64
|
||||
VisitedValidated bool
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Teams & Assignments
|
||||
// =====================
|
||||
|
||||
|
||||
type Team struct {
|
||||
TeamID int
|
||||
TeamLeadID int
|
||||
VolunteerID int
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type AdminVolunteer struct {
|
||||
AdminID int
|
||||
VolunteerID int
|
||||
IsActive bool
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type Appointment struct {
|
||||
SchedID int
|
||||
UserID int
|
||||
AddressID int
|
||||
AppointmentDate time.Time
|
||||
AppointmentTime time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Polls & Responses
|
||||
// =====================
|
||||
|
||||
type Poll struct {
|
||||
PollID int
|
||||
AddressID int
|
||||
UserID int
|
||||
ResponseURL string
|
||||
AmountDonated float64
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type PollResponse struct {
|
||||
ResponseID int
|
||||
PollID int
|
||||
Signage bool
|
||||
VotingChoice string
|
||||
DonationAmount float64
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Updates & Reactions
|
||||
// =====================
|
||||
|
||||
type Post struct {
|
||||
PostID int
|
||||
AuthorID int
|
||||
AuthorName string // for display
|
||||
Content string
|
||||
ImageURL string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
|
||||
type Reaction struct {
|
||||
ReactionID int
|
||||
PostID int
|
||||
UserID int
|
||||
ReactionType string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Volunteer Availability
|
||||
// =====================
|
||||
|
||||
type Availability struct {
|
||||
AvailabilityID int
|
||||
UserID int
|
||||
DayOfWeek string
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Chat Links
|
||||
// =====================
|
||||
|
||||
type ChatLink struct {
|
||||
ChatID int
|
||||
Platform string
|
||||
URL string
|
||||
UserID *int
|
||||
TeamID *int
|
||||
CreatedAt time.Time
|
||||
}
|
||||
28
app/internal/models/token.go
Normal file
28
app/internal/models/token.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
var jwtKey = []byte("your-secret-key") //TODO: Move to env/config
|
||||
|
||||
|
||||
func ExtractClaims(tokenStr string) (*Claims, error) {
|
||||
claims := &Claims{}
|
||||
|
||||
token, err := jwt.ParseWithClaims(tokenStr, claims, func(token *jwt.Token) (interface{}, error) {
|
||||
return jwtKey, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !token.Valid {
|
||||
return nil, fmt.Errorf("invalid token")
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
Reference in New Issue
Block a user