]> git.feebdaed.xyz Git - gomehub.git/commitdiff
Add author, timestamp, sort logic
authorseantywork <seantywork@gmail.com>
Wed, 24 Jul 2024 00:32:50 +0000 (09:32 +0900)
committerseantywork <seantywork@gmail.com>
Wed, 24 Jul 2024 00:32:50 +0000 (09:32 +0900)
ctl/base.go
pkg/dbquery/dbquery.go
pkg/edition/edition.go
pkg/edition/edition_etc.go
public/js/common.js [new file with mode: 0644]
public/js/index/index.js
public/js/mypage/article.js
public/js/mypage/index.js
view/index/index.html
view/mypage/article.html
view/mypage/index.html

index 9e1a48944a0659bc47eaed148bf7f534facb549e..64f779655d04f8b1981ab50b865e7c824635e762 100644 (file)
@@ -4,6 +4,7 @@ import (
        "encoding/json"
        "fmt"
        "net/http"
+       "time"
 
        "github.com/gin-gonic/gin"
        pkgauth "github.com/seantywork/sorrylinus-again/pkg/auth"
@@ -14,9 +15,11 @@ import (
 
 type EntryStruct struct {
        Entry []struct {
-               Title string `json:"title"`
-               Id    string `json:"id"`
-               Type  string `json:"type"`
+               Title     string    `json:"title"`
+               Id        string    `json:"id"`
+               Type      string    `json:"type"`
+               Timestamp time.Time `json:"timestamp"`
+               Author    string    `json:"author"`
        } `json:"entry"`
 }
 
@@ -254,17 +257,23 @@ func GetMediaEntry(c *gin.Context) {
 
        for k, v := range em {
 
+               t, _ := time.Parse("2006-01-02-15-04-05", v.CreatedAt)
+
                if v.Type == "article" {
 
                        entry.Entry = append(entry.Entry, struct {
-                               Title string `json:"title"`
-                               Id    string `json:"id"`
-                               Type  string `json:"type"`
+                               Title     string    `json:"title"`
+                               Id        string    `json:"id"`
+                               Type      string    `json:"type"`
+                               Timestamp time.Time `json:"timestamp"`
+                               Author    string    `json:"author"`
                        }{
 
-                               Title: v.PlainName,
-                               Id:    k,
-                               Type:  "article",
+                               Title:     v.PlainName,
+                               Id:        k,
+                               Type:      "article",
+                               Timestamp: t,
+                               Author:    v.Author,
                        })
 
                } else {
index 08ed3d7cdee20d9052695975707689f268c2d143..86c29dde535625fc07bca33ac950b2d0d38eab94 100644 (file)
@@ -28,9 +28,11 @@ type SessionStruct struct {
 
 type MediaStruct struct {
        ISPublic  bool     `json:"is_public"`
+       CreatedAt string   `json:"created_at"`
        Type      string   `json:"type"`
        Extension string   `json:"extension"`
        PlainName string   `json:"plain_name"`
+       Author    string   `json:"author"`
        AllowedId []string `json:"allowed_id"`
 }
 
@@ -493,14 +495,20 @@ func MakeSessionForUser(session_key string, id string, duration_seconds int) err
        return nil
 }
 
-func UploadArticle(content string, plain_name string, new_name string) error {
+func UploadArticle(author string, content string, plain_name string, new_name string) error {
 
        ms := MediaStruct{}
 
+       c_time := time.Now()
+
+       c_time_fmt := c_time.Format("2006-01-02-15-04-05")
+
        ms.ISPublic = true
        ms.Type = "article"
        ms.PlainName = plain_name
        ms.Extension = "json"
+       ms.CreatedAt = c_time_fmt
+       ms.Author = author
 
        this_file_path := mediaPath + new_name + ".json"
 
@@ -645,7 +653,7 @@ func GetArticle(media_key string) (string, error) {
 
 }
 
-func UploadImage(c *gin.Context, file *multipart.FileHeader, filename string, new_filename string, extension string) error {
+func UploadImage(c *gin.Context, author string, file *multipart.FileHeader, filename string, new_filename string, extension string) error {
 
        ms := MediaStruct{}
 
@@ -653,10 +661,16 @@ func UploadImage(c *gin.Context, file *multipart.FileHeader, filename string, ne
 
        this_image_path := imagePath + new_filename + "." + extension
 
+       c_time := time.Now()
+
+       c_time_fmt := c_time.Format("2006-01-02-15-04-05")
+
        ms.ISPublic = true
        ms.Type = "image"
        ms.PlainName = filename
        ms.Extension = extension
+       ms.CreatedAt = c_time_fmt
+       ms.Author = author
 
        jb, err := json.Marshal(ms)
 
@@ -755,7 +769,7 @@ func DownloadImage(c *gin.Context, watchId string) error {
        return nil
 }
 
-func UploadVideo(c *gin.Context, file *multipart.FileHeader, filename string, new_filename string, extension string) error {
+func UploadVideo(c *gin.Context, author string, file *multipart.FileHeader, filename string, new_filename string, extension string) error {
 
        ms := MediaStruct{}
 
@@ -763,10 +777,16 @@ func UploadVideo(c *gin.Context, file *multipart.FileHeader, filename string, ne
 
        this_video_path := videoPath + new_filename + "." + extension
 
+       c_time := time.Now()
+
+       c_time_fmt := c_time.Format("2006-01-02-15-04-05")
+
        ms.ISPublic = true
        ms.Type = "video"
        ms.PlainName = filename
        ms.Extension = extension
+       ms.CreatedAt = c_time_fmt
+       ms.Author = author
 
        jb, err := json.Marshal(ms)
 
index 54b59977e7288fe11b7bf6ecdc5484b6e9386b2d..d2d9d26b367822717a90940e8bacc1897ec84c53 100644 (file)
@@ -23,7 +23,7 @@ type ArticleInfo struct {
 
 func PostArticleUpload(c *gin.Context) {
 
-       _, my_type, _ := pkgauth.WhoAmI(c)
+       _, my_type, my_id := pkgauth.WhoAmI(c)
 
        if my_type != "admin" {
 
@@ -64,7 +64,7 @@ func PostArticleUpload(c *gin.Context) {
 
        plain_name := pkgauth.SanitizePlainNameValue(a_info.Title)
 
-       err = dbquery.UploadArticle(a_info.Content, plain_name, new_file_name)
+       err = dbquery.UploadArticle(my_id, a_info.Content, plain_name, new_file_name)
 
        if err != nil {
 
@@ -163,7 +163,7 @@ func GetArticleContentById(c *gin.Context) {
 
 func PostMediaUpload(c *gin.Context) {
 
-       _, my_type, _ := pkgauth.WhoAmI(c)
+       _, my_type, my_id := pkgauth.WhoAmI(c)
 
        if my_type != "admin" {
 
@@ -221,11 +221,11 @@ func PostMediaUpload(c *gin.Context) {
 
        if mediaType == "image" {
 
-               err = dbquery.UploadImage(c, file, v_fname, file_name, mediaExt)
+               err = dbquery.UploadImage(c, my_id, file, v_fname, file_name, mediaExt)
 
        } else if mediaType == "video" {
 
-               err = dbquery.UploadVideo(c, file, v_fname, file_name, mediaExt)
+               err = dbquery.UploadVideo(c, my_id, file, v_fname, file_name, mediaExt)
 
        }
 
index 0bcad25470a636c666aaf29f6a3a1fed974ca505..f29de94b1f6228476cae887f342a2c509c767371 100644 (file)
@@ -15,7 +15,7 @@ import (
 
 func PostVideoUpload(c *gin.Context) {
 
-       _, my_type, _ := pkgauth.WhoAmI(c)
+       _, my_type, my_id := pkgauth.WhoAmI(c)
 
        if my_type != "admin" {
 
@@ -62,7 +62,7 @@ func PostVideoUpload(c *gin.Context) {
 
        file_name, _ := pkgutils.GetRandomHex(32)
 
-       err := pkgdbq.UploadVideo(c, file, v_fname, file_name, extension)
+       err := pkgdbq.UploadVideo(c, my_id, file, v_fname, file_name, extension)
 
        if err != nil {
 
@@ -163,7 +163,7 @@ func GetVideoContentByID(c *gin.Context) {
 
 func PostImageUpload(c *gin.Context) {
 
-       _, my_type, _ := pkgauth.WhoAmI(c)
+       _, my_type, my_id := pkgauth.WhoAmI(c)
 
        if my_type != "admin" {
 
@@ -210,7 +210,7 @@ func PostImageUpload(c *gin.Context) {
 
        file_name, _ := pkgutils.GetRandomHex(32)
 
-       err := dbquery.UploadImage(c, file, v_fname, file_name, extension)
+       err := dbquery.UploadImage(c, my_id, file, v_fname, file_name, extension)
 
        if err != nil {
 
diff --git a/public/js/common.js b/public/js/common.js
new file mode 100644 (file)
index 0000000..a45971f
--- /dev/null
@@ -0,0 +1,98 @@
+
+function delayMs (ms) {
+    
+    return new Promise(function(res) {
+        setTimeout(res, ms)
+    })
+}
+
+
+function getNewDateSortedList(flag, fieldName, orgList){
+
+
+    let newList = []
+
+    if(flag != "asc" && flag != "desc"){
+        return orgList
+    }
+
+
+
+    for(let i = 0; i < orgList.length; i ++){
+
+
+        let d1 = Date.parse(orgList[i][fieldName])
+
+        if(newList.length == 0){
+
+            newList.push(orgList[i])
+        
+            continue
+        } 
+
+        for(let j = 0; j < newList.length; j ++){
+
+
+            let d2 = Date.parse(newList[j][fieldName])
+            
+
+            if(flag == "asc"){
+
+                if(d1 < d2){
+
+                    newList.splice(j, 0, orgList[i])
+
+                    break
+                }
+
+                if (d1 >= d2 && (j != newList.length - 1)){
+
+                    continue
+
+                } else if (d1 >= d2 && (j == newList.length - 1)){
+
+                    newList.push(orgList[i])
+
+                    break
+                }
+
+
+            } else if(flag == "desc"){
+
+
+                if(d1 > d2){
+
+                    newList.splice(j, 0, orgList[i])
+
+                    break
+                }
+
+                if (d1 <= d2 && (j != newList.length - 1)){
+
+                    continue
+
+                } else if (d1 <= d2 && (j == newList.length - 1)){
+
+                    newList.push(orgList[i])
+
+                    break
+                }
+
+
+            }
+
+
+
+
+        }
+
+    }
+
+
+    return newList
+
+
+  
+
+
+}
\ No newline at end of file
index 69e91e685fc87571817056b8bca54eeb68942a30..9d2e67108fc036d28a64b62499422eeb0b4b6977 100644 (file)
@@ -49,14 +49,17 @@ async function getContentList(){
 
   } else {
 
-    for(let i = 0; i < contentEntry.entry.length; i ++){
-
-        contentReader.innerHTML += `
-        <a href="/content/${contentEntry.entry[i].type}/${contentEntry.entry[i].id}">
-            ${contentEntry.entry[i].title}
-        </a>
-        <br>
-        `
+    let sortedEntry = getNewDateSortedList("desc", "timestamp", contentEntry.entry)
+
+    for(let i = 0; i < sortedEntry.length; i ++){
+
+      contentReader.innerHTML += `
+      <a href="/content/${sortedEntry[i].type}/${sortedEntry[i].id}">
+        ${sortedEntry[i].title} 
+      </a> [${sortedEntry[i].author}:${sortedEntry[i].timestamp}] 
+      <input type="button" onclick="deleteArticle('${sortedEntry[i].id}')" value="delete">
+      <br>
+      `
      
     }
   }
index d80e229bd79fbea5c0a9a13eea72f4613eafe2e6..ba0f21110b9b12d90a2712ad471a6048bbb3bd33 100644 (file)
@@ -294,17 +294,15 @@ async function getArticleList(){
   
     contentReader.innerHTML = ""
 
-    for(let i = 0; i < contentEntry.entry.length; i ++){
+    let sortedEntry = getNewDateSortedList("desc", "timestamp", contentEntry.entry)
 
-        if(contentEntry.entry[i].type != "article"){
-            continue
-        }
+    for(let i = 0; i < sortedEntry.length; i ++){
 
         contentReader.innerHTML += `
-        <a href="/content/${contentEntry.entry[i].type}/${contentEntry.entry[i].id}">
-            ${contentEntry.entry[i].title}
-        </a>
-        <input type="button" onclick="deleteArticle('${contentEntry.entry[i].id}')" value="delete">
+        <a href="/content/${sortedEntry[i].type}/${sortedEntry[i].id}">
+            ${sortedEntry[i].title} 
+        </a> [${sortedEntry[i].author}:${sortedEntry[i].timestamp}]
+        <input type="button" onclick="deleteArticle('${sortedEntry[i].id}')" value="delete">
         <br>
         `
         articleCount += 1
index 73873ee4f9fb871f2b88967cb88f54babe9b9338..7e085fd68bae4c1ea4709f4cdaca416149d84088 100644 (file)
@@ -339,13 +339,6 @@ async function initCCTV(){
 }
 
 
-function delayMs (ms) {
-    
-    return new Promise(function(res) {
-        setTimeout(res, ms)
-    })
-}
-
 
 async function testCCTV(){
 
index 604a61666ce22c290b26683929fae3afc0f5bf51..a72ce171666f52ce8307ce801f59555d7901c77f 100644 (file)
@@ -46,6 +46,7 @@
 
     <a href="/mypage"> My Page </a>
 
+    <script src="/public/js/common.js"></script>
     <script src="/public/js/index/index.js"></script>
   </body>
 </html>
index e576d154e97bd7243001f7d51a1fd47f0bf37c2b..b7d19eae2b936a3dffa217372d2866f56666b4c1 100644 (file)
@@ -34,7 +34,7 @@
 
         <div id="article-reader"></div>
 
-        
+        <script src="/public/js/common.js"></script>
         <script src="/public/js/mypage/article.js"></script>
 
     </body>
index 90cc4d47982b27341c446c83d97bc5494aa26a51..caa90094d2ff8f7990f8d7597f7b7cd15383257e 100644 (file)
@@ -76,6 +76,7 @@
 
   </body>
 
+  <script src="/public/js/common.js"></script>
   <script src="/public/js/mypage/index.js"></script>