Skip to content

Commit

Permalink
Make Todo Service
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Nov 19, 2023
1 parent 95277f0 commit dba6c6a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

@SpringBootApplication
@Import(WirespecConfiguration.class)
public class PetstoreApplication {
public class TodoApplication {

public static void main(String[] args) {
SpringApplication.run(PetstoreApplication.class, args);
SpringApplication.run(TodoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

import community.flock.wirespec.generated.*;
import community.flock.wirespec.integration.spring.annotations.WirespecController;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;


@WirespecController
class PetstoreController implements GetTodos, GetTodoById, CreateTodo, UpdateTodo, DeleteTodo {
class TodoController implements GetTodos, GetTodoById, CreateTodo, UpdateTodo, DeleteTodo {

private final TodoService service;

private List<Todo> store = List.of(
new Todo(UUID.randomUUID().toString(), "First todo", false),
new Todo(UUID.randomUUID().toString(), "Second todo", false),
new Todo(UUID.randomUUID().toString(), "Third todo", false)
);
public TodoController(TodoService service) {
this.service = service;
}

@Override
public CompletableFuture<CreateTodo.Response<?>> createTodo(CreateTodo.Request<?> request) {
Expand All @@ -29,7 +28,7 @@ public CompletableFuture<CreateTodo.Response<?>> createTodo(CreateTodo.Request<?
todoInput.name(),
todoInput.done()
);
store.add(todo);
service.create(todo);
var res = new CreateTodo.Response200ApplicationJson(Map.of(), todo);
return CompletableFuture.completedFuture(res);
}
Expand All @@ -45,7 +44,7 @@ public CompletableFuture<GetTodoById.Response<?>> getTodoById(GetTodoById.Reques
case GetTodoById.RequestVoid req -> req.getPath();
};
System.out.println(id);
var res = new GetTodoById.Response200ApplicationJson(Map.of(), store.get(0));
var res = new GetTodoById.Response200ApplicationJson(Map.of(), service.store.get(0));
return CompletableFuture.completedFuture(res);
}

Expand All @@ -56,7 +55,7 @@ public CompletableFuture<UpdateTodo.Response<?>> updateTodo(UpdateTodo.Request<?

@Override
public CompletableFuture<GetTodos.Response<?>> getTodos(GetTodos.Request<?> request) {
var res = new GetTodos.Response200ApplicationJson(Map.of(), store);
var res = new GetTodos.Response200ApplicationJson(Map.of(), service.store);
return CompletableFuture.completedFuture(res);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package community.flock.wirespec.examples.spring_boot_integration;

import community.flock.wirespec.generated.Todo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import static java.util.stream.Collectors.toList;

@Service
public class TodoService {

public List<Todo> store = List.of(
new Todo(UUID.randomUUID().toString(), "First todo", false),
new Todo(UUID.randomUUID().toString(), "Second todo", false),
new Todo(UUID.randomUUID().toString(), "Third todo", false)
);

public CompletableFuture<Todo> create(Todo todo) {
store.add(todo);
return CompletableFuture.completedFuture(todo);
}

public CompletableFuture<Todo> update(String id, Todo todo) {
store = store.stream()
.map(it -> {
if (it.id().equals(id)) {
return todo;
} else {
return it;
}
})
.collect(toList());
return CompletableFuture.completedFuture(todo);
}

public CompletableFuture<Todo> delete(String id) {
return store.stream()
.filter(todo -> todo.id().equals(id))
.findFirst()
.map(todo -> {
store.remove(todo);
return CompletableFuture.completedFuture(todo);
})
.orElseThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import org.springframework.web.util.pattern.PathPatternParser
import java.io.BufferedReader
import java.lang.reflect.Type
import kotlin.reflect.full.companionObjectInstance

class JacksonContentMapper(val objectMapper: ObjectMapper) : Wirespec.ContentMapper<BufferedReader> {
override fun <T> read(
Expand Down Expand Up @@ -63,9 +62,8 @@ open class WirespecConfiguration {
.methods(RequestMethod.valueOf(method))
.options(options)
.build();
println(endpoint.methods.map { it.name })
val func = endpoint.methods
.first { !listOf("REQUEST_MAPPER", "RESPONSE_MAPPER").contains(it.name) }
val ignoredMethods = listOf("REQUEST_MAPPER", "RESPONSE_MAPPER")
val func = endpoint.methods.first { !ignoredMethods.contains(it.name) }
requestMappingHandlerMapping.registerMapping(
requestMappingWirespec,
controller.value,
Expand Down

0 comments on commit dba6c6a

Please sign in to comment.