Skip to content

Commit

Permalink
Storage models (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavetok authored Oct 21, 2023
1 parent 2e6338d commit 0c82dde
Show file tree
Hide file tree
Showing 62 changed files with 656 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import static java.util.UUID.randomUUID;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import smecalculus.bezmen.core.ClientSide.PreviewRequest;
import smecalculus.bezmen.core.ClientSide.PreviewResponse;
import smecalculus.bezmen.core.ClientSide.RegistrationRequest;
import smecalculus.bezmen.core.ClientSide.RegistrationResponse;
import smecalculus.bezmen.storage.SepulkaDao;

@RequiredArgsConstructor
Expand All @@ -17,14 +23,20 @@ public class SepulkaServiceImpl implements SepulkaService {
private SepulkaDao dao;

@Override
public SepulkaNewResponse register(SepulkaNewRequest request) {
var sepulkaCreated = mapper.toEntity(request).internalId(randomUUID()).build();
var sepulkaSaved = dao.save(sepulkaCreated);
return mapper.toSlice(sepulkaSaved);
public RegistrationResponse register(RegistrationRequest request) {
var now = LocalDateTime.now();
var sepulkaCreated = mapper.toServer(request)
.internalId(randomUUID())
.revision(0)
.createdAt(now)
.updatedAt(now)
.build();
var sepulkaSaved = dao.add(sepulkaCreated);
return mapper.toClient(sepulkaSaved).build();
}

@Override
public List<Sepulka> getSepulkas() {
return dao.getSepulkas();
public List<PreviewResponse> view(PreviewRequest request) {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import smecalculus.bezmen.core.SepulkaService;
import smecalculus.bezmen.messaging.EdgeSide.RegistrationRequest;
import smecalculus.bezmen.messaging.EdgeSide.RegistrationResponse;
import smecalculus.bezmen.validation.EdgeValidator;

@RequiredArgsConstructor
Expand All @@ -18,7 +20,7 @@ public class SepulkaClientImpl implements SepulkaClient {
private SepulkaService service;

@Override
public SepulkaNewResponseEdge register(SepulkaNewRequestEdge requestEdge) {
public RegistrationResponse register(RegistrationRequest requestEdge) {
validator.validate(requestEdge);
var request = mapper.toDomain(requestEdge);
var response = service.register(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import smecalculus.bezmen.messaging.EdgeSide.RegistrationRequest;
import smecalculus.bezmen.messaging.EdgeSide.RegistrationResponse;
import smecalculus.bezmen.messaging.SepulkaClient;
import smecalculus.bezmen.messaging.SepulkaNewRequestEdge;
import smecalculus.bezmen.messaging.SepulkaNewResponseEdge;

@RestController
@RequestMapping("sepulkas")
Expand All @@ -21,7 +21,7 @@ public class SepulkaController {
private SepulkaClient client;

@PostMapping
ResponseEntity<SepulkaNewResponseEdge> register(@RequestBody SepulkaNewRequestEdge requestEdge) {
ResponseEntity<RegistrationResponse> register(@RequestBody RegistrationRequest requestEdge) {
var responseEdge = client.register(requestEdge);
return ResponseEntity.status(HttpStatus.CREATED).body(responseEdge);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package smecalculus.bezmen.storage;

public class ContentionException extends RuntimeException {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package smecalculus.bezmen.storage;

import java.time.LocalDateTime;
import java.util.UUID;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

public abstract class EdgeSide {

@Data
public static class ExistenceState {
UUID internalId;
}

@Data
public static class PreviewState {
String externalId;
LocalDateTime createdAt;
}

@Data
public static class TouchState {
Integer revision;
LocalDateTime updatedAt;
}

@Data
@Table("sepulkas")
public static class AggregateState implements Persistable<UUID> {
@Id
UUID internalId;

@Column
String externalId;

@Column
Integer revision;

@Column
LocalDateTime createdAt;

@Column
LocalDateTime updatedAt;

@Override
public UUID getId() {
return internalId;
}

@Override
public boolean isNew() {
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package smecalculus.bezmen.storage;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import smecalculus.bezmen.core.Sepulka;
import smecalculus.bezmen.core.ServerSide.AggregateState;
import smecalculus.bezmen.core.ServerSide.ExistenceState;
import smecalculus.bezmen.core.ServerSide.PreviewState;
import smecalculus.bezmen.core.ServerSide.TouchState;

/**
* Server side interface
* Port: server side
*/
public interface SepulkaDao {

Optional<Sepulka> getById(UUID id);
AggregateState add(AggregateState state);

Sepulka save(Sepulka sepulka);
Optional<ExistenceState> getBy(String externalId);

List<Sepulka> getSepulkas();
Optional<PreviewState> getBy(UUID internalId);

void updateBy(TouchState state, UUID internalId);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package smecalculus.bezmen.storage;

import static java.util.stream.Collectors.toList;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import smecalculus.bezmen.core.Sepulka;
import smecalculus.bezmen.core.ServerSide.AggregateState;
import smecalculus.bezmen.core.ServerSide.ExistenceState;
import smecalculus.bezmen.core.ServerSide.PreviewState;
import smecalculus.bezmen.core.ServerSide.TouchState;
import smecalculus.bezmen.storage.mybatis.SepulkaSqlMapper;

@RequiredArgsConstructor
Expand All @@ -20,19 +20,28 @@ public class SepulkaDaoMyBatis implements SepulkaDao {
private SepulkaSqlMapper sqlMapper;

@Override
public Optional<Sepulka> getById(@NonNull UUID internalId) {
return sqlMapper.findById(internalId.toString()).map(stateMapper::toDomain);
public AggregateState add(@NonNull AggregateState state) {
var stateEdge = stateMapper.toEdge(state);
sqlMapper.insert(stateEdge);
return state;
}

@Override
public Optional<ExistenceState> getBy(@NonNull String externalId) {
return sqlMapper.findByExternalId(externalId).map(stateMapper::toDomain);
}

@Override
public Sepulka save(@NonNull Sepulka sepulka) {
var sepulkaEdge = stateMapper.toEdge(sepulka);
sqlMapper.insert(sepulkaEdge);
return sepulka;
public Optional<PreviewState> getBy(@NonNull UUID internalId) {
return sqlMapper.findByInternalId(internalId.toString()).map(stateMapper::toDomain);
}

@Override
public List<Sepulka> getSepulkas() {
return sqlMapper.selectAll().stream().map(stateMapper::toDomain).collect(toList());
public void updateBy(TouchState state, UUID internalId) {
var stateEdge = stateMapper.toEdge(state);
var matchedCount = sqlMapper.updateBy(stateEdge, internalId.toString());
if (matchedCount == 0) {
throw new ContentionException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package smecalculus.bezmen.storage;

import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import smecalculus.bezmen.core.Sepulka;
import smecalculus.bezmen.core.ServerSide;
import smecalculus.bezmen.storage.springdata.SepulkaRepository;

@RequiredArgsConstructor
Expand All @@ -21,21 +17,31 @@ public class SepulkaDaoSpringData implements SepulkaDao {
private SepulkaRepository repository;

@Override
public Optional<Sepulka> getById(@NonNull UUID id) {
return repository.findById(id.toString()).map(mapper::toDomain);
public ServerSide.AggregateState add(@NonNull ServerSide.AggregateState state) {
var stateEdge = repository.save(mapper.toEdge(state));
return mapper.toDomain(stateEdge);
}

@Override
public Optional<ServerSide.ExistenceState> getBy(@NonNull String externalId) {
return repository
.findByExternalId(externalId, EdgeSide.ExistenceState.class)
.map(mapper::toDomain);
}

@Override
public Sepulka save(@NonNull Sepulka sepulka) {
var newSepulkaEdge = mapper.toEdge(sepulka);
var savedSepulkaEdge = repository.save(newSepulkaEdge);
return mapper.toDomain(savedSepulkaEdge);
public Optional<ServerSide.PreviewState> getBy(@NonNull UUID internalId) {
return repository
.findByInternalId(internalId.toString(), EdgeSide.PreviewState.class)
.map(mapper::toDomain);
}

@Override
public List<Sepulka> getSepulkas() {
return stream(repository.findAll().spliterator(), false)
.map(mapper::toDomain)
.collect(toList());
public void updateBy(ServerSide.TouchState state, UUID internalId) {
var stateEdge = mapper.toEdge(state);
var matchedCount = repository.updateBy(stateEdge, internalId.toString());
if (matchedCount == 0) {
throw new ContentionException();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package smecalculus.bezmen.storage;

import org.mapstruct.Mapper;
import smecalculus.bezmen.core.Sepulka;
import smecalculus.bezmen.core.ServerSide;
import smecalculus.bezmen.mapping.EdgeMapper;

@Mapper
public interface SepulkaStateMapper {
SepulkaEdge toEdge(Sepulka sepulka);
public interface SepulkaStateMapper extends EdgeMapper {
EdgeSide.AggregateState toEdge(ServerSide.AggregateState state);

Sepulka toDomain(SepulkaEdge sepulkaEdge);
ServerSide.AggregateState toDomain(EdgeSide.AggregateState state);

EdgeSide.TouchState toEdge(ServerSide.TouchState state);

ServerSide.ExistenceState toDomain(EdgeSide.ExistenceState state);

EdgeSide.PreviewState toEdge(ServerSide.PreviewState state);

ServerSide.PreviewState toDomain(EdgeSide.PreviewState state);
}
Loading

0 comments on commit 0c82dde

Please sign in to comment.