This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from ralf-ueberfuhr-ars/features/db
introduce database access (in progress)
- Loading branch information
Showing
15 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/de/sample/schulung/spring/blog/persistence/BlogPostEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package de.sample.schulung.spring.blog.persistence; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
@Getter | ||
@Setter | ||
@Entity(name = "BlogPost") // Name der Entity (JPQL) | ||
@Table(name = "BLOG_POSTS") | ||
public class BlogPostEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.UUID) | ||
private UUID id; | ||
@Size(min = 3) | ||
@NotNull | ||
private String title; | ||
@Size(min = 10) | ||
@NotNull | ||
private String content; | ||
@Column(name = "TIME_STAMP") | ||
private LocalDateTime timestamp; | ||
|
||
/* | ||
@PrePersist | ||
public void updateTimestamp() { | ||
this.timestamp = LocalDateTime.now(); | ||
} | ||
*/ | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/de/sample/schulung/spring/blog/persistence/BlogPostEntityMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.sample.schulung.spring.blog.persistence; | ||
|
||
import de.sample.schulung.spring.blog.domain.BlogPost; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.MappingTarget; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface BlogPostEntityMapper { | ||
|
||
BlogPost map(BlogPostEntity source); | ||
|
||
BlogPostEntity map(BlogPost source); | ||
|
||
void copy(BlogPostEntity source, @MappingTarget BlogPost target); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/de/sample/schulung/spring/blog/persistence/BlogPostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package de.sample.schulung.spring.blog.persistence; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.UUID; | ||
import java.util.stream.Stream; | ||
|
||
@Repository | ||
public interface BlogPostRepository | ||
extends JpaRepository<BlogPostEntity, UUID> { | ||
|
||
// oder @Query("...") | ||
Stream<BlogPostEntity> streamBlogPostEntitiesByTitleContainingIgnoreCaseOrderByTimestampDesc(String title); | ||
|
||
@Query("select p from BlogPost p where p.title = :title") | ||
Stream<BlogPostEntity> streamIrgendwie(@Param("title") String title); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/de/sample/schulung/spring/blog/persistence/JpaBlogPostSink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package de.sample.schulung.spring.blog.persistence; | ||
|
||
import de.sample.schulung.spring.blog.domain.BlogPost; | ||
import de.sample.schulung.spring.blog.domain.BlogPostSink; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.stream.Stream; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class JpaBlogPostSink implements BlogPostSink { | ||
|
||
private final BlogPostRepository repo; | ||
private final BlogPostEntityMapper mapper; | ||
|
||
@Override | ||
public long count() { | ||
return repo.count(); | ||
} | ||
|
||
@Override | ||
public void create(BlogPost post) { | ||
final var entity = mapper.map(post); | ||
final var savedEntity = repo.save(entity); | ||
//post.setId(savedEntity.getId()); | ||
mapper.copy(savedEntity, post); | ||
} | ||
|
||
@Override | ||
public Stream<BlogPost> findAll() { | ||
return repo.findAll() | ||
.stream() | ||
.map(mapper::map); | ||
} | ||
|
||
@Override | ||
public Optional<BlogPost> findById(UUID id) { | ||
return repo.findById(id) | ||
.map(mapper::map); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters