feat: implement publish endpoint and related database functionality
This commit is contained in:
parent
b09275fa67
commit
582d93a103
3 changed files with 417 additions and 30 deletions
|
|
@ -21,6 +21,8 @@ type Database interface {
|
|||
List(ctx context.Context, filter map[string]interface{}, cursor string, limit int) ([]*model.Entry, string, error)
|
||||
// GetByID retrieves a single ServerDetail by it's ID
|
||||
GetByID(ctx context.Context, id string) (*model.ServerDetail, error)
|
||||
// Publish adds a new ServerDetail to the database
|
||||
Publish(ctx context.Context, serverDetail *model.ServerDetail) error
|
||||
// Close closes the database connection
|
||||
Close() error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ func (db *MemoryDB) List(ctx context.Context, filter map[string]interface{}, cur
|
|||
include = false
|
||||
}
|
||||
case "version":
|
||||
if entry.Version != value.(string) {
|
||||
if entry.VersionDetail.Version != value.(string) {
|
||||
include = false
|
||||
}
|
||||
// Add more filter options as needed
|
||||
|
|
@ -125,17 +125,54 @@ func (db *MemoryDB) GetByID(ctx context.Context, id string) (*model.ServerDetail
|
|||
|
||||
if entry, exists := db.entries[id]; exists {
|
||||
return &model.ServerDetail{
|
||||
ID: entry.ID,
|
||||
Name: entry.Name,
|
||||
Description: entry.Description,
|
||||
Version: entry.Version,
|
||||
Repository: entry.Repository,
|
||||
ID: entry.ID,
|
||||
Name: entry.Name,
|
||||
Description: entry.Description,
|
||||
VersionDetail: entry.VersionDetail,
|
||||
Repository: entry.Repository,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
// Publish adds a new ServerDetail to the database
|
||||
func (db *MemoryDB) Publish(ctx context.Context, serverDetail *model.ServerDetail) error {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
db.mu.Lock()
|
||||
defer db.mu.Unlock()
|
||||
|
||||
// check for name
|
||||
if serverDetail.Name == "" {
|
||||
return ErrInvalidInput
|
||||
}
|
||||
|
||||
// check that the name and the version are unique
|
||||
|
||||
for _, entry := range db.entries {
|
||||
if entry.Name == serverDetail.Name && entry.VersionDetail.Version == serverDetail.VersionDetail.Version {
|
||||
return ErrAlreadyExists
|
||||
}
|
||||
}
|
||||
|
||||
if serverDetail.Repository.URL == "" {
|
||||
return ErrInvalidInput
|
||||
}
|
||||
|
||||
db.entries[serverDetail.ID] = &model.Entry{
|
||||
ID: serverDetail.ID,
|
||||
Name: serverDetail.Name,
|
||||
Description: serverDetail.Description,
|
||||
VersionDetail: serverDetail.VersionDetail,
|
||||
Repository: serverDetail.Repository,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the database connection
|
||||
// For an in-memory database, this is a no-op
|
||||
func (db *MemoryDB) Close() error {
|
||||
|
|
|
|||
|
|
@ -40,13 +40,17 @@ func NewMongoDB(ctx context.Context, connectionURI, databaseName, collectionName
|
|||
// Create indexes for better query performance
|
||||
models := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "name", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
Keys: bson.D{{Key: "name", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "id", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
// add an index for the combination of name and version
|
||||
{
|
||||
Keys: bson.D{{Key: "name", Value: 1}, {Key: "versiondetail.version", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
|
||||
_, err = collection.Indexes().CreateMany(ctx, models)
|
||||
|
|
@ -73,7 +77,9 @@ func (db *MongoDB) List(ctx context.Context, filter map[string]interface{}, curs
|
|||
}
|
||||
|
||||
// Convert Go map to MongoDB filter
|
||||
mongoFilter := bson.M{}
|
||||
mongoFilter := bson.M{
|
||||
"versiondetail.islatest": true,
|
||||
}
|
||||
// Map common filter keys to MongoDB document paths
|
||||
for k, v := range filter {
|
||||
// Handle nested fields with dot notation
|
||||
|
|
@ -164,6 +170,53 @@ func (db *MongoDB) GetByID(ctx context.Context, id string) (*model.ServerDetail,
|
|||
return &entry, nil
|
||||
}
|
||||
|
||||
// Publish adds a new ServerDetail to the database
|
||||
func (db *MongoDB) Publish(ctx context.Context, serverDetail *model.ServerDetail) error {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
// find a server detail with the same name and check that the current version is greater than the existing one
|
||||
filter := bson.M{
|
||||
"name": serverDetail.Name,
|
||||
"versiondetail.islatest": true,
|
||||
}
|
||||
|
||||
var existingEntry model.ServerDetail
|
||||
err := db.collection.FindOne(ctx, filter).Decode(&existingEntry)
|
||||
if err != nil && err != mongo.ErrNoDocuments {
|
||||
return fmt.Errorf("error checking existing entry: %w", err)
|
||||
}
|
||||
|
||||
// check that the current version is greater than the existing one
|
||||
if serverDetail.VersionDetail.Version <= existingEntry.VersionDetail.Version {
|
||||
return fmt.Errorf("version must be greater than existing version")
|
||||
}
|
||||
|
||||
// update the existing entry to not be the latest version
|
||||
if existingEntry.ID != "" {
|
||||
_, err = db.collection.UpdateOne(ctx, bson.M{"id": existingEntry.ID}, bson.M{"$set": bson.M{"versiondetail.islatest": false}})
|
||||
if err != nil {
|
||||
return fmt.Errorf("error updating existing entry: %w", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
serverDetail.ID = uuid.New().String()
|
||||
serverDetail.VersionDetail.IsLatest = true
|
||||
serverDetail.VersionDetail.ReleaseDate = time.Now().Format(time.RFC3339)
|
||||
|
||||
// Insert the entry into the database
|
||||
_, err = db.collection.InsertOne(ctx, serverDetail)
|
||||
if err != nil {
|
||||
if mongo.IsDuplicateKeyError(err) {
|
||||
return ErrAlreadyExists
|
||||
}
|
||||
return fmt.Errorf("error inserting entry: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the database connection
|
||||
func (db *MongoDB) Close() error {
|
||||
return db.client.Disconnect(context.Background())
|
||||
|
|
|
|||
Loading…
Reference in a new issue