Initial commit: party-mix-app with prefetch cache, audio preload optimizations
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
84
apps/backend/internal/handlers/versions.go
Normal file
84
apps/backend/internal/handlers/versions.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/toyffee/party-mix/internal/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GetVersions(db *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
userID := currentUserID(c)
|
||||
var rows []models.UserVersion
|
||||
if err := db.Where("user_id = ?", userID).Find(&rows).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
result := make(map[string]gin.H, len(rows))
|
||||
for _, r := range rows {
|
||||
result[r.TrackTitle] = gin.H{
|
||||
"title": r.VersionTitle,
|
||||
"artist": r.VersionArtist,
|
||||
"duration": r.VersionDuration,
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
}
|
||||
|
||||
type saveVersionReq struct {
|
||||
TrackTitle string `json:"track_title" binding:"required"`
|
||||
VersionTitle string `json:"title" binding:"required"`
|
||||
VersionArtist string `json:"artist"`
|
||||
VersionDuration string `json:"duration"`
|
||||
}
|
||||
|
||||
func SaveVersion(db *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
userID := currentUserID(c)
|
||||
var req saveVersionReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
var row models.UserVersion
|
||||
err := db.Where("user_id = ? AND track_title = ?", userID, req.TrackTitle).First(&row).Error
|
||||
if err != nil {
|
||||
row = models.UserVersion{
|
||||
ID: uuid.NewString(),
|
||||
UserID: userID,
|
||||
TrackTitle: req.TrackTitle,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
row.VersionTitle = req.VersionTitle
|
||||
row.VersionArtist = req.VersionArtist
|
||||
row.VersionDuration = req.VersionDuration
|
||||
if err := db.Save(&row).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
type deleteVersionReq struct {
|
||||
TrackTitle string `json:"track_title" binding:"required"`
|
||||
}
|
||||
|
||||
func DeleteVersion(db *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
userID := currentUserID(c)
|
||||
var req deleteVersionReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
db.Where("user_id = ? AND track_title = ?", userID, req.TrackTitle).Delete(&models.UserVersion{})
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user