Skip to content

Commit

Permalink
closes #4 closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
sombriks committed May 6, 2024
1 parent 1ef9c5f commit ac89e32
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 52 deletions.
15 changes: 8 additions & 7 deletions src/main/java/sample/htmx/TodoappSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ public class TodoappSite extends Site {
private final TodoService todoService;

public TodoappSite(TodoService todoService) {
LOG.info("open to business http://localhost:8080");
properties().put("todoService", todoService);
this.todoService = todoService;
todoService.initDB();
LOG.info("open to business http://localhost:8080");
}

public void setup() {
get("/", new IndexElement(todoService));
get("/", IndexElement.class);
group("/todos", new Router() {
@Override
public void setup() {
get("", () -> new ListTodoElement(todoService));
post("", () -> new InsertTodoElement(todoService));
get("", PathInfoHandling.CAPTURE, () -> new FindTodoElement(todoService));
put("", PathInfoHandling.CAPTURE, () -> new UpdateTodoElement(todoService));
delete("", PathInfoHandling.CAPTURE, () -> new DeleteTodoElement(todoService));
get("", ListTodoElement.class);
post("", InsertTodoElement.class);
get("", PathInfoHandling.CAPTURE, FindTodoElement.class);
put("", PathInfoHandling.CAPTURE, UpdateTodoElement.class);
delete("", PathInfoHandling.CAPTURE, DeleteTodoElement.class);
}
});
LOG.info("Setting up site");
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/sample/htmx/elements/DeleteTodoElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
import org.slf4j.LoggerFactory;
import rife.engine.Context;
import rife.engine.Element;
import rife.engine.annotations.Parameter;
import rife.engine.annotations.PathInfo;
import rife.engine.annotations.Property;
import sample.htmx.elements.processor.TemplateProcessor;
import sample.htmx.service.TodoService;

public class DeleteTodoElement implements Element {

private static final Logger LOG = LoggerFactory.getLogger(DeleteTodoElement.class);
private final TodoService todoService;

public DeleteTodoElement(TodoService todoService) {
this.todoService = todoService;
}
@Property
private TodoService todoService;

@PathInfo
private Long id;

@Override
public void process(Context c) throws Exception {
LOG.info("delete");
Long id = Long.parseLong(c.pathInfo());
var result = todoService.delete(id);
var todos = todoService.list(c.parameter("q",""));
var template = c.template("todos/list");
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/sample/htmx/elements/FindTodoElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
import org.slf4j.LoggerFactory;
import rife.engine.Context;
import rife.engine.Element;
import rife.engine.annotations.PathInfo;
import rife.engine.annotations.Property;
import sample.htmx.model.Todo;
import sample.htmx.service.TodoService;

public class FindTodoElement implements Element {

private static final Logger LOG = LoggerFactory.getLogger(FindTodoElement.class);
private final TodoService todoService;

public FindTodoElement(TodoService todoService) {
this.todoService = todoService;
}
@Property
private TodoService todoService;

@PathInfo
private Long id;

@Override
public void process(Context c) throws Exception {
LOG.info("find");
Long id = Long.parseLong(c.pathInfo());
Todo todo = todoService.find(id);
var template = c.template("todos/detail");
template.setValue("todo", todo.getDescription());
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/sample/htmx/elements/IndexElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
import org.slf4j.LoggerFactory;
import rife.engine.Context;
import rife.engine.Element;
import rife.engine.annotations.Parameter;
import rife.engine.annotations.Property;
import sample.htmx.elements.processor.TemplateProcessor;
import sample.htmx.service.TodoService;

public class IndexElement implements Element {

private static final Logger LOG = LoggerFactory.getLogger(IndexElement.class);
private final TodoService todoService;

public IndexElement(TodoService todoService) {
this.todoService = todoService;
}
@Property
private TodoService todoService;

@Parameter
private String q = "";

@Override
public void process(Context c) throws Exception {
LOG.info("index");
var todos = todoService.list(c.parameter("q",""));
var todos = todoService.list(q);
var template = c.template("index");
TemplateProcessor.populateList(template, todos);
c.print(template);
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/sample/htmx/elements/InsertTodoElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
import org.slf4j.LoggerFactory;
import rife.engine.Context;
import rife.engine.Element;
import rife.engine.annotations.Parameter;
import rife.engine.annotations.ParametersBean;
import rife.engine.annotations.Property;
import sample.htmx.elements.processor.TemplateProcessor;
import sample.htmx.model.Todo;
import sample.htmx.service.TodoService;

public class InsertTodoElement implements Element {

private static final Logger LOG = LoggerFactory.getLogger(InsertTodoElement.class);
private final TodoService todoService;

public InsertTodoElement(TodoService todoService) {
this.todoService = todoService;
}
@Property
private TodoService todoService;

@Parameter
private String q = "";

@ParametersBean
private Todo todo;

@Override
public void process(Context c) throws Exception {
LOG.info("insert");
Todo todo = new Todo(
0l,
c.parameter("description"),
c.parameterBoolean("done"),
null,
null
);
todoService.insert(todo);
var todos = todoService.list(c.parameter("q", ""));
var todos = todoService.list(q);
var template = c.template("todos/list");
TemplateProcessor.populateList(template, todos);
c.print(template);
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/sample/htmx/elements/ListTodoElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
import org.slf4j.LoggerFactory;
import rife.engine.Context;
import rife.engine.Element;
import rife.engine.annotations.Parameter;
import rife.engine.annotations.Property;
import sample.htmx.elements.processor.TemplateProcessor;
import sample.htmx.service.TodoService;

public class ListTodoElement implements Element {

private static final Logger LOG = LoggerFactory.getLogger(ListTodoElement.class);
private final TodoService todoService;

public ListTodoElement(TodoService todoService) {
this.todoService = todoService;
}
@Property
private TodoService todoService;

@Parameter
private String q = "";

@Override
public void process(Context c) throws Exception {
LOG.info("list");
var todos = todoService.list(c.parameter("q",""));
var todos = todoService.list(q);
var template = c.template("todos/list");
TemplateProcessor.populateList(template, todos);
c.print(template);
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/sample/htmx/elements/UpdateTodoElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,42 @@
import org.slf4j.LoggerFactory;
import rife.engine.Context;
import rife.engine.Element;
import rife.engine.annotations.Parameter;
import rife.engine.annotations.ParametersBean;
import rife.engine.annotations.PathInfo;
import rife.engine.annotations.Property;
import sample.htmx.elements.processor.TemplateProcessor;
import sample.htmx.model.Todo;
import sample.htmx.service.TodoService;

public class UpdateTodoElement implements Element {

private static final Logger LOG = LoggerFactory.getLogger(UpdateTodoElement.class);
private final TodoService todoService;

public UpdateTodoElement(TodoService todoService) {
this.todoService = todoService;
}
@Property
private TodoService todoService;

@Parameter
private String q = "";

@PathInfo
private Long id;

@ParametersBean
private Todo todo;

@Override
public void process(Context c) throws Exception {
LOG.info("update");
Long id = Long.parseLong(c.pathInfo());
Todo todo = new Todo(
id,
c.parameter("description"),
c.parameterBoolean("done"),
null,
null
);
// Todo todo = new Todo(
// id,
// c.parameter("description"),
// c.parameterBoolean("done"),
// null,
// null
// );
todoService.update(id, todo);
var todos = todoService.list(c.parameter("q", ""));
var todos = todoService.list(q);
var template = c.template("todos/list");
TemplateProcessor.populateList(template, todos);
c.print(template);
Expand Down

0 comments on commit ac89e32

Please sign in to comment.