Fakultas Ilmu Komputer UI

Commit 90223aed authored by Ahmad Izzudin Alifyandra's avatar Ahmad Izzudin Alifyandra

Merge branch 'alif' into 'master'

Alif

See merge request !8
parents 9acc477a e3029a06
Pipeline #81842 passed with stages
in 24 minutes and 36 seconds
TASC
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="tasc@localhost" uuid="ef3070c2-6a00-4d98-a2dc-be897ace320c">
<driver-ref>postgresql</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
<jdbc-url>jdbc:postgresql://localhost:5432/tasc</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="PROJECT" dialect="PostgreSQL" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -31,10 +31,6 @@ repositories {
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-security'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
......
......@@ -14,4 +14,5 @@ public interface AppointmentDAO {
void createAppointment(AppointmentRequest request);
void updateStatus(AppointmentRequest request, AppointmentRequest.Status status);
void updateAdminHasPermission(AppointmentRequest request, boolean permission);
}
......@@ -3,26 +3,20 @@ package com.adpro.tasc.appointment.db.mapper;
import com.adpro.tasc.appointment.db.model.Appointment;
import com.adpro.tasc.appointment.db.model.AppointmentRequest;
import com.adpro.tasc.appointment.db.model.Course;
import com.adpro.tasc.user.db.dao.UserDAO;
import com.adpro.tasc.user.db.model.User;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AppointmentMapper implements RowMapper<AppointmentRequest> {
private UserDAO userDB;
public AppointmentMapper(UserDAO userDB) {
this.userDB = userDB;
}
@Override
public AppointmentRequest mapRow(ResultSet rs, int rowNum) throws SQLException {
AppointmentRequest request = new AppointmentRequest();
request.setId(rs.getInt("id"));
request.setStudent(userDB.getUser(rs.getString("student")));
request.setTa(userDB.getUser(rs.getString("ta")));
request.setStudent(new User(rs.getString("student"), null, null, null));
request.setTa(new User(rs.getString("ta"), null, null, null));
request.setRequestTime(rs.getLong("request_time"));
request.setStatus(AppointmentRequest.Status.valueOf(rs.getString("status")));
request.setAdminHasPermission(rs.getBoolean("admin_permission"));
......
package com.adpro.tasc.appointment.db.mapper;
import com.adpro.tasc.appointment.db.model.Schedule;
import com.adpro.tasc.user.db.dao.UserDAO;
import com.adpro.tasc.user.db.model.User;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ScheduleMapper implements RowMapper<Schedule> {
private UserDAO userDB;
public ScheduleMapper(UserDAO userDB) {
this.userDB = userDB;
}
@Override
public Schedule mapRow(ResultSet rs, int rowNum) throws SQLException {
Schedule schedule = new Schedule();
schedule.setId(rs.getInt("id"));
schedule.setUser(userDB.getUser(rs.getString("user")));
schedule.setUser(new User(rs.getString("user"), null, null, null));
schedule.setAdminHasPermission(rs.getBoolean("admin_permission"));
return schedule;
......
......@@ -28,6 +28,24 @@ public class AppointmentTemplate implements AppointmentDAO {
this.template = template;
}
private AppointmentRequest updateQuery(AppointmentRequest request) {
User student = userDB.getUser(request.getStudent().getUserName());
request.setStudent(student);
User ta = userDB.getUser(request.getTa().getUserName());
request.setTa(ta);
return request;
}
private List<AppointmentRequest> updateQuery(List<AppointmentRequest> request) {
for(int i = 0; i < request.size(); i++) {
request.set(i, updateQuery(request.get(i)));
}
return request;
}
@Override
public List<AppointmentRequest> getAllAppointment() {
String sql = """
......@@ -35,7 +53,7 @@ public class AppointmentTemplate implements AppointmentDAO {
from appointment_request
""";
return template.query(sql, new AppointmentMapper(userDB));
return updateQuery(template.query(sql, new AppointmentMapper()));
}
@Override
......@@ -46,7 +64,7 @@ public class AppointmentTemplate implements AppointmentDAO {
where course=?
""";
return template.query(sql, new AppointmentMapper(userDB), course.getName());
return updateQuery(template.query(sql, new AppointmentMapper(), course.getName()));
}
@Override
......@@ -57,7 +75,7 @@ public class AppointmentTemplate implements AppointmentDAO {
where student=?
""";
List<AppointmentRequest> requests = template.query(sql, new AppointmentMapper(userDB), user.getUserName());
List<AppointmentRequest> requests = template.query(sql, new AppointmentMapper(), user.getUserName());
if(requests.isEmpty()) {
sql = """
select *
......@@ -65,10 +83,10 @@ public class AppointmentTemplate implements AppointmentDAO {
where ta=?
""";
requests = template.query(sql, new AppointmentMapper(userDB), user.getUserName());
requests = template.query(sql, new AppointmentMapper(), user.getUserName());
}
return requests;
return updateQuery(requests);
}
@Override
......@@ -80,8 +98,8 @@ public class AppointmentTemplate implements AppointmentDAO {
Appointment appointment = request.getAppointment();
template.update(sql,
request.getStudent(), request.getTa(), request.getRequestTime(),
appointment.getDate(), appointment.getDuration(), appointment.getCourse());
request.getStudent().getUserName(), request.getTa().getUserName(), request.getRequestTime(),
appointment.getDate(), appointment.getDuration(), appointment.getCourse().getName());
}
@Override
......@@ -94,4 +112,15 @@ public class AppointmentTemplate implements AppointmentDAO {
template.update(sql, status.toString(), request.getId());
}
@Override
public void updateAdminHasPermission(AppointmentRequest request, boolean permission) {
String sql = """
update appointment_request
set admin_permission=?
where id=?
""";
template.update(sql, permission, request.getId());
}
}
......@@ -3,10 +3,8 @@ package com.adpro.tasc.appointment.db.template;
import com.adpro.tasc.appointment.db.dao.ScheduleDAO;
import com.adpro.tasc.appointment.db.dao.SlotDAO;
import com.adpro.tasc.appointment.db.mapper.ScheduleMapper;
import com.adpro.tasc.appointment.db.mapper.SlotMapper;
import com.adpro.tasc.appointment.db.model.Schedule;
import com.adpro.tasc.appointment.db.model.Slot;
import com.adpro.tasc.user.db.dao.UserDAO;
import com.adpro.tasc.user.db.model.AcademicUser;
import com.adpro.tasc.user.db.model.Role;
import com.adpro.tasc.user.db.model.User;
......@@ -20,7 +18,6 @@ import java.util.List;
@Service
public class ScheduleTemplate implements ScheduleDAO {
private JdbcTemplate template;
private UserDAO userDB;
private SlotDAO slotDB;
@Autowired
......@@ -28,11 +25,6 @@ public class ScheduleTemplate implements ScheduleDAO {
this.slotDB = slotDB;
}
@Autowired
public void setUserDB(UserDAO userDB) {
this.userDB = userDB;
}
@Autowired
public void setTemplate(JdbcTemplate template) {
this.template = template;
......@@ -46,7 +38,9 @@ public class ScheduleTemplate implements ScheduleDAO {
where schedule.user=?
""";
try {
Schedule schedule = template.queryForObject(sql, new ScheduleMapper(userDB), user.getUserName());
Schedule schedule = template.queryForObject(sql, new ScheduleMapper(), user.getUserName());
schedule.setUser(user);
List<Slot> slots = slotDB.getAll(schedule);
schedule.setAvailableSlots(slots);
......@@ -65,7 +59,9 @@ public class ScheduleTemplate implements ScheduleDAO {
where schedule.user=?
""";
try {
Schedule schedule = template.queryForObject(sql, new ScheduleMapper(userDB), user.getUserName());
Schedule schedule = template.queryForObject(sql, new ScheduleMapper(), user.getUserName());
schedule.setUser(user);
List<Slot> slots = slotDB.getByDay(schedule, day);
schedule.setAvailableSlots(slots);
......
package com.adpro.tasc.config;
import org.postgresql.ds.PGSimpleDataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -21,14 +22,18 @@ public class DBConfig {
@Value("${spring.datasource.currentSchema}")
private String databaseSchema;
@Value("${spring.datasource.hikari.maximum-pool-size}")
private int maxPoolSize;
@Bean
public DataSource dataSource() {
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setUrl(databaseUrl);
dataSource.setUser(databaseUsername);
dataSource.setPassword(databasePassword);
dataSource.setCurrentSchema(databaseSchema);
return dataSource;
HikariConfig config = new HikariConfig();
config.setJdbcUrl(databaseUrl);
config.setUsername(databaseUsername);
config.setPassword(databasePassword);
config.addDataSourceProperty("currentSchema", databaseSchema);
config.setMaximumPoolSize(maxPoolSize);
return new HikariDataSource(config);
}
}
......@@ -4,17 +4,15 @@ import com.adpro.tasc.user.db.dao.UserDAO;
import com.adpro.tasc.user.db.model.AcademicUser;
import com.adpro.tasc.user.db.model.Role;
import com.adpro.tasc.user.db.model.User;
import org.apache.tomcat.util.http.parser.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.security.Principal;
@Controller
@PreAuthorize("hasRole('ADMIN')")
......@@ -23,33 +21,27 @@ public class AddRolesController {
private UserDAO userDAO;
@GetMapping("/add-roles")
public String addRoles(Model model) {
public String addRoles(Model model, Principal principal) {
User currentUser = userDAO.getUser(principal.getName());
model.addAttribute("currentUser", currentUser);
model.addAttribute("userList", userDAO.getAllUser());
return "add_roles";
}
@PostMapping(value = "/add-roles/student")
public String registerStudent(Model model, @RequestParam ("userName") String userName,
@RequestParam("fullName") String fullName,
@RequestParam("password") String password, @RequestParam("index") int index) {
public String registerStudent(Model model, @RequestParam ("userName") String userName) {
AcademicUser student = new AcademicUser(userDAO.getUser(userName));
student.setRole(Role.ROLE_STUDENT);
userDAO.updateUser(userName, student);
model.addAttribute("user", student);
return "redirect:/add-roles";
}
@PostMapping(value = "/add-roles/ta")
public String registerTA(Model model, @RequestParam ("userNameTA") String userName,
@RequestParam("fullNameTA") String fullName,
@RequestParam("passwordTA") String password, @RequestParam("indexTA") int index) {
public String registerTA(Model model, @RequestParam ("userNameTA") String userName) {
AcademicUser ta = new AcademicUser(userDAO.getUser(userName));
ta.setRole(Role.ROLE_TEACHING_ASSISTANT);
userDAO.updateUser(userName, ta);
model.addAttribute("user", ta);
return "redirect:/add-roles";
}
......
......@@ -3,16 +3,15 @@ package com.adpro.tasc.controller;
import com.adpro.tasc.appointment.db.dao.CourseDAO;
import com.adpro.tasc.appointment.db.model.Course;
import com.adpro.tasc.user.db.dao.UserDAO;
import com.adpro.tasc.user.db.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.List;
import java.security.Principal;
@Controller
public class CreateCoursesController {
......@@ -23,7 +22,9 @@ public class CreateCoursesController {
CourseDAO courseDAO;
@GetMapping(value="/create-course")
public String createCourseForm(Model model) {
public String createCourseForm(Model model, Principal principal) {
User currentUser = userDAO.getUser(principal.getName());
model.addAttribute("currentUser", currentUser);
model.addAttribute("courses", courseDAO.getAllCourse());
return "create_course";
}
......@@ -32,9 +33,14 @@ public class CreateCoursesController {
public String createCourse(@RequestParam("name") String name) {
Course newCourse = new Course();
newCourse.setName(name);
courseDAO.createCourse(newCourse);
return "redirect:/create-course";
}
@PostMapping(value="/create-course/delete")
public String deleteCourse(@RequestParam("name") String name) {
Course course = courseDAO.getCourseByName(name);
courseDAO.deleteCourse(course);
return "redirect:/create-course";
}
}
\ No newline at end of file
......@@ -35,7 +35,6 @@ public class CreateScheduleController {
private String[] daysArr = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY","FRIDAY","SATURDAY","SUNDAY"};
private List<Slot> slots = new ArrayList<>();
// private final SlotRepository slots;
@GetMapping(value="/create-schedule")
public String displaySchedule(Model model, Principal principal) {
......@@ -57,44 +56,77 @@ public class CreateScheduleController {
model.addAttribute("days",days);
model.addAttribute("slots",schedule.getAvailableSlots());
model.addAttribute("currentUser", currentUser);
return "display_schedule";
}
@GetMapping(value="/add-slot")
public String addSlotForm(Model model) {
public String addSlotForm(Model model, Principal principal) {
User currentUser = userDAO.getUser(principal.getName());
model.addAttribute("currentUser", currentUser);
model.addAttribute("days",daysArr);
return "create_slot";
}
@PostMapping(value="/add-slot")
@PostMapping(value="/add-slot/post")
public String createSlot(
@RequestParam("day") int day,
@RequestParam("startHour") int startHour,
@RequestParam("startMinute") int startMinute,
@RequestParam("endHour") int endHour,
@RequestParam("endMinute") int endMinute
@RequestParam("startTime") String startTime,
@RequestParam("endTime") String endTime,
Principal principal
)
{
System.out.println(day+" "+startHour+ " " + startMinute);
long epochStart = java.time.Duration.ofMinutes(startMinute).toMillis() +
java.time.Duration.ofHours(startHour).toMillis();
try {
String[] splittedStartTime = startTime.split("\\.");
String[] splittedEndTime = endTime.split("\\.");
if (splittedStartTime.length != 2 || splittedEndTime.length != 2) {
throw new Exception("");
}
int startHour = Integer.parseInt(splittedStartTime[0]);
int startMinute = Integer.parseInt(splittedStartTime[1]);
int endHour = Integer.parseInt(splittedEndTime[0]);
int endMinute = Integer.parseInt(splittedEndTime[1]);
long epochStart = java.time.Duration.ofMinutes(startMinute).toMillis() +
java.time.Duration.ofHours(startHour).toMillis();
long epochEnd = java.time.Duration.ofMinutes(endMinute).toMillis() +
java.time.Duration.ofHours(endHour).toMillis();
if (
!(
startHour >= 0 && startHour < 24 &&
startMinute >= 0 && startMinute < 60 &&
endHour >= 0 && endHour < 24 &&
endMinute >= 0 && endMinute < 60 &&
epochStart < epochEnd
)
) {
throw new Exception("");
}
long epochEnd = java.time.Duration.ofMinutes(endMinute).toMillis() +
java.time.Duration.ofHours(endHour).toMillis();
Slot slot1 = new Slot();
slot1.setDay(Slot.Day.valueOf(daysArr[day]));
slot1.setStartTime(epochStart);
slot1.setFinishTime(epochEnd);
Slot slot1 = new Slot();
slot1.setDay(Slot.Day.valueOf(daysArr[day]));
slot1.setStartTime(epochStart);
slot1.setFinishTime(epochEnd);
User currentUser = userDAO.getUser(principal.getName());
Schedule schedule = scheduleDAO.getUserSchedule((AcademicUser) currentUser);
slot1.setSchedule(schedule.getId());
// slots.add(slot1);
slotDAO.addSlot(slot1);
slotDAO.addSlot(slot1);
return "redirect:/create-schedule";
} catch (Exception e) {
return "redirect:/add-slot";
}
return "redirect:/create-schedule";
}
}
......@@ -10,7 +10,8 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.adpro.tasc.user.db.model.Role;
import java.security.Principal;
@Controller
public class HelloController {
......@@ -22,11 +23,6 @@ public class HelloController {
return "index";
}
@GetMapping("/home")
public String homePage() {
return "home";
}
@GetMapping("/register")
public String registerPage() {
return "register";
......@@ -37,25 +33,62 @@ public class HelloController {
@RequestParam("fullname") String fullName,
@RequestParam("password") String password) {
userDAO.createUser(new User(userName, fullName, "{noop}"+password, Role.ROLE_UNASSIGNED));
return "redirect:/";
return "redirect:/waiting";
}
@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/home-admin")
public String adminPage() {
return "homeAdmin";
public String adminPage(Model model, Principal principal) {
User currentUser = userDAO.getUser(principal.getName());
model.addAttribute("currentUser", currentUser);
return "home";
}
@PreAuthorize("hasRole('TEACHING_ASSISTANT')")
@GetMapping("/home-TA")
public String TAPage() {
return "homeTA";
public String TAPage(Model model, Principal principal) {
User currentUser = userDAO.getUser(principal.getName());
model.addAttribute("currentUser", currentUser);
return "home";
}
@PreAuthorize("hasRole('STUDENT')")
@GetMapping("/home-student")
public String StudentPage() {
return "homeStudent";
public String StudentPage(Model model, Principal principal) {
User currentUser = userDAO.getUser(principal.getName());
model.addAttribute("currentUser", currentUser);
return "home";
}
@PreAuthorize("hasRole('UNASSIGNED')")
@GetMapping("/waiting")
public String waitingPage(Model model, Principal principal) {
User currentUser = userDAO.getUser(principal.getName());
model.addAttribute("currentUser", currentUser);
return "waiting";
}
@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/nav-admin")
public String adminNavPage(Model model, Principal principal) {
User currentUser = userDAO.getUser(principal.getName());
model.addAttribute("currentUser", currentUser);
return "fragments/navbar_admin";
}
@PreAuthorize("hasRole('TEACHING_ASSISTANT')")
@GetMapping("/nav-TA"