set up backend project structure and started on initial models and routes

This commit is contained in:
wisplite
2025-08-27 02:32:52 -05:00
parent 80bf7e9cf2
commit 8263f9c50a
11 changed files with 318 additions and 7 deletions
+36
View File
@@ -0,0 +1,36 @@
package db
import (
"log"
"github.com/glebarez/sqlite"
"github.com/wisplite/raster/internal/models"
"gorm.io/gorm"
)
var db *gorm.DB
func Init() bool {
database, err := gorm.Open(sqlite.Open("raster.db"), &gorm.Config{})
if err != nil {
log.Fatal("failed to connect database: ", err)
return false
}
// Run migrations
err = database.AutoMigrate(
&models.Album{},
)
if err != nil {
log.Fatal("failed to migrate database: ", err)
return false
}
db = database
return true
}
func GetDB() *gorm.DB {
return db
}
+17
View File
@@ -0,0 +1,17 @@
package models
import (
"time"
"gorm.io/datatypes"
)
type Album struct {
ID string `gorm:"primaryKey"`
Title string `gorm:"not null"`
Description string `gorm:"not null"`
Tags datatypes.JSON `gorm:"type:json"`
Private bool `gorm:"not null"`
CreatedAt time.Time
UpdatedAt time.Time
}
+20
View File
@@ -0,0 +1,20 @@
package routes
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/wisplite/raster/internal/services"
)
func RegisterAlbumRoutes(rg *gin.RouterGroup) {
album := rg.Group("/albums")
album.GET("/getPublic", func(c *gin.Context) {
albums, err := services.GetPublicAlbums()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, albums)
})
}
+8
View File
@@ -0,0 +1,8 @@
package routes
import "github.com/gin-gonic/gin"
func RegisterRoutes(r *gin.Engine) {
rg := r.Group("/api")
RegisterAlbumRoutes(rg)
}
+25
View File
@@ -0,0 +1,25 @@
package services
import (
"github.com/wisplite/raster/internal/db"
"github.com/wisplite/raster/internal/models"
)
func GetPublicAlbums() ([]models.Album, error) {
albums := []models.Album{}
result := db.GetDB().Where("private = ?", false).Find(&albums)
if result.Error != nil {
return []models.Album{}, result.Error
}
return albums, nil
}
func GetAlbum(id string, authToken string) (models.Album, error) {
// TODO: Add authentication
album := models.Album{}
result := db.GetDB().First(&album, "id = ?", id)
if result.Error != nil {
return models.Album{}, result.Error
}
return album, nil
}