-
-
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 #15 from penguineer/groups
Add Group handling
- Loading branch information
Showing
14 changed files
with
555 additions
and
13 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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/penguineering/gartenplus/auth/group/GroupDTO.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,14 @@ | ||
package com.penguineering.gartenplus.auth.group; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.UUID; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
public record GroupDTO( | ||
@JsonProperty("id") UUID id, | ||
@JsonProperty("name") String name, | ||
@JsonProperty("description") String description | ||
) { | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/penguineering/gartenplus/auth/group/GroupEntity.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,30 @@ | ||
package com.penguineering.gartenplus.auth.group; | ||
|
||
import com.penguineering.gartenplus.auth.user.UserEntity; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.ManyToMany; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.data.jpa.domain.AbstractPersistable; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Getter | ||
@Setter | ||
@Entity(name = "systemgroup") | ||
public class GroupEntity extends AbstractPersistable<UUID> { | ||
String name; | ||
|
||
String description; | ||
|
||
@ManyToMany( | ||
mappedBy = "groups", | ||
fetch = jakarta.persistence.FetchType.LAZY | ||
) | ||
List<UserEntity> users; | ||
|
||
public GroupDTO toDTO() { | ||
return new GroupDTO(getId(), name, description); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/penguineering/gartenplus/auth/group/GroupRepository.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,10 @@ | ||
package com.penguineering.gartenplus.auth.group; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public interface GroupRepository extends CrudRepository<GroupEntity, UUID> { | ||
Optional<GroupEntity> findByName(String name); | ||
} |
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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/penguineering/gartenplus/auth/user/UserEntityService.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 com.penguineering.gartenplus.auth.user; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.transaction.Transactional; | ||
import org.hibernate.Hibernate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Service | ||
public class UserEntityService { | ||
private final UserRepository userRepository; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
public UserEntityService(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@Transactional | ||
public Optional<UserEntity> getUser(UUID userId) { | ||
return Optional | ||
.ofNullable(userId) | ||
.flatMap(userRepository::findById); | ||
} | ||
|
||
@Transactional | ||
public Optional<UserEntity> getUserWithGroups(UUID userId) { | ||
return userRepository | ||
.findById(userId) | ||
.map(u -> { | ||
Hibernate.initialize(u.getGroups()); | ||
return u; | ||
}); | ||
} | ||
|
||
@Transactional | ||
public UserEntity save(UserEntity user) { | ||
return userRepository.save(user); | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
src/main/java/com/penguineering/gartenplus/ui/content/admin/settings/SettingsLayout.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,78 @@ | ||
package com.penguineering.gartenplus.ui.content.admin.settings; | ||
|
||
import com.penguineering.gartenplus.ui.content.admin.AdminLayout; | ||
import com.penguineering.gartenplus.ui.content.admin.settings.groups.GroupSettingsPage; | ||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.HasElement; | ||
import com.vaadin.flow.component.UI; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.orderedlayout.VerticalLayout; | ||
import com.vaadin.flow.component.tabs.Tab; | ||
import com.vaadin.flow.component.tabs.Tabs; | ||
import com.vaadin.flow.router.*; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@ParentLayout(AdminLayout.class) | ||
@RoutePrefix(value = "settings") | ||
public class SettingsLayout extends VerticalLayout implements RouterLayout, BeforeEnterObserver { | ||
private static final Map<String, Class<? extends Component>> targets = new LinkedHashMap<>(); | ||
static { | ||
targets.put("Gruppen", GroupSettingsPage.class); | ||
} | ||
|
||
private final Div content; | ||
private final Tabs menu; | ||
|
||
public SettingsLayout() { | ||
super(); | ||
|
||
menu = new Tabs(); | ||
targets.keySet().stream() | ||
.map(Tab::new) | ||
.forEach(menu::add); | ||
menu.addSelectedChangeListener(this::navigateToTab); | ||
add(menu); | ||
|
||
content = new Div(); | ||
content.setSizeFull(); | ||
content.getStyle() | ||
.set("margin", "0") | ||
.set("padding", "0"); | ||
add(content); | ||
} | ||
|
||
private void navigateToTab(Tabs.SelectedChangeEvent event) { | ||
Optional.of(event.getSelectedTab()) | ||
.map(Tab::getLabel) | ||
.map(targets::get) | ||
.ifPresent(target -> UI.getCurrent().navigate(target)); | ||
} | ||
|
||
@Override | ||
public void beforeEnter(BeforeEnterEvent event) { | ||
String currentPath = event.getLocation().getPath(); | ||
targets.forEach((label, target) -> { | ||
Route route = target.getAnnotation(Route.class); | ||
if (route != null && currentPath.contains(route.value())) { | ||
menu.setSelectedTab(menu.getChildren() | ||
.filter(component -> component instanceof Tab) | ||
.map(component -> (Tab) component) | ||
.filter(tab -> tab.getLabel().equals(label)) | ||
.findFirst() | ||
.orElse(null)); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void showRouterLayoutContent(HasElement newContent) { | ||
// Previous content is automatically removed | ||
|
||
newContent.getElement() | ||
.getComponent() | ||
.ifPresent(content::add); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/penguineering/gartenplus/ui/content/admin/settings/SettingsPage.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,18 @@ | ||
package com.penguineering.gartenplus.ui.content.admin.settings; | ||
|
||
import com.penguineering.gartenplus.ui.appframe.GartenplusPage; | ||
import com.vaadin.flow.router.BeforeEnterEvent; | ||
import com.vaadin.flow.router.BeforeEnterObserver; | ||
import com.vaadin.flow.router.PageTitle; | ||
import com.vaadin.flow.router.Route; | ||
import jakarta.annotation.security.PermitAll; | ||
|
||
@Route(value = "", layout = SettingsLayout.class) | ||
@PermitAll | ||
@PageTitle("GartenPlus | Einstellungen") | ||
public class SettingsPage extends GartenplusPage implements BeforeEnterObserver { | ||
@Override | ||
public void beforeEnter(BeforeEnterEvent event) { | ||
event.getUI().navigate(event.getLocation().getPath() + "/groups"); | ||
} | ||
} |
Oops, something went wrong.