Module Controller Fixed
This commit is contained in:
@@ -110,20 +110,20 @@ public class DataBaseHandler
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
DAYS
|
||||
*/
|
||||
public Day addDay(Day day)
|
||||
/**
|
||||
DAYS
|
||||
*/
|
||||
public Day addDay(Day day)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
st.executeUpdate("insert into days (dayinyear, yearnumber, semesterid) values (" + day.getDayinyear() + ", " + day.getYearnumber() + ", " + day.getSemesterid() + ")");
|
||||
return getDay(day.getDayinyear(), day.getYearnumber(), day.getSemesterid());
|
||||
} catch (SQLException e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
st.executeUpdate("insert into days (dayinyear, yearnumber, semesterid) values (" + day.getDayinyear() + ", " + day.getYearnumber() + ", " + day.getSemesterid() + ")");
|
||||
return getDay(day.getDayinyear(), day.getYearnumber(), day.getSemesterid());
|
||||
} catch (SQLException e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Day getDay(int id)
|
||||
{
|
||||
@@ -172,7 +172,7 @@ public class DataBaseHandler
|
||||
}
|
||||
|
||||
/**
|
||||
ENTRIES
|
||||
ENTRIES
|
||||
*/
|
||||
//method that returns all entries of a day based on the given parameters
|
||||
public Entry[] getEntries(int dayinweek, int weeknumber, int semesterid, User user)
|
||||
@@ -227,7 +227,7 @@ public class DataBaseHandler
|
||||
}
|
||||
|
||||
/**
|
||||
SEMESTER
|
||||
SEMESTER
|
||||
*/
|
||||
//get a semester object by its name
|
||||
public Semester getSemester(String name)
|
||||
@@ -312,7 +312,7 @@ public class DataBaseHandler
|
||||
{
|
||||
try
|
||||
{
|
||||
//st.executeUpdate("DELETE FROM module WHERE semesterId = " + id);
|
||||
st.executeUpdate("DELETE FROM modules WHERE semesterId = " + id);
|
||||
|
||||
st.executeUpdate("delete from semester where id=" + id);
|
||||
}
|
||||
@@ -346,17 +346,6 @@ public class DataBaseHandler
|
||||
MODULE
|
||||
*/
|
||||
|
||||
//get a module object by its name
|
||||
public Module getModule(String name) {
|
||||
try {
|
||||
ResultSet rs = st.executeQuery("SELECT * FROM modules WHERE name = '" + name + "'");
|
||||
return resultSetToModule(rs);
|
||||
}
|
||||
catch (Exception ex){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//get a module object by its id
|
||||
public Module getModule(int id) {
|
||||
try {
|
||||
@@ -368,15 +357,49 @@ public class DataBaseHandler
|
||||
}
|
||||
}
|
||||
|
||||
public Module getModule(int userid, int semesterid, String name)
|
||||
{
|
||||
try
|
||||
{
|
||||
ResultSet rs = st.executeQuery("select * from modules where userid=" + userid + " and semesterid=" + semesterid + " and name='" + name + "'");
|
||||
|
||||
return resultSetToModule(rs);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//get module objects by its semester and its lecturer
|
||||
public Module[] getModules(int userid) {
|
||||
try {
|
||||
ResultSet rs = st.executeQuery("SELECT * FROM modules WHERE userid = " + userid);
|
||||
List<Module> moduleList = new ArrayList<>();
|
||||
Module m = null;
|
||||
while((m = resultSetToModule(rs)) != null)
|
||||
{
|
||||
moduleList.add(m);
|
||||
}
|
||||
|
||||
return moduleList.toArray(new Module[0]);
|
||||
|
||||
}
|
||||
catch (Exception ex){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Module addModule(Module m)
|
||||
{
|
||||
try {
|
||||
st.executeUpdate("INSERT INTO modules (lecturerId, semesterId, name) " +
|
||||
"Values (" + m.getLecturerId() + m.getSemesterId() + m.getName() + ")");
|
||||
return getModule(m.getId());
|
||||
st.executeUpdate("insert into modules (userid, semesterid, name, activated) " +
|
||||
"Values (" + m.getUserid() + ", " + m.getSemesterId() + ", '" + m.getName() + "', " + m.getActivated() + ")");
|
||||
return getModule(m.getUserid(), m.getSemesterId(), m.getName());
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -402,11 +425,11 @@ public class DataBaseHandler
|
||||
|
||||
public Module updateModule(Module m)
|
||||
{
|
||||
System.out.println(m.getActivated());
|
||||
|
||||
try {
|
||||
st.executeUpdate("UPDATE modules SET lecturerId=" + m.getLecturerId() + ", " +
|
||||
"semesterId=" + m.getSemesterId() + ", " +
|
||||
"name='" + m.getName() + "'");
|
||||
return getModule(m.getId());
|
||||
st.executeUpdate("update modules set activated=" + m.getActivated() + " where userid=" + m.getUserid() + " and semesterid=" + m.getSemesterId() + " and name='" + m.getName() + "'");
|
||||
return getModule(m.getUserid(), m.getSemesterId(), m.getName());
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
@@ -430,9 +453,10 @@ public class DataBaseHandler
|
||||
rs.next();
|
||||
return new Module(
|
||||
rs.getInt("id"),
|
||||
rs.getInt("lecturerId"),
|
||||
rs.getInt("semesterId"),
|
||||
rs.getString("name"));
|
||||
rs.getInt("userid"),
|
||||
rs.getInt("semesterid"),
|
||||
rs.getString("name"),
|
||||
rs.getInt("activated"));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
@@ -440,7 +464,7 @@ public class DataBaseHandler
|
||||
}
|
||||
|
||||
/**
|
||||
USER
|
||||
USER
|
||||
*/
|
||||
|
||||
|
||||
|
||||
@@ -2,24 +2,30 @@ package com.example.backend.anwprj.Module;
|
||||
|
||||
//module data class
|
||||
public class Module {
|
||||
private final int id;
|
||||
private final int lecturerId;
|
||||
private final int semesterId;
|
||||
private final String name;
|
||||
private int id;
|
||||
private int userid;
|
||||
private int semesterid;
|
||||
private String name;
|
||||
private int activated;
|
||||
|
||||
public Module(int id, int lecturerId, int semesterId, String name){
|
||||
|
||||
public Module(int id, int userid, int semesterid, String name, int activated)
|
||||
{
|
||||
this.id = id;
|
||||
this.lecturerId = lecturerId;
|
||||
this.semesterId = semesterId;
|
||||
this.userid = userid;
|
||||
this.semesterid = semesterid;
|
||||
this.name = name;
|
||||
this.activated = activated;
|
||||
}
|
||||
|
||||
public int getId() { return id; }
|
||||
|
||||
public int getLecturerId() { return lecturerId; }
|
||||
public int getUserid() { return userid; }
|
||||
|
||||
public int getSemesterId() { return semesterId; }
|
||||
public int getSemesterId() { return semesterid; }
|
||||
|
||||
public String getName() { return name; }
|
||||
|
||||
public int getActivated() { return activated; }
|
||||
|
||||
}
|
||||
|
||||
@@ -17,39 +17,38 @@ public class ModuleController {
|
||||
db = DataBaseHandler.getInstance();
|
||||
}
|
||||
|
||||
@CrossOrigin
|
||||
@GetMapping("/module")
|
||||
public ResponseEntity<Module> 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")
|
||||
// public ResponseEntity<Module> 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<Module> getModule(
|
||||
@RequestParam(name="name") String name,
|
||||
public ResponseEntity<Module[]> getModules(
|
||||
@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);
|
||||
Module[] modules = db.getModules(user.getId());
|
||||
if (modules != null) {
|
||||
return new ResponseEntity<>(modules, HttpStatus.OK);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
|
||||
Reference in New Issue
Block a user