This commit is contained in:
@@ -185,6 +185,28 @@ func GetData(server config.Server, apiVersion string) (data *ExportData, err err
|
||||
return &export, nil
|
||||
}
|
||||
|
||||
func DeleteData(target config.Server, path, id string) {
|
||||
url := fmt.Sprintf("%s/%s/%s/%s", target.Url, config.Config.ApiVersion, path, id)
|
||||
client := resty.New()
|
||||
client.SetLoggerWarnLevel(true)
|
||||
token := GetToken(client, target)
|
||||
_, err := client.R().
|
||||
SetAuthToken(token).
|
||||
SetHeader("Accept", "application/json").
|
||||
Delete(url)
|
||||
if err != nil {
|
||||
slog.Info(url, "error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateData(target config.Server, path string, v any) {
|
||||
client := resty.New()
|
||||
client.SetLoggerWarnLevel(true)
|
||||
token := GetToken(client, target)
|
||||
var result any
|
||||
PostJSON(client, target, token, config.Config.ApiVersion, path, v, &result)
|
||||
}
|
||||
|
||||
func WriteJson(data *ExportData, fileName string) error {
|
||||
jsonData, err := json.MarshalIndent(data, "", " ")
|
||||
if err != nil {
|
||||
|
||||
@@ -33,7 +33,7 @@ var syncCmd = &cobra.Command{
|
||||
slog.Info("iterate sourceData", "table", "Player", "count", len(sourceData.Player))
|
||||
sourceData.Player.CompareIds(&targetData.Player)
|
||||
slog.Info("iterate sourceData", "table", "MediaLofi", "count source", len(sourceData.MediaLoFi), "count target", len(targetData.MediaLoFi))
|
||||
missing, obsolete := sourceData.MediaLoFi.CompareIds(&targetData.MediaLoFi)
|
||||
missing, obsolete, changed := sourceData.MediaLoFi.CompareIds(&targetData.MediaLoFi)
|
||||
for _, lofi_id := range missing {
|
||||
if !config.DryRun {
|
||||
slog.Info("add MediaLofi", "id", lofi_id)
|
||||
@@ -42,6 +42,13 @@ var syncCmd = &cobra.Command{
|
||||
for _, lofi_id := range obsolete {
|
||||
if !config.DryRun {
|
||||
slog.Info("delete MediaLofi", "id", lofi_id)
|
||||
api.DeleteData(localServer, "media/lofis", lofi_id)
|
||||
}
|
||||
}
|
||||
for _, lofi_id := range changed {
|
||||
if !config.DryRun {
|
||||
slog.Info("update MediaLofi", "id", lofi_id)
|
||||
// api.UpdateData(localServer, "media/lofis", sourceData.MediaLoFi.GetMediaLofi(lofi_id))
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -88,11 +88,14 @@ func (handler *MediaLoFiHandler) Delete(c *gin.Context) {
|
||||
func (handler *MediaLoFiHandler) ListPage(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
user := session.Get(globals.UserKey)
|
||||
logger := slog.Get(c)
|
||||
lofis, err := handler.Service.List(c.Request.Context())
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#ListPage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#ListPage", "lofis count", len(lofis))
|
||||
c.HTML(http.StatusOK, "lofi", gin.H{
|
||||
"title": "Kontor MediaFiles",
|
||||
"version": "0.4.0",
|
||||
@@ -101,3 +104,102 @@ func (handler *MediaLoFiHandler) ListPage(c *gin.Context) {
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) EditPage(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
user := session.Get(globals.UserKey)
|
||||
id := c.Param("id")
|
||||
logger := slog.Get(c)
|
||||
lofi, err := handler.Service.Get(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#EditPage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#EditPage", "lofi", lofi)
|
||||
c.HTML(http.StatusOK, "lofi_detail", gin.H{
|
||||
"title": "Kontor MediaFLofi",
|
||||
"version": "0.4.0",
|
||||
"message": "MediaFiles...",
|
||||
"lofi": lofi,
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) ChangeReviewPage(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
logger := slog.Get(c)
|
||||
lofi, err := handler.Service.Get(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#ChangeReviewPage", "error", err.Error())
|
||||
c.Redirect(http.StatusNotFound, "/web/media-lofis")
|
||||
return
|
||||
}
|
||||
lofi.Review = !lofi.Review
|
||||
handler.Service.Update(c.Request.Context(), lofi.ConvertMediaLoFiToDTO())
|
||||
logger.Info("MediaLoFiHandler#ChangeReviewPage", "lofi", lofi)
|
||||
session := sessions.Default(c)
|
||||
user := session.Get(globals.UserKey)
|
||||
lofis, err := handler.Service.List(c.Request.Context())
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#ChangeReviewPage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#ListPage", "lofis count", len(lofis))
|
||||
c.HTML(http.StatusOK, "lofi", gin.H{
|
||||
"title": "Kontor MediaFiles",
|
||||
"version": "0.4.0",
|
||||
"message": "MediaFiles...",
|
||||
"lofis": lofis,
|
||||
"user": user,
|
||||
})
|
||||
c.Redirect(http.StatusCreated, "/web/media-lofis")
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) UpdatePage(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
user := session.Get(globals.UserKey)
|
||||
id := c.Param("id")
|
||||
logger := slog.Get(c)
|
||||
lofi, err := handler.Service.Get(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#UpdatePage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
var form models.MediaLoFiForm
|
||||
if err := c.ShouldBind(&form); err != nil {
|
||||
logger.Info("MediaLoFiHandler#UpdatePage", "error", err.Error())
|
||||
c.HTML(http.StatusBadRequest, "lofi_detail", gin.H{
|
||||
"Form": form,
|
||||
"Errors": err.Error(),
|
||||
"user": user,
|
||||
})
|
||||
return
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#UpdatePage", "lofi", lofi)
|
||||
//handler.Service.Update(c.Request.Context(), form.ConvertFormToMediaLofiDTO())
|
||||
c.Redirect(http.StatusFound, "web/media-lofis")
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) DeletePage(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
user := session.Get(globals.UserKey)
|
||||
id := c.Param("id")
|
||||
logger := slog.Get(c)
|
||||
lofi, err := handler.Service.Get(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#DeletePage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#DeletePage", "lofi", id)
|
||||
c.HTML(http.StatusOK, "lofi_delete", gin.H{
|
||||
"title": "Kontor MediaFiles",
|
||||
"version": "0.4.0",
|
||||
"message": "MediaFiles...",
|
||||
"lofi": lofi,
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"kontor-api-gin/internal/config"
|
||||
"kontor-api-gin/internal/helpers"
|
||||
"log/slog"
|
||||
"time"
|
||||
@@ -35,10 +36,36 @@ func (media_lofi *MediaLoFi) TableName() string {
|
||||
return "media_lofi"
|
||||
}
|
||||
|
||||
func (media_lofi *MediaLoFi) ConvertMediaLoFiToDTO() MediaLoFiDTO {
|
||||
dto := MediaLoFiDTO{
|
||||
ID: media_lofi.ID,
|
||||
CreatedAt: helpers.KontorTime{Time: media_lofi.CreatedAt},
|
||||
UpdatedAt: helpers.KontorTime{Time: media_lofi.UpdatedAt},
|
||||
DeletedAt: media_lofi.DeletedAt,
|
||||
Version: media_lofi.Version,
|
||||
WebLink: media_lofi.WebLink,
|
||||
Review: media_lofi.Review,
|
||||
ShouldDownload: media_lofi.ShouldDownload,
|
||||
Title: media_lofi.Title,
|
||||
FileName: media_lofi.FileName,
|
||||
}
|
||||
return dto
|
||||
}
|
||||
|
||||
type MediaLofiAdd struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type MediaLoFiForm struct {
|
||||
Title string `form:"title" binding:"required"`
|
||||
Url string `form:"url" binding:"required"`
|
||||
}
|
||||
|
||||
func (form *MediaLoFiForm) ConvertFormToMediaLofiDTO() MediaLoFiDTO {
|
||||
media_lofi := MediaLoFiDTO{}
|
||||
return media_lofi
|
||||
}
|
||||
|
||||
type MediaLoFiDTO struct {
|
||||
ID string `json:"id" binding:"required"`
|
||||
CreatedAt helpers.KontorTime `json:"created_date"`
|
||||
@@ -52,9 +79,62 @@ type MediaLoFiDTO struct {
|
||||
WebLink string `json:"url"`
|
||||
}
|
||||
|
||||
func (source MediaLoFiDTO) Equals(target MediaLoFiDTO) bool {
|
||||
if config.Verbose > 1 {
|
||||
slog.Info("MediaLoFiDTO#Equals", "id", source.ID)
|
||||
}
|
||||
equals := true
|
||||
if source.Title != target.Title {
|
||||
if config.Verbose > 1 {
|
||||
slog.Info("MediaLoFiDTO#Equals: Title changed")
|
||||
}
|
||||
equals = false
|
||||
}
|
||||
if source.WebLink != target.WebLink {
|
||||
if config.Verbose > 1 {
|
||||
slog.Info("MediaLoFiDTO#Equals: WebLink changed")
|
||||
}
|
||||
equals = false
|
||||
}
|
||||
if source.Version != target.Version {
|
||||
if config.Verbose > 1 {
|
||||
slog.Info("MediaLoFiDTO#Equals: Version changed")
|
||||
}
|
||||
equals = false
|
||||
}
|
||||
if source.FileName != target.FileName {
|
||||
if config.Verbose > 1 {
|
||||
slog.Info("MediaLoFiDTO#Equals: FileName changed")
|
||||
}
|
||||
equals = false
|
||||
}
|
||||
if source.Review != target.Review {
|
||||
if config.Verbose > 1 {
|
||||
slog.Info("MediaLoFiDTO#Equals: Review changed")
|
||||
}
|
||||
equals = false
|
||||
}
|
||||
if source.ShouldDownload != target.ShouldDownload {
|
||||
if config.Verbose > 1 {
|
||||
slog.Info("MediaLoFiDTO#Equals: ShouldDownload changed")
|
||||
}
|
||||
equals = false
|
||||
}
|
||||
return equals
|
||||
}
|
||||
|
||||
type MediaLoFiDTOList []MediaLoFiDTO
|
||||
|
||||
func (source *MediaLoFiDTOList) CompareIds(target *MediaLoFiDTOList) (missing, obsolete []string) {
|
||||
func (source MediaLoFiDTOList) GetMediaLofi(lofi_id string) *MediaLoFiDTO {
|
||||
for _, lofi := range source {
|
||||
if lofi.ID == lofi_id {
|
||||
return &lofi
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (source *MediaLoFiDTOList) CompareIds(target *MediaLoFiDTOList) (missing, obsolete, changed []string) {
|
||||
sources := make(map[string]MediaLoFiDTO, len(*source))
|
||||
targets := make(map[string]MediaLoFiDTO, len(*target))
|
||||
for _, source := range *source {
|
||||
@@ -66,7 +146,12 @@ func (source *MediaLoFiDTOList) CompareIds(target *MediaLoFiDTOList) (missing, o
|
||||
// iterate source IDs
|
||||
for id := range sources {
|
||||
if _, ok := targets[id]; ok {
|
||||
slog.Debug(fmt.Sprintf("ID %s found in both lists", id))
|
||||
if config.Verbose > 1 {
|
||||
slog.Info(fmt.Sprintf("ID %s found in both lists", id))
|
||||
}
|
||||
if !sources[id].Equals(targets[id]) {
|
||||
changed = append(changed, id)
|
||||
}
|
||||
} else {
|
||||
missing = append(missing, id)
|
||||
}
|
||||
@@ -78,26 +163,15 @@ func (source *MediaLoFiDTOList) CompareIds(target *MediaLoFiDTOList) (missing, o
|
||||
obsolete = append(obsolete, id)
|
||||
}
|
||||
}
|
||||
slog.Info("CompareIds", "missing", len(missing), "obsolete", len(obsolete))
|
||||
slog.Info("CompareIds", "missing", len(missing), "obsolete", len(obsolete), "changed", len(changed))
|
||||
slog.Debug("CompareIds", "missing", missing, "obsolete", obsolete)
|
||||
return missing, obsolete
|
||||
return missing, obsolete, changed
|
||||
}
|
||||
|
||||
func ConvertMediaLoFisToDtos(media_lofis []MediaLoFi) []MediaLoFiDTO {
|
||||
dtos := make([]MediaLoFiDTO, len(media_lofis))
|
||||
func ConvertMediaLoFisToDtos(media_lofis []MediaLoFi) MediaLoFiDTOList {
|
||||
dtos := make(MediaLoFiDTOList, len(media_lofis))
|
||||
for i, media_lofi := range media_lofis {
|
||||
dtos[i] = MediaLoFiDTO{
|
||||
ID: media_lofi.ID,
|
||||
CreatedAt: helpers.KontorTime{Time: media_lofi.CreatedAt},
|
||||
UpdatedAt: helpers.KontorTime{Time: media_lofi.UpdatedAt},
|
||||
DeletedAt: media_lofi.DeletedAt,
|
||||
Version: media_lofi.Version,
|
||||
WebLink: media_lofi.WebLink,
|
||||
Review: media_lofi.Review,
|
||||
ShouldDownload: media_lofi.ShouldDownload,
|
||||
Title: media_lofi.Title,
|
||||
FileName: media_lofi.FileName,
|
||||
}
|
||||
dtos[i] = media_lofi.ConvertMediaLoFiToDTO()
|
||||
}
|
||||
return dtos
|
||||
}
|
||||
|
||||
@@ -61,6 +61,10 @@ func New(admin *handlers.AdminHandler, bookshelf *handlers.BookshelfHandler, com
|
||||
m.GET("/media-files", media.MediaFileHandler.ListPage)
|
||||
m.GET("/files", media.MediaFileHandler.ListPage)
|
||||
m.GET("/media-lofis", media.MediaLoFiHandler.ListPage)
|
||||
m.GET("/media-lofis/:id/edit", media.MediaLoFiHandler.EditPage)
|
||||
m.POST("/media-lofis/:id/edit", media.MediaLoFiHandler.UpdatePage)
|
||||
m.GET("/media-lofis/:id/change-review", media.MediaLoFiHandler.ChangeReviewPage)
|
||||
m.GET("/media-lofis/:id/delete", media.MediaLoFiHandler.DeletePage)
|
||||
m.GET("/lofis", media.MediaLoFiHandler.ListPage)
|
||||
|
||||
api := router.Group("/api")
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
<tbody>
|
||||
{{ range .lofis }}
|
||||
<tr>
|
||||
<th scope="row"><a href="/media/lofis/{{.ID}}">{{.Title}}</a></th>
|
||||
<th scope="row"><a href="/web/media-lofis/{{.ID}}/edit">{{.Title}}</a></th>
|
||||
{{ if .WebLink }}
|
||||
<td><a href="{{.WebLink}}">{{.WebLink}}</a></td>
|
||||
{{ else }}
|
||||
@@ -43,13 +43,13 @@
|
||||
{{ end }}
|
||||
<td>
|
||||
{{ if .Review }}
|
||||
<img src="/public/icons/tick.png" alt="true" width="24" height="24">
|
||||
<a href="/web/media-lofis/{{.ID}}/change-review"><img src="/public/icons/tick.png" alt="true" width="24" height="24"></a>
|
||||
{{ else }}
|
||||
<img src="/public/icons/cross.png" alt="true" width="24" height="24">
|
||||
<a href="/web/media-lofis/{{.ID}}/change-review"><img src="/public/icons/cross.png" alt="true" width="24" height="24"></a>
|
||||
{{ end }}
|
||||
</td>
|
||||
<td><a href="/media/lofis/edit/{{.ID}}" class="btn btn-outline-primary btn-sm active" role="button" aria-pressed="true">Edit</a></td>
|
||||
<td><a href="/media/lofis/delete/{{.ID}}" class="btn btn-outline-danger btn-sm active" role="button" aria-pressed="true">Delete</a></td>
|
||||
<td><a href="/web/media-lofis/{{.ID}}/edit" class="btn btn-outline-primary btn-sm active" role="button" aria-pressed="true">Edit</a></td>
|
||||
<td><a href="/web/media-lofis/{{.ID}}/delete" class="btn btn-outline-danger btn-sm active" role="button" aria-pressed="true">Delete</a></td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</tbody>
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
{{ define "lofi_detail" }}
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
{{ template "head" . }}
|
||||
|
||||
<body>
|
||||
{{ template "navbar" . }}
|
||||
<div class="row">
|
||||
<h1>Media Lofi</h1>
|
||||
</div>
|
||||
<div class="row">
|
||||
<form class="form-inline" action="/web/media-lofis">
|
||||
<input type="text" placeholder="Search" name="query">
|
||||
<input>
|
||||
<input type="checkbox" name="review" {{ if .Review }}checked{{ end }}>Review
|
||||
</label>
|
||||
<label>
|
||||
<input type="checkbox" name="should_download" {{ if .ShouldDownload }}checked{{ end }}>Download
|
||||
</label>
|
||||
<button type="submit">Search</button>
|
||||
<div class="pill-nav"><a href="/web/media-lofis/add">Add Lofi</a></div>
|
||||
</form>
|
||||
</div>
|
||||
{{ if .Errors }}
|
||||
<div class="notification is-danger">{{ .Errors }}</div>
|
||||
{{ end }}
|
||||
<div class="row">
|
||||
<div class="maincolumn">
|
||||
<form action="/web/media-lofis/{{.lofi.ID}}/edit" method="POST">
|
||||
<label>URL</label>
|
||||
<input type="text" name="weblink" value="{{.lofi.WebLink}}">
|
||||
|
||||
<label>Titel</label>
|
||||
<input type="text" name="title" value="{{.lofi.Title}}">
|
||||
|
||||
<button type="submit">Update</button>
|
||||
<button type="button" onclick="/web/media-lofis">Cancel</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{{ template "footer" . }}
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
Reference in New Issue
Block a user