Skip to content

Commit

Permalink
Context for tasks todo (#24)
Browse files Browse the repository at this point in the history
* new version npm, and run frontend with --host flag

* new configs to editorconfig

* new configs to properties (CORS) and new banner

* (clean code) Java class names changed. Added context feature for todo
  • Loading branch information
gulybyte authored Jan 18, 2024
1 parent 0944f83 commit 97c3b04
Show file tree
Hide file tree
Showing 29 changed files with 220 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.{vue,yml,yaml}]
[*.{vue,yml,yaml,html,css}]
indent_size = 2

[*.md]
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@

### Versions:
- **NodeJS**: LTS 18
- **NPM**: ^9.8
- **NPM**: ^10.2
- **Java**: LTS 17 OpenJDK

# Run Project
### [API (Backend)](/todo-api/README.md)
### [UI (Frontend)](/todo-ui/README.md)
### [Build API (Backend)](/todo-api/README.md)
### [Build UI (Frontend)](/todo-ui/README.md)

<!-- Run:
```
docker-compose -f "docker-compose.yml" up -d --build
``` -->

#### open browser:
```
Expand Down
19 changes: 7 additions & 12 deletions todo-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@ cd todo-api
./gradlew i
```

<!-- ### Compile and Minify for Build Local
**Compile and create image Docker**:
```sh
./gradlew build docker --info
``` -->

### Run with Hot-Reload for Development

In your IDE (Eclipse, IntelliJ IDEA, Visual Studio Code, etc).

Don't worry about the database; Spring will automatically initialize it for you using Docker

### Compile and Minify for Production

**Compile**:
```sh
./gradlew build
```

**Run**:
```sh
java -Dspring.profiles.active=build -jar build/libs/todo-1.jar
```
16 changes: 15 additions & 1 deletion todo-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ plugins {
id 'io.spring.dependency-management' version '1.1.0'
id 'org.hibernate.orm' version '6.2.2.Final'
//id 'org.graalvm.buildtools.native' version '0.9.20'
//id 'pl.allegro.tech.build.axion-release' version '1.13.6'
//id 'com.palantir.docker' version '0.31.0'
}

//version = scmVersion.version

group = 'io.github.gulybyte'
version = '1'

java {
sourceCompatibility = '17'
sourceCompatibility(JavaVersion.VERSION_17)
targetCompatibility(JavaVersion.VERSION_17)
}

configurations {
Expand Down Expand Up @@ -58,4 +63,13 @@ hibernate {
associationManagement true
}
}
/*
String imageName = "todo:$version"
docker {
name imageName
files "build/libs/${bootJar.archiveFileName.get()}"
buildArgs([JAR_FILE: bootJar.archiveFileName.get()])
dockerfile file("src/docker/Dockerfile")
}
*/
12 changes: 6 additions & 6 deletions todo-api/compose.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
volumes:
postgres-data:
todo-api-volume-postgres:

networks:
backend:

todo-api-network:
driver: bridge

services:
postgres:
image: 'postgres:15.2-alpine'
image: 'postgres:16.1-alpine'
environment:
- 'POSTGRES_DB=todo-api'
- 'POSTGRES_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
ports:
- '15432:5432'
networks:
- backend
- todo-api-network
volumes:
- postgres-data:/var/lib/postgresql/data
- todo-api-volume-postgres:/var/lib/postgresql/data
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ public void doFilter(final ServletRequest req,final ServletResponse res, final F
var allowDomains = todoProperty.getDomains().split(",");
var allowOrigins = new HashSet<>(Arrays.asList(allowDomains));

if(allowOrigins.contains(origin)){
if(allowOrigins.contains("dev-mode")) {
response.setHeader("Access-Control-Allow-Origin", "*");
} else if(allowOrigins.contains(origin)){
response.setHeader("Access-Control-Allow-Origin", origin);
} else {
// TODO: exception
}

response.setHeader("Access-Control-Allow-Credentials", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.github.gulybyte.todo.filter.body.TodoPutFilter;
import io.github.gulybyte.todo.filter.body.TodoPutContextFilter;
import io.github.gulybyte.todo.filter.body.TodoPutDescriptionFilter;
import io.github.gulybyte.todo.model.Todo;
import io.github.gulybyte.todo.service.ServiceTodo;
import jakarta.validation.Valid;
Expand All @@ -37,10 +38,14 @@ public ResponseEntity<Todo> save(@RequestBody @Valid Todo todo) {


@PutMapping("update-description")
public ResponseEntity<Todo> updateDescription(@RequestBody @Valid TodoPutFilter todoBody) {
public ResponseEntity<Todo> updateDescription(@RequestBody @Valid TodoPutDescriptionFilter todoBody) {
return ResponseEntity.ok(service.updateDescription(todoBody));
}

@PutMapping("update-context")
public ResponseEntity<Todo> updateContext(@RequestBody @Valid TodoPutContextFilter todoBody) {
return ResponseEntity.ok(service.updateContext(todoBody));
}

@GetMapping
public ResponseEntity<List<Todo>> findAllWithoutMarkDone(){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.gulybyte.todo.filter.body;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data @Builder
@AllArgsConstructor @NoArgsConstructor
public class TodoPutContextFilter {

@NotNull(message = "Id cannot be null.")
private Long id;

@Size(max = 20, message = "Your context of the todo is too long (max: 20 characters)")
private String context;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Data @Builder
@AllArgsConstructor @NoArgsConstructor
public class TodoPutFilter {
public class TodoPutDescriptionFilter {

@NotNull(message = "Id cannot be null.")
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ public class Todo {

@Column
@NotBlank(message = "The description cannot be blank.")
@Size(max = 350, message = "Description is too long (max: 350 characters).")
@Size(max = 350, message = "Your description is too long (max: 350 characters).")
private String description;

@Column
@Size(max = 20, message = "Your context of the todo is too long (max: 20 characters).")
private String contextTodo;

@Column
private Boolean done;

Expand All @@ -47,6 +51,8 @@ public void beforeSave() {
final var DATE_TIME_NOW = LocalDateTime.now();
setCreatedDate(DATE_TIME_NOW);
setOrderTodo(DATE_TIME_NOW);
if (getContextTodo() == null || getContextTodo() == "")
setContextTodo("none");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

import org.springframework.data.domain.Page;

import io.github.gulybyte.todo.filter.body.TodoPutFilter;
import io.github.gulybyte.todo.filter.body.TodoPutContextFilter;
import io.github.gulybyte.todo.filter.body.TodoPutDescriptionFilter;
import io.github.gulybyte.todo.model.Todo;

public interface ServiceTodo {

Todo save(Todo todo);
Todo updateDescription(TodoPutFilter todoBody);
Todo updateDescription(TodoPutDescriptionFilter todoBody);
Todo updateContext(TodoPutContextFilter todoBody);

List<Todo> findAllWithoutMarkDone();
Page<Todo> findAllWithMarkDone(int pageNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import io.github.gulybyte.todo.exception.status.ConflictException;
import io.github.gulybyte.todo.exception.status.NotFoundException;
import io.github.gulybyte.todo.filter.body.TodoPutFilter;
import io.github.gulybyte.todo.filter.body.TodoPutContextFilter;
import io.github.gulybyte.todo.filter.body.TodoPutDescriptionFilter;
import io.github.gulybyte.todo.model.Todo;
import io.github.gulybyte.todo.repository.TodoRepository;
import io.github.gulybyte.todo.service.ServiceTodo;
Expand All @@ -34,7 +35,7 @@ public Todo save(Todo todo) {


@Override @Transactional
public Todo updateDescription(TodoPutFilter todoBody) {
public Todo updateDescription(TodoPutDescriptionFilter todoBody) {
var todoToSave = repository.findById(todoBody.getId()).map(todo -> {
if (todo.getDescription().equals(todoBody.getDescription()))
throw new ConflictException("Feature description remains the same");
Expand All @@ -47,6 +48,20 @@ public Todo updateDescription(TodoPutFilter todoBody) {
}


@Override @Transactional
public Todo updateContext(TodoPutContextFilter todoBody) {
var todoToSave = repository.findById(todoBody.getId()).map(todo -> {
if (todo.getContextTodo().equals(todoBody.getContext()))
throw new ConflictException("Feature context remains the same");
todo.setContextTodo(todoBody.getContext());
return todo;
})
.orElseThrow(() -> new NotFoundException(NOT_FOUND));

return repository.save(todoToSave);
}


@Override
public List<Todo> findAllWithoutMarkDone() {
var todo = repository.findAllWithoutMarkDone();
Expand Down
2 changes: 1 addition & 1 deletion todo-api/src/main/resources/application-build.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
todoapi.domains=http://localhost:5173,http://localhost:4173,http://localhost:8080
todoapi.domains=dev-mode
spring.datasource.url=jdbc:postgresql://localhost:15432/todo-api

spring.datasource.username=postgres
Expand Down
4 changes: 1 addition & 3 deletions todo-api/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
todoapi.domains=http://localhost:5173,http://localhost:4173,http://localhost:8080
todoapi.domains=dev-mode
spring.datasource.url=jdbc:postgresql://localhost:15432/todo-api
#jdbc:postgresql://localhost:5432/todo-api

spring.datasource.username=postgres
spring.datasource.password=postgres

spring.docker.compose.file=./todo-api/compose.yaml
spring.docker.compose.lifecycle-management=start-and-stop


server.address=0.0.0.0
server.port=8080
9 changes: 9 additions & 0 deletions todo-api/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@
\/_//_/ \/_____/ \ \ \/ \/____/ \/_/\/_/ \/_____/ \/___/ \/___/ \/_/ \/____/ \/____/
\ \_\
\/_/

Java: 17
Dependency Management: Gradle - Groovy
Spring Boot: 3.1.0

GitHub: https://github.com/gulybyte/sample-todo
Licence: Creative Commons Zero v1.0 Universal

_________________________________________________________________________________________________________
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ CREATE TABLE todo (
order_todo timestamp(6) NULL,
CONSTRAINT todo_pkey PRIMARY KEY (id)
);

INSERT INTO todo (created_date,description,done,done_date,order_todo) VALUES
('2022-11-11 12:03:03.604757','Welcome!.',false,NULL,'2022-11-11 12:03:03.604757');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE todo ADD context_todo varchar(20);

UPDATE todo SET context_todo = 'none' WHERE context_todo IS NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import io.github.gulybyte.todo.model.Todo;
import io.github.gulybyte.todo.util.TodoCreator;
import static io.github.gulybyte.todo.util.ArgumentosMatchersTodo.*;
import static io.github.gulybyte.todo.util.ArgumentsMatchersTodo.*;

import static org.junit.jupiter.api.Assertions.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import io.github.gulybyte.todo.repository.TodoRepository;
import io.github.gulybyte.todo.service.impl.ServiceTodoImpl;
import io.github.gulybyte.todo.util.TodoCreator;
import static io.github.gulybyte.todo.util.ArgumentosMatchersTodo.*;
import static io.github.gulybyte.todo.util.ArgumentsMatchersTodo.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
Expand Down Expand Up @@ -61,7 +61,7 @@ void updateDescription() {
when(repository.findById(anyLong())).thenReturn(anyOptionalTodo());
when(repository.save(todoParamCaptor.capture())).thenReturn(anyTodo());

var response = service.updateDescription(anyTodoPutFilter(TodoCreator.createTodoPutFilterWithNewDescription()));
var response = service.updateDescription(anyTodoPutDescriptionFilter(TodoCreator.createTodoPutDescriptionFilterWithNewDescription()));
var capturedParameterResponse = todoParamCaptor.getValue();

assertEquals(response, repository.save(any()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import io.github.gulybyte.todo.service.impl.ServiceTodoImpl;
import io.github.gulybyte.todo.util.TodoCreator;

import static io.github.gulybyte.todo.util.ArgumentosMatchersTodo.*;
import static io.github.gulybyte.todo.util.ArgumentsMatchersTodo.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
Expand All @@ -40,7 +40,7 @@ void updateDescription() {
when(repository.findById(anyLong())).thenReturn(anyOptionalTodo());

assertThrows(ConflictException.class,
() -> service.updateDescription(anyTodoPutFilter(TodoCreator.createTodoPutFilterDefault())));
() -> service.updateDescription(anyTodoPutDescriptionFilter(TodoCreator.createTodoPutDescriptionFilterDefault())));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import io.github.gulybyte.todo.service.impl.ServiceTodoImpl;
import io.github.gulybyte.todo.util.TodoCreator;

import static io.github.gulybyte.todo.util.ArgumentosMatchersTodo.*;
import static io.github.gulybyte.todo.util.ArgumentsMatchersTodo.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
Expand All @@ -40,7 +40,7 @@ void updateDescription() {
when(repository.findById(anyLong())).thenReturn(anyEmptyOptionalTodo());

assertThrows(NotFoundException.class,
() -> service.updateDescription(anyTodoPutFilter(TodoCreator.createTodoPutFilterDefault())));
() -> service.updateDescription(anyTodoPutDescriptionFilter(TodoCreator.createTodoPutDescriptionFilterDefault())));

}

Expand Down
Loading

0 comments on commit 97c3b04

Please sign in to comment.