Skip to content

Commit 401fd35

Browse files
committed
Added a new API for updating resources, tools and forwards
This closes issue wanaku-ai#162
1 parent 81cd5c6 commit 401fd35

File tree

12 files changed

+338
-2
lines changed

12 files changed

+338
-2
lines changed

core/core-persistence/core-persistence-api/src/main/java/ai/wanaku/core/persistence/api/WanakuRepository.java

+9
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@ public interface WanakuRepository<A, B extends WanakuEntity, C> {
7474
default List<A> convertToModels(List<B> entities) {
7575
return entities.stream().map(entity -> convertToModel(entity)).collect(Collectors.toList());
7676
}
77+
78+
79+
/**
80+
* Updates a model to the repository.
81+
*
82+
* @param model the model to update
83+
* @return true if the record was updated
84+
*/
85+
boolean update(C id, A model);
7786
}

core/core-persistence/core-persistence-file/src/main/java/ai/wanaku/core/persistence/file/AbstractFileRepository.java

+13
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,17 @@ public A findById(K id) {
116116
}
117117

118118
abstract Class<T> getEntityClass();
119+
120+
@Override
121+
public boolean update(K id, A model) {
122+
final A byId = findById(id);
123+
if (byId == null) {
124+
return false;
125+
}
126+
127+
deleteById(id);
128+
persist(model);
129+
130+
return true;
131+
}
119132
}

core/core-persistence/core-persistence-mongodb/src/main/java/ai/wanaku/core/persistence/mongodb/AbstractMongoDBRepository.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.mongodb.client.MongoCursor;
99
import com.mongodb.client.model.Filters;
1010
import com.mongodb.client.result.DeleteResult;
11+
import com.mongodb.client.result.UpdateResult;
1112
import org.bson.Document;
1213

1314
import java.util.ArrayList;
@@ -25,12 +26,17 @@ public AbstractMongoDBRepository(MongoClient mongoClient, WanakuMarshallerServic
2526

2627
@Override
2728
public void persist(A model) {
29+
Document document = toDocument(model);
30+
31+
mongoCollection().insertOne(document);
32+
}
33+
34+
private Document toDocument(A model) {
2835
T entity = convertToEntity(model);
2936
String marshalled = wanakuMarshallerService.marshal(entity);
3037
Document document = Document.parse(marshalled);
3138
document.append("_id", entity.getId());
32-
33-
mongoCollection().insertOne(document);
39+
return document;
3440
}
3541

3642
@Override
@@ -73,4 +79,12 @@ protected MongoCollection<Document> mongoCollection() {
7379
}
7480

7581
abstract Class<T> getEntityClass();
82+
83+
@Override
84+
public boolean update(K id, A model) {
85+
Document document = toDocument(model);
86+
final UpdateResult id1 = mongoCollection().updateOne(Filters.eq("_id", id), document);
87+
88+
return id1.getModifiedCount() > 1;
89+
}
7690
}

routers/wanaku-router/src/main/webui/openapi.json

+84
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,34 @@
339339
"tags" : [ "Forwards Resource" ]
340340
}
341341
},
342+
"/api/v1/forwards/update" : {
343+
"post" : {
344+
"requestBody" : {
345+
"content" : {
346+
"application/json" : {
347+
"schema" : {
348+
"$ref" : "#/components/schemas/ForwardReference"
349+
}
350+
}
351+
},
352+
"required" : true
353+
},
354+
"responses" : {
355+
"200" : {
356+
"description" : "OK"
357+
},
358+
"500" : {
359+
"description" : "Wanaku error",
360+
"content" : { }
361+
},
362+
"400" : {
363+
"description" : "Bad Request"
364+
}
365+
},
366+
"summary" : "Update",
367+
"tags" : [ "Forwards Resource" ]
368+
}
369+
},
342370
"/api/v1/management/info/version" : {
343371
"get" : {
344372
"responses" : {
@@ -563,6 +591,34 @@
563591
"tags" : [ "Resources Resource" ]
564592
}
565593
},
594+
"/api/v1/resources/update" : {
595+
"post" : {
596+
"requestBody" : {
597+
"content" : {
598+
"application/json" : {
599+
"schema" : {
600+
"$ref" : "#/components/schemas/ResourceReference"
601+
}
602+
}
603+
},
604+
"required" : true
605+
},
606+
"responses" : {
607+
"200" : {
608+
"description" : "OK"
609+
},
610+
"500" : {
611+
"description" : "Wanaku error",
612+
"content" : { }
613+
},
614+
"400" : {
615+
"description" : "Bad Request"
616+
}
617+
},
618+
"summary" : "Update",
619+
"tags" : [ "Resources Resource" ]
620+
}
621+
},
566622
"/api/v1/tools/add" : {
567623
"post" : {
568624
"requestBody" : {
@@ -634,6 +690,34 @@
634690
"summary" : "Remove",
635691
"tags" : [ "Tools Resource" ]
636692
}
693+
},
694+
"/api/v1/tools/update" : {
695+
"post" : {
696+
"requestBody" : {
697+
"content" : {
698+
"application/json" : {
699+
"schema" : {
700+
"$ref" : "#/components/schemas/ToolReference"
701+
}
702+
}
703+
},
704+
"required" : true
705+
},
706+
"responses" : {
707+
"200" : {
708+
"description" : "OK"
709+
},
710+
"500" : {
711+
"description" : "Wanaku error",
712+
"content" : { }
713+
},
714+
"400" : {
715+
"description" : "Bad Request"
716+
}
717+
},
718+
"summary" : "Update",
719+
"tags" : [ "Tools Resource" ]
720+
}
637721
}
638722
},
639723
"info" : {

routers/wanaku-router/src/main/webui/openapi.yaml

+57
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,25 @@ paths:
220220
summary: Remove Forward
221221
tags:
222222
- Forwards Resource
223+
/api/v1/forwards/update:
224+
post:
225+
requestBody:
226+
content:
227+
application/json:
228+
schema:
229+
$ref: "#/components/schemas/ForwardReference"
230+
required: true
231+
responses:
232+
"200":
233+
description: OK
234+
"500":
235+
description: Wanaku error
236+
content: {}
237+
"400":
238+
description: Bad Request
239+
summary: Update
240+
tags:
241+
- Forwards Resource
223242
/api/v1/management/info/version:
224243
get:
225244
responses:
@@ -374,6 +393,25 @@ paths:
374393
summary: Remove
375394
tags:
376395
- Resources Resource
396+
/api/v1/resources/update:
397+
post:
398+
requestBody:
399+
content:
400+
application/json:
401+
schema:
402+
$ref: "#/components/schemas/ResourceReference"
403+
required: true
404+
responses:
405+
"200":
406+
description: OK
407+
"500":
408+
description: Wanaku error
409+
content: {}
410+
"400":
411+
description: Bad Request
412+
summary: Update
413+
tags:
414+
- Resources Resource
377415
/api/v1/tools/add:
378416
post:
379417
requestBody:
@@ -424,6 +462,25 @@ paths:
424462
summary: Remove
425463
tags:
426464
- Tools Resource
465+
/api/v1/tools/update:
466+
post:
467+
requestBody:
468+
content:
469+
application/json:
470+
schema:
471+
$ref: "#/components/schemas/ToolReference"
472+
required: true
473+
responses:
474+
"200":
475+
description: OK
476+
"500":
477+
description: Wanaku error
478+
content: {}
479+
"400":
480+
description: Bad Request
481+
summary: Update
482+
tags:
483+
- Tools Resource
427484
info:
428485
title: wanaku-router API
429486
version: 0.0.4-SNAPSHOT

servers/wanaku-server-quarkus/src/main/java/ai/wanaku/server/quarkus/api/v1/forwards/ForwardsBean.java

+4
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,8 @@ public List<ForwardReference> listForwards() {
157157

158158
return services.stream().toList();
159159
}
160+
161+
public void update(ForwardReference resource) {
162+
forwardReferenceRepository.update(resource.getName(), resource);
163+
}
160164
}

servers/wanaku-server-quarkus/src/main/java/ai/wanaku/server/quarkus/api/v1/forwards/ForwardsResource.java

+8
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,12 @@ public Response removeForward(ForwardReference reference) {
4646
public RestResponse<WanakuResponse<List<ForwardReference>>> listForwards() {
4747
return RestResponse.ok(new WanakuResponse<>(forwardsBean.listForwards()));
4848
}
49+
50+
@Path("/update")
51+
@POST
52+
@Consumes(MediaType.APPLICATION_JSON)
53+
public Response update(ForwardReference resource) throws WanakuException {
54+
forwardsBean.update(resource);
55+
return Response.ok().build();
56+
}
4957
}

servers/wanaku-server-quarkus/src/main/java/ai/wanaku/server/quarkus/api/v1/resources/ResourcesBean.java

+4
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ public void remove(String name) {
6464
}
6565

6666
}
67+
68+
public void update(ResourceReference resource) {
69+
resourceReferenceRepository.update(resource.getName(), resource);
70+
}
6771
}

servers/wanaku-server-quarkus/src/main/java/ai/wanaku/server/quarkus/api/v1/resources/ResourcesResource.java

+8
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,12 @@ public Response remove(@QueryParam("resource") String resource) throws WanakuExc
5858
resourcesBean.remove(resource);
5959
return Response.ok().build();
6060
}
61+
62+
@Path("/update")
63+
@POST
64+
@Consumes(MediaType.APPLICATION_JSON)
65+
public Response update(ResourceReference resource) throws WanakuException {
66+
resourcesBean.update(resource);
67+
return Response.ok().build();
68+
}
6169
}

servers/wanaku-server-quarkus/src/main/java/ai/wanaku/server/quarkus/api/v1/tools/ToolsBean.java

+4
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,8 @@ public void remove(String name) {
6767
toolReferenceRepository.deleteById(name);
6868
}
6969
}
70+
71+
public void update(ToolReference resource) {
72+
toolReferenceRepository.update(resource.getName(), resource);
73+
}
7074
}

servers/wanaku-server-quarkus/src/main/java/ai/wanaku/server/quarkus/api/v1/tools/ToolsResource.java

+8
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ public Response remove(@QueryParam("tool") String tool) throws WanakuException {
5959
toolsBean.remove(tool);
6060
return Response.ok().build();
6161
}
62+
63+
@Path("/update")
64+
@POST
65+
@Consumes(MediaType.APPLICATION_JSON)
66+
public Response update(ToolReference resource) throws WanakuException {
67+
toolsBean.update(resource);
68+
return Response.ok().build();
69+
}
6270
}

0 commit comments

Comments
 (0)