Skip to content

Commit

Permalink
feat(api): create basic colis CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-dclrcq committed Feb 20, 2021
1 parent c937414 commit c97915d
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 33 deletions.
28 changes: 23 additions & 5 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>code-with-quarkus</artifactId>
<groupId>com.ineat</groupId>
<artifactId>colis-tracker</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
Expand Down Expand Up @@ -36,15 +36,15 @@
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive</artifactId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
<artifactId>quarkus-reactive-pg-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand All @@ -64,11 +64,29 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
9 changes: 8 additions & 1 deletion api/src/main/docker/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
version: '3.8'
version: '2'

services:
database:
image: postgres
environment:
POSTGRES_USER: colistracker
POSTGRES_PASSWORD: colistracker
POSTGRES_DB: colistracker
ports:
- 5432:5432
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.acme.domain.model;
package com.ineat.colistracker.domain.model;


import java.time.LocalDateTime;


public class Colis {
public String id;
public Long id;
public String name;
public String trackingId;
public LocalDateTime created;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.acme.domain.repository;
package com.ineat.colistracker.domain.repository;

import com.ineat.colistracker.domain.model.Colis;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import org.acme.domain.model.Colis;

public interface ColisRepository {
Uni<Void> create(Colis colis);
Uni<Colis> create(Colis colis);

Multi<Colis> all();

Uni<Boolean> delete(Long id);

Uni<Colis> update(Colis colis);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
package org.acme.domain.service;
package com.ineat.colistracker.domain.service;

import com.ineat.colistracker.domain.model.Colis;
import com.ineat.colistracker.domain.repository.ColisRepository;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
@Slf4j
public class ColisService {

@Inject
ColisRepository colisRepository;

public Uni<Colis> create(Colis colis) {
return colisRepository
.create(colis)
.onItem().invoke(created -> log.info("Created colis with id {} and tracking id {}", created.id, created.trackingId));
}

public Multi<Colis> findAll() {
return colisRepository.all();
}

public Uni<Boolean> delete(Long id) {
return colisRepository
.delete(id)
.onItem().invoke(aBoolean -> log.info("Colis with id {} deleted : {}", id, aBoolean));
}

public Uni<Colis> update(Colis colis) {
return colisRepository
.update(colis)
.onItem().invoke(updated -> log.info("Colis with id {} successfully updated", updated.id));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
package org.acme.infrastructure.api.dto;
package com.ineat.colistracker.infrastructure.api.dto;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalDateTime;

public class ColisDTO {
public Long id;
public String name;
public String trackingId;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public LocalDateTime created;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public LocalDateTime updated;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.ineat.colistracker.infrastructure.api.dto;

public class CreateOrUpdateColisDTO {
public String name;
public String trackingId;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.acme.infrastructure.api.mapper;
package com.ineat.colistracker.infrastructure.api.mapper;

import org.acme.domain.model.Colis;
import org.acme.infrastructure.api.dto.ColisDTO;
import com.ineat.colistracker.domain.model.Colis;
import com.ineat.colistracker.infrastructure.api.dto.ColisDTO;
import com.ineat.colistracker.infrastructure.api.dto.CreateOrUpdateColisDTO;
import org.mapstruct.Mapper;

@Mapper(componentModel = "cdi")
public interface ColisMapper {
Colis toDomain(ColisDTO dto);

ColisDTO toEntity(Colis colis);
ColisDTO toDTO(Colis colis);

Colis toDomain(CreateOrUpdateColisDTO dto);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
package org.acme.infrastructure.api.resource;
package com.ineat.colistracker.infrastructure.api.resource;

import com.ineat.colistracker.domain.model.Colis;
import com.ineat.colistracker.domain.service.ColisService;
import com.ineat.colistracker.infrastructure.api.dto.ColisDTO;
import com.ineat.colistracker.infrastructure.api.dto.CreateOrUpdateColisDTO;
import com.ineat.colistracker.infrastructure.api.mapper.ColisMapper;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;

@Path("/api/colis")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ColisResource {

@Inject
ColisService colisService;

@Inject
ColisMapper colisMapper;

@POST
public Uni<Response> create(CreateOrUpdateColisDTO body) {
final Colis colis = colisMapper.toDomain(body);

return colisService.create(colis)
.onItem().transform(created -> colisMapper.toDTO(created))
.onItem().transform(created -> Response.status(Response.Status.CREATED).entity(created).build());
}

@GET
public Multi<ColisDTO> findAll() {
return colisService.findAll()
.onItem().transform(colis -> colisMapper.toDTO(colis));
}

@DELETE
@Path("/{id}")
public Uni<Response> delete(@PathParam("id") Long id) {
return colisService.delete(id)
.onItem().transform(res -> Response.ok().build());
}

@PUT
@Path("/{id}")
public Uni<ColisDTO> update(@PathParam("id") Long id, CreateOrUpdateColisDTO dto) {
final Colis colis = colisMapper.toDomain(dto);
colis.id = id;

return colisService.update(colis)
.onItem().transform(updated -> colisMapper.toDTO(updated));
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package org.acme.infrastructure.database.entity;
package com.ineat.colistracker.infrastructure.database.entity;

import io.quarkus.hibernate.reactive.panache.PanacheEntity;
import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Table(name = "colis")
public class ColisEntity {
@Id
@Column(name = "id", columnDefinition = "SERIAL")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;

public String name;

@Column(name = "tracking_id")
public String trackingId;

@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
public LocalDateTime created;

@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
public LocalDateTime updated;

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.acme.infrastructure.database.mapper;
package com.ineat.colistracker.infrastructure.database.mapper;

import org.acme.domain.model.Colis;
import org.acme.infrastructure.database.entity.ColisEntity;
import com.ineat.colistracker.domain.model.Colis;
import com.ineat.colistracker.infrastructure.database.entity.ColisEntity;
import org.mapstruct.Mapper;

@Mapper(componentModel = "cdi")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.acme.infrastructure.database.repository;
package com.ineat.colistracker.infrastructure.database.repository;

import com.ineat.colistracker.domain.model.Colis;
import com.ineat.colistracker.infrastructure.database.entity.ColisEntity;
import com.ineat.colistracker.infrastructure.database.mapper.ColisMapper;
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import org.acme.domain.model.Colis;
import org.acme.domain.repository.ColisRepository;
import org.acme.infrastructure.database.entity.ColisEntity;
import org.acme.infrastructure.database.mapper.ColisMapper;
import com.ineat.colistracker.domain.repository.ColisRepository;
import org.hibernate.reactive.mutiny.Mutiny;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -18,14 +19,26 @@ public class ColisPanacheRepository implements ColisRepository, PanacheRepositor
ColisMapper mapper;

@Override
public Uni<Void> create(Colis colis) {
public Uni<Colis> create(Colis colis) {
final ColisEntity entity = mapper.toEntity(colis);
return persist(entity);

return persistAndFlush(entity)
.map(persisted -> mapper.toDomain(entity));
}

@Override
public Multi<Colis> all() {
return findAll().stream()
.onItem().transform(entity -> mapper.toDomain(entity));
}

@Override
public Uni<Boolean> delete(Long id) {
return deleteById(id);
}

@Override
public Uni<Colis> update(Colis colis) {
return Uni.createFrom().nullItem();
}
}
7 changes: 7 additions & 0 deletions api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
quarkus.flyway.migrate-at-start = true
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=colistracker
quarkus.datasource.password=colistracker
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/colistracker
quarkus.datasource.reactive.url=postgresql://localhost:5432/colistracker
quarkus.datasource.jdbc=false
8 changes: 8 additions & 0 deletions api/src/main/resources/db/migration/V1.0.0__Colis.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE colis
(
id BIGSERIAL,
name TEXT,
tracking_id TEXT,
created TIMESTAMP,
updated TIMESTAMP
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.acme;
package com.ineat.colistracker;

import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.acme;
package com.ineat.colistracker;

import io.quarkus.test.junit.NativeImageTest;

Expand Down
6 changes: 6 additions & 0 deletions docs/database.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= Database

[plantuml, db-model, png]
....
....

0 comments on commit c97915d

Please sign in to comment.