Skip to content

Commit

Permalink
Create Prepared Statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Krayaty committed Apr 30, 2021
1 parent 8392e53 commit efadd57
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 16 deletions.
Binary file modified Documentation/DB/DB-Requests.xlsx
Binary file not shown.
30 changes: 25 additions & 5 deletions backend/src/main/java/org/bffl/controller/MainController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.bffl.controller;

import org.bffl.dbConnector.dao.model.Assigned_target;
import org.bffl.dbConnector.dao.model.Tag;
import org.bffl.dbConnector.services.AppService;
import org.bffl.iamConnector.iamConfig.KeycloakSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -21,15 +21,35 @@ public class MainController {
@Autowired
private AppService app_Service;

@GetMapping("/allShortURLsByGroup")
public ResponseEntity<List<Object>> getAllShortURLsByGroup(@RequestParam("group_name") String group_name){
return this.app_Service.getAllShortURLsWithCurrentTargetByGroupId(group_name);
@GetMapping("/shortURLsByGroup")
public ResponseEntity<List<Object>> getAllShortURLsWithCurrentTargetByGroup(@RequestParam("group_name") String group_name){
return this.app_Service.getAllShortURLsWithCurrentTargetByGroup(group_name);
}

@GetMapping("/groupsOfUser")
public ResponseEntity<List<Object>> getAllGroupsByUser(HttpServletRequest request){
String user_id = KeycloakSecurityConfig.getAccessToken(request).getSubject();
return this.app_Service.getAllGroupsOfGivenUser(user_id);
return this.app_Service.getAllGroupsOfUser(user_id);
}

@GetMapping("/shortURLByID")
public ResponseEntity<List<Object>> getShortURLWithCurrentTargetByID(@RequestParam("short_url_id") int short_url_id){
return this.app_Service.getShortURLWithCurrentTargetByID(short_url_id);
}

@GetMapping("/tagsAssignedToShortURL")
public ResponseEntity<List<Object>> getAllTagsAssignedToShortURL(@RequestParam("short_url_id") int short_url_id){
return this.app_Service.getAllTagsAssignedToShortURL(short_url_id);
}

@GetMapping("/possibleTagsForShortURL")
public ResponseEntity<List<Object>> getAllTagsOfGroup(@RequestParam("short_url_id") int short_url_id){
return this.app_Service.getAllPossibleTagsForShortURL(short_url_id);
}

@GetMapping("/tagsByGroup")
public ResponseEntity<List<Tag>> getAllTagsOfGroup(@RequestParam("group_name") String group_name){
return this.app_Service.getAllTagsOfGroup(group_name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,25 @@ public interface Short_urlRepo extends JpaRepository<Short_url, String> {
") FROM assigned_target X " +
"GROUP BY X.short_url_id" +
") AS T ON S.id = T.short_url_id;")
public List<Object> findAllShortURLsWithCurrentTargetByGroupName(String searched_group_name);
public List<Object> findAllShortURLsWithCurrentTargetByGroup(String searched_group_name);


@Query(nativeQuery = true, value =
"SELECT S.*, T.url, T.assign_timestamp " +
"FROM (" +
"SELECT *" +
"FROM short_url " +
"WHERE id = :searched_short_url_id" +
") AS S INNER JOIN (" +
"SELECT X.short_url_id, MAX(X.assign_timestamp) AS assign_timestamp, (" +
"SELECT url " +
"FROM assigned_target " +
"WHERE short_url_id = X.short_url_id AND assign_timestamp = MAX(X.assign_timestamp)" +
") FROM assigned_target X " +
"WHERE X.short_url_id = :searched_short_url_id " +
"GROUP BY X.short_url_id" +
") AS T ON S.id = T.short_url_id")
public List<Object> findShortURLWithCurrentTargetByID(int searched_short_url_id);

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,27 @@
import java.util.List;

@Repository
public interface TagRepo extends JpaRepository<Tag, String> {}
public interface TagRepo extends JpaRepository<Tag, String> {

@Query(nativeQuery = true, value =
"SELECT T.title, T.description, T.color " +
"FROM url_has_tag UhT, Tag T " +
"WHERE UhT.tag_id = T.id AND UhT.short_url_id = :searched_short_url_id;")
public List<Object> findAllTagsAssignedToShortURL(int searched_short_url_id);


/*@Query(nativeQuery = true, value =
"SELECT id, title, description, color " +
"FROM Tag " +
"WHERE group_name = :searched_group_name")
public List<Object> findAllTagsOfGroup(String searched_group_name);*/

public List<Tag> findAllTagsOfGroup(String searched_group_name);

@Query(nativeQuery = true, value =
"SELECT T.id, T.title, T.description, T.color " +
"FROM Tag T, short_url S " +
"WHERE T.group_name = S.group_name AND S.id = :searched_group_name")
public List<Object> findAllPossibleTagsForShortURL(int searched_short_url_id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public interface User_has_groupRepo extends JpaRepository<User_has_group, Compos
"AND (end_timestamp IS NULL " +
"OR end_timestamp > CURRENT_TIMESTAMP) " +
"ORDER BY end_timestamp DESC;")
public List<Object> findAlLGroupsOfGivenUser(String searched_user_id);
public List<Object> findAlLGroupsOfUser(String searched_user_id);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.bffl.dbConnector.services;

import org.bffl.dbConnector.dao.model.Assigned_target;
import org.bffl.dbConnector.dao.model.Tag;
import org.bffl.dbConnector.dao.repos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -38,20 +38,39 @@ public class AppService {
@Autowired
private User_has_groupRepo user_has_groupRepo;

public ResponseEntity<List<Assigned_target>> findAllTargetURLs() {
return new ResponseEntity<List<Assigned_target>>(this.assigned_targetRepo.findAll(), HttpStatus.OK);
public ResponseEntity<List<Object>> getAllShortURLsWithCurrentTargetByGroup(String group_name) {
return new ResponseEntity<List<Object>>(
this.short_urlRepo.findAllShortURLsWithCurrentTargetByGroup(group_name),
HttpStatus.OK);
}

public ResponseEntity<List<Object>> getAllShortURLsWithCurrentTargetByGroupId(String group_id) {
System.out.println(group_id);
public ResponseEntity<List<Object>> getAllGroupsOfUser(String user_id) {
return new ResponseEntity<List<Object>>(
this.short_urlRepo.findAllShortURLsWithCurrentTargetByGroupName(group_id),
this.user_has_groupRepo.findAlLGroupsOfUser(user_id),
HttpStatus.OK);
}

public ResponseEntity<List<Object>> getAllGroupsOfGivenUser(String user_id) {
public ResponseEntity<List<Object>> getShortURLWithCurrentTargetByID(int short_url_id){
return new ResponseEntity<List<Object>>(
this.user_has_groupRepo.findAlLGroupsOfGivenUser(user_id),
this.short_urlRepo.findShortURLWithCurrentTargetByID(short_url_id),
HttpStatus.OK);
}

public ResponseEntity<List<Object>> getAllTagsAssignedToShortURL(int short_url_id){
return new ResponseEntity<List<Object>>(
this.tagRepo.findAllTagsAssignedToShortURL(short_url_id),
HttpStatus.OK);
}

public ResponseEntity<List<Object>> getAllPossibleTagsForShortURL(int short_url_id){
return new ResponseEntity<List<Object>>(
this.tagRepo.findAllPossibleTagsForShortURL(short_url_id),
HttpStatus.OK);
}

public ResponseEntity<List<Tag>> getAllTagsOfGroup(String group_name){
return new ResponseEntity<List<Tag>>(
this.tagRepo.findAllTagsOfGroup(group_name),
HttpStatus.OK);
}

Expand Down

0 comments on commit efadd57

Please sign in to comment.