Skip to content

Commit 01cfcbb

Browse files
committed
synchronization commit
1 parent 05cf899 commit 01cfcbb

File tree

4 files changed

+94
-31
lines changed

4 files changed

+94
-31
lines changed

server/src/routes/collection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import express from "express";
2-
import { authorisedOnly } from "../middleware/auth";
2+
import { authorisedOnly, unauthorisedOnly } from "../middleware/auth";
33
import { prisma } from "../../db";
44
import axios from "axios";
55
import { Service } from "../types/microservices";

services/db_access/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55

66
"github.com/gin-gonic/gin"
77
"github.com/marsian83/one-panel/services/db_access/configs"
8+
"github.com/marsian83/one-panel/services/db_access/src/handlers"
89
"github.com/marsian83/one-panel/services/db_access/src/mongodb"
9-
"github.com/marsian83/one-panel/services/db_access/src/routes"
1010
"go.mongodb.org/mongo-driver/mongo"
1111
)
1212

@@ -20,10 +20,10 @@ func main() {
2020
router := gin.Default()
2121
dbClient = mongodb.GetClient()
2222

23-
router.GET("/ping", routes.Ping)
24-
25-
router.POST("/allocate", routes.AllotDatabase)
26-
router.POST("/entry", routes.NewEntry)
23+
router.GET("/ping", handlers.Ping)
24+
router.PUT("/entries", handlers.GetEntries)
25+
router.POST("/allocate", handlers.AllotDatabase)
26+
router.POST("/entry", handlers.NewEntry)
2727

2828
fmt.Printf("ok see here %s\n", configs.Env.Mongodb_Hostname)
2929
router.Run(fmt.Sprintf(":%s", port))

services/db_access/src/routes/index.go renamed to services/db_access/src/handlers/index.go

+82-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package routes
1+
package handlers
22

33
import (
44
"context"
@@ -63,12 +63,14 @@ func NewEntry(c *gin.Context) {
6363
}
6464

6565
type Entry struct {
66-
Name string `bson:"name"`
67-
Data []struct {
68-
ID int `json:"id"`
69-
Item interface{} `json:"item"`
70-
} `bson:"data"`
71-
NextID int `bson:"next_id"`
66+
ID int `json:"id"`
67+
Data interface{} `json:"data"`
68+
}
69+
70+
type Collection struct {
71+
Name string `bson:"name"`
72+
Data []Entry `bson:"data"`
73+
NextID int `bson:"next_id"`
7274
}
7375

7476
mongodb_hostname := configs.Env.Mongodb_Hostname
@@ -91,7 +93,7 @@ func NewEntry(c *gin.Context) {
9193
var err error
9294

9395
if true {
94-
// Parse Data -> Unmarshal JSON
96+
// If Object field is set, parse the JSON data
9597
err = json.Unmarshal([]byte(userRequest.Data), &newData)
9698
if err != nil {
9799
msg := fmt.Sprintf("Error parsing JSON: %s", err)
@@ -100,33 +102,48 @@ func NewEntry(c *gin.Context) {
100102
}
101103
}
102104

103-
existingEntry := Entry{}
105+
existingCollection := Collection{}
104106

105-
err = collection.FindOne(ctx, bson.M{"name": userRequest.Collection}).Decode(&existingEntry)
107+
err = collection.FindOne(ctx, bson.M{"name": userRequest.Collection}).Decode(&existingCollection)
106108

107109
var nextID int
108-
109110
if err == nil {
110111
// Entry exists, update its value
111-
nextID = existingEntry.NextID
112+
nextID = existingCollection.NextID
112113
} else {
113114
nextID = 0
114115
}
115116

116-
newEntry := struct {
117-
ID int `json:"id"`
118-
Item interface{} `json:"item"`
119-
}{
120-
ID: nextID,
121-
Item: newData,
122-
}
117+
if err == nil {
118+
// Entry exists, update its value
119+
newEntry := Entry{
120+
ID: nextID,
121+
Data: newData,
122+
}
123123

124-
update := bson.M{
125-
"$push": bson.M{"data": newEntry},
126-
"$set": bson.M{"next_id": nextID + 1},
127-
}
124+
update := bson.M{"$push": bson.M{"data": newEntry}, "$set": bson.M{"next_id": nextID + 1}}
125+
_, err := collection.UpdateOne(ctx, bson.M{"name": userRequest.Collection}, update)
128126

129-
_, err = collection.UpdateOne(ctx, bson.M{"name": userRequest.Collection}, update)
127+
if err != nil {
128+
c.JSON(http.StatusOK, gin.H{
129+
"message": "Error updating value",
130+
"code": 1})
131+
}
132+
} else {
133+
// Entry doesn't exist, create a new one
134+
newEntry := Collection{
135+
Name: userRequest.Collection,
136+
Data: []Entry{
137+
{
138+
ID: 0,
139+
Data: newData,
140+
},
141+
},
142+
NextID: 1,
143+
}
144+
145+
_, err = collection.InsertOne(ctx, newEntry)
146+
}
130147

131148
if err != nil {
132149
c.JSON(http.StatusOK, gin.H{
@@ -139,3 +156,44 @@ func NewEntry(c *gin.Context) {
139156
"message": fmt.Sprintf("Successfully Created/Updated Entry in %s", userRequest.Collection),
140157
"code": 0})
141158
}
159+
160+
func GetEntries(c *gin.Context) {
161+
type UserRequest struct {
162+
DBName string `json:"db_name"`
163+
Artifact string `json:"artifact"`
164+
Collection string `json:"collection"`
165+
}
166+
167+
type Entry struct {
168+
Name string `bson:"name"`
169+
Data []struct {
170+
ID int `json:"id"`
171+
Data interface{} `json:"data"`
172+
} `bson:"data"`
173+
}
174+
175+
var userRequest UserRequest
176+
if err := c.BindJSON(&userRequest); err != nil {
177+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
178+
return
179+
}
180+
181+
client := mongodb.GetClient()
182+
db := client.Database(userRequest.DBName)
183+
collection := db.Collection(userRequest.Artifact)
184+
185+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
186+
defer cancel()
187+
188+
entry := Entry{}
189+
fmt.Println(userRequest)
190+
if err := collection.FindOne(ctx, bson.M{"name": userRequest.Collection}).Decode(&entry); err != nil {
191+
fmt.Println(err)
192+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
193+
return
194+
}
195+
196+
c.JSON(http.StatusOK, gin.H{
197+
"data": entry.Data,
198+
"code": 0})
199+
}

services/db_access/src/mongodb/mongodb.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package mongodb
22

33
import (
44
"context"
5+
"reflect"
56
"sync"
67

78
"github.com/marsian83/one-panel/services/db_access/configs"
9+
"go.mongodb.org/mongo-driver/bson"
10+
"go.mongodb.org/mongo-driver/bson/bsontype"
811
"go.mongodb.org/mongo-driver/mongo"
912
"go.mongodb.org/mongo-driver/mongo/options"
1013
)
@@ -18,7 +21,9 @@ func GetClient() *mongo.Client {
1821
once.Do(func() {
1922
db_uri := configs.Env.Mongodb_URI
2023

21-
clientOptions := options.Client().ApplyURI(db_uri)
24+
tM := reflect.TypeOf(bson.M{})
25+
reg := bson.NewRegistryBuilder().RegisterTypeMapEntry(bsontype.EmbeddedDocument, tM).Build()
26+
clientOptions := options.Client().ApplyURI(db_uri).SetRegistry(reg)
2227

2328
client, err := mongo.Connect(context.TODO(), clientOptions)
2429

0 commit comments

Comments
 (0)