Skip to content

Commit 0f622e5

Browse files
work on getCollection and fix update collection
1 parent 72acaa1 commit 0f622e5

3 files changed

Lines changed: 59 additions & 23 deletions

File tree

collections/GetCollection.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,61 @@
11
package collections
22

33
import (
4+
"encoding/json"
45
"github.com/gin-gonic/gin"
5-
"io/ioutil"
66
"net/http"
7+
"os"
78
"path/filepath"
9+
"strings"
810
)
911

1012
func GetCollection(c *gin.Context) {
1113
collection := c.Param("collection")
12-
dir := filepath.Join("database", collection)
14+
dirPath := filepath.Join("database", collection)
15+
configPath := filepath.Join(dirPath, "config.json")
1316

14-
files, err := ioutil.ReadDir(dir)
17+
// read config.json
18+
data, err := os.ReadFile(configPath)
1519
if err != nil {
16-
c.JSON(http.StatusNotFound, gin.H{"error": "Collection not found"})
20+
c.JSON(http.StatusNotFound, gin.H{"error": "Collection config not found"})
1721
return
1822
}
1923

20-
total := len(files)
21-
if total < 0 {
22-
total = 0 // prevent negative count if directory is empty
24+
// parse JSON
25+
var raw map[string]interface{}
26+
if err := json.Unmarshal(data, &raw); err != nil {
27+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid config.json"})
28+
return
29+
}
30+
31+
// get "config" key
32+
config, ok := raw["config"]
33+
if !ok {
34+
c.JSON(http.StatusBadRequest, gin.H{"error": `"config" key not found in config.json`})
35+
return
36+
}
37+
38+
// count JSON files (excluding config.json)
39+
files, err := os.ReadDir(dirPath)
40+
if err != nil {
41+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read collection directory"})
42+
return
43+
}
44+
45+
count := 0
46+
for _, file := range files {
47+
if file.IsDir() {
48+
continue
49+
}
50+
name := file.Name()
51+
if strings.HasSuffix(name, ".json") && name != "config.json" {
52+
count++
53+
}
2354
}
2455

2556
c.JSON(http.StatusOK, gin.H{
2657
"collection": collection,
27-
"count": total,
58+
"config": config,
59+
"count": count,
2860
})
2961
}

collections/UpdateCollection.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@ import (
1111
)
1212

1313
func UpdateCollection(c *gin.Context) {
14-
// get id from /collections/:id
1514
id := c.Param("collection")
1615
if id == "" {
1716
c.JSON(http.StatusBadRequest, gin.H{"error": "missing id"})
1817
return
1918
}
2019

20+
dir := "database/" + id
21+
22+
// check if dir exists
23+
info, err := os.Stat(dir)
24+
if os.IsNotExist(err) {
25+
fmt.Printf("directory %s does not exist\n", dir)
26+
os.Exit(1) // or return error if inside a function
27+
}
28+
29+
// check if it's really a directory
30+
if !info.IsDir() {
31+
fmt.Printf("%s exists but is not a directory\n", dir)
32+
os.Exit(1)
33+
}
34+
2135
// parse JSON body into a map
2236
var body map[string]interface{}
2337
if err := c.ShouldBindJSON(&body); err != nil {
@@ -26,7 +40,7 @@ func UpdateCollection(c *gin.Context) {
2640
}
2741

2842
// ensure database/{id} directory exists
29-
dir := filepath.Join("database", id)
43+
dir = filepath.Join("database", id)
3044
if err := os.MkdirAll(dir, 0755); err != nil {
3145
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("could not create directory: %v", err)})
3246
return
@@ -62,8 +76,8 @@ func UpdateCollection(c *gin.Context) {
6276

6377
// success
6478
c.JSON(http.StatusOK, gin.H{
65-
"message": "collection updated",
66-
"path": filePath,
67-
"german_date": germanDate,
79+
"message": "collection updated",
80+
"path": filePath,
81+
"date": germanDate,
6882
})
6983
}

main.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ import (
77
"go-database-json/records"
88
)
99

10-
// Extracted users map for authentication and response
11-
var users = map[string]string{
12-
"foo": "bar",
13-
}
14-
15-
// Extracted users map for authentication and response
16-
var tokens = map[string]string{
17-
"foo": "JDJhJDEwJGt1cUs5eVprMUszdmlZVTlGWXZxSWV3dlUuM0RUcTM1dHlMWFRCNWtIQTZXeG9nRC5IUVdh",
18-
}
19-
2010
func main() {
2111
r := gin.Default()
2212
// r.Use(cors.Default())

0 commit comments

Comments
 (0)