From 89ba108078395b892c260a2cf6bcc3be721943c8 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Wed, 13 May 2020 15:50:08 +0200 Subject: [PATCH 1/2] Add location implementation --- backend/.idea/compiler.xml | 1 + .../de/hft/geotime/entities/Location.java | 27 +++++++++++++++++++ .../hft/geotime/entities/TimetrackUser.java | 3 +++ .../UserOnlyLocationProjection.java | 19 +++++++++++++ .../repositories/LocationRepository.java | 14 ++++++++++ backend/src/main/resources/data.sql | 13 +++++---- 6 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 backend/src/main/java/de/hft/geotime/entities/Location.java create mode 100644 backend/src/main/java/de/hft/geotime/entities/projections/UserOnlyLocationProjection.java create mode 100644 backend/src/main/java/de/hft/geotime/repositories/LocationRepository.java diff --git a/backend/.idea/compiler.xml b/backend/.idea/compiler.xml index eb684d8..a269fd6 100644 --- a/backend/.idea/compiler.xml +++ b/backend/.idea/compiler.xml @@ -8,6 +8,7 @@ + diff --git a/backend/src/main/java/de/hft/geotime/entities/Location.java b/backend/src/main/java/de/hft/geotime/entities/Location.java new file mode 100644 index 0000000..e8cae67 --- /dev/null +++ b/backend/src/main/java/de/hft/geotime/entities/Location.java @@ -0,0 +1,27 @@ +package de.hft.geotime.entities; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Entity +public class Location { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private double latitude; + private double longitude; + + private int radius; + +} diff --git a/backend/src/main/java/de/hft/geotime/entities/TimetrackUser.java b/backend/src/main/java/de/hft/geotime/entities/TimetrackUser.java index af6deb5..d3b6b8c 100644 --- a/backend/src/main/java/de/hft/geotime/entities/TimetrackUser.java +++ b/backend/src/main/java/de/hft/geotime/entities/TimetrackUser.java @@ -34,4 +34,7 @@ public class TimetrackUser { @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL) private List accounts; + @ManyToOne + private Location location; + } diff --git a/backend/src/main/java/de/hft/geotime/entities/projections/UserOnlyLocationProjection.java b/backend/src/main/java/de/hft/geotime/entities/projections/UserOnlyLocationProjection.java new file mode 100644 index 0000000..6899618 --- /dev/null +++ b/backend/src/main/java/de/hft/geotime/entities/projections/UserOnlyLocationProjection.java @@ -0,0 +1,19 @@ +package de.hft.geotime.entities.projections; + +import de.hft.geotime.entities.TimetrackUser; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.rest.core.config.Projection; + +@Projection(name = "onlyLocation", types = TimetrackUser.class) +public interface UserOnlyLocation { + + @Value("#{target.location.longitude}") + double getLongitude(); + + @Value("#{target.location.latitude}") + double getLatitude(); + + @Value("#{target.location.radius}") + int getRadius(); + +} diff --git a/backend/src/main/java/de/hft/geotime/repositories/LocationRepository.java b/backend/src/main/java/de/hft/geotime/repositories/LocationRepository.java new file mode 100644 index 0000000..b631c34 --- /dev/null +++ b/backend/src/main/java/de/hft/geotime/repositories/LocationRepository.java @@ -0,0 +1,14 @@ +package de.hft.geotime.repositories; + +import de.hft.geotime.entities.Location; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + + +@RepositoryRestResource( + path = "locations", + itemResourceRel = "locations", + collectionResourceRel = "locations" +) +public interface LocationRepository extends PagingAndSortingRepository { +} diff --git a/backend/src/main/resources/data.sql b/backend/src/main/resources/data.sql index 1668db4..49f0053 100644 --- a/backend/src/main/resources/data.sql +++ b/backend/src/main/resources/data.sql @@ -6,14 +6,17 @@ DELETE FROM role; INSERT INTO role (id, `name`) VALUES (1, 'Admin'); +INSERT INTO location (id, latitude, longitude, radius) VALUES + (1, 48.804372, 9.521177, 50); + /* password is the firstname in lowercase e.g. marcel or tobias https://bcrypt-generator.com/ with 10 rounds */ -INSERT INTO timetrack_user (id, firstname, lastname, password, username, role_id) VALUES - (1, 'Marcel', 'Schwarz' ,'$2y$10$pDBv7dEaAiNs5Kr1.8g4XuTFx48zGxJu77rei4TlO.sDOF2yHWxo.', 'scma', 1), - (2, 'Tobias', 'Wieck' ,'$2y$10$Fxj5cGrZblGKjIExvS/MquEE0lgyYo1ILxPgPR2vSiaaLKkqJ.C.u', 'wito', 1), - (3, 'Tim', 'Zieger' ,'$2y$10$pYGHZhoaelceImO7aIN4nOkWJBp.oqNGFYaRAonHkYF4u9ljqPelC', 'ziti', 1), - (4, 'Simon', 'Kellner' ,'$2y$10$Puzm/Nr/Dyq3nQxlkXGIfubS5JPtXJSOf2e6mrQ6HhVYQN9YiQQsC', 'kesi', 1); +INSERT INTO timetrack_user (id, firstname, lastname, password, username, role_id, location_id) VALUES + (1, 'Marcel', 'Schwarz' ,'$2y$10$pDBv7dEaAiNs5Kr1.8g4XuTFx48zGxJu77rei4TlO.sDOF2yHWxo.', 'scma', 1, 1), + (2, 'Tobias', 'Wieck' ,'$2y$10$Fxj5cGrZblGKjIExvS/MquEE0lgyYo1ILxPgPR2vSiaaLKkqJ.C.u', 'wito', 1, 1), + (3, 'Tim', 'Zieger' ,'$2y$10$pYGHZhoaelceImO7aIN4nOkWJBp.oqNGFYaRAonHkYF4u9ljqPelC', 'ziti', 1, 1), + (4, 'Simon', 'Kellner' ,'$2y$10$Puzm/Nr/Dyq3nQxlkXGIfubS5JPtXJSOf2e6mrQ6HhVYQN9YiQQsC', 'kesi', 1, 1); INSERT INTO timetrack_account (description, `name`, revenue, user_id) VALUES ('Gleitzeit Marcel', 'Primary Marcel', 16.0, 1), From 71ee4b9bd96aac52de04a214c64dd0e1c8876264 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Wed, 13 May 2020 15:50:54 +0200 Subject: [PATCH 2/2] Add projection userAllEmbedded to the whoami endpoint --- .../geotime/controllers/UserController.java | 6 ++--- .../UserAllEmbeddedProjection.java | 23 +++++++++++++++++++ .../UserOnlyLocationProjection.java | 2 +- 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 backend/src/main/java/de/hft/geotime/entities/projections/UserAllEmbeddedProjection.java diff --git a/backend/src/main/java/de/hft/geotime/controllers/UserController.java b/backend/src/main/java/de/hft/geotime/controllers/UserController.java index 3a52cb4..e1cd793 100644 --- a/backend/src/main/java/de/hft/geotime/controllers/UserController.java +++ b/backend/src/main/java/de/hft/geotime/controllers/UserController.java @@ -1,7 +1,7 @@ package de.hft.geotime.controllers; import de.hft.geotime.entities.TimetrackUser; -import de.hft.geotime.entities.projections.UserWithRoleProjection; +import de.hft.geotime.entities.projections.UserAllEmbeddedProjection; import de.hft.geotime.repositories.TimetrackUserRepository; import org.springframework.data.projection.ProjectionFactory; import org.springframework.http.HttpStatus; @@ -27,9 +27,9 @@ public class UserController { } @GetMapping("/whoami") - public UserWithRoleProjection getUsername(Authentication authentication) { + public UserAllEmbeddedProjection getUsername(Authentication authentication) { TimetrackUser user = userRepository.findFirstByUsername(authentication.getName()); - return projectionFactory.createProjection(UserWithRoleProjection.class, user); + return projectionFactory.createProjection(UserAllEmbeddedProjection.class, user); } @PostMapping("/sign-up") diff --git a/backend/src/main/java/de/hft/geotime/entities/projections/UserAllEmbeddedProjection.java b/backend/src/main/java/de/hft/geotime/entities/projections/UserAllEmbeddedProjection.java new file mode 100644 index 0000000..535ae5d --- /dev/null +++ b/backend/src/main/java/de/hft/geotime/entities/projections/UserAllEmbeddedProjection.java @@ -0,0 +1,23 @@ +package de.hft.geotime.entities.projections; + +import de.hft.geotime.entities.Location; +import de.hft.geotime.entities.Role; +import de.hft.geotime.entities.TimetrackUser; +import org.springframework.data.rest.core.config.Projection; + +@Projection(name = "allEmbedded", types = TimetrackUser.class) +public interface UserAllEmbeddedProjection { + + long getId(); + + String getFirstname(); + + String getLastname(); + + String getUsername(); + + Role getRole(); + + Location getLocation(); + +} diff --git a/backend/src/main/java/de/hft/geotime/entities/projections/UserOnlyLocationProjection.java b/backend/src/main/java/de/hft/geotime/entities/projections/UserOnlyLocationProjection.java index 6899618..1419a0b 100644 --- a/backend/src/main/java/de/hft/geotime/entities/projections/UserOnlyLocationProjection.java +++ b/backend/src/main/java/de/hft/geotime/entities/projections/UserOnlyLocationProjection.java @@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.data.rest.core.config.Projection; @Projection(name = "onlyLocation", types = TimetrackUser.class) -public interface UserOnlyLocation { +public interface UserOnlyLocationProjection { @Value("#{target.location.longitude}") double getLongitude();