Skip to content

Commit

Permalink
Added Sample query to get user permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Wiens committed Jul 10, 2024
1 parent dcae064 commit 4637e83
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import us.dot.its.jpo.ode.api.accessors.postgres.UserRepository;
import us.dot.its.jpo.ode.api.asn1.DecoderManager;
import us.dot.its.jpo.ode.api.models.postgres.User;
import us.dot.its.jpo.ode.api.models.postgres.derived.UserOrgRole;
import us.dot.its.jpo.ode.api.models.postgres.tables.Users;

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -49,18 +53,38 @@ public static void main(String[] args) {
// };
// }

@PersistenceContext
private EntityManager entityManager;

@Autowired
private UserRepository userRepository;

// @Bean
// public void test(){
// System.out.println("Found Users: " + userRepository.count());
// List<User> users = userRepository.findAll();
@Bean
public void test(){
System.out.println("Found Users: " + userRepository.count());
// List<User> users = userRepository.findAll();

// for(User user: users){
// System.out.println(user);
// }
String queryString = String.format(
"SELECT new us.dot.its.jpo.ode.api.models.postgres.derived.UserOrgRole(u.email, o.name, r.name) " +
"FROM Users u JOIN UserOrganization uo on u.user_id = uo.user_id " +
"JOIN Organizations o on uo.organization_id = o.organization_id "+
"JOIN Roles r on uo.role_id = r.role_id " +
"where u.email = '%s' ", "test@gmail.com");

System.out.println(queryString);

// for(User user: users){
// System.out.println(user);
// }
// }
TypedQuery<UserOrgRole> query
= entityManager.createQuery(queryString, UserOrgRole.class);
List<UserOrgRole> resultList = query.getResultList();

System.out.println(resultList.size());
for(UserOrgRole userOrgRole: resultList){
System.out.println(userOrgRole);
}
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import org.springframework.data.jpa.repository.JpaRepository;

import us.dot.its.jpo.ode.api.models.postgres.User;
import us.dot.its.jpo.ode.api.models.postgres.tables.Users;

public interface UserRepository extends JpaRepository<User, UUID> {
public interface UserRepository extends JpaRepository<Users, UUID> {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package us.dot.its.jpo.ode.api.models.postgres.derived;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -9,6 +10,7 @@
@Setter
@EqualsAndHashCode
@Getter
@AllArgsConstructor
public class UserOrgRole {
private String email;
private String organization_name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public class UserOrganization {

@Id
private int user_organization_id;



private UUID user_id;
private int organization_id;
private int role_id;
Expand Down

0 comments on commit 4637e83

Please sign in to comment.