Commit main
This commit is contained in:
22
internal/store/database.go
Normal file
22
internal/store/database.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func Open() (*sql.DB, error) {
|
||||
db, err := sql.Open("sqlite3", "C:/Users/dilan/Documents/Go/my_website_db.db")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db open: %w", err)
|
||||
}
|
||||
|
||||
if err := db.Ping(); err != nil {
|
||||
return nil, fmt.Errorf("db ping: %w", err)
|
||||
}
|
||||
|
||||
fmt.Println("Connected to Database...")
|
||||
return db, nil
|
||||
}
|
||||
37
internal/store/project_store.go
Normal file
37
internal/store/project_store.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package store
|
||||
|
||||
import "database/sql"
|
||||
|
||||
type Project struct {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Link string `json:"link"`
|
||||
TechStack string `json:"tech_stack"`
|
||||
}
|
||||
|
||||
type SQLiteProjectStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewSQLiteProjectStore(db *sql.DB) *SQLiteProjectStore {
|
||||
return &SQLiteProjectStore{db: db}
|
||||
}
|
||||
|
||||
func (s *SQLiteProjectStore) GetAllProjects() ([]Project, error) {
|
||||
rows, err := s.db.Query("SELECT id, title, description, link, tech_stack FROM projects")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var projects []Project
|
||||
for rows.Next() {
|
||||
var p Project
|
||||
if err := rows.Scan(&p.ID, &p.Title, &p.Description, &p.Link, &p.TechStack); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
projects = append(projects, p)
|
||||
}
|
||||
return projects, nil
|
||||
}
|
||||
Reference in New Issue
Block a user