Add backend/CLAUDE.md
This commit is contained in:
@@ -0,0 +1,129 @@
|
|||||||
|
# How we work
|
||||||
|
You never start or stop servers. Unless that is e.g. a postgres that you clear or you have explicit consent that I give.
|
||||||
|
If I give specific instructions you always communicate your understanding of the topic before you go ahead and implement. This is not necessary if it's really basic or a fresh repo and you are told to just lay foundations.
|
||||||
|
|
||||||
|
# Architecture
|
||||||
|
- We always feature first package structure
|
||||||
|
```
|
||||||
|
src/
|
||||||
|
└── main/
|
||||||
|
└── java/
|
||||||
|
└── com/example/app/
|
||||||
|
└── order/
|
||||||
|
├── OrderEntity.java # JPA-Entity (Datenbank-Mapping)
|
||||||
|
├── Order.java # Domain-/DTO-Model
|
||||||
|
├── OrderRepo.java # Datenbankzugriff (Hibernate Panache)
|
||||||
|
├── OrderService.java # Business-Logik
|
||||||
|
├── OrderMapper.java # Mapping Entity > DTO
|
||||||
|
└── OrderResource.java # REST-Endpoints + Mapping
|
||||||
|
```
|
||||||
|
- We always use `Resource` > `Service` > `Repository`
|
||||||
|
- We use DTOs
|
||||||
|
- We use Java classes as mappers that always offer a method map for single entity and lists like:
|
||||||
|
```java
|
||||||
|
@ApplicationScoped
|
||||||
|
public class OrderMapper
|
||||||
|
{
|
||||||
|
public Order map(OrderEntity input)
|
||||||
|
{
|
||||||
|
return new Order(input.getId(), input.getTimestamp());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Order> map(List<OrderEntity> input)
|
||||||
|
{
|
||||||
|
return input.stream().map(this::map).toList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
- Mapping happens before we return the data in our resources like:
|
||||||
|
```java
|
||||||
|
@GET
|
||||||
|
@Path("/orders")
|
||||||
|
public List<Order> get()
|
||||||
|
{
|
||||||
|
return orderMapper.map(orderRepo.list());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
- We use Builder Pattern for POJOs that are not records
|
||||||
|
- Our Entities have suffix `Entity` and have an ID of type String like this:
|
||||||
|
```java
|
||||||
|
@Entity
|
||||||
|
@Table(name = "order")
|
||||||
|
public class OrderEntity
|
||||||
|
{
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.UUID)
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
public ProductEntity setId(String id)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId()
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
- We use the Repository pattern. Every Entity that needs to be queried gets a repostiory like:
|
||||||
|
```java
|
||||||
|
@ApplicationScoped
|
||||||
|
public class OrderRepo implements PanacheRepositoryBase<OrderEntity, String>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
# Naming Conventions
|
||||||
|
- We keep naming short and accurate like OrderCreationService.create() and NOT OrderCreationService.createOrder()
|
||||||
|
|
||||||
|
# Code Style
|
||||||
|
- Opening braces `{` always go on their own line (Allman style), never at the end of the previous line.
|
||||||
|
- `catch` and `else` keywords also go on their own line
|
||||||
|
- We do not use constructor injection. We do field injection:
|
||||||
|
|
||||||
|
```java
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
OrderService orderService;
|
||||||
|
|
||||||
|
@ConfigProperty(name = "dev.dinauer.r9t.workdir", defaultValue = "/var/lib/r9t")
|
||||||
|
String path;
|
||||||
|
```
|
||||||
|
- When we use if statements we most of the time go the happy path within the if statement like:
|
||||||
|
```java
|
||||||
|
public int getPrice()
|
||||||
|
{
|
||||||
|
if (orderItem.price != null)
|
||||||
|
{
|
||||||
|
return orderItem.price;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
- Using breaks or continue like this when we could just invert the if is a code smell:
|
||||||
|
```java
|
||||||
|
public int sumPrice(Order order)
|
||||||
|
{
|
||||||
|
int total = 0;
|
||||||
|
for (Item item : order.items)
|
||||||
|
{
|
||||||
|
if (item.price == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
total += item.price;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
- We never use ternary operators
|
||||||
|
- Don't Repeat Yourself: Duplizierten Code in wiederverwendbare Funktionen/Module auslagern statt copy-pasten.
|
||||||
|
- No comments at all
|
||||||
|
- Ursachen von Fehlern finden. Nicht Symptome beheben. Fehler nicht stillschweigend verschlucken.
|
||||||
Reference in New Issue
Block a user