From 22df05010bca812727481e32803f8e306c4c9428 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 9 May 2020 21:05:06 +0200 Subject: [PATCH] Expose user resource Add projection withRole Remove TimetrackUser reference from TimetrackAccount --- .../timetrackaccount/TimetrackAccount.java | 8 ++++---- .../hft/geotime/user/BasicUserProjection.java | 18 ++++++++++++++++++ .../de/hft/geotime/user/TimetrackUser.java | 16 ++++++++++++---- .../geotime/user/TimetrackUserRepository.java | 12 ++++++++++-- backend/src/main/resources/data.sql | 8 ++++++++ 5 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 backend/src/main/java/de/hft/geotime/user/BasicUserProjection.java diff --git a/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java b/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java index 7a220f1..9f98630 100644 --- a/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java +++ b/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java @@ -1,11 +1,13 @@ package de.hft.geotime.timetrackaccount; -import de.hft.geotime.user.TimetrackUser; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.*; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; @Data @NoArgsConstructor @@ -16,8 +18,6 @@ public class TimetrackAccount { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; - @OneToOne - private TimetrackUser timetrackUser; // TimetrackUser Id (Lazy) [REMOVE] private double revenue; private String name; private String description; diff --git a/backend/src/main/java/de/hft/geotime/user/BasicUserProjection.java b/backend/src/main/java/de/hft/geotime/user/BasicUserProjection.java new file mode 100644 index 0000000..815a62d --- /dev/null +++ b/backend/src/main/java/de/hft/geotime/user/BasicUserProjection.java @@ -0,0 +1,18 @@ +package de.hft.geotime.user; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.rest.core.config.Projection; + +@Projection(name = "withRole", types = TimetrackUser.class) +public interface BasicUserProjection { + + String getFirstname(); + + String getLastname(); + + String getUsername(); + + @Value("#{target.role.name}") + String getRole(); + +} diff --git a/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java b/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java index 8dcc7c7..66c2cea 100644 --- a/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java +++ b/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java @@ -1,5 +1,6 @@ package de.hft.geotime.user; +import com.fasterxml.jackson.annotation.JsonIgnore; import de.hft.geotime.role.Role; import de.hft.geotime.timetrackaccount.TimetrackAccount; import lombok.AllArgsConstructor; @@ -18,14 +19,21 @@ public class TimetrackUser { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; + @Column(unique = true) private String username; - private String password; // strip + + @JsonIgnore + private String password; + private String firstname; + private String lastname; + @OneToOne - private Role role; // Projection (String) - @OneToMany - private List timetrackAccounts; // Lazy List + private Role role; + + @OneToMany(fetch = FetchType.LAZY) + private List accounts; } diff --git a/backend/src/main/java/de/hft/geotime/user/TimetrackUserRepository.java b/backend/src/main/java/de/hft/geotime/user/TimetrackUserRepository.java index 5eb7367..815a191 100644 --- a/backend/src/main/java/de/hft/geotime/user/TimetrackUserRepository.java +++ b/backend/src/main/java/de/hft/geotime/user/TimetrackUserRepository.java @@ -1,11 +1,19 @@ package de.hft.geotime.user; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; +import org.springframework.data.rest.core.annotation.RestResource; import javax.websocket.server.PathParam; -public interface TimetrackUserRepository extends CrudRepository { +@RepositoryRestResource( + path = "users", + itemResourceRel = "users", + collectionResourceRel = "users" +) +public interface TimetrackUserRepository extends PagingAndSortingRepository { + @RestResource(path = "byUsername", rel = "byUsername") TimetrackUser findFirstByUsername(@PathParam("username") String username); } diff --git a/backend/src/main/resources/data.sql b/backend/src/main/resources/data.sql index 861a41f..7e008c2 100644 --- a/backend/src/main/resources/data.sql +++ b/backend/src/main/resources/data.sql @@ -6,6 +6,10 @@ DELETE FROM role; INSERT INTO role (id, `name`) VALUES (1, 'Admin'); +INSERT INTO timetrack_account (id, description, `name`, revenue) VALUES + (1, 'Gleitzeit Marcel', 'Primary Marcel', 16.0), + (2, 'Festgeld Marcel', 'Secondary Marcel', 25.0); + /* password is the firstname in lowercase e.g. marcel or tobias https://bcrypt-generator.com/ with 10 rounds */ @@ -15,4 +19,8 @@ INSERT INTO timetrack_user (id, firstname, lastname, password, username, role_id (3, 'Tim', 'Zieger' ,'$2y$10$pYGHZhoaelceImO7aIN4nOkWJBp.oqNGFYaRAonHkYF4u9ljqPelC', 'ziti', 1), (4, 'Simon', 'Kellner' ,'$2y$10$Puzm/Nr/Dyq3nQxlkXGIfubS5JPtXJSOf2e6mrQ6HhVYQN9YiQQsC', 'kesi', 1); +INSERT INTO timetrack_user_accounts (timetrack_user_id, accounts_id) VALUES + (1, 1), + (1, 2); + SET FOREIGN_KEY_CHECKS=1; \ No newline at end of file