From e92712d1d676e333625f6348283d29623068e2ab Mon Sep 17 00:00:00 2001 From: AlexE030 Date: Sat, 20 May 2023 00:45:58 +0200 Subject: [PATCH] Added Modules to DBHandler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Datenbankeinträge fehlen noch. --- .../backend/anwprj/DataBaseHandler.java | 98 ++++++++++++ .../example/backend/anwprj/Module/Module.java | 4 +- .../anwprj/controller/ModuleController.java | 140 +++++++++++------- 3 files changed, 190 insertions(+), 52 deletions(-) diff --git a/backend/src/main/java/com/example/backend/anwprj/DataBaseHandler.java b/backend/src/main/java/com/example/backend/anwprj/DataBaseHandler.java index 0ec212f..280063e 100644 --- a/backend/src/main/java/com/example/backend/anwprj/DataBaseHandler.java +++ b/backend/src/main/java/com/example/backend/anwprj/DataBaseHandler.java @@ -6,6 +6,7 @@ import com.example.backend.anwprj.Entries.Entry; import com.example.backend.anwprj.Semesters.Semester; import com.example.backend.anwprj.Users.User; import com.example.backend.anwprj.Week.Week; +import com.example.backend.anwprj.Module.Module; import java.sql.*; import java.util.ArrayList; @@ -339,6 +340,103 @@ public class DataBaseHandler } } + /** + MODULE + */ + + //get a semester object by its name + public Module getModule(String name) { + try { + ResultSet rs = st.executeQuery("SELECT * FROM SEMESTER WHERE name = '" + name + "'"); + return resultSetToModule(rs); + } + catch (Exception ex){ + return null; + } + } + + //get a semester object by its id + public Module getModule(int id) { + try { + ResultSet rs = st.executeQuery("SELECT * FROM Module WHERE id=" + id); + return resultSetToModule(rs); + } + catch (Exception ex) { + return null; + } + } + + public Module addModule(Module m) + { + try { + st.executeUpdate("INSERT INTO Module (lecturerId, semesterId, name) " + + "Values (" + m.getLecturerId() + m.getSemesterId() + m.getName() + ")"); + return getModule(m.getId()); + + } + catch (Exception e) { + return null; + } + } + + public Module[] getAllModules() { + try + { + ResultSet rs = st.executeQuery("SELECT * FROM semester"); + List moduleList = new ArrayList<>(); + Module m = null; + while((m = resultSetToModule(rs)) != null) + { + moduleList.add(m); + } + + return moduleList.toArray(new Module[0]); + } + catch(Exception e) + { + return null; + } + } + + public Module updateModule(Module m) + { + try { + st.executeUpdate("UPDATE module SET lecturerId=" + m.getLecturerId() + ", " + + "semesterId=" + m.getSemesterId() + ", " + + "name='" + m.getName() + "'"); + return getModule(m.getId()); + } + catch (SQLException ex) { + ex.printStackTrace(); + return null; + } + } + + public boolean deleteModule(int id) + { + try { + st.executeUpdate("DELETE FROM module WHERE id=" + id); + return true; + } + catch(Exception ex) { + return false; + } + } + + public Module resultSetToModule(ResultSet rs) { + try { + rs.next(); + return new Module( + rs.getInt("id"), + rs.getInt("lecturerId"), + rs.getInt("semesterId"), + rs.getString("name")); + } + catch (Exception ex) { + return null; + } + } + /** USER */ diff --git a/backend/src/main/java/com/example/backend/anwprj/Module/Module.java b/backend/src/main/java/com/example/backend/anwprj/Module/Module.java index 83388a9..4046fc8 100644 --- a/backend/src/main/java/com/example/backend/anwprj/Module/Module.java +++ b/backend/src/main/java/com/example/backend/anwprj/Module/Module.java @@ -7,9 +7,9 @@ public class Module { private final int semesterId; private final String name; - public Module(int id, int userId, int semesterId, String name){ + public Module(int id, int lecturerId, int semesterId, String name){ this.id = id; - this.lecturerId = userId; + this.lecturerId = lecturerId; this.semesterId = semesterId; this.name = name; } diff --git a/backend/src/main/java/com/example/backend/anwprj/controller/ModuleController.java b/backend/src/main/java/com/example/backend/anwprj/controller/ModuleController.java index 830eca6..3038e37 100644 --- a/backend/src/main/java/com/example/backend/anwprj/controller/ModuleController.java +++ b/backend/src/main/java/com/example/backend/anwprj/controller/ModuleController.java @@ -2,61 +2,101 @@ package com.example.backend.anwprj.controller; import com.example.backend.anwprj.DataBaseHandler; import com.example.backend.anwprj.Module.Module; +import com.example.backend.anwprj.Semesters.Semester; import com.example.backend.anwprj.Users.User; +import com.example.backend.anwprj.Users.UserPermissions; import com.example.backend.anwprj.Users.UserSessionIDHandler; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController public class ModuleController { -// private final DataBaseHandler db; -// -// { -// db = DataBaseHandler.getInstance(); -// } -// -// @CrossOrigin -// @GetMapping("placeholder") -// public ResponseEntity getModule( -// @RequestParam(name="id") int id, -// @RequestParam(name="sessionid") String sessionID) { -// -// User user = UserSessionIDHandler.getUserBySessionID(sessionID); -// -// if (user != null){ -// Module module = db.getModule(id); -// if(module != null) { -// return new ResponseEntity<>(module, HttpStatus.OK); -// } -// -// return new ResponseEntity<>(HttpStatus.NOT_FOUND); -// } -// -// return new ResponseEntity<>(HttpStatus.FORBIDDEN); -// } -// -// @CrossOrigin -// @GetMapping("placeholder") -// public ResponseEntity getModule( -// @RequestParam(name="name") String name, -// @RequestParam(name="sessionid") String sessionID) { -// -// User user = UserSessionIDHandler.getUserBySessionID(sessionID); -// -// if(user != null) -// { -// Module module = db.getModule(name); -// if (module != null) { -// return new ResponseEntity<>(module, HttpStatus.OK); -// } -// -// return new ResponseEntity<>(HttpStatus.NOT_FOUND); -// } -// -// return new ResponseEntity<>(HttpStatus.FORBIDDEN); -// } + private final DataBaseHandler db; + + { + db = DataBaseHandler.getInstance(); + } + + @CrossOrigin + @GetMapping("/module") + public ResponseEntity getModule( + @RequestParam(name="id") int id, + @RequestParam(name="sessionid") String sessionID) { + + User user = UserSessionIDHandler.getUserBySessionID(sessionID); + + if (user != null){ + Module module = db.getModule(id); + if(module != null) { + return new ResponseEntity<>(module, HttpStatus.OK); + } + + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + return new ResponseEntity<>(HttpStatus.FORBIDDEN); + } + + @CrossOrigin + @GetMapping("/module/id") + public ResponseEntity getModule( + @RequestParam(name="name") String name, + @RequestParam(name="sessionid") String sessionID) { + + User user = UserSessionIDHandler.getUserBySessionID(sessionID); + + if(user != null) + { + Module module = db.getModule(name); + if (module != null) { + return new ResponseEntity<>(module, HttpStatus.OK); + } + + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + return new ResponseEntity<>(HttpStatus.FORBIDDEN); + } + + @CrossOrigin + @GetMapping("/module/all") + public ResponseEntity getModule(@RequestParam(name="sessionid") String sessionid) { + User u = UserSessionIDHandler.getUserBySessionID(sessionid); + + if(u != null) { + if(u.getPermissions() == UserPermissions.admin.getValue()) { + return new ResponseEntity<>(db.getAllModules(), HttpStatus.OK); + } + } + + return null; + } + + @CrossOrigin + @PutMapping("/module") + public ResponseEntity updateModule(@RequestBody Module module, @RequestParam(name="sessionid") String sessionID) { + User u = UserSessionIDHandler.getUserBySessionID(sessionID); + if(u != null && u.getPermissions() == UserPermissions.admin.getValue()) { + Module m = db.updateModule(module); + + if(m != null) { + return new ResponseEntity<>(m, HttpStatus.OK); + } + + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + return new ResponseEntity<>(HttpStatus.FORBIDDEN); + } + + @CrossOrigin + @DeleteMapping("/module") + public void deleteModule(@RequestParam(name="sessionid") String sessionid, @RequestParam(name="id") int id) { + User u = UserSessionIDHandler.getUserBySessionID(sessionid); + if(u != null && u.getPermissions() == UserPermissions.admin.getValue()) { + db.deleteModule(id); + } + + } }