Added Modules to DBHandler
Datenbankeinträge fehlen noch.
This commit is contained in:
@@ -6,6 +6,7 @@ import com.example.backend.anwprj.Entries.Entry;
|
|||||||
import com.example.backend.anwprj.Semesters.Semester;
|
import com.example.backend.anwprj.Semesters.Semester;
|
||||||
import com.example.backend.anwprj.Users.User;
|
import com.example.backend.anwprj.Users.User;
|
||||||
import com.example.backend.anwprj.Week.Week;
|
import com.example.backend.anwprj.Week.Week;
|
||||||
|
import com.example.backend.anwprj.Module.Module;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.ArrayList;
|
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<Module> 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
|
USER
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ public class Module {
|
|||||||
private final int semesterId;
|
private final int semesterId;
|
||||||
private final String name;
|
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.id = id;
|
||||||
this.lecturerId = userId;
|
this.lecturerId = lecturerId;
|
||||||
this.semesterId = semesterId;
|
this.semesterId = semesterId;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,61 +2,101 @@ package com.example.backend.anwprj.controller;
|
|||||||
|
|
||||||
import com.example.backend.anwprj.DataBaseHandler;
|
import com.example.backend.anwprj.DataBaseHandler;
|
||||||
import com.example.backend.anwprj.Module.Module;
|
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.User;
|
||||||
|
import com.example.backend.anwprj.Users.UserPermissions;
|
||||||
import com.example.backend.anwprj.Users.UserSessionIDHandler;
|
import com.example.backend.anwprj.Users.UserSessionIDHandler;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class ModuleController {
|
public class ModuleController {
|
||||||
// private final DataBaseHandler db;
|
private final DataBaseHandler db;
|
||||||
//
|
|
||||||
// {
|
{
|
||||||
// db = DataBaseHandler.getInstance();
|
db = DataBaseHandler.getInstance();
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @CrossOrigin
|
@CrossOrigin
|
||||||
// @GetMapping("placeholder")
|
@GetMapping("/module")
|
||||||
// public ResponseEntity<Module> getModule(
|
public ResponseEntity<Module> getModule(
|
||||||
// @RequestParam(name="id") int id,
|
@RequestParam(name="id") int id,
|
||||||
// @RequestParam(name="sessionid") String sessionID) {
|
@RequestParam(name="sessionid") String sessionID) {
|
||||||
//
|
|
||||||
// User user = UserSessionIDHandler.getUserBySessionID(sessionID);
|
User user = UserSessionIDHandler.getUserBySessionID(sessionID);
|
||||||
//
|
|
||||||
// if (user != null){
|
if (user != null){
|
||||||
// Module module = db.getModule(id);
|
Module module = db.getModule(id);
|
||||||
// if(module != null) {
|
if(module != null) {
|
||||||
// return new ResponseEntity<>(module, HttpStatus.OK);
|
return new ResponseEntity<>(module, HttpStatus.OK);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return new ResponseEntity<>(HttpStatus.FORBIDDEN);
|
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @CrossOrigin
|
@CrossOrigin
|
||||||
// @GetMapping("placeholder")
|
@GetMapping("/module/id")
|
||||||
// public ResponseEntity<Module> getModule(
|
public ResponseEntity<Module> getModule(
|
||||||
// @RequestParam(name="name") String name,
|
@RequestParam(name="name") String name,
|
||||||
// @RequestParam(name="sessionid") String sessionID) {
|
@RequestParam(name="sessionid") String sessionID) {
|
||||||
//
|
|
||||||
// User user = UserSessionIDHandler.getUserBySessionID(sessionID);
|
User user = UserSessionIDHandler.getUserBySessionID(sessionID);
|
||||||
//
|
|
||||||
// if(user != null)
|
if(user != null)
|
||||||
// {
|
{
|
||||||
// Module module = db.getModule(name);
|
Module module = db.getModule(name);
|
||||||
// if (module != null) {
|
if (module != null) {
|
||||||
// return new ResponseEntity<>(module, HttpStatus.OK);
|
return new ResponseEntity<>(module, HttpStatus.OK);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return new ResponseEntity<>(HttpStatus.FORBIDDEN);
|
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
@CrossOrigin
|
||||||
|
@GetMapping("/module/all")
|
||||||
|
public ResponseEntity<Module[]> 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<Module> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user