Skip to content

Commit

Permalink
works on #3 - issues with insert, update and delete, works fine for f…
Browse files Browse the repository at this point in the history
…ind and select
  • Loading branch information
sombriks committed May 6, 2024
1 parent ac89e32 commit 4a418b5
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 75 deletions.
1 change: 1 addition & 0 deletions .idea/libraries/bld.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/libraries/compile.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [sample-htmx-rife2][repo]

Sample [rife2][rife2]/[htmx][htmx] todo app
Sample [rife2][rife2]/[htmx][htmx] todos app

## Requirements

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/sample/htmx/elements/DeleteTodoElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public class DeleteTodoElement implements Element {
private TodoService todoService;

@PathInfo
private Long id;
private Integer id;

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

public class FindTodoElement implements Element {
Expand All @@ -17,14 +17,14 @@ public class FindTodoElement implements Element {
private TodoService todoService;

@PathInfo
private Long id;
private Integer id;

@Override
public void process(Context c) throws Exception {
LOG.info("find");
Todo todo = todoService.find(id);
Todos todos = todoService.find(id);
var template = c.template("todos/detail");
template.setValue("todo", todo.getDescription());
template.setValue("todo", todos.getDescription());
c.print(template);
}
}
6 changes: 3 additions & 3 deletions src/main/java/sample/htmx/elements/InsertTodoElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import rife.engine.annotations.ParametersBean;
import rife.engine.annotations.Property;
import sample.htmx.elements.processor.TemplateProcessor;
import sample.htmx.model.Todo;
import sample.htmx.model.Todos;
import sample.htmx.service.TodoService;

public class InsertTodoElement implements Element {
Expand All @@ -22,12 +22,12 @@ public class InsertTodoElement implements Element {
private String q = "";

@ParametersBean
private Todo todo;
private Todos todos;

@Override
public void process(Context c) throws Exception {
LOG.info("insert");
todoService.insert(todo);
todoService.insert(todos);
var todos = todoService.list(q);
var template = c.template("todos/list");
TemplateProcessor.populateList(template, todos);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/sample/htmx/elements/UpdateTodoElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import rife.engine.annotations.PathInfo;
import rife.engine.annotations.Property;
import sample.htmx.elements.processor.TemplateProcessor;
import sample.htmx.model.Todo;
import sample.htmx.model.Todos;
import sample.htmx.service.TodoService;

public class UpdateTodoElement implements Element {
Expand All @@ -23,10 +23,10 @@ public class UpdateTodoElement implements Element {
private String q = "";

@PathInfo
private Long id;
private Integer id;

@ParametersBean
private Todo todo;
private Todos todos;

@Override
public void process(Context c) throws Exception {
Expand All @@ -38,7 +38,7 @@ public void process(Context c) throws Exception {
// null,
// null
// );
todoService.update(id, todo);
todoService.update(id, todos);
var todos = todoService.list(q);
var template = c.template("todos/list");
TemplateProcessor.populateList(template, todos);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package sample.htmx.elements.processor;

import rife.template.Template;
import sample.htmx.model.Todo;
import sample.htmx.model.Todos;

import java.util.List;

public class TemplateProcessor {
public static void populateList(Template template, List<Todo> todos) {
public static void populateList(Template template, List<Todos> todos) {
todos.forEach(todo -> {
template.setValue("id", todo.getId());
template.setValue("description", todo.getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import java.time.LocalDateTime;
import java.util.Objects;

public class Todo {
public class Todos {

private Long id;
private int id;
private String description;
private Boolean done;
private boolean done;
private LocalDateTime created;
private LocalDateTime updated;

public Todo() {
public Todos() {
}

public Todo(
Long id,
public Todos(
int id,
String description,
Boolean done,
boolean done,
LocalDateTime created,
LocalDateTime updated
) {
Expand All @@ -28,11 +28,11 @@ public Todo(
this.updated = updated;
}

public Long getId() {
public int getId() {
return id;
}

public void setId(Long id) {
public void setId(int id) {
this.id = id;
}

Expand All @@ -44,11 +44,11 @@ public void setDescription(String description) {
this.description = description;
}

public Boolean getDone() {
public boolean getDone() {
return done;
}

public void setDone(Boolean done) {
public void setDone(boolean done) {
this.done = done;
}

Expand All @@ -72,8 +72,8 @@ public void setUpdated(LocalDateTime updated) {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Todo todo = (Todo) o;
return Objects.equals(id, todo.id) && Objects.equals(description, todo.description) && Objects.equals(done, todo.done) && Objects.equals(created, todo.created) && Objects.equals(updated, todo.updated);
Todos todos = (Todos) o;
return Objects.equals(id, todos.id) && Objects.equals(description, todos.description) && Objects.equals(done, todos.done) && Objects.equals(created, todos.created) && Objects.equals(updated, todos.updated);
}

@Override
Expand Down
78 changes: 34 additions & 44 deletions src/main/java/sample/htmx/service/TodoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@
import org.slf4j.LoggerFactory;
import rife.database.Datasource;
import rife.database.DbQueryManager;
import rife.database.queries.Delete;
import rife.database.queries.Insert;
import rife.database.queries.Select;
import rife.database.queries.Update;
import sample.htmx.model.Todo;
import rife.database.querymanagers.generic.GenericQueryManager;
import rife.database.querymanagers.generic.GenericQueryManagerFactory;
import rife.database.querymanagers.generic.RestoreQuery;
import sample.htmx.model.Todos;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

public class TodoService {

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

private final Datasource ds;
private final GenericQueryManager<Todos> queryManager;
private final DbQueryManager manager;

public TodoService(Datasource ds) {
this.ds = ds;
queryManager = GenericQueryManagerFactory.instance(ds, Todos.class);
manager = new DbQueryManager(ds);
this.ds = ds;
}

public TodoService() {
Expand All @@ -45,64 +46,53 @@ updated timestamp not null default now()
LOG.info("todos table created");
}

public List<Todo> list(String q) throws Exception {
public List<Todos> list(String q) throws Exception {
LOG.info("list q={}", q);
var select = new Select(ds)
.from("todos")
.where("lower(description)", "like", String.format("%s%s%s", "%", q, "%"));
var todos = new ArrayList<Todo>();
manager.executeFetchAll(select, rp -> {
todos.add(new Todo(
rp.getLong("id"),
rp.getString("description"),
rp.getBoolean("done"),
Instant.ofEpochMilli(rp.getDate("created").getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(),
Instant.ofEpochMilli(rp.getDate("updated").getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()
));
});
return todos;
.where("lower(description)", "like", //
String.format("%s%s%s", "%", q, "%")); // FIXME safe?
return queryManager.restore(new RestoreQuery(select));
}

public Todo find(Long id) throws Exception {
public Todos find(Integer id) throws Exception {
LOG.info("list id={}", id);
var select = new Select(ds).from("todos").where("id", "=", id);
var todos = new ArrayList<Todo>();
var result = manager.executeFetchFirst(select, rp -> todos.add(new Todo(
rp.getLong("id"),
rp.getString("description"),
rp.getBoolean("done"),
Instant.ofEpochMilli(rp.getDate("created").getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(),
Instant.ofEpochMilli(rp.getDate("updated").getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()
)));
if (result) return todos.get(0);
throw new Exception("not found");
var select = new Select(ds)
.from("todos")
.where("id", "=", id);
var result = queryManager.restore(new RestoreQuery(select));
if(result.isEmpty())
throw new Exception("not found");
return result.get(0);
}

public int insert(Todo todo) {
LOG.info("insert todo={}", todo);
public int insert(Todos todos) {
LOG.info("insert todo={}", todos);
var insert = new Insert(ds)
.into("todos")
.field("description", todo.getDescription())
.field("done", todo.getDone());
.field("description", todos.getDescription())
.field("done", todos.getDone());
return manager.executeUpdate(insert);
}

public int update(Long id, Todo todo) {
LOG.info("update id={}, todo={}", id, todo);
public int update(Integer id, Todos todos) {
LOG.info("update id={}, todo={}", id, todos);
var update = new Update(ds)
.table("todos")
.field("description", todo.getDescription())
.field("done", todo.getDone())
.field("description", todos.getDescription())
.field("done", todos.getDone())
.field("updated", LocalDateTime.now())
.where("id", "=", id);
return manager.executeUpdate(update);
// return queryManager.update(todos);
}

public int delete(Long id) {
public boolean delete(Integer id) {
LOG.info("delete id={}", id);
var delete = new Delete(ds)
.from("todos")
.where("id", "=", id);
return manager.executeUpdate(delete);
// var delete = new Delete(ds)
// .from("todos")
// .where("id", "=", id);
// return manager.executeUpdate(delete);
return queryManager.delete(id);
}
}

0 comments on commit 4a418b5

Please sign in to comment.