add with http code

This commit is contained in:
mr
2024-07-19 11:27:58 +02:00
parent 650168f39c
commit d28662d70b
16 changed files with 96 additions and 94 deletions

View File

@@ -131,7 +131,7 @@ func (m *MongoDB) createCollection(collection_name string, new_collection *mongo
}
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, error) {
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) {
filter := bson.M{"_id": GetObjIDFromString(id)}
targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
@@ -141,12 +141,12 @@ func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, error) {
result, err := targetDBCollection.DeleteOne(MngoCtx, filter, opts)
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return 0, err
return 0, 404, err
}
return result.DeletedCount, nil
return result.DeletedCount, 200, nil
}
func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_name string) (string, error) {
func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_name string) (string, int, error) {
filter := bson.M{"_id": GetObjIDFromString(id)}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
@@ -154,13 +154,13 @@ func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_na
result, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(set, true))
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return "", err
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return "", 404, err
}
return result.UpsertedID.(primitive.ObjectID).Hex(), nil
return result.UpsertedID.(primitive.ObjectID).Hex(), 200, nil
}
func (m *MongoDB) StoreOne(obj interface{}, collection_name string) (string, error) {
func (m *MongoDB) StoreOne(obj interface{}, collection_name string) (string, int, error) {
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
@@ -169,24 +169,24 @@ func (m *MongoDB) StoreOne(obj interface{}, collection_name string) (string, err
result, err := targetDBCollection.InsertOne(MngoCtx, obj)
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return "", err
return "", 409, err
}
return result.InsertedID.(primitive.ObjectID).Hex(), nil
return result.InsertedID.(primitive.ObjectID).Hex(), 200, nil
}
func (m *MongoDB) LoadOne(id string, collection_name string) (res *mongo.SingleResult, err error) {
func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) {
filter := bson.M{"_id": GetObjIDFromString(id)}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res = targetDBCollection.FindOne(MngoCtx, filter)
res := targetDBCollection.FindOne(MngoCtx, filter)
if res.Err() != nil {
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
err = res.Err()
return nil, err
err := res.Err()
return nil, 404, err
}
return res, nil
return res, 200, nil
}