move models into db package

This commit is contained in:
sentriz
2020-02-19 20:08:37 +00:00
parent e67588623b
commit a42edc3441
23 changed files with 133 additions and 145 deletions

View File

@@ -3,11 +3,11 @@ package ctrladmin
import (
"encoding/gob"
"fmt"
"strings"
"html/template"
"log"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/Masterminds/sprig"
@@ -18,7 +18,7 @@ import (
"github.com/wader/gormstore"
"senan.xyz/g/gonic/assets"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
"senan.xyz/g/gonic/server/ctrlbase"
"senan.xyz/g/gonic/version"
)
@@ -96,22 +96,22 @@ func New(base *ctrlbase.Controller) *Controller {
type templateData struct {
// common
Flashes []interface{}
User *model.User
User *db.User
Version string
// home
AlbumCount int
ArtistCount int
TrackCount int
RequestRoot string
RecentFolders []*model.Album
AllUsers []*model.User
RecentFolders []*db.Album
AllUsers []*db.User
LastScanTime time.Time
IsScanning bool
Playlists []*model.Playlist
Playlists []*db.Playlist
//
CurrentLastFMAPIKey string
CurrentLastFMAPISecret string
SelectedUser *model.User
SelectedUser *db.User
}
type adminHandler func(r *http.Request) *Response
@@ -168,7 +168,7 @@ func (c *Controller) H(h adminHandler) http.Handler {
return
}
}
if user, ok := r.Context().Value(CtxUser).(*model.User); ok {
if user, ok := r.Context().Value(CtxUser).(*db.User); ok {
resp.data.User = user
}
buff := c.buffPool.Get()

View File

@@ -7,7 +7,7 @@ import (
"strconv"
"time"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
"senan.xyz/g/gonic/scanner"
"senan.xyz/g/gonic/server/lastfm"
)
@@ -54,7 +54,7 @@ func (c *Controller) ServeHome(r *http.Request) *Response {
data.LastScanTime = time.Unix(i, 0)
}
// ** begin playlists box
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
c.DB.
Where("user_id = ?", user.ID).
Limit(20).
@@ -80,7 +80,7 @@ func (c *Controller) ServeChangeOwnPasswordDo(r *http.Request) *Response {
flashW: []string{err.Error()},
}
}
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
user.Password = passwordOne
c.DB.Save(user)
return &Response{redirect: "/admin/home"}
@@ -105,14 +105,14 @@ func (c *Controller) ServeLinkLastFMDo(r *http.Request) *Response {
flashW: []string{err.Error()},
}
}
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
user.LastFMSession = sessionKey
c.DB.Save(&user)
return &Response{redirect: "/admin/home"}
}
func (c *Controller) ServeUnlinkLastFMDo(r *http.Request) *Response {
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
user.LastFMSession = ""
c.DB.Save(&user)
return &Response{redirect: "/admin/home"}
@@ -210,7 +210,7 @@ func (c *Controller) ServeCreateUserDo(r *http.Request) *Response {
flashW: []string{err.Error()},
}
}
user := model.User{
user := db.User{
Name: username,
Password: passwordOne,
}
@@ -273,7 +273,7 @@ func (c *Controller) ServeUploadPlaylistDo(r *http.Request) *Response {
code: 500,
}
}
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
var playlistCount int
var errors []string
for _, headers := range r.MultipartForm.File {

View File

@@ -5,8 +5,7 @@ import (
"net/http"
"github.com/gorilla/sessions"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
)
func (c *Controller) WithSession(next http.Handler) http.Handler {
@@ -47,7 +46,7 @@ func (c *Controller) WithAdminSession(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// session and user exist at this point
session := r.Context().Value(CtxSession).(*sessions.Session)
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
if !user.IsAdmin {
sessAddFlashW(session, []string{"you are not an admin"})
sessLogSave(session, w, r)

View File

@@ -8,15 +8,14 @@ import (
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
)
func playlistParseLine(c *Controller, path string) (int, error) {
if strings.HasPrefix(path, "#") || strings.TrimSpace(path) == "" {
return 0, nil
}
var track model.Track
var track db.Track
query := c.DB.Raw(`
SELECT tracks.id FROM TRACKS
JOIN albums ON tracks.album_id = albums.id
@@ -62,8 +61,8 @@ func playlistParseUpload(c *Controller, userID int, header *multipart.FileHeader
if err := scanner.Err(); err != nil {
return []string{fmt.Sprintf("iterating playlist file: %v", err)}, true
}
playlist := &model.Playlist{}
c.DB.FirstOrCreate(playlist, model.Playlist{
playlist := &db.Playlist{}
c.DB.FirstOrCreate(playlist, db.Playlist{
Name: playlistName,
UserID: userID,
})