Skip to content

Commit ef890cc

Browse files
committed
Created Replace method.
Fixed Create method.
1 parent 9e3791b commit ef890cc

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

go-memory/memory_repository.go

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func (c *MemoryRepository[T, Q]) Create(ctx context.Context, value T) (string, e
7171
return id, nil
7272
}
7373

74+
func (c *MemoryRepository[T, Q]) Replace(ctx context.Context, id string, value T) error {
75+
c.Items[id] = value
76+
return nil
77+
}
78+
7479
func (c *MemoryRepository[T, Q]) Update(ctx context.Context, id string, value T) (bool, error) {
7580
_, ok := c.Items[id]
7681
if !ok {

go-mongorepo/mongo_repository.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,33 @@ func (c *MongoRepository[T]) Create(
9393
ctx context.Context,
9494
data T,
9595
) (string, error) {
96-
error := data.Valid()
97-
if error != nil {
98-
return "", error
96+
err := data.Valid()
97+
if err != nil {
98+
return "", err
99+
}
100+
101+
result, err := c.Collection.InsertOne(ctx, data)
102+
if err != nil {
103+
return "", err
104+
}
105+
106+
return result.InsertedID.(primitive.ObjectID).Hex(), err
107+
}
108+
109+
func (c *MongoRepository[T]) Replace(
110+
ctx context.Context,
111+
id string,
112+
data T,
113+
) error {
114+
err := data.Valid()
115+
if err != nil {
116+
return err
99117
}
100118

101-
result, error := c.Collection.InsertOne(ctx, data)
102-
return result.InsertedID.(primitive.ObjectID).Hex(), error
119+
_, err = c.Collection.ReplaceOne(ctx, bson.M{
120+
"_id": id,
121+
}, data)
122+
return err
103123
}
104124

105125
func (c *MongoRepository[T]) Update(

go/repository.go

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type Repository[T Valid, Q Query] interface {
66
GetById[T]
77
GetList[T, Q]
88
Create[T]
9+
Replace[T]
910
Update[T]
1011
Delete
1112
}
@@ -34,6 +35,10 @@ type Create[T Valid] interface {
3435
Create(ctx context.Context, data T) (string, error)
3536
}
3637

38+
type Replace[T Valid] interface {
39+
Replace(ctx context.Context, id string, data T) error
40+
}
41+
3742
type Update[T Valid] interface {
3843
Update(ctx context.Context, id string, data T) (bool, error)
3944
}

0 commit comments

Comments
 (0)